/lib/eztemplate/classes/eztemplateexecuteoperator.php

https://github.com/zerustech/ezpublish · PHP · 345 lines · 274 code · 39 blank · 32 comment · 46 complexity · 353bb5d6c1c5e322a442332fa549b3e5 MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the eZTemplateExecuteOperator class.
  4. *
  5. * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  6. * @license For full copyright and license information view LICENSE file distributed with this source code.
  7. * @version //autogentag//
  8. * @package lib
  9. */
  10. /*!
  11. \class eZTemplateExecuteOperator eztemplateexecuteoperator.php
  12. \brief The class eZTemplateExecuteOperator does
  13. */
  14. class eZTemplateExecuteOperator
  15. {
  16. /**
  17. * Constructor
  18. *
  19. * @param string $fetchName
  20. * @param string $fetchAliasName
  21. */
  22. public function __construct( $fetchName = 'fetch', $fetchAliasName = 'fetch_alias' )
  23. {
  24. $this->Operators = array( $fetchName, $fetchAliasName );
  25. $this->Fetch = $fetchName;
  26. $this->FetchAlias = $fetchAliasName;
  27. }
  28. /*!
  29. Returns the operators in this class.
  30. */
  31. function operatorList()
  32. {
  33. return $this->Operators;
  34. }
  35. /*!
  36. \return true to tell the template engine that the parameter list exists per operator type.
  37. */
  38. function namedParameterPerOperator()
  39. {
  40. return true;
  41. }
  42. function operatorTemplateHints()
  43. {
  44. return array( $this->Fetch => array( 'input' => false,
  45. 'output' => true,
  46. 'parameters' => true,
  47. 'element-transformation' => true,
  48. 'transform-parameters' => true,
  49. 'element-transformation-func' => 'fetchTransform' ),
  50. $this->FetchAlias => array( 'input' => false,
  51. 'output' => true,
  52. 'parameters' => true,
  53. 'element-transformation' => true,
  54. 'transform-parameters' => true,
  55. 'element-transformation-func' => 'fetchTransform' )
  56. );
  57. }
  58. function fetchTransform( $operatorName, &$node, $tpl, &$resourceData,
  59. $element, $lastElement, $elementList, $elementTree, &$parameters )
  60. {
  61. $parameterTranslation = false;
  62. $constParameters = array();
  63. if ( $operatorName == $this->Fetch )
  64. {
  65. if ( !eZTemplateNodeTool::isConstantElement( $parameters[0] ) ||
  66. !eZTemplateNodeTool::isConstantElement( $parameters[1] ) )
  67. {
  68. return false;
  69. }
  70. $moduleName = eZTemplateNodeTool::elementConstantValue( $parameters[0] );
  71. $functionName = eZTemplateNodeTool::elementConstantValue( $parameters[1] );
  72. $moduleFunctionInfo = eZFunctionHandler::moduleFunctionInfo( $moduleName );
  73. if ( !$moduleFunctionInfo->isValid() )
  74. {
  75. eZDebug::writeError( "Cannot execute module '$moduleName', no module found", __METHOD__ );
  76. return array();
  77. }
  78. $fetchParameters = array();
  79. if ( isset( $parameters[2] ) )
  80. $fetchParameters = $parameters[2];
  81. }
  82. else if ( $operatorName == $this->FetchAlias )
  83. {
  84. if ( !eZTemplateNodeTool::isConstantElement( $parameters[0] ) )
  85. {
  86. return false;
  87. }
  88. $aliasFunctionName = eZTemplateNodeTool::elementConstantValue( $parameters[0] );
  89. $aliasSettings = eZINI::instance( 'fetchalias.ini' );
  90. if ( $aliasSettings->hasSection( $aliasFunctionName ) )
  91. {
  92. $moduleFunctionInfo = eZFunctionHandler::moduleFunctionInfo( $aliasSettings->variable( $aliasFunctionName, 'Module' ) );
  93. if ( !$moduleFunctionInfo->isValid() )
  94. {
  95. eZDebug::writeError( "Cannot execute function '$aliasFunctionName' in module '$moduleName', no valid data", __METHOD__ );
  96. return array();
  97. }
  98. $functionName = $aliasSettings->variable( $aliasFunctionName, 'FunctionName' );
  99. $functionArray = array();
  100. if ( $aliasSettings->hasVariable( $aliasFunctionName, 'Parameter' ) )
  101. {
  102. $parameterTranslation = $aliasSettings->variable( $aliasFunctionName, 'Parameter' );
  103. }
  104. if ( $aliasSettings->hasVariable( $aliasFunctionName, 'Constant' ) )
  105. {
  106. $constantParameterArray = $aliasSettings->variable( $aliasFunctionName, 'Constant' );
  107. if ( is_array( $constantParameterArray ) )
  108. {
  109. foreach ( array_keys( $constantParameterArray ) as $constKey )
  110. {
  111. if ( $moduleFunctionInfo->isParameterArray( $functionName, $constKey ) )
  112. $constParameters[$constKey] = explode( ';', $constantParameterArray[$constKey] );
  113. else
  114. $constParameters[$constKey] = $constantParameterArray[$constKey];
  115. }
  116. }
  117. }
  118. }
  119. else
  120. {
  121. $placement = eZTemplateNodeTool::extractFunctionNodePlacement( $node );
  122. $tpl->warning( 'fetch_alias', "Fetch alias '$aliasFunctionName' is not defined in fetchalias.ini", $placement );
  123. return array();
  124. }
  125. $fetchParameters = array();
  126. if ( isset( $parameters[1] ) )
  127. $fetchParameters = $parameters[1];
  128. }
  129. else
  130. {
  131. return false;
  132. }
  133. $functionDefinition = $moduleFunctionInfo->preExecute( $functionName );
  134. if ( $functionDefinition === false )
  135. {
  136. return false;
  137. }
  138. $isDynamic = false;
  139. $isVariable = false;
  140. if ( eZTemplateNodeTool::isConstantElement( $fetchParameters ) )
  141. {
  142. $staticParameters = eZTemplateNodeTool::elementConstantValue( $fetchParameters );
  143. $functionKeys = array_keys( $staticParameters );
  144. }
  145. else if ( eZTemplateNodeTool::isDynamicArrayElement( $fetchParameters ) )
  146. {
  147. $isDynamic = true;
  148. $dynamicParameters = eZTemplateNodeTool::elementDynamicArray( $fetchParameters );
  149. $functionKeys = eZTemplateNodeTool::elementDynamicArrayKeys( $fetchParameters );
  150. }
  151. else if ( eZTemplateNodeTool::isVariableElement( $fetchParameters ) or
  152. eZTemplateNodeTool::isInternalCodePiece( $fetchParameters ) )
  153. {
  154. $isVariable = true;
  155. }
  156. else
  157. {
  158. $functionKeys = array();
  159. }
  160. $paramCount = 0;
  161. $values = array();
  162. if ( $isVariable )
  163. {
  164. $values[] = $fetchParameters;
  165. $parametersCode = 'array( ';
  166. foreach( $functionDefinition['parameters'] as $parameterDefinition )
  167. {
  168. if ( $paramCount != 0 )
  169. {
  170. $parametersCode .= ',' . "\n";
  171. }
  172. ++$paramCount;
  173. $parameterName = $parameterDefinition['name'];
  174. if ( $parameterTranslation )
  175. {
  176. if ( in_array( $parameterName, array_keys( $parameterTranslation ) ) )
  177. {
  178. $parameterName = $parameterTranslation[$parameterName];
  179. }
  180. }
  181. $defaultValue = 'null';
  182. if ( !$parameterDefinition['required'] )
  183. $defaultValue = eZPHPCreator::variableText( $parameterDefinition['default'], 0, 0, false );
  184. $parametersSelection = '%1%[\'' . $parameterName . '\']';
  185. $parametersCode .= '( isset( ' . $parametersSelection . ' ) ? ' . $parametersSelection . " : $defaultValue )";
  186. }
  187. $parametersCode .= ' )';
  188. }
  189. else
  190. {
  191. $parametersCode = 'array( ';
  192. if ( count( $functionDefinition['parameters'] ) )
  193. {
  194. foreach( $functionDefinition['parameters'] as $parameterDefinition )
  195. {
  196. if ( $paramCount != 0 )
  197. {
  198. $parametersCode .= ',' . "\n";
  199. }
  200. ++$paramCount;
  201. $parameterName = $parameterDefinition['name'];
  202. if ( $parameterTranslation )
  203. {
  204. if ( in_array( $parameterName, array_keys( $parameterTranslation ) ) )
  205. {
  206. $parameterName = $parameterTranslation[$parameterName];
  207. }
  208. }
  209. $parametersCode .= ' \'' . $parameterName . '\' => ';
  210. if ( in_array( $parameterName, $functionKeys ) )
  211. {
  212. if ( $isDynamic )
  213. {
  214. if ( eZTemplateNodeTool::isConstantElement( $dynamicParameters[$parameterName] ) )
  215. {
  216. $parametersCode .= eZPHPCreator::variableText( eZTemplateNodeTool::elementConstantValue( $dynamicParameters[$parameterName] ), 0, 0, false );
  217. }
  218. else
  219. {
  220. $values[] = $dynamicParameters[$parameterName];
  221. $parametersCode .= '%' . count( $values ) . '%';
  222. }
  223. }
  224. else
  225. {
  226. $parametersCode .= eZPHPCreator::variableText( $staticParameters[$parameterName], 0, 0, false );
  227. }
  228. }
  229. else if( $constParameters &&
  230. isset( $constParameters[$parameterName] ) )
  231. {
  232. $parametersCode .= eZPHPCreator::variableText( $constParameters[$parameterName], 0, 0, false );
  233. }
  234. else
  235. {
  236. if ( $parameterDefinition['required'] )
  237. {
  238. return false;
  239. }
  240. else if ( isset( $parameterDefinition['default'] ) )
  241. {
  242. $parametersCode .= eZPHPCreator::variableText( $parameterDefinition['default'], 0, 0, false );
  243. }
  244. else
  245. {
  246. $parametersCode .= 'null';
  247. }
  248. }
  249. }
  250. }
  251. $parametersCode .= ' )';
  252. }
  253. $code = '%output% = call_user_func_array( array( new ' . $functionDefinition['call_method']['class'] . '(), \'' . $functionDefinition['call_method']['method'] . '\' ),' . "\n" .
  254. ' ' . $parametersCode . ' );';
  255. $code .= "\n";
  256. $code .= '%output% = isset( %output%[\'result\'] ) ? %output%[\'result\'] : null;' . "\n";
  257. return array( eZTemplateNodeTool::createCodePieceElement( $code, $values ) );
  258. }
  259. /*!
  260. See eZTemplateOperator::namedParameterList()
  261. */
  262. function namedParameterList()
  263. {
  264. return array( 'fetch' => array( 'module_name' => array( 'type' => 'string',
  265. 'required' => true,
  266. 'default' => false ),
  267. 'function_name' => array( 'type' => 'string',
  268. 'required' => true,
  269. 'default' => false ),
  270. 'function_parameters' => array( 'type' => 'array',
  271. 'required' => false,
  272. 'default' => array() ) ),
  273. 'fetch_alias' => array( 'function_name' => array( 'type' => 'string',
  274. 'required' => true,
  275. 'default' => false ),
  276. 'function_parameters' => array( 'type' => 'array',
  277. 'required' => false,
  278. 'default' => array() ) ) );
  279. }
  280. /*!
  281. Calls a specified module function and returns the result.
  282. */
  283. function modify( $tpl, $operatorName, $operatorParameters,
  284. $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters, $placement )
  285. {
  286. $functionName = $namedParameters['function_name'];
  287. $functionParameters = $namedParameters['function_parameters'];
  288. if ( $operatorName == $this->Fetch )
  289. {
  290. $moduleName = $namedParameters['module_name'];
  291. $result = eZFunctionHandler::execute( $moduleName, $functionName, $functionParameters );
  292. $operatorValue = $result;
  293. }
  294. else if ( $operatorName == $this->FetchAlias )
  295. {
  296. $result = eZFunctionHandler::executeAlias( $functionName, $functionParameters );
  297. $operatorValue = $result;
  298. }
  299. }
  300. /// \privatesection
  301. public $Operators;
  302. public $Fetch;
  303. public $FetchAlias;
  304. }
  305. ?>