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

   1  <?php
   2  //
   3  // Definition of eZTemplateDefFunction class
   4  //
   5  // Created on: <28-Feb-2005 16:03:02 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 eZTemplateDefFunction eztemplatedeffunction.php
  31    \ingroup eZTemplateFunctions
  32    \brief Allows to define/undefine template variables in any place.
  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      {def $var1=<value1> [$var2=<value2> ...]}
  40  \endcode
  41  
  42    Example:
  43  \code
  44      {def $i=10 $j=20}
  45      {def $s1='hello' $s2='world'}
  46      ...
  47      {set $i=$i+1}
  48      ...
  49      {undef $i}
  50      {undef $s1 $s2}
  51      {undef}
  52  \endcode
  53  */
  54  
  55  define ( 'EZ_TEMPLATE_DEF_FUNCTION_NAME',   'def'   );
  56  define ( 'EZ_TEMPLATE_UNDEF_FUNCTION_NAME', 'undef' );
  57  class eZTemplateDefFunction
  58  {
  59      /*!
  60       * Returns an array of the function names, required for eZTemplate::registerFunctions.
  61       */
  62      function &functionList()
  63      {
  64          $functionList = array( EZ_TEMPLATE_DEF_FUNCTION_NAME, EZ_TEMPLATE_UNDEF_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();
  76      }
  77  
  78  
  79      /*!
  80       * Returns the array with hits for the template compiler.
  81       */
  82      function functionTemplateHints()
  83      {
  84          return array( EZ_TEMPLATE_DEF_FUNCTION_NAME   => array( 'parameters' => true,
  85                                                                  'static' => false,
  86                                                                  'transform-parameters' => true,
  87                                                                  'tree-transformation' => true ),
  88                        EZ_TEMPLATE_UNDEF_FUNCTION_NAME => array( 'parameters' => true,
  89                                                                  'static' => false,
  90                                                                  'transform-parameters' => true,
  91                                                                  'tree-transformation' => true ) );
  92      }
  93  
  94      /*!
  95       * Compiles the function into PHP code.
  96       */
  97      function templateNodeTransformation( $functionName, &$node,
  98                                           &$tpl, &$parameters, $privateData )
  99      {
 100          $undef = ( $functionName == 'undef' );
 101          $newNodes = array();
 102  
 103          if ( !$parameters )
 104          {
 105              if ( !$undef )
 106                  // prevent execution of the function in processed mode
 107                  return array( eZTemplateNodeTool::createCodePieceNode( "// an error occured in $functionName" ) );
 108  
 109              // {undef} called w/o arguments => destroy all local variables
 110              $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "// undef all" );
 111              $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$tpl->unsetLocalVariables();" );
 112              return $newNodes;
 113          }
 114  
 115          $nodePlacement = eZTemplateNodeTool::extractFunctionNodePlacement( $node );
 116          foreach ( array_keys( $parameters ) as $parameterName )
 117          {
 118              $parameterData = $parameters[$parameterName];
 119              if ( $undef )
 120              {
 121                  $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "// undef \$$parameterName" );
 122                  // generates "$tpl->unsetLocalVariable();"
 123                  $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( array( $namespaceValue = false,
 124                                                                                    $scope = EZ_TEMPLATE_NAMESPACE_SCOPE_LOCAL,
 125                                                                                    $parameterName ),
 126                                                                             array( 'remember_set' => false, 'local-variable' => true ) );
 127              }
 128              else
 129              {
 130                  $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "// def \$$parameterName" );
 131                  // generates "$tpl->setLocalVariable();"
 132                  $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $parameterData, $nodePlacement, array( 'local-variable' => true ),
 133                                                                        array( $namespaceValue = false, $scope = EZ_TEMPLATE_NAMESPACE_SCOPE_LOCAL, $parameterName ),
 134                                                                        $onlyExisting = false, $overwrite = true, false, $rememberSet = false );
 135              }
 136          }
 137  
 138          return $newNodes;
 139      }
 140  
 141      /*!
 142       * Actually executes the function (in processed mode).
 143       */
 144      function process( &$tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace )
 145      {
 146          $undef = ( $functionName == EZ_TEMPLATE_UNDEF_FUNCTION_NAME ) ? true : false;
 147  
 148          if ( $undef && !count( $functionParameters ) ) // if {undef} called w/o arguments
 149          {
 150              // destroy all variables defined in the current template using {def}
 151              $tpl->unsetLocalVariables();
 152          }
 153  
 154          foreach ( array_keys( $functionParameters ) as $key )
 155          {
 156              $varName  = $key;
 157              $param    = $functionParameters[$varName];
 158              $varValue = $tpl->elementValue( $param, $rootNamespace, $currentNamespace, $functionPlacement );
 159  
 160  
 161              if ( $undef ) // {undef}
 162              {
 163                  if ( !$tpl->hasLocalVariable( $varName, $rootNamespace ) )
 164                      $tpl->warning( EZ_TEMPLATE_UNDEF_FUNCTION_NAME, "Variable '$varName' is not defined with {def}." );
 165                  else
 166                      $tpl->unsetLocalVariable( $varName, $rootNamespace );
 167  
 168              }
 169              else // {def}
 170              {
 171                  if ( $tpl->hasVariable( $varName, $rootNamespace ) ) // if the variable already exists
 172                  {
 173                      // we don't create new variable but just assign value to the existing one.
 174                      $tpl->warning( EZ_TEMPLATE_DEF_FUNCTION_NAME, "Variable '$varName' is already defined." );
 175                      $tpl->setVariable( $varName, $varValue, $rootNamespace );
 176                  }
 177                  else
 178                      // create a new local variable and assign a value to it.
 179                      $tpl->setLocalVariable( $varName, $varValue, $rootNamespace );
 180  
 181              }
 182          }
 183      }
 184  
 185      /*!
 186       * Returns false, telling the template parser that the function cannot have children.
 187       */
 188      function hasChildren()
 189      {
 190          return false;
 191      }
 192  }
 193  
 194  ?>


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