PageRenderTime 37ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/Smarty/sysplugins/smarty_internal_compile_foreach.php

https://gitlab.com/webbroteam/satisfaction-mvc
PHP | 298 lines | 178 code | 16 blank | 104 comment | 45 complexity | 77a81fc2ba37b4a3147c86cc1af81eb7 MD5 | raw file
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Foreach
  4. * Compiles the {foreach} {foreachelse} {/foreach} tags
  5. *
  6. * @package Smarty
  7. * @subpackage Compiler
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * Smarty Internal Plugin Compile Foreach Class
  12. *
  13. * @package Smarty
  14. * @subpackage Compiler
  15. */
  16. class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_ForeachSection
  17. {
  18. /**
  19. * Attribute definition: Overwrites base class.
  20. *
  21. * @var array
  22. * @see Smarty_Internal_CompileBase
  23. */
  24. public $required_attributes = array('from', 'item');
  25. /**
  26. * Attribute definition: Overwrites base class.
  27. *
  28. * @var array
  29. * @see Smarty_Internal_CompileBase
  30. */
  31. public $optional_attributes = array('name', 'key');
  32. /**
  33. * Attribute definition: Overwrites base class.
  34. *
  35. * @var array
  36. * @see Smarty_Internal_CompileBase
  37. */
  38. public $shorttag_order = array('from', 'item', 'key', 'name');
  39. /**
  40. * counter
  41. *
  42. * @var int
  43. */
  44. public $counter = 0;
  45. /**
  46. * Name of this tag
  47. *
  48. * @var string
  49. */
  50. public $tagName = 'foreach';
  51. /**
  52. * Valid properties of $smarty.foreach.name.xxx variable
  53. *
  54. * @var array
  55. */
  56. public $nameProperties = array('first', 'last', 'index', 'iteration', 'show', 'total');
  57. /**
  58. * Valid properties of $item@xxx variable
  59. *
  60. * @var array
  61. */
  62. public $itemProperties = array('first', 'last', 'index', 'iteration', 'show', 'total', 'key');
  63. /**
  64. * Flag if tag had name attribute
  65. *
  66. * @var bool
  67. */
  68. public $isNamed = false;
  69. /**
  70. * Compiles code for the {foreach} tag
  71. *
  72. * @param array $args array with attributes from parser
  73. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  74. * @param array $parameter array with compilation parameter
  75. *
  76. * @return string compiled code
  77. * @throws \SmartyCompilerException
  78. */
  79. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
  80. {
  81. $compiler->loopNesting ++;
  82. // init
  83. $this->isNamed = false;
  84. // check and get attributes
  85. $_attr = $this->getAttributes($compiler, $args);
  86. $from = $_attr[ 'from' ];
  87. $item = $compiler->getId($_attr[ 'item' ]);
  88. if ($item === false) {
  89. $item = $compiler->getVariableName($_attr[ 'item' ]);
  90. }
  91. $key = $name = null;
  92. $attributes = array('item' => $item);
  93. if (isset($_attr[ 'key' ])) {
  94. $key = $compiler->getId($_attr[ 'key' ]);
  95. if ($key === false) {
  96. $key = $compiler->getVariableName($_attr[ 'key' ]);
  97. }
  98. $attributes[ 'key' ] = $key;
  99. }
  100. if (isset($_attr[ 'name' ])) {
  101. $this->isNamed = true;
  102. $name = $attributes[ 'name' ] = $compiler->getId($_attr[ 'name' ]);
  103. }
  104. foreach ($attributes as $a => $v) {
  105. if ($v === false) {
  106. $compiler->trigger_template_error("'{$a}' attribute/variable has illegal value", null, true);
  107. }
  108. }
  109. $fromName = $compiler->getVariableName($_attr[ 'from' ]);
  110. if ($fromName) {
  111. foreach (array('item', 'key') as $a) {
  112. if (isset($attributes[ $a ]) && $attributes[ $a ] == $fromName) {
  113. $compiler->trigger_template_error("'{$a}' and 'from' may not have same variable name '{$fromName}'",
  114. null, true);
  115. }
  116. }
  117. }
  118. $itemVar = "\$_smarty_tpl->tpl_vars['{$item}']";
  119. $local = '$__foreach_' . $attributes[ 'item' ] . '_' . $this->counter ++ . '_';
  120. // search for used tag attributes
  121. $itemAttr = array();
  122. $namedAttr = array();
  123. $this->scanForProperties($attributes, $compiler);
  124. if (!empty($this->matchResults[ 'item' ])) {
  125. $itemAttr = $this->matchResults[ 'item' ];
  126. }
  127. if (!empty($this->matchResults[ 'named' ])) {
  128. $namedAttr = $this->matchResults[ 'named' ];
  129. }
  130. if (isset($itemAttr[ 'first' ])) {
  131. $itemAttr[ 'index' ] = true;
  132. }
  133. if (isset($namedAttr[ 'first' ])) {
  134. $namedAttr[ 'index' ] = true;
  135. }
  136. if (isset($namedAttr[ 'last' ])) {
  137. $namedAttr[ 'iteration' ] = true;
  138. $namedAttr[ 'total' ] = true;
  139. }
  140. if (isset($itemAttr[ 'last' ])) {
  141. $itemAttr[ 'iteration' ] = true;
  142. $itemAttr[ 'total' ] = true;
  143. }
  144. $keyTerm = '';
  145. if (isset($itemAttr[ 'key' ])) {
  146. $keyTerm = "{$itemVar}->key => ";
  147. } elseif (isset($attributes[ 'key' ])) {
  148. $keyTerm = "\$_smarty_tpl->tpl_vars['{$key}']->value => ";
  149. }
  150. if ($this->isNamed) {
  151. $foreachVar = "\$_smarty_tpl->tpl_vars['__smarty_foreach_{$attributes['name']}']";
  152. }
  153. $this->openTag($compiler, 'foreach', array('foreach', $compiler->nocache, $local, $itemVar, true));
  154. // maybe nocache because of nocache variables
  155. $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
  156. $needTotal = isset($itemAttr[ 'show' ]) || isset($itemAttr[ 'total' ]) || isset($namedAttr[ 'total' ]) ||
  157. isset($namedAttr[ 'show' ]) || isset($itemAttr[ 'last' ]) || isset($namedAttr[ 'last' ]);
  158. // generate output code
  159. $output = "<?php\n";
  160. $output .= "\$_from = \$_smarty_tpl->smarty->ext->_foreach->init(\$_smarty_tpl, $from, " .
  161. var_export($item, true);
  162. if ($name || $needTotal || $key) {
  163. $output .= ', ' . var_export($needTotal, true);
  164. }
  165. if ($name || $key) {
  166. $output .= ', ' . var_export($key, true);
  167. }
  168. if ($name) {
  169. $output .= ', ' . var_export($name, true) . ', ' . var_export($namedAttr, true);
  170. }
  171. $output .= ");\n";
  172. if (isset($itemAttr[ 'show' ])) {
  173. $output .= "{$itemVar}->show = ({$itemVar}->total > 0);\n";
  174. }
  175. if (isset($itemAttr[ 'iteration' ])) {
  176. $output .= "{$itemVar}->iteration = 0;\n";
  177. }
  178. if (isset($itemAttr[ 'index' ])) {
  179. $output .= "{$itemVar}->index = -1;\n";
  180. }
  181. $output .= "foreach (\$_from as {$keyTerm}{$itemVar}->value) {\n";
  182. $output .= "{$itemVar}->_loop = true;\n";
  183. if (isset($attributes[ 'key' ]) && isset($itemAttr[ 'key' ])) {
  184. $output .= "\$_smarty_tpl->tpl_vars['{$key}']->value = {$itemVar}->key;\n";
  185. }
  186. if (isset($itemAttr[ 'iteration' ])) {
  187. $output .= "{$itemVar}->iteration++;\n";
  188. }
  189. if (isset($itemAttr[ 'index' ])) {
  190. $output .= "{$itemVar}->index++;\n";
  191. }
  192. if (isset($itemAttr[ 'first' ])) {
  193. $output .= "{$itemVar}->first = !{$itemVar}->index;\n";
  194. }
  195. if (isset($itemAttr[ 'last' ])) {
  196. $output .= "{$itemVar}->last = {$itemVar}->iteration == {$itemVar}->total;\n";
  197. }
  198. if ($this->isNamed) {
  199. if (isset($namedAttr[ 'iteration' ])) {
  200. $output .= "{$foreachVar}->value['iteration']++;\n";
  201. }
  202. if (isset($namedAttr[ 'index' ])) {
  203. $output .= "{$foreachVar}->value['index']++;\n";
  204. }
  205. if (isset($namedAttr[ 'first' ])) {
  206. $output .= "{$foreachVar}->value['first'] = !{$foreachVar}->value['index'];\n";
  207. }
  208. if (isset($namedAttr[ 'last' ])) {
  209. $output .= "{$foreachVar}->value['last'] = {$foreachVar}->value['iteration'] == {$foreachVar}->value['total'];\n";
  210. }
  211. }
  212. $output .= "{$local}saved = {$itemVar};\n";
  213. $output .= "?>";
  214. return $output;
  215. }
  216. }
  217. /**
  218. * Smarty Internal Plugin Compile Foreachelse Class
  219. *
  220. * @package Smarty
  221. * @subpackage Compiler
  222. */
  223. class Smarty_Internal_Compile_Foreachelse extends Smarty_Internal_CompileBase
  224. {
  225. /**
  226. * Compiles code for the {foreachelse} tag
  227. *
  228. * @param array $args array with attributes from parser
  229. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  230. * @param array $parameter array with compilation parameter
  231. *
  232. * @return string compiled code
  233. */
  234. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
  235. {
  236. // check and get attributes
  237. $_attr = $this->getAttributes($compiler, $args);
  238. list($openTag, $nocache, $local, $itemVar, $foo) = $this->closeTag($compiler, array('foreach'));
  239. $this->openTag($compiler, 'foreachelse', array('foreachelse', $nocache, $local, $itemVar, false));
  240. $output = "<?php\n";
  241. $output .= "{$itemVar} = {$local}saved;\n";
  242. $output .= "}\n";
  243. $output .= "if (!{$itemVar}->_loop) {\n?>";
  244. return $output;
  245. }
  246. }
  247. /**
  248. * Smarty Internal Plugin Compile Foreachclose Class
  249. *
  250. * @package Smarty
  251. * @subpackage Compiler
  252. */
  253. class Smarty_Internal_Compile_Foreachclose extends Smarty_Internal_CompileBase
  254. {
  255. /**
  256. * Compiles code for the {/foreach} tag
  257. *
  258. * @param array $args array with attributes from parser
  259. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  260. * @param array $parameter array with compilation parameter
  261. *
  262. * @return string compiled code
  263. */
  264. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
  265. {
  266. $compiler->loopNesting --;
  267. // must endblock be nocache?
  268. if ($compiler->nocache) {
  269. $compiler->tag_nocache = true;
  270. }
  271. list($openTag, $compiler->nocache, $local, $itemVar, $restore) =
  272. $this->closeTag($compiler, array('foreach', 'foreachelse'));
  273. $output = "<?php\n";
  274. if ($restore) {
  275. $output .= "{$itemVar} = {$local}saved;\n";
  276. }
  277. $output .= "}\n";
  278. $output .= "\$_smarty_tpl->smarty->ext->_foreach->restore(\$_smarty_tpl);\n";
  279. $output .= "?>";
  280. return $output;
  281. }
  282. }