/lib/eztemplate/classes/eztemplateexecuteoperator.php

https://github.com/Yannix/ezpublish · PHP · 342 lines · 274 code · 39 blank · 29 comment · 46 complexity · 03eb6f1a7afe4c28d651c6eb2924a3ea MD5 · raw file

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