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

   1  <?php
   2  //
   3  // Definition of eZTemplateSetFunction class
   4  //
   5  // Created on: <05-Mar-2002 13:55:25 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 eZTemplateSetFunction eztemplatesetfunction.php
  31    \ingroup eZTemplateFunctions
  32    \brief Sets template variables code using function 'set'
  33  
  34    Allows for setting template variables from templates using
  35    a template function. This is mainly used for optimizations.
  36  
  37    The let function will define new variables and initialize them with
  38    a value while set only sets values to existing variables.
  39    The let function is also scoped with children which means that the
  40    variables are unset when the children are processed.
  41  
  42  \code
  43  // Example template code
  44  {let object=$item1 some_text='abc' integer=1}
  45    {set object=$item2 some_text='def'}
  46  
  47  {/let}
  48  
  49  {set name=NewNamespace place='/etc/test.tpl'}
  50  
  51  \endcode
  52  */
  53  
  54  define( 'EZ_TEMPLATE_SET_SCOPE_RELATIVE', 1 );
  55  define( 'EZ_TEMPLATE_SET_SCOPE_ROOT', 2 );
  56  define( 'EZ_TEMPLATE_SET_SCOPE_GLOBAL', 3 );
  57  
  58  class eZTemplateSetFunction
  59  {
  60      /*!
  61       Initializes the function with the function names $setName and $letName.
  62      */
  63      function eZTemplateSetFunction( $setName = 'set', $letName = 'let', $defaultName = 'default' )
  64      {
  65          $this->SetName = $setName;
  66          $this->LetName = $letName;
  67          $this->DefaultName = $defaultName;
  68      }
  69  
  70      /*!
  71       Returns an array of the function names, required for eZTemplate::registerFunctions.
  72      */
  73      function functionList()
  74      {
  75          return array( $this->SetName, $this->LetName, $this->DefaultName );
  76      }
  77  
  78      function functionTemplateStatistics( $functionName, &$node, &$tpl, $resourceData, $namespace, &$stats )
  79      {
  80          $newNamespace = $namespace;
  81          $parameters = eZTemplateNodeTool::extractFunctionNodeParameters( $node );
  82          if ( $functionName == $this->SetName or
  83               $functionName == $this->LetName or
  84               $functionName == $this->DefaultName )
  85          {
  86              if ( isset( $parameters['-name'] ) )
  87              {
  88                  $nameData = $parameters['-name'];
  89                  $nameDataInspection = eZTemplateCompiler::inspectVariableData( $tpl,
  90                                                                                 $nameData, false,
  91                                                                                 $resourceData );
  92                  if ( $nameDataInspection['is-constant'] and
  93                       !$nameDataInspection['has-operators'] and
  94                       !$nameDataInspection['has-attributes'] )
  95                  {
  96                      $parameterNamespace = $nameDataInspection['new-data'][0][1];
  97                      $newNamespace = $tpl->mergeNamespace( $namespace, $parameterNamespace );
  98                  }
  99              }
 100          }
 101          if ( $functionName == $this->SetName )
 102          {
 103              foreach ( array_keys( $parameters ) as $name )
 104              {
 105                  if ( $name == '-name' )
 106                      continue;
 107                  $parameter =& $parameters[$name];
 108                  eZTemplateCompiler::setVariableStatistics( $stats, $newNamespace, $name, array( 'is_modified' => true ) );
 109                  eZTemplateCompiler::calculateVariableNodeStatistics( $tpl, $parameter, false, $resourceData, $namespace, $stats );
 110              }
 111          }
 112          else if ( $functionName == $this->LetName )
 113          {
 114              foreach ( array_keys( $parameters ) as $name )
 115              {
 116                  if ( $name == '-name' )
 117                      continue;
 118                  $parameter =& $parameters[$name];
 119                  eZTemplateCompiler::setVariableStatistics( $stats, $newNamespace, $name, array( 'is_created' => true,
 120                                                                                               'is_removed' => true ) );
 121                  eZTemplateCompiler::calculateVariableNodeStatistics( $tpl, $parameter, false, $resourceData, $namespace, $stats );
 122              }
 123          }
 124          else if ( $functionName == $this->DefaultName )
 125          {
 126              foreach ( array_keys( $parameters ) as $name )
 127              {
 128                  if ( $name == '-name' )
 129                      continue;
 130                  $parameter =& $parameters[$name];
 131                  eZTemplateCompiler::setVariableStatistics( $stats, $newNamespace, $name, array( ) );
 132                  eZTemplateCompiler::calculateVariableNodeStatistics( $tpl, $parameter, false, $resourceData, $namespace, $stats );
 133              }
 134          }
 135          if ( $functionName == $this->LetName or
 136               $functionName == $this->DefaultName )
 137          {
 138              $functionChildren = eZTemplateNodeTool::extractFunctionNodeChildren( $node );
 139              if ( is_array( $functionChildren ) )
 140              {
 141                  eZTemplateCompiler::calculateVariableStatisticsChildren( $tpl, $functionChildren, $resourceData, $newNamespace, $stats );
 142              }
 143          }
 144      }
 145  
 146  
 147      function functionTemplateHints()
 148      {
 149          return array( $this->LetName => array( 'parameters' => true,
 150                                                 'static' => false,
 151                                                 'tree-transformation' => true,
 152                                                 'transform-children' => true,
 153                                                 'transform-parameters' => true ),
 154                        $this->SetName => array( 'parameters' => true,
 155                                                 'static' => false,
 156                                                 'tree-transformation' => true,
 157                                                 'transform-children' => true,
 158                                                 'transform-parameters' => true ),
 159                        $this->DefaultName => array( 'parameters' => true,
 160                                                     'static' => false,
 161                                                     'tree-transformation' => true,
 162                                                     'transform-children' => true,
 163                                                     'transform-parameters' => true ) );
 164      }
 165  
 166      function templateNodeTransformation( $functionName, &$node,
 167                                           &$tpl, $parameters, $privateData )
 168      {
 169          switch( $functionName )
 170          {
 171              case $this->SetName:
 172              case $this->DefaultName:
 173              case $this->LetName:
 174              {
 175                  $scope = EZ_TEMPLATE_NAMESPACE_SCOPE_RELATIVE;
 176                  if ( isset( $parameters['-scope'] ) )
 177                  {
 178                      if ( !eZTemplateNodeTool::isStaticElement( $parameters['-scope'] ) )
 179                          return false;
 180                      $scopeText = eZTemplateNodeTool::elementStaticValue( $parameters['-scope'] );
 181                      if ( $scopeText == 'relative' )
 182                          $scope = EZ_TEMPLATE_NAMESPACE_SCOPE_RELATIVE;
 183                      else if ( $scopeText == 'root' )
 184                          $scope = EZ_TEMPLATE_NAMESPACE_SCOPE_LOCAL;
 185                      else if ( $scopeText == 'global' )
 186                          $scope = EZ_TEMPLATE_NAMESPACE_SCOPE_GLOBAL;
 187                  }
 188  
 189                  $parameters = eZTemplateNodeTool::extractFunctionNodeParameters( $node );
 190                  $namespaceValue = false;
 191                  if ( isset( $parameters['-name'] ) )
 192                  {
 193                      if ( !eZTemplateNodeTool::isStaticElement( $parameters['-name'] ) )
 194                      {
 195                          return false;
 196                      }
 197  
 198                      $namespaceValue = eZTemplateNodeTool::elementStaticValue( $parameters['-name'] );
 199                  }
 200  
 201                  $variableList = array();
 202                  $setVarNodes = array();
 203                  foreach ( array_keys( $parameters ) as $parameterName )
 204                  {
 205                      if ( $parameterName == '-name' or $parameterName == '-scope'  )
 206                      {
 207                          continue;
 208                      }
 209  
 210                      $parameterData =& $parameters[$parameterName];
 211  
 212                      $setVarNodes[] = eZTemplateNodeTool::createVariableNode(
 213                              false, $parameterData, eZTemplateNodeTool::extractFunctionNodePlacement( $node ),
 214                              array(), array( $namespaceValue, $scope, $parameterName ),
 215                              ( $functionName == $this->SetName ), ( $functionName != $this->DefaultName ),
 216                              false, ( $functionName == $this->DefaultName ) );
 217  
 218                      if ( $functionName == $this->LetName or $functionName == $this->DefaultName )
 219                      {
 220                          $variableList[] = $parameterName;
 221                      }
 222                  }
 223  
 224                  if ( ( $functionName == $this->LetName or $functionName == $this->DefaultName ) and
 225                       $namespaceValue )
 226                  {
 227                      $setVarNodes[] = eZTemplateNodeTool::createNamespaceChangeNode( $namespaceValue );
 228                  }
 229  
 230                  if ( $functionName == $this->LetName or $functionName == $this->DefaultName )
 231                  {
 232                      $childNodes = eZTemplateNodeTool::extractFunctionNodeChildren( $node );
 233                  }
 234                  else
 235                  {
 236                      $childNodes = array();
 237                  }
 238  
 239                  $unsetVarNodes = array();
 240  
 241                  if ( ( $functionName == $this->LetName or $functionName == $this->DefaultName ) and
 242                       $namespaceValue )
 243                  {
 244                      $unsetVarNodes[] = eZTemplateNodeTool::createNamespaceRestoreNode();
 245                  }
 246  
 247                  if ( $functionName == $this->LetName or $functionName == $this->DefaultName )
 248                  {
 249                      foreach( $variableList as $parameterName )
 250                      {
 251                          $unsetVarNodes[] = eZTemplateNodeTool::createVariableUnsetNode( array( $namespaceValue,
 252                                                                                                 EZ_TEMPLATE_NAMESPACE_SCOPE_RELATIVE,
 253                                                                                                 $parameterName ),
 254                                                                                          array( 'remember_set' => $functionName == $this->DefaultName ) );
 255                      }
 256                  }
 257  
 258                  return array_merge( $setVarNodes, $childNodes, $unsetVarNodes );
 259              } break;
 260          }
 261  
 262      }
 263  
 264      function templateHookProcess( $functionName, $functionHookName, $functionHook,
 265                                    &$tpl, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace )
 266      {
 267      }
 268  
 269      function defineVariables( &$tpl, $functionParameters, $functionPlacement, $name, $rootNamespace, &$currentNamespace )
 270      {
 271          $oldCurrentNamespace = $currentNamespace;
 272          $definedVariables = array();
 273          foreach ( array_keys( $functionParameters ) as $key )
 274          {
 275              $item =& $functionParameters[$key];
 276              switch ( $key )
 277              {
 278                  case '-name':
 279                      break;
 280  
 281                  default:
 282                  {
 283                      if ( !$tpl->hasVariable( $key, $name ) )
 284                      {
 285                          $itemValue =& $tpl->elementValue( $item, $rootNamespace, $currentNamespace, $functionPlacement );
 286                          $tpl->setVariableRef( $key, $itemValue, $name );
 287                          $definedVariables[] = $key;
 288                      }
 289                      else
 290                      {
 291                          $varname = $key;
 292                          if ( $name != '' )
 293                              $varname = "$name:$varname";
 294                          $tpl->warning( $this->SetName, "Variable '$varname' already exists, cannot define" );
 295                      }
 296                  } break;
 297              }
 298          }
 299          $currentNamespace = $name;
 300          return array( $definedVariables,
 301                        $oldCurrentNamespace );
 302      }
 303  
 304      function createDefaultVariables( &$tpl, $functionParameters, $functionPlacement, $name, $rootNamespace, &$currentNamespace )
 305      {
 306          $oldCurrentNamespace = $currentNamespace;
 307          $definedVariables = array();
 308          foreach ( array_keys( $functionParameters ) as $key )
 309          {
 310              $item =& $functionParameters[$key];
 311              switch ( $key )
 312              {
 313                  case '-name':
 314                      break;
 315  
 316                  default:
 317                  {
 318                      if ( !$tpl->hasVariable( $key, $name ) )
 319                      {
 320                          $itemValue =& $tpl->elementValue( $item, $rootNamespace, $currentNamespace, $functionPlacement );
 321                          $tpl->setVariableRef( $key, $itemValue, $name );
 322                          $definedVariables[] = $key;
 323                      }
 324                  } break;
 325              }
 326          }
 327          $currentNamespace = $name;
 328          return array( $definedVariables,
 329                        $oldCurrentNamespace );
 330      }
 331  
 332      function cleanupVariables( &$tpl, $rootNamespace, &$currentNamespace, &$setData )
 333      {
 334          $definedVariables =& $setData[0];
 335          foreach ( $definedVariables as $variable )
 336          {
 337              $tpl->unsetVariable( $variable, $currentNamespace );
 338          }
 339          $currentNamespace = $setData[1];
 340      }
 341  
 342      /*!
 343       Loads the file specified in the parameter 'uri' with namespace 'name'.
 344      */
 345      function process( &$tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace )
 346      {
 347          if ( $functionName != $this->SetName and
 348               $functionName != $this->LetName and
 349               $functionName != $this->DefaultName )
 350              return null;
 351  
 352          $children = $functionChildren;
 353          $parameters = $functionParameters;
 354  
 355          $scope = EZ_TEMPLATE_SET_SCOPE_RELATIVE;
 356          if ( isset( $parameters['-scope'] ) )
 357          {
 358              $scopeText = $tpl->elementValue( $parameters['-scope'], $rootNamespace, $currentNamespace, $functionPlacement );
 359              if ( $scopeText == 'relative' )
 360                  $scope = EZ_TEMPLATE_SET_SCOPE_RELATIVE;
 361              else if ( $scopeText == 'root' )
 362                  $scope = EZ_TEMPLATE_SET_SCOPE_ROOT;
 363              else if ( $scopeText == 'global' )
 364                  $scope = EZ_TEMPLATE_SET_SCOPE_GLOBAL;
 365              else
 366                  $tpl->warning( $functionName, "Scope value '$scopeText' is not valid, use either 'relative', 'root' or 'global'" );
 367          }
 368  
 369          $name = null;
 370          if ( isset( $parameters['-name'] ) )
 371              $name = $tpl->elementValue( $parameters['-name'], $rootNamespace, $currentNamespace, $functionPlacement );
 372          if ( $name === null )
 373          {
 374              if ( $scope == EZ_TEMPLATE_SET_SCOPE_RELATIVE )
 375                  $name = $currentNamespace;
 376              else if ( $scope == EZ_TEMPLATE_SET_SCOPE_ROOT )
 377                  $name = $rootNamespace;
 378              else
 379                  $name = '';
 380          }
 381          else
 382          {
 383              if ( $scope == EZ_TEMPLATE_SET_SCOPE_RELATIVE and
 384                   $currentNamespace != '' )
 385                  $name = "$currentNamespace:$name";
 386              else if ( $scope == EZ_TEMPLATE_SET_SCOPE_ROOT and
 387                        $rootNamespace != '' )
 388                  $name = "$rootNamespace:$name";
 389          }
 390  
 391          $definedVariables = array();
 392          if ( $functionName == $this->SetName )
 393          {
 394              foreach ( array_keys( $functionParameters ) as $key )
 395              {
 396                  $item =& $functionParameters[$key];
 397                  switch ( $key )
 398                  {
 399                      case '-name':
 400                      case '-scope':
 401                          break;
 402  
 403                      default:
 404                      {
 405                          if ( $tpl->hasVariable( $key, $name ) )
 406                          {
 407                              unset( $itemValue );
 408                              $itemValue = $tpl->elementValue( $item, $rootNamespace, $currentNamespace, $functionPlacement );
 409                              $tpl->setVariableRef( $key, $itemValue, $name );
 410                          }
 411                          else
 412                          {
 413                              $varname = $key;
 414                              if ( $name != '' )
 415                                  $varname = "$name:$varname";
 416                              $tpl->warning( $functionName, "Variable '$varname' doesn't exist, cannot set" );
 417                          }
 418                      } break;
 419                  }
 420              }
 421          }
 422          else if ( $functionName == $this->DefaultName )
 423          {
 424              $definedVariables = eZTemplateSetFunction::createDefaultVariables( $tpl, $functionParameters, $functionPlacement, $name, $rootNamespace, $currentNamespace );
 425          }
 426          else
 427          {
 428              $definedVariables = eZTemplateSetFunction::defineVariables( $tpl, $functionParameters, $functionPlacement, $name, $rootNamespace, $currentNamespace );
 429          }
 430          if ( $functionName == $this->LetName or
 431               $functionName == $this->DefaultName )
 432          {
 433              if ( is_array( $functionChildren ) )
 434              {
 435                  foreach ( array_keys( $functionChildren ) as $childKey )
 436                  {
 437                      $child =& $functionChildren[$childKey];
 438                      $tpl->processNode( $child, $textElements, $rootNamespace, $name );
 439                  }
 440              }
 441              eZTemplateSetFunction::cleanupVariables( $tpl, $rootNamespace, $currentNamespace, $definedVariables );
 442          }
 443      }
 444  
 445      /*!
 446       Returns false, telling the template parser that this is a single tag.
 447      */
 448      function hasChildren()
 449      {
 450          return array( $this->SetName => false,
 451                        $this->LetName => true,
 452                        $this->DefaultName => true );
 453      }
 454  
 455      /// The name of the set function
 456      var $SetName;
 457      var $LetName;
 458      var $DefaultName;
 459  }
 460  
 461  ?>


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