/libs/sysplugins/smarty_internal_compile_foreach.php

https://github.com/cabenitez/factuweb · PHP · 229 lines · 137 code · 16 blank · 76 comment · 40 complexity · 65cdb8eb99a3d2ead83c82377d59c99f MD5 · raw file

  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Foreach
  4. *
  5. * Compiles the {foreach} {foreachelse} {/foreach} tags
  6. *
  7. * @package Smarty
  8. * @subpackage Compiler
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Smarty Internal Plugin Compile Foreach Class
  13. *
  14. * @package Smarty
  15. * @subpackage Compiler
  16. */
  17. class Smarty_Internal_Compile_Foreach extends Smarty_Internal_CompileBase
  18. {
  19. /**
  20. * Attribute definition: Overwrites base class.
  21. *
  22. * @var array
  23. * @see Smarty_Internal_CompileBase
  24. */
  25. public $required_attributes = array('from', 'item');
  26. /**
  27. * Attribute definition: Overwrites base class.
  28. *
  29. * @var array
  30. * @see Smarty_Internal_CompileBase
  31. */
  32. public $optional_attributes = array('name', 'key');
  33. /**
  34. * Attribute definition: Overwrites base class.
  35. *
  36. * @var array
  37. * @see Smarty_Internal_CompileBase
  38. */
  39. public $shorttag_order = array('from','item','key','name');
  40. /**
  41. * Compiles code for the {foreach} tag
  42. *
  43. * @param array $args array with attributes from parser
  44. * @param object $compiler compiler object
  45. * @param array $parameter array with compilation parameter
  46. * @return string compiled code
  47. */
  48. public function compile($args, $compiler, $parameter)
  49. {
  50. // check and get attributes
  51. $_attr = $this->getAttributes($compiler, $args);
  52. $from = $_attr['from'];
  53. $item = $_attr['item'];
  54. if (!strncmp("\$_smarty_tpl->tpl_vars[$item]", $from, strlen($item) + 24)) {
  55. $compiler->trigger_template_error("item variable {$item} may not be the same variable as at 'from'", $compiler->lex->taglineno);
  56. }
  57. if (isset($_attr['key'])) {
  58. $key = $_attr['key'];
  59. } else {
  60. $key = null;
  61. }
  62. $this->openTag($compiler, 'foreach', array('foreach', $compiler->nocache, $item, $key));
  63. // maybe nocache because of nocache variables
  64. $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
  65. if (isset($_attr['name'])) {
  66. $name = $_attr['name'];
  67. $has_name = true;
  68. $SmartyVarName = '$smarty.foreach.' . trim($name, '\'"') . '.';
  69. } else {
  70. $name = null;
  71. $has_name = false;
  72. }
  73. $ItemVarName = '$' . trim($item, '\'"') . '@';
  74. // evaluates which Smarty variables and properties have to be computed
  75. if ($has_name) {
  76. $usesSmartyFirst = strpos($compiler->lex->data, $SmartyVarName . 'first') !== false;
  77. $usesSmartyLast = strpos($compiler->lex->data, $SmartyVarName . 'last') !== false;
  78. $usesSmartyIndex = strpos($compiler->lex->data, $SmartyVarName . 'index') !== false;
  79. $usesSmartyIteration = strpos($compiler->lex->data, $SmartyVarName . 'iteration') !== false;
  80. $usesSmartyShow = strpos($compiler->lex->data, $SmartyVarName . 'show') !== false;
  81. $usesSmartyTotal = strpos($compiler->lex->data, $SmartyVarName . 'total') !== false;
  82. } else {
  83. $usesSmartyFirst = false;
  84. $usesSmartyLast = false;
  85. $usesSmartyTotal = false;
  86. $usesSmartyShow = false;
  87. }
  88. $usesPropFirst = $usesSmartyFirst || strpos($compiler->lex->data, $ItemVarName . 'first') !== false;
  89. $usesPropLast = $usesSmartyLast || strpos($compiler->lex->data, $ItemVarName . 'last') !== false;
  90. $usesPropIndex = $usesPropFirst || strpos($compiler->lex->data, $ItemVarName . 'index') !== false;
  91. $usesPropIteration = $usesPropLast || strpos($compiler->lex->data, $ItemVarName . 'iteration') !== false;
  92. $usesPropShow = strpos($compiler->lex->data, $ItemVarName . 'show') !== false;
  93. $usesPropTotal = $usesSmartyTotal || $usesSmartyShow || $usesPropShow || $usesPropLast || strpos($compiler->lex->data, $ItemVarName . 'total') !== false;
  94. // generate output code
  95. $output = "<?php ";
  96. $output .= " \$_smarty_tpl->tpl_vars[$item] = new Smarty_Variable; \$_smarty_tpl->tpl_vars[$item]->_loop = false;\n";
  97. if ($key != null) {
  98. $output .= " \$_smarty_tpl->tpl_vars[$key] = new Smarty_Variable;\n";
  99. }
  100. $output .= " \$_from = $from; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array');}\n";
  101. if ($usesPropTotal) {
  102. $output .= " \$_smarty_tpl->tpl_vars[$item]->total= \$_smarty_tpl->_count(\$_from);\n";
  103. }
  104. if ($usesPropIteration) {
  105. $output .= " \$_smarty_tpl->tpl_vars[$item]->iteration=0;\n";
  106. }
  107. if ($usesPropIndex) {
  108. $output .= " \$_smarty_tpl->tpl_vars[$item]->index=-1;\n";
  109. }
  110. if ($usesPropShow) {
  111. $output .= " \$_smarty_tpl->tpl_vars[$item]->show = (\$_smarty_tpl->tpl_vars[$item]->total > 0);\n";
  112. }
  113. if ($has_name) {
  114. if ($usesSmartyTotal) {
  115. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['total'] = \$_smarty_tpl->tpl_vars[$item]->total;\n";
  116. }
  117. if ($usesSmartyIteration) {
  118. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']=0;\n";
  119. }
  120. if ($usesSmartyIndex) {
  121. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['index']=-1;\n";
  122. }
  123. if ($usesSmartyShow) {
  124. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['show']=(\$_smarty_tpl->tpl_vars[$item]->total > 0);\n";
  125. }
  126. }
  127. $output .= "foreach (\$_from as \$_smarty_tpl->tpl_vars[$item]->key => \$_smarty_tpl->tpl_vars[$item]->value) {\n\$_smarty_tpl->tpl_vars[$item]->_loop = true;\n";
  128. if ($key != null) {
  129. $output .= " \$_smarty_tpl->tpl_vars[$key]->value = \$_smarty_tpl->tpl_vars[$item]->key;\n";
  130. }
  131. if ($usesPropIteration) {
  132. $output .= " \$_smarty_tpl->tpl_vars[$item]->iteration++;\n";
  133. }
  134. if ($usesPropIndex) {
  135. $output .= " \$_smarty_tpl->tpl_vars[$item]->index++;\n";
  136. }
  137. if ($usesPropFirst) {
  138. $output .= " \$_smarty_tpl->tpl_vars[$item]->first = \$_smarty_tpl->tpl_vars[$item]->index === 0;\n";
  139. }
  140. if ($usesPropLast) {
  141. $output .= " \$_smarty_tpl->tpl_vars[$item]->last = \$_smarty_tpl->tpl_vars[$item]->iteration === \$_smarty_tpl->tpl_vars[$item]->total;\n";
  142. }
  143. if ($has_name) {
  144. if ($usesSmartyFirst) {
  145. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['first'] = \$_smarty_tpl->tpl_vars[$item]->first;\n";
  146. }
  147. if ($usesSmartyIteration) {
  148. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']++;\n";
  149. }
  150. if ($usesSmartyIndex) {
  151. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['index']++;\n";
  152. }
  153. if ($usesSmartyLast) {
  154. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['last'] = \$_smarty_tpl->tpl_vars[$item]->last;\n";
  155. }
  156. }
  157. $output .= "?>";
  158. return $output;
  159. }
  160. }
  161. /**
  162. * Smarty Internal Plugin Compile Foreachelse Class
  163. *
  164. * @package Smarty
  165. * @subpackage Compiler
  166. */
  167. class Smarty_Internal_Compile_Foreachelse extends Smarty_Internal_CompileBase
  168. {
  169. /**
  170. * Compiles code for the {foreachelse} tag
  171. *
  172. * @param array $args array with attributes from parser
  173. * @param object $compiler compiler object
  174. * @param array $parameter array with compilation parameter
  175. * @return string compiled code
  176. */
  177. public function compile($args, $compiler, $parameter)
  178. {
  179. // check and get attributes
  180. $_attr = $this->getAttributes($compiler, $args);
  181. list($openTag, $nocache, $item, $key) = $this->closeTag($compiler, array('foreach'));
  182. $this->openTag($compiler, 'foreachelse', array('foreachelse', $nocache, $item, $key));
  183. return "<?php }\nif (!\$_smarty_tpl->tpl_vars[$item]->_loop) {\n?>";
  184. }
  185. }
  186. /**
  187. * Smarty Internal Plugin Compile Foreachclose Class
  188. *
  189. * @package Smarty
  190. * @subpackage Compiler
  191. */
  192. class Smarty_Internal_Compile_Foreachclose extends Smarty_Internal_CompileBase
  193. {
  194. /**
  195. * Compiles code for the {/foreach} tag
  196. *
  197. * @param array $args array with attributes from parser
  198. * @param object $compiler compiler object
  199. * @param array $parameter array with compilation parameter
  200. * @return string compiled code
  201. */
  202. public function compile($args, $compiler, $parameter)
  203. {
  204. // check and get attributes
  205. $_attr = $this->getAttributes($compiler, $args);
  206. // must endblock be nocache?
  207. if ($compiler->nocache) {
  208. $compiler->tag_nocache = true;
  209. }
  210. list($openTag, $compiler->nocache, $item, $key) = $this->closeTag($compiler, array('foreach', 'foreachelse'));
  211. return "<?php } ?>";
  212. }
  213. }