/lib/eztemplate/classes/eztemplatetextoperator.php

https://github.com/lserwatka/ezpublish · PHP · 307 lines · 235 code · 20 blank · 52 comment · 34 complexity · 453ab3ea3dac3d83f82ec20f6bfd92f3 MD5 · raw file

  1. <?php
  2. //
  3. // Definition of eZTemplateTextOperator class
  4. //
  5. // Created on: <01-Aug-2002 11:38:40 amos>
  6. //
  7. // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  8. // SOFTWARE NAME: eZ Publish
  9. // SOFTWARE RELEASE: 4.1.x
  10. // COPYRIGHT NOTICE: Copyright (C) 1999-2010 eZ Systems AS
  11. // SOFTWARE LICENSE: GNU General Public License v2.0
  12. // NOTICE: >
  13. // This program is free software; you can redistribute it and/or
  14. // modify it under the terms of version 2.0 of the GNU General
  15. // Public License as published by the Free Software Foundation.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU General Public License for more details.
  21. //
  22. // You should have received a copy of version 2.0 of the GNU General
  23. // Public License along with this program; if not, write to the Free
  24. // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25. // MA 02110-1301, USA.
  26. //
  27. //
  28. // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  29. //
  30. /*! \file
  31. */
  32. /*!
  33. \class eZTemplateTextOperator eztemplatetextoperator.php
  34. \brief The class eZTemplateTextOperator does
  35. */
  36. class eZTemplateTextOperator
  37. {
  38. /*!
  39. Constructor
  40. */
  41. function eZTemplateTextOperator()
  42. {
  43. $this->Operators= array( 'concat', 'indent' );
  44. foreach ( $this->Operators as $operator )
  45. {
  46. $name = $operator . 'Name';
  47. $name[0] = $name[0] & "\xdf";
  48. $this->$name = $operator;
  49. }
  50. }
  51. /*!
  52. Returns the operators in this class.
  53. */
  54. function operatorList()
  55. {
  56. return $this->Operators;
  57. }
  58. function operatorTemplateHints()
  59. {
  60. return array( $this->ConcatName => array( 'input' => true,
  61. 'output' => true,
  62. 'parameters' => true,
  63. 'element-transformation' => true,
  64. 'transform-parameters' => true,
  65. 'input-as-parameter' => true,
  66. 'element-transformation-func' => 'concatTransformation'),
  67. $this->IndentName => array( 'input' => true,
  68. 'output' => true,
  69. 'parameters' => 3,
  70. 'element-transformation' => true,
  71. 'transform-parameters' => true,
  72. 'input-as-parameter' => true,
  73. 'element-transformation-func' => 'indentTransformation') ) ;
  74. }
  75. /*!
  76. \return true to tell the template engine that the parameter list exists per operator type.
  77. */
  78. function namedParameterPerOperator()
  79. {
  80. return true;
  81. }
  82. /*!
  83. See eZTemplateOperator::namedParameterList
  84. */
  85. function namedParameterList()
  86. {
  87. return array( $this->IndentName => array( 'indent_count' => array( 'type' => 'integer',
  88. 'required' => true,
  89. 'default' => false ),
  90. 'indent_type' => array( 'type' => 'identifier',
  91. 'required' => false,
  92. 'default' => 'space' ),
  93. 'indent_filler' => array( 'type' => 'string',
  94. 'required' => false,
  95. 'default' => false ) ) );
  96. }
  97. function indentTransformation( $operatorName, &$node, $tpl, &$resourceData,
  98. $element, $lastElement, $elementList, $elementTree, &$parameters )
  99. {
  100. $values = array();
  101. $count = $type = $filler = false;
  102. $paramCount = count( $parameters );
  103. if ( $paramCount == 4 )
  104. {
  105. if ( eZTemplateNodeTool::isStaticElement( $parameters[3] ) )
  106. {
  107. $filler = eZTemplateNodeTool::elementStaticValue( $parameters[3] );
  108. }
  109. }
  110. if ( $paramCount >= 3 )
  111. {
  112. if ( eZTemplateNodeTool::isStaticElement( $parameters[2] ) )
  113. {
  114. $type = eZTemplateNodeTool::elementStaticValue( $parameters[2] );
  115. if ( $type == 'space' )
  116. {
  117. $filler = ' ';
  118. }
  119. else if ( $type == 'tab' )
  120. {
  121. $filler = "\t";
  122. }
  123. else if ( $type != 'custom' )
  124. {
  125. $filler = ' ';
  126. }
  127. }
  128. }
  129. if ( $paramCount >= 2 )
  130. {
  131. if ( eZTemplateNodeTool::isStaticElement( $parameters[1] ) )
  132. {
  133. $count = eZTemplateNodeTool::elementStaticValue( $parameters[1] );
  134. }
  135. if ( $paramCount < 3 )
  136. {
  137. $type = 'space';
  138. $filler = ' ';
  139. }
  140. }
  141. $newElements = array();
  142. if ( $count and $type and $filler )
  143. {
  144. $tmpCount = 0;
  145. $values[] = $parameters[0];
  146. if ( $count < 0 )
  147. {
  148. //$code = ( $tpl->error( "indent", "Count parameter can not be negative" );
  149. $code = ( "\$tpl->error( \"indent\", \"Count parameter can not be negative, string won't be indented\" );\n" .
  150. "%output% = %1%;\n" );
  151. }
  152. else
  153. {
  154. $indentation = str_repeat( $filler, $count );
  155. $code = ( "%output% = '$indentation' . str_replace( '\n', '\n$indentation', %1% );\n" );
  156. }
  157. }
  158. else if ( $filler and $type )
  159. {
  160. $tmpCount = 1;
  161. $values[] = $parameters[0];
  162. $values[] = $parameters[1];
  163. $code = ( "if ( %2% < 0 )\n{" .
  164. "\$tpl->error( \"indent\", \"Count parameter can not be negative, string won't be indented\" );\n" .
  165. "%output% = %1%;\n" .
  166. "}else{\n" .
  167. "%tmp1% = str_repeat( '$filler', %2% );\n" .
  168. "%output% = %tmp1% . str_replace( '\n', '\n' . %tmp1%, %1% );\n" .
  169. "}\n");
  170. }
  171. else
  172. {
  173. $tmpCount = 2;
  174. $code = ( "if ( %2% < 0 ){\n" .
  175. "\$tpl->error( \"indent\", \"Count parameter can not be negative, string won't be indented\" );\n" .
  176. "%output% = %1%;\n" .
  177. "}else{" .
  178. "if ( %3% == 'tab' )\n{\n\t%tmp1% = \"\\t\";\n}\nelse " .
  179. "if ( %3% == 'space' )\n{\n\t%tmp1% = ' ';\n}\nelse\n" );
  180. if ( count ( $parameters ) == 4 )
  181. {
  182. $code .= "{\n\t%tmp1% = %4%;\n}\n";
  183. }
  184. else
  185. {
  186. $code.= "{\n\t%tmp1% = ' ';\n}\n";
  187. }
  188. $code .= ( "%tmp2% = str_repeat( %tmp1%, %2% );\n" .
  189. "%output% = %tmp2% . str_replace( '\n', '\n' . %tmp2%, %1% );\n" );
  190. $code .= "}\n";
  191. foreach ( $parameters as $parameter )
  192. {
  193. $values[] = $parameter;
  194. }
  195. }
  196. $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values, 'false', $tmpCount );
  197. return $newElements;
  198. }
  199. function concatTransformation( $operatorName, &$node, $tpl, &$resourceData,
  200. $element, $lastElement, $elementList, $elementTree, &$parameters )
  201. {
  202. $values = array();
  203. $function = $operatorName;
  204. if ( ( count( $parameters ) < 1 ) )
  205. {
  206. return false;
  207. }
  208. if ( ( count( $parameters ) == 1 ) and
  209. eZTemplateNodeTool::isStaticElement( $parameters[0] ) )
  210. {
  211. return array( eZTemplateNodeTool::createStaticElement( eZTemplateNodeTool::elementStaticValue( $parameters[0] ) ) );
  212. }
  213. $newElements = array();
  214. $counter = 1;
  215. $code = "%output% = ( ";
  216. foreach ( $parameters as $parameter )
  217. {
  218. $values[] = $parameter;
  219. if ( $counter > 1 )
  220. {
  221. $code .= ' . ';
  222. }
  223. $code .= "%$counter%";
  224. $counter++;
  225. }
  226. $code .= " );\n";
  227. $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values );
  228. return $newElements;
  229. }
  230. /*!
  231. Handles concat and indent operators.
  232. */
  233. function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters,
  234. $placement )
  235. {
  236. switch ( $operatorName )
  237. {
  238. case $this->ConcatName:
  239. {
  240. $operands = array();
  241. if ( $operatorValue !== null )
  242. $operands[] = $operatorValue;
  243. for ( $i = 0; $i < count( $operatorParameters ); ++$i )
  244. {
  245. $operand = $tpl->elementValue( $operatorParameters[$i], $rootNamespace, $currentNamespace, $placement );
  246. if ( !is_object( $operand ) )
  247. $operands[] = $operand;
  248. }
  249. $operatorValue = implode( '', $operands );
  250. } break;
  251. case $this->IndentName:
  252. {
  253. if( $namedParameters['indent_count'] < 0 )
  254. {
  255. eZDebug::writeError( 'The value of the "count" argument is negative, indent() will not be called' );
  256. break;
  257. }
  258. $indentCount = $namedParameters['indent_count'];
  259. $indentType = $namedParameters['indent_type'];
  260. $filler = false;
  261. switch ( $indentType )
  262. {
  263. case 'space':
  264. default:
  265. {
  266. $filler = ' ';
  267. } break;
  268. case 'tab':
  269. {
  270. $filler = "\t";
  271. } break;
  272. case 'custom':
  273. {
  274. $filler = $namedParameters['indent_filler'];
  275. } break;
  276. }
  277. $fillText = str_repeat( $filler, $indentCount );
  278. $operatorValue = $fillText . str_replace( "\n", "\n" . $fillText, $operatorValue );
  279. } break;
  280. }
  281. }
  282. /// \privatesection
  283. public $ConcatName;
  284. public $Operators;
  285. }
  286. ?>