[ Index ]
 

Code source de eZ Publish 3.9.0

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

title

Body

[fermer]

/kernel/common/ -> ezobjectforwarder.php (source)

   1  <?php
   2  //
   3  // Definition of eZObjectforwarder class
   4  //
   5  // Created on: <14-Sep-2002 15:38:26 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  /*! \file ezobjectforwarder.php
  30  */
  31  
  32  /*!
  33    \class eZObjectforwarder ezobjectforwarder.php
  34    \brief The class eZObjectforwarder does
  35  
  36  */
  37  
  38  class eZObjectForwarder
  39  {
  40      function eZObjectForwarder( $rules )
  41      {
  42          $this->Rules =& $rules;
  43      }
  44  
  45      function functionList()
  46      {
  47          return array_keys( $this->Rules );
  48      }
  49  
  50      function functionTemplateHints()
  51      {
  52          $hints = array();
  53          foreach ( $this->Rules as $name => $data )
  54          {
  55              $hints[$name] = array( 'parameters' => true,
  56                                     'static' => false,
  57                                     'transform-children' => true,
  58                                     'tree-transformation' => true,
  59                                     'transform-parameters' => true );
  60          }
  61          return $hints;
  62      }
  63  
  64      function templateNodeTransformation( $functionName, &$node,
  65                                           &$tpl, $parameters, $privateData )
  66      {
  67          if ( !isset( $this->Rules[$functionName] ) )
  68              return false;
  69          $rule =& $this->Rules[$functionName];
  70          $resourceData =& $privateData['resource-data'];
  71  
  72          $parameters = eZTemplateNodeTool::extractFunctionNodeParameters( $node );
  73          $inputName = $rule['input_name'];
  74          if ( !isset( $parameters[$inputName] ) )
  75          {
  76              return false;
  77          }
  78          $inputData = $parameters[$inputName];
  79          $outputName = $rule['output_name'];
  80  
  81          $newNodes = array();
  82  
  83          $viewDir = '';
  84          $renderMode = false;
  85          if ( isset( $rule["render_mode"] ) )
  86          {
  87              $renderMode = $rule["render_mode"];
  88          }
  89          if ( isset( $parameters['render-mode'] ) )
  90          {
  91              $renderData = $parameters['render-mode'];
  92              if ( !eZTemplateNodeTool::isStaticElement( $renderData ) )
  93              {
  94                  return false;
  95              }
  96              $renderMode = eZTemplateNodeTool::elementStaticValue( $renderData );
  97          }
  98          if ( $renderMode )
  99              $view_dir .= "/render-$renderMode";
 100  
 101          $viewValue = false;
 102          $viewName = false;
 103          if ( $rule['use_views'] )
 104          {
 105              $optionalViews = isset( $rule['optional_views'] ) ? $rule['optional_views'] : false;
 106              $viewName = $rule['use_views'];
 107              if ( isset( $parameters[$viewName] ) )
 108              {
 109                  $viewData = $parameters[$viewName];
 110                  if ( !eZTemplateNodeTool::isStaticElement( $viewData ) )
 111                  {
 112                      return false;
 113                  }
 114                  $viewValue = eZTemplateNodeTool::elementStaticValue( $viewData );
 115                  $viewDir .= '/' . $viewValue;
 116              }
 117              else
 118              {
 119                  if ( !$optionalViews )
 120                  {
 121                      return false;
 122                  }
 123              }
 124          }
 125  
 126          $namespaceValue = false;
 127          if ( isset( $rule['namespace'] ) )
 128          {
 129              $namespaceValue = $rule['namespace'];
 130          }
 131  
 132          $variableList = array();
 133  
 134          $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $inputData, false, array(),
 135                                                                array( $namespaceValue, EZ_TEMPLATE_NAMESPACE_SCOPE_RELATIVE, $outputName ) );
 136          $variableList[] = $outputName;
 137  
 138          foreach ( array_keys( $parameters ) as $parameterName )
 139          {
 140              if ( $parameterName == $inputName or
 141                   $parameterName == $outputName or
 142                   $parameterName == $viewName )
 143                  continue;
 144              $parameterData =& $parameters[$parameterName];
 145              $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $parameterData, false, array(),
 146                                                                    array( $namespaceValue, EZ_TEMPLATE_NAMESPACE_SCOPE_RELATIVE, $parameterName ) );
 147              $variableList[] = $parameterName;
 148          }
 149  
 150          $templateRoot = $rule["template_root"];
 151          $matchFileArray = eZTemplateDesignResource::overrideArray();
 152  
 153          if ( is_string( $templateRoot ) )
 154          {
 155              $resourceNodes = $this->resourceAcquisitionTransformation( $functionName, $node, $rule, $inputData,
 156                                                                         $outputName, $namespaceValue,
 157                                                                         $templateRoot, $viewDir, $viewValue,
 158                                                                         $matchFileArray, 0, $resourceData );
 159              // If the transformation failed we return false to invoke interpreted mode
 160              if ( $resourceNodes === false )
 161                  return false;
 162              $newNodes = array_merge( $newNodes, $resourceNodes );
 163          }
 164          else
 165          {
 166              if ( isset( $templateRoot['type'] ) and
 167                   $templateRoot['type'] == 'multi_match' and
 168                   isset( $templateRoot['attributes'] ) and
 169                   isset( $templateRoot['matches'] ) )
 170              {
 171                  $rootAttributes = $templateRoot['attributes'];
 172                  $attributeAccessData = array();
 173                  $attributeAccessData[] = eZTemplateNodeTool::createVariableElement( $outputName, $namespaceValue, EZ_TEMPLATE_NAMESPACE_SCOPE_RELATIVE );
 174                  foreach ( $rootAttributes as $rootAttributeName )
 175                  {
 176                      $attributeAccessData[] = eZTemplateNodeTool::createAttributeLookupElement( $rootAttributeName );
 177                  }
 178                  $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $attributeAccessData, false,
 179                                                                        array( 'spacing' => 0 ), 'templateRootMatch' );
 180                  $rootMatches = $templateRoot['matches'];
 181                  $rootMatchCounter = 0;
 182                  foreach ( $rootMatches as $rootMatch )
 183                  {
 184                      $rootMatchValue = $rootMatch[0];
 185                      $templateRoot = $rootMatch[1];
 186                      $rootMatchValueText = eZPHPCreator::variableText( $rootMatchValue, 0, 0, false );
 187                      $code = '';
 188                      if ( $rootMatchCounter > 0 )
 189                          $code .= "else " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . "";
 190                      $code .= "if " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . "( \$templateRootMatch == $rootMatchValueText )\n{";
 191                      $newNodes[] = eZTemplateNodeTool::createCodePieceNode( $code );
 192  
 193                      $resourceNodes = $this->resourceAcquisitionTransformation( $functionName, $node, $rule, $inputData,
 194                                                                                 $outputName, $namespaceValue,
 195                                                                                 $templateRoot, $viewDir, $viewValue,
 196                                                                                 $matchFileArray, 4, $resourceData );
 197                      // If the transformation failed we return false to invoke interpreted mode
 198                      if ( $resourceNodes === false )
 199                          return false;
 200                      $newNodes = array_merge( $newNodes, $resourceNodes );
 201                      $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "}" );
 202                      ++$rootMatchCounter;
 203                  }
 204                  $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( 'templateRootMatch' );
 205              }
 206          }
 207  
 208          foreach ( $variableList as $variableName )
 209          {
 210              $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( array( $namespaceValue, EZ_TEMPLATE_NAMESPACE_SCOPE_RELATIVE, $variableName ) );
 211          }
 212  
 213          return $newNodes;
 214      }
 215  
 216      function resourceAcquisitionTransformation( $functionName, &$node, $rule, $inputData,
 217                                                  $outputName, $namespaceValue,
 218                                                  $templateRoot, $viewDir, $viewValue,
 219                                                  $matchFileArray, $acquisitionSpacing,
 220                                                  &$resourceData )
 221      {
 222          $startRoot = '/' . $templateRoot . $viewDir;
 223          $viewFileMatchName = '/' . $templateRoot . '/' . $viewValue . '.tpl';
 224          $startRootLength = strlen( $startRoot );
 225          $matchList = array();
 226          $viewFileMatch = null;
 227          foreach ( $matchFileArray as $matchFile )
 228          {
 229              $path = $matchFile['template'];
 230              $subPath = substr( $path, 0, $startRootLength );
 231              if ( $subPath == $startRoot and
 232                   $path[$startRootLength] == '/' )
 233              {
 234                  $matchFile['match_part'] = substr( $path, $startRootLength + 1 );
 235                  $matchList[] = $matchFile;
 236              }
 237              if ( $path == $viewFileMatchName )
 238                  $viewFileMatch = $matchFile;
 239          }
 240          $designKeysName = 'dKeys';
 241          $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . "( !isset( \$$designKeysName ) )\n" .
 242                                                                 "{\n" .
 243                                                                 "    \$resH =& \$tpl->resourceHandler( 'design' );\n" .
 244                                                                 "    \$$designKeysName =& \$resH->keys();\n" .
 245                                                                 "}", array( 'spacing' => $acquisitionSpacing ) );
 246          $attributeKeys =& $rule["attribute_keys"];
 247          if ( isset( $attributeKeys ) )
 248          {
 249              $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . "( !isset( \$" . $designKeysName . "Stack ) )\n" .
 250                                                                     "{\n" .
 251                                                                     "    \$" . $designKeysName . "Stack = array();\n" .
 252                                                                     "}\n" .
 253                                                                     "\$" . $designKeysName . "Stack[] = \$$designKeysName;",
 254                                                                     array( 'spacing' => $acquisitionSpacing ) );
 255              foreach ( $attributeKeys as $designKey => $attributeKeyArray )
 256              {
 257                  $attributeAccessData = array();
 258                  $attributeAccessData[] = eZTemplateNodeTool::createVariableElement( $outputName, $namespaceValue, EZ_TEMPLATE_NAMESPACE_SCOPE_RELATIVE );
 259                  foreach ( $attributeKeyArray as $attributeKey )
 260                  {
 261                      $attributeAccessData[] = eZTemplateNodeTool::createAttributeLookupElement( $attributeKey );
 262                  }
 263                  $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $attributeAccessData, false,
 264                                                                        array( 'spacing' => 0 ), 'dKey' );
 265                  $designKeyText = eZPHPCreator::variableText( $designKey, 0, 0, false );
 266                  $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$" . $designKeysName . "[$designKeyText] = \$dKey;",
 267                                                                         array( 'spacing' => $acquisitionSpacing ) );
 268                  $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( 'dKey' );
 269              }
 270          }
 271  
 272          $attributeAccess =& $rule["attribute_access"];
 273  
 274          $hasAttributeAccess = false;
 275          if ( is_array( $attributeAccess ) )
 276          {
 277              $hasAttributeAccess = count( $attributeAccess ) > 0;
 278              $attributeAccessCount = 0;
 279              foreach ( $attributeAccess as $attributeAccessEntries )
 280              {
 281                  $attributeAccessData = $inputData;
 282                  $spacing = $acquisitionSpacing;
 283                  if ( $attributeAccessCount > 1 )
 284                  {
 285                      $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "else " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . " if ( !$resourceFound )\n{\n", array( 'spacing' => $acquisitionSpacing ) );
 286                      $spacing += 4;
 287                  }
 288                  else if ( $attributeAccessCount > 0 )
 289                  {
 290                      $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . "( !\$resourceFound )\n{\n", array( 'spacing' => $acquisitionSpacing ) );
 291                      $spacing += 4;
 292                  }
 293                  foreach ( $attributeAccessEntries as $attributeAccessName )
 294                  {
 295  //                    $attributeAccessData[] = eZTemplateNodeTool::createCodePieceNode( "" . ( $resourceData['use-comments'] ? ( "/*TC:" . __LINE__ . "*/" ) : "" ) . "" );
 296                      $attributeAccessData[] = eZTemplateNodeTool::createAttributeLookupElement( $attributeAccessName );
 297                  }
 298                  $accessNodes = array();
 299                  $accessNodes[] = eZTemplateNodeTool::createVariableNode( false, $attributeAccessData, false,
 300                                                                           array( 'spacing' => $spacing ), 'attributeAccess' );
 301  
 302                  $acquisitionNodes = array();
 303                  $templateCounter = 0;
 304                  $hasAcquisitionNodes = false;
 305                  $matchLookupArray = array();
 306                  foreach ( $matchList as $matchItem )
 307                  {
 308                      $tmpAcquisitionNodes = array();
 309                      $matchPart = $matchItem['match_part'];
 310                      if ( preg_match( "/^(.+)\.tpl$/", $matchPart, $matches ) )
 311                          $matchPart = $matches[1];
 312                      $code = "if " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . "( \$attributeAccess == '$matchPart' )\n{\n";
 313                      if ( $templateCounter > 0 )
 314                          $code = "else " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . "" . $code;
 315                      $tmpAcquisitionNodes[] = eZTemplateNodeTool::createCodePieceNode( $code, array( 'spacing' => $spacing ) );
 316  
 317                      $defaultMatchSpacing = $spacing;
 318                      $useArrayLookup = false;
 319                      $addFileResource = true;
 320                      if ( isset( $matchItem['custom_match'] ) )
 321                      {
 322                          $customSpacing = $spacing + 4;
 323                          $defaultMatchSpacing = $spacing + 4;
 324                          $customMatchList = $matchItem['custom_match'];
 325                          $matchCount = 0;
 326                          foreach ( $customMatchList as $customMatch )
 327                          {
 328                              $matchConditionCount = count( $customMatch['conditions'] );
 329                              $code = '';
 330                              if ( $matchCount > 0 )
 331                              {
 332                                  $code = "else " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . "";
 333                              }
 334                              if ( $matchConditionCount > 0 )
 335                              {
 336                                  if ( $matchCount > 0 )
 337                                      $code .= " ";
 338                                  $code .= "if " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . "( ";
 339                              }
 340                              $ifLength = strlen( $code );
 341                              $conditionCount = 0;
 342                              if ( isset( $customMatch['conditions'] ) )
 343                              {
 344                                  foreach ( $customMatch['conditions'] as $conditionName => $conditionValue )
 345                                  {
 346                                      if ( $conditionCount > 0 )
 347                                          $code .= " and\n" . str_repeat( ' ', $ifLength );
 348                                      $conditionNameText = eZPHPCreator::variableText( $conditionName, 0 );
 349                                      $conditionValueText = eZPHPCreator::variableText( $conditionValue, 0 );
 350                                      if ( $conditionNameText == '"url_alias"' )
 351                                      {
 352                                          $code .= "isset( \$" . $designKeysName . "[$conditionNameText] ) and " .
 353                                                   "( strpos(\$" . $designKeysName . "[$conditionNameText], $conditionValueText ) === 0 )";
 354                                      }
 355                                      else
 356                                      {
 357                                          $code .= "isset( \$" . $designKeysName . "[$conditionNameText] ) and " .
 358                                                   "( ( is_array( \$" . $designKeysName . "[$conditionNameText] ) and in_array( $conditionValueText, \$" . $designKeysName . "[$conditionNameText] ) ) or " .
 359                                                   "\$" . $designKeysName . "[$conditionNameText] == $conditionValueText )";
 360                                      }
 361                                      ++$conditionCount;
 362                                  }
 363                              }
 364                              if ( $matchConditionCount > 0 )
 365                              {
 366                                  $code .= " )\n";
 367                              }
 368                              if ( $matchConditionCount > 0 or $matchCount > 0 )
 369                              {
 370                                  $code .= "{";
 371                              }
 372                              $matchFile = $customMatch['match_file'];
 373                              $tmpAcquisitionNodes[] = eZTemplateNodeTool::createCodePieceNode( $code, array( 'spacing' => $customSpacing ) );
 374                              $hasAcquisitionNodes = true;
 375                              // If $matchFile is an array we cannot create a transformation for this entry
 376                              if ( is_array( $matchFile ) )
 377                                  return false;
 378                              $tmpAcquisitionNodes[] = eZTemplateNodeTool::createResourceAcquisitionNode( '',
 379                                                                                                       $matchFile, $matchFile,
 380                                                                                                       EZ_RESOURCE_FETCH, false,
 381                                                                                                       $node[4], array( 'spacing' => $customSpacing + 4 ),
 382                                                                                                       $rule['namespace'] );
 383                              if ( $matchConditionCount > 0 or $matchCount > 0 )
 384                              {
 385                                  $tmpAcquisitionNodes[] = eZTemplateNodeTool::createCodePieceNode( "}", array( 'spacing' => $customSpacing ) );
 386                              }
 387                              ++$matchCount;
 388                              if ( $matchConditionCount == 0 )
 389                              {
 390                                  $addFileResource = false;
 391                                  break;
 392                              }
 393                          }
 394                          if ( $addFileResource )
 395                              $tmpAcquisitionNodes[] = eZTemplateNodeTool::createCodePieceNode( "else " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . " \n{", array( 'spacing' => $customSpacing ) );
 396                      }
 397                      else
 398                      {
 399                          $matchFile = $matchItem['base_dir'] . $matchItem['template'];
 400                          $matchLookupArray[$matchPart] = $matchFile;
 401                          $useArrayLookup = true;
 402                      }
 403  
 404                      if ( !$useArrayLookup )
 405                      {
 406                          if ( $addFileResource )
 407                          {
 408                              $matchFile = $matchItem['base_dir'] . $matchItem['template'];
 409                              // If $matchFile is an array we cannot create a transformation for this entry
 410                              if ( is_array( $matchFile ) )
 411                                  return false;
 412                              $tmpAcquisitionNodes[] = eZTemplateNodeTool::createResourceAcquisitionNode( '',
 413                                                                                                          $matchFile, $matchFile,
 414                                                                                                          EZ_RESOURCE_FETCH, false,
 415                                                                                                          $node[4], array( 'spacing' => $defaultMatchSpacing + 4 ),
 416                                                                                                          $rule['namespace'] );
 417                              $hasAcquisitionNodes = true;
 418                              if ( isset( $matchItem['custom_match'] ) )
 419                                  $tmpAcquisitionNodes[] = eZTemplateNodeTool::createCodePieceNode( "}", array( 'spacing' => $customSpacing ) );
 420                          }
 421                          ++$templateCounter;
 422                          $tmpAcquisitionNodes[] = eZTemplateNodeTool::createCodePieceNode( "}", array( 'spacing' => $spacing ) );
 423                          $acquisitionNodes = array_merge( $acquisitionNodes, $tmpAcquisitionNodes );
 424                      }
 425                  }
 426  
 427                  if ( count( $matchLookupArray ) > 0 )
 428                  {
 429                      $newNodes = array_merge( $newNodes, $accessNodes );
 430                      $accessNodes = array();
 431                      // If $matchFile is an array we cannot create a transformation for this entry
 432                      if ( is_array( $matchLookupArray ) )
 433                          return false;
 434                      $newNodes[] = eZTemplateNodeTool::createResourceAcquisitionNode( '',
 435                                                                                       $matchLookupArray, false,
 436                                                                                       EZ_RESOURCE_FETCH, false,
 437                                                                                       $node[4], array( 'spacing' => $spacing ),
 438                                                                                       $rule['namespace'], 'attributeAccess' );
 439                      if ( $hasAcquisitionNodes )
 440                      {
 441                          $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "else " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . "\n{", array( 'spacing' => $spacing ) );
 442                          $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode();
 443                      }
 444                  }
 445                  if ( $hasAcquisitionNodes )
 446                  {
 447                      $newNodes = array_merge( $newNodes, $accessNodes, $acquisitionNodes );
 448  
 449                      if ( $attributeAccessCount > 0 )
 450                      {
 451                          $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "}", array( 'spacing' => $acquisitionSpacing ) );
 452                      }
 453                      ++$attributeAccessCount;
 454                  }
 455                  else if ( count( $matchLookupArray ) == 0 )
 456                  {
 457                      $newNodes[] = eZTemplateNodeTool::createErrorNode( "Failed to load template",
 458                                                                         $functionName,
 459                                                                         eZTemplateNodeTool::extractFunctionNodePlacement( $node ),
 460                                                                         array( 'spacing' => $acquisitionSpacing ) );
 461                  }
 462                  if ( count( $matchLookupArray ) > 0 and $hasAcquisitionNodes )
 463                  {
 464                      $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode();
 465                      $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "}", array( 'spacing' => $spacing ) );
 466                  }
 467              }
 468          }
 469          if ( $viewFileMatch !== null )
 470          {
 471              $mainSpacing = 0;
 472              if ( $hasAttributeAccess )
 473              {
 474                  $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "else " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . "\n{\n", array( 'spacing' => $acquisitionSpacing ) );
 475                  $mainSpacing = 4;
 476              }
 477              $templateCounter = 0;
 478  
 479              $basedir = $viewFileMatch['base_dir'];
 480              $template = $viewFileMatch['template'];
 481              $file = $basedir . $template;
 482  
 483              $addFileResource = true;
 484              if ( isset( $viewFileMatch['custom_match'] ) )
 485              {
 486                  $spacing = $mainSpacing + 4;
 487                  $customMatchList = $viewFileMatch['custom_match'];
 488                  $matchCount = 0;
 489                  foreach ( $customMatchList as $customMatch )
 490                  {
 491                      $matchConditionCount = count( $customMatch['conditions'] );
 492                      $code = '';
 493                      if ( $matchCount > 0 )
 494                      {
 495                          $code = "else " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . "";
 496                      }
 497                      if ( $matchConditionCount > 0 )
 498                      {
 499                          if ( $matchCount > 0 )
 500                              $code .= " ";
 501                          $code .= "if " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . "( ";
 502  
 503                          $ifLength = strlen( $code );
 504                          $conditionCount = 0;
 505  
 506                          foreach ( $customMatch['conditions'] as $conditionName => $conditionValue )
 507                          {
 508                              if ( $conditionCount > 0 )
 509                                  $code .= " and\n" . str_repeat( ' ', $ifLength );
 510                              $conditionNameText = eZPHPCreator::variableText( $conditionName, 0 );
 511                              $conditionValueText = eZPHPCreator::variableText( $conditionValue, 0 );
 512                              if ( $conditionNameText == '"url_alias"' )
 513                              {
 514                                  $code .= "isset( \$" . $designKeysName . "[$conditionNameText] ) and " .
 515                                           "( strpos(\$" . $designKeysName . "[$conditionNameText], $conditionValueText ) === 0 )";
 516                              }
 517                              else
 518                              {
 519                                  $code .= "isset( \$" . $designKeysName . "[$conditionNameText] ) and " .
 520                                           "( ( is_array( \$" . $designKeysName . "[$conditionNameText] ) and in_array( $conditionValueText, \$" . $designKeysName . "[$conditionNameText] ) ) or " .
 521                                           "\$" . $designKeysName . "[$conditionNameText] == $conditionValueText )";
 522                              }
 523                              ++$conditionCount;
 524                          }
 525                          $code .= " )\n";
 526                      }
 527                      if ( $matchConditionCount > 0 or $matchCount > 0 )
 528                      {
 529                          $code .= "{";
 530                      }
 531                      $matchFile = $customMatch['match_file'];
 532                      $newNodes[] = eZTemplateNodeTool::createCodePieceNode( $code, array( 'spacing' => $acquisitionSpacing ) );
 533                      // If $matchFile is an array we cannot create a transformation for this entry
 534                      if ( is_array( $matchFile ) )
 535                          return false;
 536                      $newNodes[] = eZTemplateNodeTool::createResourceAcquisitionNode( '',
 537                                                                                       $matchFile, $matchFile,
 538                                                                                       EZ_RESOURCE_FETCH, false,
 539                                                                                       $node[4], array( 'spacing' => $spacing ),
 540                                                                                       $rule['namespace'] );
 541                      if ( $matchConditionCount > 0 or $matchCount > 0 )
 542                      {
 543                          $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "}", array( 'spacing' => $acquisitionSpacing ) );
 544                      }
 545                      ++$matchCount;
 546                      if ( $matchConditionCount == 0 )
 547                      {
 548                          if ( $matchCount > 0 )
 549                              $addFileResource = false;
 550                          break;
 551                      }
 552                  }
 553                  if ( $addFileResource )
 554                      $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "else " . ( $resourceData['use-comments'] ? ( "/*OF:" . __LINE__ . "*/" ) : "" ) . "\n{", array( 'spacing' => $acquisitionSpacing ) );
 555              }
 556              if ( $addFileResource )
 557              {
 558                  // If $matchFile is an array we cannot create a transformation for this entry
 559                  if ( is_array( $file ) )
 560                      return false;
 561                  $newNodes[] = eZTemplateNodeTool::createResourceAcquisitionNode( '',
 562                                                                                   $file, $file,
 563                                                                                   EZ_RESOURCE_FETCH, false,
 564                                                                                   $node[4], array( 'spacing' => $mainSpacing ),
 565                                                                                   $rule['namespace'] );
 566              }
 567              if ( isset( $viewFileMatch['custom_match'] ) and $addFileResource )
 568                  $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "}", array( 'spacing' => $acquisitionSpacing ) );
 569  
 570              if ( $hasAttributeAccess )
 571                  $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "}\n", array( 'spacing' => $acquisitionSpacing ) );
 572          }
 573          if ( isset( $attributeKeys ) )
 574          {
 575              $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$$designKeysName = array_pop( \$" . $designKeysName . "Stack );",
 576                                                                     array( 'spacing' => $acquisitionSpacing ) );
 577          }
 578          return $newNodes;
 579      }
 580  
 581      function process( &$tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace )
 582      {
 583          if ( !isset( $this->Rules[$functionName] ) )
 584          {
 585              $tpl->undefinedFunction( $functionName );
 586              return;
 587          }
 588          $rule =& $this->Rules[$functionName];
 589          $template_dir = $rule["template_root"];
 590          $input_name =& $rule["input_name"];
 591          $outCurrentNamespace = $currentNamespace;
 592          if ( isset( $rule['namespace'] ) )
 593          {
 594              $ruleNamespace = $rule['namespace'];
 595              if ( $ruleNamespace != '' )
 596              {
 597                  if ( $outCurrentNamespace != '' )
 598                      $outCurrentNamespace .= ':' . $ruleNamespace;
 599                  else
 600                      $outCurrentNamespace = $ruleNamespace;
 601              }
 602          }
 603  
 604          $params = $functionParameters;
 605          if ( !isset( $params[$input_name] ) )
 606          {
 607              $tpl->missingParameter( $functionName, $input_name );
 608              return;
 609          }
 610  
 611          $old_nspace = $rootNamespace;
 612  
 613          $input_var =& $tpl->elementValue( $params[$input_name], $rootNamespace, $currentNamespace, $functionPlacement );
 614          if ( !is_object( $input_var ) )
 615          {
 616              $tpl->warning( $functionName, "Parameter $input_name is not an object" );
 617              return;
 618          }
 619  
 620          $txt = "";
 621          $attribute_access =& $rule["attribute_access"];
 622          $view_mode = "";
 623          $view_dir = "";
 624          $view_var = null;
 625          $renderMode = false;
 626          if ( isset( $rule["render_mode"] ) )
 627          {
 628              $renderMode = $rule["render_mode"];
 629          }
 630          if ( isset( $params['render-mode'] ) )
 631          {
 632              $renderMode =& $tpl->elementValue( $params['render-mode'], $rootNamespace, $currentNamespace, $functionPlacement );
 633          }
 634          if ( $renderMode )
 635              $view_dir .= "/render-$renderMode";
 636          if ( $rule["use_views"] )
 637          {
 638              $view_var =& $rule["use_views"];
 639              if ( !isset( $params[$view_var] ) )
 640              {
 641                  if ( !isset( $rule['optional_views'] ) or
 642                       !$rule['optional_views'] )
 643                      $tpl->warning( $functionName, "No view specified, skipping views" );
 644              }
 645              else
 646              {
 647                  $view_mode =& $tpl->elementValue( $params[$view_var], $rootNamespace, $currentNamespace, $functionPlacement );
 648                  $view_dir .= "/" . $view_mode;
 649              }
 650          }
 651  
 652          $resourceKeys = false;
 653          if ( isset( $rule['attribute_keys'] ) )
 654          {
 655              $resourceKeys = array();
 656              $attributeKeys =& $rule['attribute_keys'];
 657              foreach( $attributeKeys as $attributeKey => $attributeSelection )
 658              {
 659                  $keyValue =& $tpl->variableAttribute( $input_var, $attributeSelection );
 660                  $resourceKeys[] = array( $attributeKey, $keyValue );
 661              }
 662          }
 663  
 664          $output_var =& $input_var;
 665          $res = null;
 666          $tried_files = array();
 667          $extraParameters = array();
 668          if ( $resourceKeys !== false )
 669              $extraParameters['ezdesign:keys'] = $resourceKeys;
 670          if ( is_array( $template_dir ) )
 671          {
 672              $templateRoot = $template_dir;
 673              $template_dir = '';
 674              if ( !isset( $templateRoot['type'] ) )
 675                  $tpl->error( $functionName,
 676                               'No template root type defined' );
 677              else if ( $templateRoot['type'] == 'multi_match' )
 678              {
 679                  if ( !isset( $templateRoot['attributes'] ) )
 680                      $tpl->error( $functionName,
 681                                   'No template root attributes defined' );
 682                  else if ( !isset( $templateRoot['matches'] ) )
 683                      $tpl->error( $functionName,
 684                                   'No template root matches defined' );
 685                  else
 686                  {
 687                      $templateRootValue =& $tpl->variableAttribute( $input_var, $templateRoot['attributes'] );
 688                      $templateRootMatches =& $templateRoot['matches'];
 689                      foreach ( $templateRootMatches as $templateRootMatch )
 690                      {
 691                          if ( $templateRootMatch[0] == $templateRootValue )
 692                          {
 693                              $template_dir = $templateRootMatch[1];
 694                              if ( is_array( $template_dir ) )
 695                              {
 696                                  $templateDirAttributesList = $template_dir[1];
 697                                  $template_dir = $template_dir[0];
 698                                  $attributeValues = array();
 699                                  foreach ( $templateDirAttributesList as $templateDirAttributes )
 700                                  {
 701                                      $attributeValues[] =& $tpl->variableAttribute( $input_var, $templateDirAttributes );
 702                                  }
 703                                  $template_dir .= implode( '/', $attributeValues );
 704                              }
 705                              break;
 706                          }
 707                      }
 708                  }
 709              }
 710              else
 711                  $tpl->error( $functionName,
 712                               'Unknown template root type: ' . $templateRoot['type'] );
 713          }
 714  
 715          $resourceData = null;
 716          $root = null;
 717          $canCache = false;
 718          if ( is_array( $attribute_access ) )
 719          {
 720              for ( $i = 0; $i < count( $attribute_access ) && !$res; ++$i )
 721              {
 722                  $attribute_access_array =& $attribute_access[$i];
 723                  $output_var =& $tpl->variableAttribute( $input_var, $attribute_access_array );
 724                  $incfile =& $output_var;
 725                  $uri = "design:$template_dir$view_dir/$incfile.tpl";
 726                  $resourceData =& $tpl->loadURIRoot( $uri, false, $extraParameters );
 727                  if ( $resourceData === null )
 728                      $tried_files[] = $uri;
 729                  else
 730                      break;
 731              }
 732              if ( $resourceData === null )
 733              {
 734                  $uri = "design:$template_dir/$view_mode.tpl";
 735                  $resourceData =& $tpl->loadURIRoot( $uri, false, $extraParameters );
 736                  if ( $resourceData === null )
 737                      $tried_files[] = $uri;
 738              }
 739          }
 740  
 741          if ( $resourceData !== null )
 742          {
 743              $designUsedKeys = array();
 744              $designMatchedKeys = array();
 745              if ( isset( $extraParameters['ezdesign:used_keys'] ) )
 746                  $designUsedKeys = $extraParameters['ezdesign:used_keys'];
 747              if ( isset( $extraParameters['ezdesign:matched_keys'] ) )
 748                  $designMatchedKeys = $extraParameters['ezdesign:matched_keys'];
 749              if ( $outCurrentNamespace != '' )
 750                  $designKeyNamespace = $outCurrentNamespace . ':DesignKeys';
 751              else
 752                  $designKeyNamespace = 'DesignKeys';
 753  
 754              $sub_text = "";
 755              $output_name =& $rule["output_name"];
 756              $setVariableArray = array();
 757              $tpl->setVariableRef( $output_name, $input_var, $outCurrentNamespace );
 758              $setVariableArray[] = $output_name;
 759              // Set design keys
 760              $tpl->setVariable( 'used', $designUsedKeys, $designKeyNamespace );
 761              $tpl->setVariable( 'matched', $designMatchedKeys, $designKeyNamespace );
 762              // Set function parameters
 763              foreach ( array_keys( $params ) as $paramName )
 764              {
 765                  if ( $paramName == $input_name or
 766                       $paramName == $view_var )
 767                      continue;
 768                  $paramValue =& $tpl->elementValue( $params[$paramName], $old_nspace, $currentNamespace, $functionPlacement );
 769                  $tpl->setVariableRef( $paramName, $paramValue, $outCurrentNamespace );
 770                  $setVariableArray[] = $paramName;
 771              }
 772              // Set constant variables
 773              if ( isset( $rule['constant_template_variables'] ) )
 774              {
 775                  foreach ( $rule['constant_template_variables'] as $constantTemplateVariableKey => $constantTemplateVariableValue )
 776                  {
 777                      if ( $constantTemplateVariableKey == $input_name or
 778                           $constantTemplateVariableKey == $view_var or
 779                           $tpl->hasVariable( $constantTemplateVariableKey, $currentNamespace ) )
 780                          continue;
 781                      $tpl->setVariableRef( $constantTemplateVariableKey, $constantTemplateVariableValue, $outCurrentNamespace );
 782                      $setVariableArray[] = $constantTemplateVariableKey;
 783                  }
 784              }
 785  
 786              $templateCompilationUsed = false;
 787              if ( $resourceData['compiled-template'] )
 788              {
 789                  if ( $tpl->executeCompiledTemplate( $resourceData, $textElements, $outCurrentNamespace, $outCurrentNamespace, $extraParameters ) )
 790                      $templateCompilationUsed = true;
 791              }
 792              if ( !$templateCompilationUsed and
 793                   $resourceData['root-node'] )
 794              {
 795                  $root =& $resourceData['root-node'];
 796                  $tpl->process( $root, $sub_text, $outCurrentNamespace, $outCurrentNamespace );
 797                  $tpl->setIncludeOutput( $uri, $sub_text );
 798  
 799                  $textElements[] = $sub_text;
 800              }
 801              foreach ( $setVariableArray as $setVariableName )
 802              {
 803                  $tpl->unsetVariable( $setVariableName, $outCurrentNamespace );
 804              }
 805          }
 806          else
 807          {
 808              $tpl->warning( $functionName,
 809                             "None of the templates " . implode( ", ", $tried_files ) .
 810                             " could be found" );
 811          }
 812      }
 813  
 814      function hasChildren()
 815      {
 816          return false;
 817      }
 818  
 819      var $Rules;
 820  };
 821  
 822  ?>


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