[ 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/ -> eztemplateiffunction.php (source)

   1  <?php
   2  //
   3  // Definition of eZTemplateIfFunction class
   4  //
   5  // Created on: <07-Feb-2005 16:31:03 vs>
   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 eZTemplateIfFunction eztemplateiffunction.php
  31    \ingroup eZTemplateFunctions
  32    \brief Conditional execution in templates
  33  
  34    This class allows to execute on of two or more code pieces depending
  35    on a condition.
  36  
  37    Syntax:
  38  \code
  39    {if <condition>}
  40    [{elseif <condition>}]
  41    [{else}]
  42    {/if}
  43  \endcode
  44  
  45    Example:
  46  \code
  47    {if eq( $var, true() )}
  48      Hello world
  49    {else}
  50      No world here, move along.
  51    {/if}
  52  \endcode
  53  */
  54  
  55  define ( 'EZ_TEMPLATE_IF_FUNCTION_NAME', 'if' );
  56  class eZTemplateIfFunction
  57  {
  58      /*!
  59       * Returns an array of the function names, required for eZTemplate::registerFunctions.
  60       */
  61      function &functionList()
  62      {
  63          //eZDebug::writeDebug( "if::functionList()" );
  64          $functionList = array( EZ_TEMPLATE_IF_FUNCTION_NAME );
  65          return $functionList;
  66      }
  67  
  68      /*!
  69       * Returns the attribute list which is 'delimiter', 'elseif' and 'else'.
  70       * key:   parameter name
  71       * value: can have children
  72       */
  73      function attributeList()
  74      {
  75          return array( 'elseif'    => false,
  76                        'else'      => false );
  77      }
  78  
  79  
  80      /*!
  81       * Returns the array with hits for the template compiler.
  82       */
  83      function functionTemplateHints()
  84      {
  85          return array( EZ_TEMPLATE_IF_FUNCTION_NAME => array( 'parameters' => true,
  86                                                               'static' => false,
  87                                                               'transform-parameters' => true,
  88                                                               'tree-transformation' => true ) );
  89      }
  90  
  91      /*!
  92       * Compiles the function and its children into PHP code.
  93       */
  94      function templateNodeTransformation( $functionName, &$node,
  95                                           &$tpl, $parameters, $privateData )
  96      {
  97          $tpl->ElseifCounter++;
  98          $newNodes       = array();
  99          $nodesToPrepend = array();
 100          $nodesToAppend  = array();
 101          $nodePlacement  = eZTemplateNodeTool::extractFunctionNodePlacement( $node );
 102          $uniqid        =  md5( $nodePlacement[2] ) . "_" . $tpl->ElseifCounter;
 103          $children       = eZTemplateNodeTool::extractFunctionNodeChildren( $node );
 104  
 105  
 106          $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "// if begins" );
 107          $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $parameters['condition'], $nodePlacement, array( 'treat-value-as-non-object' => true ), 'if_cond' );
 108          $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if ( \$if_cond )\n{" );
 109          $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode();
 110  
 111          if ( is_array( $children ) )
 112          {
 113              foreach ( array_keys( $children ) as $childKey )
 114              {
 115                  $child =& $children[$childKey];
 116  
 117                  if ( $child[0] == EZ_TEMPLATE_NODE_FUNCTION )
 118                  {
 119                      $childFunctionName =& $child[2];
 120                      $childChildren     = eZTemplateNodeTool::extractFunctionNodeChildren( $child );
 121                      $childFunctionArgs =& $child[3];
 122  
 123                      if ( $childFunctionName == 'elseif' )
 124                      {
 125                          $compiledElseifCondition = eZTemplateCompiler::processElementTransformationList( $tpl, $child, $childFunctionArgs['condition'], $privateData );
 126                          $nodesToPrepend[] = eZTemplateNodeTool::createVariableNode( false, $compiledElseifCondition,
 127                                                                                      $nodePlacement,
 128                                                                                      array( 'text-result' => true ),
 129                                                                                      "elseif_cond_$uniqid" );
 130                          $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode();
 131                          $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "}\nelseif ( \$elseif_cond_$uniqid )\n{" );
 132                          $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode();
 133                          $nodesToAppend[] = eZTemplateNodeTool::createVariableUnsetNode( "elseif_cond_$uniqid" );
 134                          // increment unique elseif counter
 135                          $uniqid        =  md5( $nodePlacement[2] ) . "_" . ++$tpl->ElseifCounter;
 136                      }
 137                      elseif ( $childFunctionName == 'else' )
 138                      {
 139                          $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode();
 140                          $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "}\nelse\n{" );
 141                          $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode();
 142                      }
 143                      elseif ( $childFunctionName == 'break' )
 144                          $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "break;\n" );
 145                      elseif ( $childFunctionName == 'continue' )
 146                          $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "continue;\n" );
 147                      elseif ( $childFunctionName == 'skip' )
 148                          $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$skipDelimiter = true;\ncontinue;\n" );
 149  
 150                      // let other functions (ones not listed in the conditions above) be transformed
 151                      if ( in_array( $childFunctionName, array( 'elseif', 'else', 'break', 'continue', 'skip' ) ) )
 152                           continue;
 153                  }
 154                  $newNodes[] = $child;
 155              }
 156          }
 157  
 158          $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode();
 159          $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "}" );
 160          $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "unset( \$if_cond );" );
 161          $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "// if ends\n" );
 162  
 163          $newNodes = array_merge( $nodesToPrepend, $newNodes, $nodesToAppend );
 164  
 165          return $newNodes;
 166      }
 167  
 168      /*!
 169       * Actually executes the function and its children (in processed mode).
 170       */
 171      function process( &$tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace )
 172      {
 173          if ( count( $functionParameters ) == 0 || !count( $functionParameters['condition'] ) )
 174          {
 175              eZDebug::writeError( "Not enough arguments passed to 'if' function." );
 176              return;
 177          }
 178  
 179          $ifValue = $tpl->elementValue( $functionParameters['condition'], $rootNamespace, $currentNamespace, $functionPlacement );
 180  
 181          $show        = $ifValue; // whether to show the child being currently processing
 182          $showWasTrue = $show;    // true if $show has been assigned 'true' value at least once in the current {if}...{/if} block.
 183          $foundElse   = false;    // true if 'else' has been met once in the current {if}...{/if} block.
 184  
 185          if ( !is_array( $functionChildren ) )
 186          {
 187              return;
 188          }
 189          foreach ( $functionChildren as $key => $child )
 190          {
 191              $childType = $child[0];
 192  
 193              // parse 'elseif', 'else' functions
 194              if ( $childType == EZ_TEMPLATE_NODE_FUNCTION )
 195              {
 196                  $childFunctionName      =& $child[2];
 197                  $childFunctionArgs      =& $child[3];
 198                  $childFunctionPlacement =& $child[4];
 199  
 200                  if ( $childFunctionName == 'else' )
 201                  {
 202                      if ( $foundElse  )
 203                      {
 204                          eZDebug::writeError( "Duplicated 'else'" );
 205                          $show = false;
 206                          continue;
 207                      }
 208  
 209                      $show = !$showWasTrue;
 210                      $foundElse = true;
 211                      continue;
 212                  }
 213                  elseif ( $childFunctionName == 'elseif' )
 214                  {
 215                      if ( $foundElse  )
 216                      {
 217                          eZDebug::writeError( "There should be no more 'elseif' after 'else'" );
 218                          $show = false;
 219                          continue;
 220                      }
 221  
 222                      if ( !isset( $childFunctionArgs['condition'] ) || !count( $childFunctionArgs['condition'] ) ) // no arguments passes to 'elseif' (should not happen)
 223                          $elseifValue = false;
 224                      else
 225                          $elseifValue = $tpl->elementValue( $childFunctionArgs['condition'], $rootNamespace, $currentNamespace, $functionPlacement );
 226  
 227                      if ( !$showWasTrue && $elseifValue )
 228                          $show = $showWasTrue = true;
 229                      else
 230                          $show = false;
 231                      continue;
 232                  }
 233                  elseif ( $childFunctionName == 'break' )
 234                  {
 235                      if ( !$show )
 236                              continue;
 237                      return array( 'breakFunctionFound' => 1 );
 238                  }
 239                  elseif ( $childFunctionName == 'continue' )
 240                  {
 241                      if ( !$show )
 242                              continue;
 243                      return array( 'continueFunctionFound' => 1 );
 244                  }
 245                  elseif ( $childFunctionName == 'skip' )
 246                  {
 247                      if ( !$show )
 248                              continue;
 249                      return array( 'skipFunctionFound' => 1 );
 250                  }
 251              }
 252  
 253              if ( $show )
 254              {
 255                  $rslt = $tpl->processNode( $child, $textElements, $rootNamespace, $currentNamespace );
 256  
 257                  // handle 'break', 'continue' and 'skip'
 258                  if ( is_array( $rslt ) && ( array_key_exists( 'breakFunctionFound', $rslt ) ||
 259                                              array_key_exists( 'continueFunctionFound', $rslt )  ||
 260                                              array_key_exists( 'skipFunctionFound', $rslt ) ) )
 261                  {
 262                      return $rslt;
 263                  }
 264              }
 265          }
 266      }
 267  
 268      /*!
 269       * Returns true, telling the template parser that the function can have children.
 270       */
 271      function hasChildren()
 272      {
 273          return true;
 274      }
 275  }
 276  
 277  ?>


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