00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 include_once( 'lib/eztemplate/classes/eztemplate.php' );
00041
00042 class eZTemplateExecuteOperator
00043 {
00044
00045
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
00056
00057 function &operatorList()
00058 {
00059 return $this->Operators;
00060 }
00061
00062
00063
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 $fetchParameters = array();
00161 if ( isset( $parameters[1] ) )
00162 $fetchParameters = $parameters[1];
00163 }
00164 else
00165 {
00166 return false;
00167 }
00168
00169 $functionDefinition =& $moduleFunctionInfo->preExecute( $functionName );
00170 if ( $functionDefinition === false )
00171 {
00172 return false;
00173 }
00174
00175 $isDynamic = false;
00176 $isVariable = false;
00177 if ( eZTemplateNodeTool::isStaticElement( $fetchParameters ) )
00178 {
00179 $staticParameters = eZTemplateNodeTool::elementStaticValue( $fetchParameters );
00180 $functionKeys = array_keys( $staticParameters );
00181 }
00182 else if ( eZTemplateNodeTool::isDynamicArrayElement( $fetchParameters ) )
00183 {
00184 $isDynamic = true;
00185 $dynamicParameters = eZTemplateNodeTool::elementDynamicArray( $fetchParameters );
00186 $functionKeys = eZTemplateNodeTool::elementDynamicArrayKeys( $fetchParameters );
00187 }
00188 else if ( eZTemplateNodeTool::isVariableElement( $fetchParameters ) or
00189 eZTemplateNodeTool::isInternalCodePiece( $fetchParameters ) )
00190 {
00191 $isVariable = true;
00192 }
00193 else
00194 {
00195 $functionKeys = array();
00196 }
00197
00198 $paramCount = 0;
00199 $values = array();
00200 $code = 'include_once( \'' . $functionDefinition['call_method']['include_file'] . '\' );' . "\n";
00201 if ( $isVariable )
00202 {
00203 $values[] = $fetchParameters;
00204
00205 $parametersCode = 'array( ';
00206 foreach( $functionDefinition['parameters'] as $parameterDefinition )
00207 {
00208 if ( $paramCount != 0 )
00209 {
00210 $parametersCode .= ',' . "\n";
00211 }
00212 ++$paramCount;
00213
00214 $parameterName = $parameterDefinition['name'];
00215
00216 if ( $parameterTranslation )
00217 {
00218 if ( in_array( $parameterName, array_keys( $parameterTranslation ) ) )
00219 {
00220 $parameterName = $parameterTranslation[$parameterName];
00221 }
00222 }
00223
00224 $defaultValue = 'null';
00225 if ( !$parameterDefinition['required'] )
00226 $defaultValue = eZPHPCreator::variableText( $parameterDefinition['default'], 0, 0, false );
00227
00228 $parametersSelection = '%1%[\'' . $parameterName . '\']';
00229 $parametersCode .= '( isset( ' . $parametersSelection . ' ) ? ' . $parametersSelection . " : $defaultValue )";
00230 }
00231
00232 $parametersCode .= ' )';
00233
00234 }
00235 else
00236 {
00237 $parametersCode = 'array( ';
00238 if ( count( $functionDefinition['parameters'] ) )
00239 {
00240 foreach( $functionDefinition['parameters'] as $parameterDefinition )
00241 {
00242 if ( $paramCount != 0 )
00243 {
00244 $parametersCode .= ',' . "\n";
00245 }
00246 ++$paramCount;
00247
00248 $parameterName = $parameterDefinition['name'];
00249
00250 if ( $parameterTranslation )
00251 {
00252 if ( in_array( $parameterName, array_keys( $parameterTranslation ) ) )
00253 {
00254 $parameterName = $parameterTranslation[$parameterName];
00255 }
00256 }
00257
00258 $parametersCode .= ' \'' . $parameterName . '\' => ';
00259
00260 if ( in_array( $parameterName, $functionKeys ) )
00261 {
00262 if ( $isDynamic )
00263 {
00264 if ( eZTemplateNodeTool::isStaticElement( $dynamicParameters[$parameterName] ) )
00265 {
00266 $parametersCode .= eZPHPCreator::variableText( eZTemplateNodeTool::elementStaticValue( $dynamicParameters[$parameterName] ), 0, 0, false );
00267 }
00268 else
00269 {
00270 $values[] = $dynamicParameters[$parameterName];
00271 $parametersCode .= '%' . count( $values ) . '%';
00272 }
00273 }
00274 else
00275 {
00276 $parametersCode .= eZPHPCreator::variableText( $staticParameters[$parameterName], 0, 0, false );
00277 }
00278 }
00279 else if( $constParameters &&
00280 isset( $constParameters[$parameterName] ) )
00281 {
00282 $parametersCode .= eZPHPCreator::variableText( $constParameters[$parameterName], 0, 0, false );
00283 }
00284 else
00285 {
00286 if ( $parameterDefinition['required'] )
00287 {
00288 return false;
00289 }
00290 else if ( isset( $parameterDefinition['default'] ) )
00291 {
00292 $parametersCode .= eZPHPCreator::variableText( $parameterDefinition['default'], 0, 0, false );
00293 }
00294 else
00295 {
00296 $parametersCode .= 'null';
00297 }
00298 }
00299 }
00300 }
00301
00302 $parametersCode .= ' )';
00303 }
00304
00305 $code .= '%output% = ';
00306 if ( $moduleFunctionInfo->UseOldCall )
00307 {
00308 $code .= 'call_user_method_array( ' . $functionDefinition['call_method']['method'] . ', new ' . $functionDefinition['call_method']['class'] . '(), ' . $parametersCode . ' );';
00309 }
00310 else
00311 {
00312 $code .= 'call_user_func_array( array( new ' . $functionDefinition['call_method']['class'] . '(), \'' . $functionDefinition['call_method']['method'] . '\' ),' . "\n" .
00313 ' ' . $parametersCode . ' );';
00314 }
00315 $code .= "\n";
00316
00317 $code .= '%output% = isset( %output%[\'result\'] ) ? %output%[\'result\'] : null;' . "\n";
00318
00319 return array( eZTemplateNodeTool::createCodePieceElement( $code, $values ) );
00320 }
00321
00322
00323
00324
00325 function namedParameterList()
00326 {
00327 return array( 'fetch' => array( 'module_name' => array( 'type' => 'string',
00328 'required' => true,
00329 'default' => false ),
00330 'function_name' => array( 'type' => 'string',
00331 'required' => true,
00332 'default' => false ),
00333 'function_parameters' => array( 'type' => 'array',
00334 'required' => false,
00335 'default' => array() ) ),
00336 'fetch_alias' => array( 'function_name' => array( 'type' => 'string',
00337 'required' => true,
00338 'default' => false ),
00339 'function_parameters' => array( 'type' => 'array',
00340 'required' => false,
00341 'default' => array() ) ) );
00342 }
00343
00344
00345
00346
00347 function modify( &$tpl, &$operatorName, &$operatorParameters,
00348 &$rootNamespace, &$currentNamespace, &$operatorValue, &$namedParameters )
00349 {
00350 $functionName = $namedParameters['function_name'];
00351 $functionParameters = $namedParameters['function_parameters'];
00352
00353 include_once( 'lib/ezutils/classes/ezfunctionhandler.php' );
00354
00355 if ( $operatorName == $this->Fetch )
00356 {
00357 $moduleName = $namedParameters['module_name'];
00358 $result = eZFunctionHandler::execute( $moduleName, $functionName, $functionParameters );
00359 $operatorValue = $result;
00360 }
00361 else if ( $operatorName == $this->FetchAlias )
00362 {
00363 $result = eZFunctionHandler::executeAlias( $functionName, $functionParameters );
00364 $operatorValue = $result;
00365 }
00366 }
00367
00368
00369 var $Operators;
00370
00371 var $Fetch;
00372 var $FetchAlias;
00373 }
00374
00375 ?>