eZ Publish  [trunk]
eztemplateexecuteoperator.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZTemplateExecuteOperator class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package lib
00009  */
00010 
00011 /*!
00012   \class eZTemplateExecuteOperator eztemplateexecuteoperator.php
00013   \brief The class eZTemplateExecuteOperator does
00014 
00015 */
00016 
00017 class eZTemplateExecuteOperator
00018 {
00019     /*!
00020      Constructor
00021     */
00022     function eZTemplateExecuteOperator( $fetchName = 'fetch', $fetchAliasName = 'fetch_alias' )
00023     {
00024         $this->Operators = array( $fetchName, $fetchAliasName );
00025         $this->Fetch = $fetchName;
00026         $this->FetchAlias = $fetchAliasName;
00027     }
00028 
00029     /*!
00030      Returns the operators in this class.
00031     */
00032     function operatorList()
00033     {
00034         return $this->Operators;
00035     }
00036 
00037     /*!
00038      \return true to tell the template engine that the parameter list exists per operator type.
00039     */
00040     function namedParameterPerOperator()
00041     {
00042         return true;
00043     }
00044 
00045     function operatorTemplateHints()
00046     {
00047         return array( $this->Fetch => array( 'input' => false,
00048                                              'output' => true,
00049                                              'parameters' => true,
00050                                              'element-transformation' => true,
00051                                              'transform-parameters' => true,
00052                                              'element-transformation-func' => 'fetchTransform' ),
00053                       $this->FetchAlias => array( 'input' => false,
00054                                                   'output' => true,
00055                                                   'parameters' => true,
00056                                                   'element-transformation' => true,
00057                                                   'transform-parameters' => true,
00058                                                   'element-transformation-func' => 'fetchTransform' )
00059                       );
00060     }
00061 
00062     function fetchTransform( $operatorName, &$node, $tpl, &$resourceData,
00063                              $element, $lastElement, $elementList, $elementTree, &$parameters )
00064     {
00065         $parameterTranslation = false;
00066         $constParameters = array();
00067 
00068         if ( $operatorName == $this->Fetch )
00069         {
00070             if ( !eZTemplateNodeTool::isConstantElement( $parameters[0] ) ||
00071                  !eZTemplateNodeTool::isConstantElement( $parameters[1] ) )
00072             {
00073                 return false;
00074             }
00075 
00076             $moduleName = eZTemplateNodeTool::elementConstantValue( $parameters[0] );
00077             $functionName = eZTemplateNodeTool::elementConstantValue( $parameters[1] );
00078 
00079             $moduleFunctionInfo = eZFunctionHandler::moduleFunctionInfo( $moduleName );
00080             if ( !$moduleFunctionInfo->isValid() )
00081             {
00082                 eZDebug::writeError( "Cannot execute  module '$moduleName', no module found", __METHOD__ );
00083                 return array();
00084             }
00085             $fetchParameters = array();
00086             if ( isset( $parameters[2] ) )
00087                 $fetchParameters =  $parameters[2];
00088         }
00089         else if ( $operatorName == $this->FetchAlias )
00090         {
00091             if ( !eZTemplateNodeTool::isConstantElement( $parameters[0] ) )
00092             {
00093                 return false;
00094             }
00095 
00096             $aliasFunctionName = eZTemplateNodeTool::elementConstantValue( $parameters[0] );
00097 
00098             $aliasSettings = eZINI::instance( 'fetchalias.ini' );
00099             if ( $aliasSettings->hasSection( $aliasFunctionName ) )
00100             {
00101                 $moduleFunctionInfo = eZFunctionHandler::moduleFunctionInfo( $aliasSettings->variable( $aliasFunctionName, 'Module' ) );
00102                 if ( !$moduleFunctionInfo->isValid() )
00103                 {
00104                     eZDebug::writeError( "Cannot execute function '$aliasFunctionName' in module '$moduleName', no valid data", __METHOD__ );
00105                     return array();
00106                 }
00107 
00108                 $functionName = $aliasSettings->variable( $aliasFunctionName, 'FunctionName' );
00109 
00110                 $functionArray = array();
00111                 if ( $aliasSettings->hasVariable( $aliasFunctionName, 'Parameter' ) )
00112                 {
00113                     $parameterTranslation = $aliasSettings->variable( $aliasFunctionName, 'Parameter' );
00114                 }
00115 
00116                 if ( $aliasSettings->hasVariable( $aliasFunctionName, 'Constant' ) )
00117                 {
00118                     $constantParameterArray = $aliasSettings->variable( $aliasFunctionName, 'Constant' );
00119                     if ( is_array( $constantParameterArray ) )
00120                     {
00121                         foreach ( array_keys( $constantParameterArray ) as $constKey )
00122                         {
00123                             if ( $moduleFunctionInfo->isParameterArray( $functionName, $constKey ) )
00124                                 $constParameters[$constKey] = explode( ';', $constantParameterArray[$constKey] );
00125                             else
00126                                 $constParameters[$constKey] = $constantParameterArray[$constKey];
00127                         }
00128                     }
00129                 }
00130             }
00131             else
00132             {
00133                 $placement = eZTemplateNodeTool::extractFunctionNodePlacement( $node );
00134                 $tpl->warning( 'fetch_alias', "Fetch alias '$aliasFunctionName' is not defined in fetchalias.ini", $placement );
00135                 return array();
00136             }
00137 
00138             $fetchParameters = array();
00139             if ( isset( $parameters[1] ) )
00140                 $fetchParameters = $parameters[1];
00141         }
00142         else
00143         {
00144             return false;
00145         }
00146 
00147         $functionDefinition = $moduleFunctionInfo->preExecute( $functionName );
00148         if ( $functionDefinition === false )
00149         {
00150             return false;
00151         }
00152 
00153         $isDynamic = false;
00154         $isVariable = false;
00155         if ( eZTemplateNodeTool::isConstantElement( $fetchParameters ) )
00156         {
00157             $staticParameters = eZTemplateNodeTool::elementConstantValue( $fetchParameters );
00158             $functionKeys = array_keys( $staticParameters );
00159         }
00160         else if ( eZTemplateNodeTool::isDynamicArrayElement( $fetchParameters ) )
00161         {
00162             $isDynamic = true;
00163             $dynamicParameters = eZTemplateNodeTool::elementDynamicArray( $fetchParameters );
00164             $functionKeys = eZTemplateNodeTool::elementDynamicArrayKeys( $fetchParameters );
00165         }
00166         else if ( eZTemplateNodeTool::isVariableElement( $fetchParameters ) or
00167                   eZTemplateNodeTool::isInternalCodePiece( $fetchParameters ) )
00168         {
00169             $isVariable = true;
00170         }
00171         else
00172         {
00173             $functionKeys = array();
00174         }
00175 
00176         $paramCount = 0;
00177         $values = array();
00178         if ( $isVariable )
00179         {
00180             $values[] = $fetchParameters;
00181 
00182             $parametersCode = 'array( ';
00183             foreach( $functionDefinition['parameters'] as $parameterDefinition )
00184             {
00185                 if ( $paramCount != 0 )
00186                 {
00187                     $parametersCode .= ',' . "\n";
00188                 }
00189                 ++$paramCount;
00190 
00191                 $parameterName = $parameterDefinition['name'];
00192 
00193                 if ( $parameterTranslation )
00194                 {
00195                     if ( in_array( $parameterName, array_keys( $parameterTranslation ) ) )
00196                     {
00197                         $parameterName = $parameterTranslation[$parameterName];
00198                     }
00199                 }
00200 
00201                 $defaultValue = 'null';
00202                 if ( !$parameterDefinition['required'] )
00203                     $defaultValue = eZPHPCreator::variableText( $parameterDefinition['default'], 0, 0, false );
00204 
00205                 $parametersSelection = '%1%[\'' . $parameterName . '\']';
00206                 $parametersCode .= '( isset( ' . $parametersSelection . ' ) ? ' . $parametersSelection . " : $defaultValue )";
00207             }
00208 
00209             $parametersCode .= ' )';
00210 
00211         }
00212         else
00213         {
00214             $parametersCode = 'array( ';
00215             if ( count( $functionDefinition['parameters'] ) )
00216             {
00217                 foreach( $functionDefinition['parameters'] as $parameterDefinition )
00218                 {
00219                     if ( $paramCount != 0 )
00220                     {
00221                         $parametersCode .= ',' . "\n";
00222                     }
00223                     ++$paramCount;
00224 
00225                     $parameterName = $parameterDefinition['name'];
00226 
00227                     if ( $parameterTranslation )
00228                     {
00229                         if ( in_array( $parameterName, array_keys( $parameterTranslation ) ) )
00230                         {
00231                             $parameterName = $parameterTranslation[$parameterName];
00232                         }
00233                     }
00234 
00235                     $parametersCode .= '    \'' . $parameterName . '\' => ';
00236 
00237                     if ( in_array( $parameterName, $functionKeys ) )
00238                     {
00239                         if ( $isDynamic )
00240                         {
00241                             if ( eZTemplateNodeTool::isConstantElement( $dynamicParameters[$parameterName] ) )
00242                             {
00243                                 $parametersCode .= eZPHPCreator::variableText( eZTemplateNodeTool::elementConstantValue( $dynamicParameters[$parameterName] ), 0, 0, false );
00244                             }
00245                             else
00246                             {
00247                                 $values[] = $dynamicParameters[$parameterName];
00248                                 $parametersCode .= '%' . count( $values ) . '%';
00249                             }
00250                         }
00251                         else
00252                         {
00253                             $parametersCode .= eZPHPCreator::variableText( $staticParameters[$parameterName], 0, 0, false );
00254                         }
00255                     }
00256                     else if( $constParameters &&
00257                              isset( $constParameters[$parameterName] ) )
00258                     {
00259                         $parametersCode .= eZPHPCreator::variableText( $constParameters[$parameterName], 0, 0, false );
00260                     }
00261                     else
00262                     {
00263                         if ( $parameterDefinition['required'] )
00264                         {
00265                             return false;
00266                         }
00267                         else if ( isset( $parameterDefinition['default'] ) )
00268                         {
00269                             $parametersCode .= eZPHPCreator::variableText( $parameterDefinition['default'], 0, 0, false );
00270                         }
00271                         else
00272                         {
00273                             $parametersCode .= 'null';
00274                         }
00275                     }
00276                 }
00277             }
00278 
00279             $parametersCode .= ' )';
00280         }
00281 
00282         $code = '%output% = call_user_func_array( array( new ' . $functionDefinition['call_method']['class'] . '(), \'' . $functionDefinition['call_method']['method'] . '\' ),' . "\n" .
00283                  '  ' . $parametersCode . ' );';
00284         $code .= "\n";
00285 
00286         $code .= '%output% = isset( %output%[\'result\'] ) ? %output%[\'result\'] : null;' . "\n";
00287 
00288         return array( eZTemplateNodeTool::createCodePieceElement( $code, $values ) );
00289     }
00290 
00291     /*!
00292      See eZTemplateOperator::namedParameterList()
00293     */
00294     function namedParameterList()
00295     {
00296         return array( 'fetch' => array( 'module_name' => array( 'type' => 'string',
00297                                                                 'required' => true,
00298                                                                 'default' => false ),
00299                                         'function_name' => array( 'type' => 'string',
00300                                                                   'required' => true,
00301                                                                   'default' => false ),
00302                                         'function_parameters' => array( 'type' => 'array',
00303                                                                         'required' => false,
00304                                                                         'default' => array() ) ),
00305                       'fetch_alias' => array( 'function_name' => array( 'type' => 'string',
00306                                                                   'required' => true,
00307                                                                   'default' => false ),
00308                                               'function_parameters' => array( 'type' => 'array',
00309                                                                               'required' => false,
00310                                                                               'default' => array() ) ) );
00311     }
00312 
00313     /*!
00314      Calls a specified module function and returns the result.
00315     */
00316     function modify( $tpl, $operatorName, $operatorParameters,
00317                      $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters, $placement )
00318     {
00319         $functionName = $namedParameters['function_name'];
00320         $functionParameters = $namedParameters['function_parameters'];
00321 
00322         if ( $operatorName == $this->Fetch )
00323         {
00324             $moduleName = $namedParameters['module_name'];
00325             $result = eZFunctionHandler::execute( $moduleName, $functionName, $functionParameters );
00326             $operatorValue = $result;
00327         }
00328         else if ( $operatorName == $this->FetchAlias )
00329         {
00330             $result = eZFunctionHandler::executeAlias( $functionName, $functionParameters );
00331             $operatorValue = $result;
00332         }
00333     }
00334 
00335     /// \privatesection
00336     public $Operators;
00337 
00338     public $Fetch;
00339     public $FetchAlias;
00340 }
00341 
00342 ?>