PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/smarty/sysplugins/smarty_internal_compile_foreach.php

https://github.com/ACCORD5/TrellisDesk
PHP | 192 lines | 132 code | 14 blank | 46 comment | 39 complexity | 31b1dcc15462a4a9812d8eff52f45b33 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. class Smarty_Internal_Compile_Foreach extends Smarty_Internal_CompileBase {
  15. /**
  16. * Compiles code for the {foreach} tag
  17. *
  18. * @param array $args array with attributes from parser
  19. * @param object $compiler compiler object
  20. * @return string compiled code
  21. */
  22. public function compile($args, $compiler)
  23. {
  24. $this->compiler = $compiler;
  25. $this->required_attributes = array('from', 'item');
  26. $this->optional_attributes = array('name', 'key');
  27. $tpl = $compiler->template;
  28. // check and get attributes
  29. $_attr = $this->_get_attributes($args);
  30. $this->_open_tag('foreach', array('foreach',$this->compiler->nocache));
  31. // maybe nocache because of nocache variables
  32. $this->compiler->nocache = $this->compiler->nocache | $this->compiler->tag_nocache;
  33. $from = $_attr['from'];
  34. $item = $_attr['item'];
  35. if (isset($_attr['key'])) {
  36. $key = $_attr['key'];
  37. } else {
  38. $key = null;
  39. }
  40. if (isset($_attr['name'])) {
  41. $name = $_attr['name'];
  42. $has_name = true;
  43. $SmartyVarName = '$smarty.foreach.' . trim($name,'\'"') . '.';
  44. } else {
  45. $name = null;
  46. $has_name = false;
  47. }
  48. $ItemVarName = '$' . trim($item,'\'"') . '@';
  49. // evaluates which Smarty variables and properties have to be computed
  50. if ($has_name) {
  51. $usesSmartyFirst = strpos($tpl->template_source, $SmartyVarName . 'first') !== false;
  52. $usesSmartyLast = strpos($tpl->template_source, $SmartyVarName . 'last') !== false;
  53. $usesSmartyIndex = strpos($tpl->template_source, $SmartyVarName . 'index') !== false;
  54. $usesSmartyIteration = strpos($tpl->template_source, $SmartyVarName . 'iteration') !== false;
  55. $usesSmartyShow = strpos($tpl->template_source, $SmartyVarName . 'show') !== false;
  56. $usesSmartyTotal = $usesSmartyLast || strpos($tpl->template_source, $SmartyVarName . 'total') !== false;
  57. } else {
  58. $usesSmartyFirst = false;
  59. $usesSmartyLast = false;
  60. $usesSmartyTotal = false;
  61. }
  62. $usesPropFirst = $usesSmartyFirst || strpos($tpl->template_source, $ItemVarName . 'first') !== false;
  63. $usesPropLast = $usesSmartyLast || strpos($tpl->template_source, $ItemVarName . 'last') !== false;
  64. $usesPropIndex = $usesPropFirst || strpos($tpl->template_source, $ItemVarName . 'index') !== false;
  65. $usesPropIteration = $usesPropLast || strpos($tpl->template_source, $ItemVarName . 'iteration') !== false;
  66. $usesPropShow = strpos($tpl->template_source, $ItemVarName . 'show') !== false;
  67. $usesPropTotal = $usesSmartyTotal || $usesPropLast || strpos($tpl->template_source, $ItemVarName . 'total') !== false;
  68. // generate output code
  69. $output = "<?php ";
  70. $output .= " \$_smarty_tpl->tpl_vars[$item] = new Smarty_Variable;\n";
  71. if ($key != null) {
  72. $output .= " \$_smarty_tpl->tpl_vars[$key] = new Smarty_Variable;\n";
  73. }
  74. $output .= " \$_from = $from; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array');}\n";
  75. if ($usesPropTotal) {
  76. $output .= " \$_smarty_tpl->tpl_vars[$item]->total=count(\$_from);\n";
  77. }
  78. if ($usesPropIteration) {
  79. $output .= " \$_smarty_tpl->tpl_vars[$item]->iteration=0;\n";
  80. }
  81. if ($usesPropIndex) {
  82. $output .= " \$_smarty_tpl->tpl_vars[$item]->index=-1;\n";
  83. }
  84. if ($has_name) {
  85. if ($usesSmartyTotal) {
  86. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['total'] = \$_smarty_tpl->tpl_vars[$item]->total;\n";
  87. }
  88. if ($usesSmartyIteration) {
  89. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']=0;\n";
  90. }
  91. if ($usesSmartyIndex) {
  92. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['index']=-1;\n";
  93. }
  94. }
  95. $output .= "if (count(\$_from) > 0){\n";
  96. $output .= " foreach (\$_from as \$_smarty_tpl->tpl_vars[$item]->key => \$_smarty_tpl->tpl_vars[$item]->value){\n";
  97. if ($key != null) {
  98. $output .= " \$_smarty_tpl->tpl_vars[$key]->value = \$_smarty_tpl->tpl_vars[$item]->key;\n";
  99. }
  100. if ($usesPropIteration) {
  101. $output .= " \$_smarty_tpl->tpl_vars[$item]->iteration++;\n";
  102. }
  103. if ($usesPropIndex) {
  104. $output .= " \$_smarty_tpl->tpl_vars[$item]->index++;\n";
  105. }
  106. if ($usesPropFirst) {
  107. $output .= " \$_smarty_tpl->tpl_vars[$item]->first = \$_smarty_tpl->tpl_vars[$item]->index === 0;\n";
  108. }
  109. if ($usesPropLast) {
  110. $output .= " \$_smarty_tpl->tpl_vars[$item]->last = \$_smarty_tpl->tpl_vars[$item]->iteration === \$_smarty_tpl->tpl_vars[$item]->total;\n";
  111. }
  112. if ($has_name) {
  113. if ($usesSmartyFirst) {
  114. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['first'] = \$_smarty_tpl->tpl_vars[$item]->first;\n";
  115. }
  116. if ($usesSmartyIteration) {
  117. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']++;\n";
  118. }
  119. if ($usesSmartyIndex) {
  120. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['index']++;\n";
  121. }
  122. if ($usesSmartyLast) {
  123. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['last'] = \$_smarty_tpl->tpl_vars[$item]->last;\n";
  124. }
  125. }
  126. $output .= "?>";
  127. return $output;
  128. }
  129. }
  130. /**
  131. * Smarty Internal Plugin Compile Foreachelse Class
  132. */
  133. class Smarty_Internal_Compile_Foreachelse extends Smarty_Internal_CompileBase {
  134. /**
  135. * Compiles code for the {foreachelse} tag
  136. *
  137. * @param array $args array with attributes from parser
  138. * @param object $compiler compiler object
  139. * @return string compiled code
  140. */
  141. public function compile($args, $compiler)
  142. {
  143. $this->compiler = $compiler;
  144. // check and get attributes
  145. $_attr = $this->_get_attributes($args);
  146. list($_open_tag, $nocache) = $this->_close_tag(array('foreach'));
  147. $this->_open_tag('foreachelse',array('foreachelse', $nocache));
  148. return "<?php }} else { ?>";
  149. }
  150. }
  151. /**
  152. * Smarty Internal Plugin Compile Foreachclose Class
  153. */
  154. class Smarty_Internal_Compile_Foreachclose extends Smarty_Internal_CompileBase {
  155. /**
  156. * Compiles code for the {/foreach} tag
  157. *
  158. * @param array $args array with attributes from parser
  159. * @param object $compiler compiler object
  160. * @return string compiled code
  161. */
  162. public function compile($args, $compiler)
  163. {
  164. $this->compiler = $compiler;
  165. // check and get attributes
  166. $_attr = $this->_get_attributes($args);
  167. // must endblock be nocache?
  168. if ($this->compiler->nocache) {
  169. $this->compiler->tag_nocache = true;
  170. }
  171. list($_open_tag, $this->compiler->nocache) = $this->_close_tag(array('foreach', 'foreachelse'));
  172. if ($_open_tag == 'foreachelse')
  173. return "<?php } ?>";
  174. else
  175. return "<?php }} ?>";
  176. }
  177. }
  178. ?>