eZ Publish  [4.0]
eztemplateexecuteoperator.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZTemplateExecuteOperator class
00004 //
00005 // Created on: <06-Oct-2002 17:53:19 amos>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*! \file eztemplateexecuteoperator.php
00032 */
00033 
00034 /*!
00035   \class eZTemplateExecuteOperator eztemplateexecuteoperator.php
00036   \brief The class eZTemplateExecuteOperator does
00037 
00038 */
00039 
00040 //include_once( 'lib/eztemplate/classes/eztemplate.php' );
00041 
00042 class eZTemplateExecuteOperator
00043 {
00044     /*!
00045      Constructor
00046     */
00047     function eZTemplateExecuteOperator( $fetchName = 'fetch', $fetchAliasName = 'fetch_alias' )
00048     {
00049         $this->Operators = array( $fetchName, $fetchAliasName );
00050         $this->Fetch = $fetchName;
00051         $this->FetchAlias = $fetchAliasName;
00052     }
00053 
00054     /*!
00055      Returns the operators in this class.
00056     */
00057     function operatorList()
00058     {
00059         return $this->Operators;
00060     }
00061 
00062     /*!
00063      \return true to tell the template engine that the parameter list exists per operator type.
00064     */
00065     function namedParameterPerOperator()
00066     {
00067         return true;
00068     }
00069 
00070     function operatorTemplateHints()
00071     {
00072         return array( $this->Fetch => array( 'input' => false,
00073                                              'output' => true,
00074                                              'parameters' => true,
00075                                              'element-transformation' => true,
00076                                              'transform-parameters' => true,
00077                                              'element-transformation-func' => 'fetchTransform' ),
00078                       $this->FetchAlias => array( 'input' => false,
00079                                                   'output' => true,
00080                                                   'parameters' => true,
00081                                                   'element-transformation' => true,
00082                                                   'transform-parameters' => true,
00083                                                   'element-transformation-func' => 'fetchTransform' )
00084                       );
00085     }
00086 
00087     function fetchTransform( $operatorName, &$node, $tpl, &$resourceData,
00088                              $element, $lastElement, $elementList, $elementTree, &$parameters )
00089     {
00090         $parameterTranslation = false;
00091         $constParameters = array();
00092 
00093         if ( $operatorName == $this->Fetch )
00094         {
00095             if ( !eZTemplateNodeTool::isStaticElement( $parameters[0] ) ||
00096                  !eZTemplateNodeTool::isStaticElement( $parameters[1] ) )
00097             {
00098                 return false;
00099             }
00100 
00101             $moduleName = eZTemplateNodeTool::elementStaticValue( $parameters[0] );
00102             $functionName = eZTemplateNodeTool::elementStaticValue( $parameters[1] );
00103 
00104             //include_once( 'lib/ezutils/classes/ezfunctionhandler.php' );
00105             $moduleFunctionInfo = eZFunctionHandler::moduleFunctionInfo( $moduleName );
00106             if ( !$moduleFunctionInfo->isValid() )
00107             {
00108                 eZDebug::writeError( "Cannot execute  module '$moduleName', no module found",
00109                                      'eZFunctionHandler::execute' );
00110                 return array();
00111             }
00112             $fetchParameters = array();
00113             if ( isset( $parameters[2] ) )
00114                 $fetchParameters =  $parameters[2];
00115         }
00116         else if ( $operatorName == $this->FetchAlias )
00117         {
00118             if ( !eZTemplateNodeTool::isStaticElement( $parameters[0] ) )
00119             {
00120                 return false;
00121             }
00122 
00123             $aliasFunctionName = eZTemplateNodeTool::elementStaticValue( $parameters[0] );
00124 
00125             $aliasSettings = eZINI::instance( 'fetchalias.ini' );
00126             if ( $aliasSettings->hasSection( $aliasFunctionName ) )
00127             {
00128                 //include_once( 'lib/ezutils/classes/ezfunctionhandler.php' );
00129                 $moduleFunctionInfo = eZFunctionHandler::moduleFunctionInfo( $aliasSettings->variable( $aliasFunctionName, 'Module' ) );
00130                 if ( !$moduleFunctionInfo->isValid() )
00131                 {
00132                     eZDebug::writeError( "Cannot execute function '$aliasFunctionName' in module '$moduleName', no valid data",
00133                                          'eZFunctionHandler::executeAlias' );
00134                     return array();
00135                 }
00136 
00137                 $functionName = $aliasSettings->variable( $aliasFunctionName, 'FunctionName' );
00138 
00139                 $functionArray = array();
00140                 if ( $aliasSettings->hasVariable( $aliasFunctionName, 'Parameter' ) )
00141                 {
00142                     $parameterTranslation = $aliasSettings->variable( $aliasFunctionName, 'Parameter' );
00143                 }
00144 
00145                 if ( $aliasSettings->hasVariable( $aliasFunctionName, 'Constant' ) )
00146                 {
00147                     $constantParameterArray = $aliasSettings->variable( $aliasFunctionName, 'Constant' );
00148                     if ( is_array( $constantParameterArray ) )
00149                     {
00150                         foreach ( array_keys( $constantParameterArray ) as $constKey )
00151                         {
00152                             if ( $moduleFunctionInfo->isParameterArray( $functionName, $constKey ) )
00153                                 $constParameters[$constKey] = explode( ';', $constantParameterArray[$constKey] );
00154                             else
00155                                 $constParameters[$constKey] = $constantParameterArray[$constKey];
00156                         }
00157                     }
00158                 }
00159             }
00160             else
00161             {
00162                 $placement = eZTemplateNodeTool::extractFunctionNodePlacement( $node );
00163                 $tpl->warning( 'fetch_alias', "Fetch alias '$aliasFunctionName' is not defined in fetchalias.ini", $placement );
00164                 return array();
00165             }
00166 
00167             $fetchParameters = array();
00168             if ( isset( $parameters[1] ) )
00169                 $fetchParameters = $parameters[1];
00170         }
00171         else
00172         {
00173             return false;
00174         }
00175 
00176         $functionDefinition = $moduleFunctionInfo->preExecute( $functionName );
00177         if ( $functionDefinition === false )
00178         {
00179             return false;
00180         }
00181 
00182         $isDynamic = false;
00183         $isVariable = false;
00184         if ( eZTemplateNodeTool::isStaticElement( $fetchParameters ) )
00185         {
00186             $staticParameters = eZTemplateNodeTool::elementStaticValue( $fetchParameters );
00187             $functionKeys = array_keys( $staticParameters );
00188         }
00189         else if ( eZTemplateNodeTool::isDynamicArrayElement( $fetchParameters ) )
00190         {
00191             $isDynamic = true;
00192             $dynamicParameters = eZTemplateNodeTool::elementDynamicArray( $fetchParameters );
00193             $functionKeys = eZTemplateNodeTool::elementDynamicArrayKeys( $fetchParameters );
00194         }
00195         else if ( eZTemplateNodeTool::isVariableElement( $fetchParameters ) or
00196                   eZTemplateNodeTool::isInternalCodePiece( $fetchParameters ) )
00197         {
00198             $isVariable = true;
00199         }
00200         else
00201         {
00202             $functionKeys = array();
00203         }
00204 
00205         $paramCount = 0;
00206         $values = array();
00207         if ( $isVariable )
00208         {
00209             $values[] = $fetchParameters;
00210 
00211             $parametersCode = 'array( ';
00212             foreach( $functionDefinition['parameters'] as $parameterDefinition )
00213             {
00214                 if ( $paramCount != 0 )
00215                 {
00216                     $parametersCode .= ',' . "\n";
00217                 }
00218                 ++$paramCount;
00219 
00220                 $parameterName = $parameterDefinition['name'];
00221 
00222                 if ( $parameterTranslation )
00223                 {
00224                     if ( in_array( $parameterName, array_keys( $parameterTranslation ) ) )
00225                     {
00226                         $parameterName = $parameterTranslation[$parameterName];
00227                     }
00228                 }
00229 
00230                 $defaultValue = 'null';
00231                 if ( !$parameterDefinition['required'] )
00232                     $defaultValue = eZPHPCreator::variableText( $parameterDefinition['default'], 0, 0, false );
00233 
00234                 $parametersSelection = '%1%[\'' . $parameterName . '\']';
00235                 $parametersCode .= '( isset( ' . $parametersSelection . ' ) ? ' . $parametersSelection . " : $defaultValue )";
00236             }
00237 
00238             $parametersCode .= ' )';
00239 
00240         }
00241         else
00242         {
00243             $parametersCode = 'array( ';
00244             if ( count( $functionDefinition['parameters'] ) )
00245             {
00246                 foreach( $functionDefinition['parameters'] as $parameterDefinition )
00247                 {
00248                     if ( $paramCount != 0 )
00249                     {
00250                         $parametersCode .= ',' . "\n";
00251                     }
00252                     ++$paramCount;
00253 
00254                     $parameterName = $parameterDefinition['name'];
00255 
00256                     if ( $parameterTranslation )
00257                     {
00258                         if ( in_array( $parameterName, array_keys( $parameterTranslation ) ) )
00259                         {
00260                             $parameterName = $parameterTranslation[$parameterName];
00261                         }
00262                     }
00263 
00264                     $parametersCode .= '    \'' . $parameterName . '\' => ';
00265 
00266                     if ( in_array( $parameterName, $functionKeys ) )
00267                     {
00268                         if ( $isDynamic )
00269                         {
00270                             if ( eZTemplateNodeTool::isStaticElement( $dynamicParameters[$parameterName] ) )
00271                             {
00272                                 $parametersCode .= eZPHPCreator::variableText( eZTemplateNodeTool::elementStaticValue( $dynamicParameters[$parameterName] ), 0, 0, false );
00273                             }
00274                             else
00275                             {
00276                                 $values[] = $dynamicParameters[$parameterName];
00277                                 $parametersCode .= '%' . count( $values ) . '%';
00278                             }
00279                         }
00280                         else
00281                         {
00282                             $parametersCode .= eZPHPCreator::variableText( $staticParameters[$parameterName], 0, 0, false );
00283                         }
00284                     }
00285                     else if( $constParameters &&
00286                              isset( $constParameters[$parameterName] ) )
00287                     {
00288                         $parametersCode .= eZPHPCreator::variableText( $constParameters[$parameterName], 0, 0, false );
00289                     }
00290                     else
00291                     {
00292                         if ( $parameterDefinition['required'] )
00293                         {
00294                             return false;
00295                         }
00296                         else if ( isset( $parameterDefinition['default'] ) )
00297                         {
00298                             $parametersCode .= eZPHPCreator::variableText( $parameterDefinition['default'], 0, 0, false );
00299                         }
00300                         else
00301                         {
00302                             $parametersCode .= 'null';
00303                         }
00304                     }
00305                 }
00306             }
00307 
00308             $parametersCode .= ' )';
00309         }
00310 
00311         $code = '%output% = call_user_func_array( array( new ' . $functionDefinition['call_method']['class'] . '(), \'' . $functionDefinition['call_method']['method'] . '\' ),' . "\n" .
00312                  '  ' . $parametersCode . ' );';
00313         $code .= "\n";
00314 
00315         $code .= '%output% = isset( %output%[\'result\'] ) ? %output%[\'result\'] : null;' . "\n";
00316 
00317         return array( eZTemplateNodeTool::createCodePieceElement( $code, $values ) );
00318     }
00319 
00320     /*!
00321      See eZTemplateOperator::namedParameterList()
00322     */
00323     function namedParameterList()
00324     {
00325         return array( 'fetch' => array( 'module_name' => array( 'type' => 'string',
00326                                                                 'required' => true,
00327                                                                 'default' => false ),
00328                                         'function_name' => array( 'type' => 'string',
00329                                                                   'required' => true,
00330                                                                   'default' => false ),
00331                                         'function_parameters' => array( 'type' => 'array',
00332                                                                         'required' => false,
00333                                                                         'default' => array() ) ),
00334                       'fetch_alias' => array( 'function_name' => array( 'type' => 'string',
00335                                                                   'required' => true,
00336                                                                   'default' => false ),
00337                                               'function_parameters' => array( 'type' => 'array',
00338                                                                               'required' => false,
00339                                                                               'default' => array() ) ) );
00340     }
00341 
00342     /*!
00343      Calls a specified module function and returns the result.
00344     */
00345     function modify( $tpl, $operatorName, $operatorParameters,
00346                      $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters )
00347     {
00348         $functionName = $namedParameters['function_name'];
00349         $functionParameters = $namedParameters['function_parameters'];
00350 
00351         //include_once( 'lib/ezutils/classes/ezfunctionhandler.php' );
00352 
00353         if ( $operatorName == $this->Fetch )
00354         {
00355             $moduleName = $namedParameters['module_name'];
00356             $result = eZFunctionHandler::execute( $moduleName, $functionName, $functionParameters );
00357             $operatorValue = $result;
00358         }
00359         else if ( $operatorName == $this->FetchAlias )
00360         {
00361             $result = eZFunctionHandler::executeAlias( $functionName, $functionParameters );
00362             $operatorValue = $result;
00363         }
00364     }
00365 
00366     /// \privatesection
00367     public $Operators;
00368 
00369     public $Fetch;
00370     public $FetchAlias;
00371 }
00372 
00373 ?>