[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/eztemplate/classes/ -> eztemplateswitchfunction.php (source)

   1  <?php
   2  //
   3  // Definition of eZTemplateSwitchFunction class
   4  //
   5  // Created on: <06-Mar-2002 08:07:54 amos>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*!
  30    \class eZTemplateSwitchFunction eztemplateswitchfunction.php
  31    \ingroup eZTemplateFunctions
  32    \brief Handles conditional output in templates using function "switch"
  33  
  34    This allows for writing switch/case sentences (similar to if/else if/else)
  35    which you normally find in programming languages. With this you can display
  36    text depending on a certain template variable.
  37  
  38  \code
  39  // Example template code
  40  {* Matches $a against $b or $c *}
  41  {switch match=$a}
  42  {case match=$b}
  43  Matched $b
  44  {/case}
  45  {case match=$c}
  46  Matched $c
  47  {/case}
  48  {/switch}
  49  
  50  \endcode
  51  
  52  TODO: Add support for custom operations when matching
  53  {case process=$match|gt(5)}
  54  Matched $c
  55  {/case}
  56  
  57  
  58  */
  59  
  60  class eZTemplateSwitchFunction
  61  {
  62      /*!
  63       Initializes the function with the name $name, default is "switch".
  64      */
  65      function eZTemplateSwitchFunction()
  66      {
  67          $this->SwitchName = 'switch';
  68      }
  69  
  70      /*!
  71       Returns an array of the function names, required for eZTemplate::registerFunctions.
  72      */
  73      function functionList()
  74      {
  75          return array( $this->SwitchName );
  76      }
  77  
  78      function functionTemplateHints()
  79      {
  80          return array( $this->SwitchName => array( 'parameters' => true,
  81                                                    'static' => false,
  82                                                    'transform-children' => false,
  83                                                    'tree-transformation' => true,
  84                                                    'transform-parameters' => true ) );
  85      }
  86  
  87      /*!
  88       Returns the attribute list which is case.
  89      */
  90      function attributeList()
  91      {
  92          return array( "case" => true );
  93      }
  94  
  95      function templateNodeCaseTransformation( &$tpl, &$newNodes, &$caseNodes, &$caseCounter, &$node, $privateData )
  96      {
  97          if ( $node[2] == 'case' )
  98          {
  99              if ( is_array( $node[3] ) && count( $node[3] ) )
 100              {
 101                  if ( isset( $node[3]['match'] ) )
 102                  {
 103                      $match = $node[3]['match'];
 104                      $match = eZTemplateCompiler::processElementTransformationList( $tpl, $node, $match, $privateData );
 105  
 106                      $dynamicCase = false;
 107                      if ( eZTemplateNodeTool::isStaticElement( $match ) )
 108                      {
 109                          $matchValue = eZTemplateNodeTool::elementStaticValue( $match );
 110                          $caseText = eZPHPCreator::variableText( $matchValue, 0, 0, false );
 111                      }
 112                      else
 113                      {
 114                          $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $match, false, array(), 'case' . $caseCounter );
 115                          $caseText = "\$case" . $caseCounter;
 116                          ++$caseCounter;
 117                          $dynamicCase = true;
 118                      }
 119  
 120                      $caseNodes[] = eZTemplateNodeTool::createCodePieceNode( "    case $caseText:\n    {" );
 121                      if ( $dynamicCase )
 122                          $caseNodes[] = eZTemplateNodeTool::createCodePieceNode( "        unset( $caseText );" );
 123                  }
 124                  else if ( isset( $node[3]['in'] ) )
 125                  {
 126                      return false;
 127                  }
 128              }
 129              else
 130              {
 131                  $caseNodes[] = eZTemplateNodeTool::createCodePieceNode( "    default:\n    {" );
 132              }
 133  
 134              $children = eZTemplateNodeTool::extractFunctionNodeChildren( $node );
 135              $children = eZTemplateCompiler::processNodeTransformationNodes( $tpl, $node, $children, $privateData );
 136  
 137              $caseNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode( 8 );
 138              $caseNodes = array_merge( $caseNodes, $children );
 139              $caseNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode( 8 );
 140  
 141              $caseNodes[] = eZTemplateNodeTool::createCodePieceNode( "    } break;" );
 142          }
 143      }
 144  
 145  
 146      function templateNodeTransformation( $functionName, &$node,
 147                                           &$tpl, $parameters, $privateData )
 148      {
 149          $newNodes = array();
 150          $namespaceValue = false;
 151          $varName = 'match';
 152  
 153          if ( !isset( $parameters['match'] ) )
 154          {
 155              return false;
 156          }
 157  
 158          if ( isset( $parameters['name'] ) )
 159          {
 160              $nameData = $parameters['name'];
 161              if ( !eZTemplateNodeTool::isStaticElement( $nameData ) )
 162                  return false;
 163              $namespaceValue = eZTemplateNodeTool::elementStaticValue( $nameData );
 164          }
 165  
 166          if ( isset( $parameters['var'] ) )
 167          {
 168              $varData = $parameters['var'];
 169              if ( !eZTemplateNodeTool::isStaticElement( $varData ) )
 170                  return false;
 171              $varName = eZTemplateNodeTool::elementStaticValue( $varData );
 172          }
 173  
 174          $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $parameters['match'], false, array(),
 175                                                                array( $namespaceValue, EZ_TEMPLATE_NAMESPACE_SCOPE_RELATIVE, $varName ) );
 176          $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $parameters['match'],
 177                                                                eZTemplateNodeTool::extractFunctionNodePlacement( $node ),
 178                                                                array( 'variable-name' => 'match',
 179                                                                       'text-result' => true ), 'match' );
 180  //                                                                       'text-result' => false ) );
 181          if ( isset( $parameters['name'] ) )
 182          {
 183              $newNodes[] = eZTemplateNodeTool::createNamespaceChangeNode( $parameters['name'] );
 184          }
 185  
 186          $tmpNodes = array();
 187          $children = eZTemplateNodeTool::extractFunctionNodeChildren( $node );
 188          $caseNodes = array();
 189          $caseCounter = 1;
 190          if ( is_array( $children ) )
 191          {
 192              foreach ( $children as $child )
 193              {
 194                  $childType = $child[0];
 195                  if ( $childType == EZ_TEMPLATE_NODE_FUNCTION )
 196                  {
 197                      if ( $this->templateNodeCaseTransformation( $tpl, $tmpNodes, $caseNodes, $caseCounter, $child, $privateData ) === false )
 198                      {
 199                          return false;
 200                      }
 201                  }
 202              }
 203          }
 204          $newNodes = array_merge( $newNodes, $tmpNodes );
 205          $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "switch ( \$match )\n{" );
 206          $newNodes = array_merge( $newNodes, $caseNodes );
 207  
 208          $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "}" );
 209          $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( 'match' );
 210          if ( isset( $parameters['name'] ) )
 211          {
 212              $newNodes[] = eZTemplateNodeTool::createNamespaceRestoreNode();
 213          }
 214          $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( array( $namespaceValue, EZ_TEMPLATE_NAMESPACE_SCOPE_RELATIVE, 'match' ) );
 215  
 216          return $newNodes;
 217      }
 218  
 219      /*!
 220       Processes the function with all it's children.
 221      */
 222      function process( &$tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace )
 223      {
 224          $children = $functionChildren;
 225          $params = $functionParameters;
 226          $name = "";
 227          if ( isset( $params["name"] ) )
 228              $name = $tpl->elementValue( $params["name"], $rootNamespace, $currentNamespace, $functionPlacement );
 229          $varName = false;
 230          if ( isset( $params["var"] ) )
 231              $varName = $tpl->elementValue( $params["var"], $rootNamespace, $currentNamespace, $functionPlacement );
 232          if ( $currentNamespace != "" )
 233          {
 234              if ( $name != "" )
 235                  $name = "$currentNamespace:$name";
 236              else
 237                  $name = $currentNamespace;
 238          }
 239          if ( isset( $params["match"] ) )
 240              $match = $tpl->elementValue( $params["match"], $rootNamespace, $currentNamespace, $functionPlacement );
 241          else
 242          {
 243              $tpl->missingParameter( $this->SwitchName, "match" );
 244              return false;
 245          }
 246  
 247          $items = array();
 248          $in_items = array();
 249          $def = null;
 250          $case = null;
 251          if ( is_array( $children ) )
 252          {
 253              foreach( $children as $child )
 254              {
 255                  $childType = $child[0];
 256                  if ( $childType == EZ_TEMPLATE_NODE_FUNCTION )
 257                  {
 258                      switch ( $child[2] )
 259                      {
 260                          case "case":
 261                          {
 262                              $child_params = $child[3];
 263                              if ( isset( $child_params["match"] ) )
 264                              {
 265                                  $child_match = $child_params["match"];
 266                                  $child_match = $tpl->elementValue( $child_match, $rootNamespace, $currentNamespace, $functionPlacement );
 267                                  if ( !isset( $items[$child_match] ) )
 268                                  {
 269                                      $items[$child_match] = $child;
 270                                      if ( is_null( $case ) and
 271                                           $match == $child_match )
 272                                      {
 273                                          $case = $child;
 274                                      }
 275                                  }
 276                                  else
 277                                  {
 278                                      $tpl->warning( $this->SwitchName, "Match value $child_match already set, skipping", $functionPlacement );
 279                                  }
 280                              }
 281                              else if ( isset( $child_params["in"] ) )
 282                              {
 283                                  $key_name = null;
 284                                  if ( isset( $child_params["key"] ) )
 285                                  {
 286                                      $child_key = $child_params["key"];
 287                                      $key_name = $tpl->elementValue( $child_key, $rootNamespace, $currentNamespace, $functionPlacement );
 288                                  }
 289                                  $child_in = $child_params["in"];
 290                                  $child_in = $tpl->elementValue( $child_in, $rootNamespace, $currentNamespace, $functionPlacement );
 291                                  if ( !is_array( $child_in ) )
 292                                      break;
 293                                  if ( is_null( $case ) )
 294                                  {
 295                                      if ( is_null( $key_name ) )
 296                                      {
 297                                          if ( in_array( $match, $child_in ) )
 298                                          {
 299                                              $case = $child;
 300                                          }
 301                                      }
 302                                      else
 303                                      {
 304                                          foreach( $child_in as $child_in_element )
 305                                          {
 306                                              if ( !is_array( $key_name ) )
 307                                                  $key_name_array = array( $key_name );
 308                                              else
 309                                                  $key_name_array = $key_name;
 310                                              $child_value = $tpl->variableAttribute( $child_in_element, $key_name );
 311                                              if ( $child_value == $match )
 312                                              {
 313                                                  $case = $child;
 314                                                  break;
 315                                              }
 316                                          }
 317                                      }
 318                                  }
 319                              }
 320                              else
 321                              {
 322                                  $def = $child;
 323                              }
 324                          } break;
 325                          default:
 326                          {
 327                              $tpl->warning( $this->SwitchName, "Only case functions are allowed as children, found \""
 328                                             . $child[2] . "\"", $functionPlacement );
 329                          } break;
 330                      }
 331                  }
 332                  else if ( $childType == EZ_TEMPLATE_NODE_TEXT )
 333                  {
 334                      // Ignore text.
 335                  }
 336                  else
 337                  {
 338                      $tpl->warning( $this->SwitchName, "Only functions are allowed as children, found \""
 339                                     . $childType . "\"", $functionPlacement );
 340                  }
 341              }
 342          }
 343  
 344          if ( is_null( $case ) )
 345          {
 346              $case = $def;
 347          }
 348  
 349          if ( $case !== null )
 350          {
 351              if ( $varName !== false )
 352                  $tpl->setVariable( $varName, $match, $name );
 353              else
 354                  $tpl->setVariable( "match", $match, $name );
 355              $case_children = $case[1];
 356              if ( $case_children )
 357              {
 358                  foreach( $case_children as $case_child )
 359                  {
 360                      $tpl->processNode( $case_child, $textElements, $rootNamespace, $name );
 361                  }
 362              }
 363          }
 364          else
 365              $tpl->warning( $this->SwitchName, "No case match and no default case", $functionPlacement );
 366          return;
 367      }
 368  
 369      /*!
 370       Returns true.
 371      */
 372      function hasChildren()
 373      {
 374          return true;
 375      }
 376  
 377      /// The name of the switch function
 378      var $SwitchName;
 379  }
 380  
 381  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7