PageRenderTime 24ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/library/vendor/Smarty/sysplugins/smarty_internal_compile_section.php

https://gitlab.com/piotr-zuralski/demo-zuralski-trojmiastopl-articles
PHP | 173 lines | 101 code | 27 blank | 45 comment | 21 complexity | 577c0cc37b8988facd7fd9f53881c00f MD5 | raw file
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Section
  4. *
  5. * Compiles the {section} {sectionelse} {/section} tags
  6. *
  7. * @package Smarty
  8. * @subpackage Compiler
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Smarty Internal Plugin Compile Section Class
  13. */
  14. class Smarty_Internal_Compile_Section extends Smarty_Internal_CompileBase {
  15. // attribute definitions
  16. public $required_attributes = array('name', 'loop');
  17. public $shorttag_order = array('name', 'loop');
  18. public $optional_attributes = array('start', 'step', 'max', 'show');
  19. /**
  20. * Compiles code for the {section} tag
  21. *
  22. * @param array $args array with attributes from parser
  23. * @param object $compiler compiler object
  24. * @return string compiled code
  25. */
  26. public function compile($args, $compiler)
  27. {
  28. $this->compiler = $compiler;
  29. // check and get attributes
  30. $_attr = $this->_get_attributes($args);
  31. $this->_open_tag('section', array('section',$this->compiler->nocache));
  32. // maybe nocache because of nocache variables
  33. $this->compiler->nocache = $this->compiler->nocache | $this->compiler->tag_nocache;
  34. $output = "<?php ";
  35. $section_name = $_attr['name'];
  36. $output .= "unset(\$_smarty_tpl->tpl_vars['smarty']->value['section'][$section_name]);\n";
  37. $section_props = "\$_smarty_tpl->tpl_vars['smarty']->value['section'][$section_name]";
  38. foreach ($_attr as $attr_name => $attr_value) {
  39. switch ($attr_name) {
  40. case 'loop':
  41. $output .= "{$section_props}['loop'] = is_array(\$_loop=$attr_value) ? count(\$_loop) : max(0, (int)\$_loop); unset(\$_loop);\n";
  42. break;
  43. case 'show':
  44. if (is_bool($attr_value))
  45. $show_attr_value = $attr_value ? 'true' : 'false';
  46. else
  47. $show_attr_value = "(bool)$attr_value";
  48. $output .= "{$section_props}['show'] = $show_attr_value;\n";
  49. break;
  50. case 'name':
  51. $output .= "{$section_props}['$attr_name'] = $attr_value;\n";
  52. break;
  53. case 'max':
  54. case 'start':
  55. $output .= "{$section_props}['$attr_name'] = (int)$attr_value;\n";
  56. break;
  57. case 'step':
  58. $output .= "{$section_props}['$attr_name'] = ((int)$attr_value) == 0 ? 1 : (int)$attr_value;\n";
  59. break;
  60. }
  61. }
  62. if (!isset($_attr['show']))
  63. $output .= "{$section_props}['show'] = true;\n";
  64. if (!isset($_attr['loop']))
  65. $output .= "{$section_props}['loop'] = 1;\n";
  66. if (!isset($_attr['max']))
  67. $output .= "{$section_props}['max'] = {$section_props}['loop'];\n";
  68. else
  69. $output .= "if ({$section_props}['max'] < 0)\n" . " {$section_props}['max'] = {$section_props}['loop'];\n";
  70. if (!isset($_attr['step']))
  71. $output .= "{$section_props}['step'] = 1;\n";
  72. if (!isset($_attr['start']))
  73. $output .= "{$section_props}['start'] = {$section_props}['step'] > 0 ? 0 : {$section_props}['loop']-1;\n";
  74. else {
  75. $output .= "if ({$section_props}['start'] < 0)\n" . " {$section_props}['start'] = max({$section_props}['step'] > 0 ? 0 : -1, {$section_props}['loop'] + {$section_props}['start']);\n" . "else\n" . " {$section_props}['start'] = min({$section_props}['start'], {$section_props}['step'] > 0 ? {$section_props}['loop'] : {$section_props}['loop']-1);\n";
  76. }
  77. $output .= "if ({$section_props}['show']) {\n";
  78. if (!isset($_attr['start']) && !isset($_attr['step']) && !isset($_attr['max'])) {
  79. $output .= " {$section_props}['total'] = {$section_props}['loop'];\n";
  80. } else {
  81. $output .= " {$section_props}['total'] = min(ceil(({$section_props}['step'] > 0 ? {$section_props}['loop'] - {$section_props}['start'] : {$section_props}['start']+1)/abs({$section_props}['step'])), {$section_props}['max']);\n";
  82. }
  83. $output .= " if ({$section_props}['total'] == 0)\n" . " {$section_props}['show'] = false;\n" . "} else\n" . " {$section_props}['total'] = 0;\n";
  84. $output .= "if ({$section_props}['show']):\n";
  85. $output .= "
  86. for ({$section_props}['index'] = {$section_props}['start'], {$section_props}['iteration'] = 1;
  87. {$section_props}['iteration'] <= {$section_props}['total'];
  88. {$section_props}['index'] += {$section_props}['step'], {$section_props}['iteration']++):\n";
  89. $output .= "{$section_props}['rownum'] = {$section_props}['iteration'];\n";
  90. $output .= "{$section_props}['index_prev'] = {$section_props}['index'] - {$section_props}['step'];\n";
  91. $output .= "{$section_props}['index_next'] = {$section_props}['index'] + {$section_props}['step'];\n";
  92. $output .= "{$section_props}['first'] = ({$section_props}['iteration'] == 1);\n";
  93. $output .= "{$section_props}['last'] = ({$section_props}['iteration'] == {$section_props}['total']);\n";
  94. $output .= "?>";
  95. return $output;
  96. }
  97. }
  98. /**
  99. * Smarty Internal Plugin Compile Sectionelse Class
  100. */
  101. class Smarty_Internal_Compile_Sectionelse extends Smarty_Internal_CompileBase {
  102. /**
  103. * Compiles code for the {sectionelse} tag
  104. *
  105. * @param array $args array with attributes from parser
  106. * @param object $compiler compiler object
  107. * @return string compiled code
  108. */
  109. public function compile($args, $compiler)
  110. {
  111. $this->compiler = $compiler;
  112. // check and get attributes
  113. $_attr = $this->_get_attributes($args);
  114. list($_open_tag, $nocache) = $this->_close_tag(array('section'));
  115. $this->_open_tag('sectionelse',array('sectionelse', $nocache));
  116. return "<?php endfor; else: ?>";
  117. }
  118. }
  119. /**
  120. * Smarty Internal Plugin Compile Sectionclose Class
  121. */
  122. class Smarty_Internal_Compile_Sectionclose extends Smarty_Internal_CompileBase {
  123. /**
  124. * Compiles code for the {/section} tag
  125. *
  126. * @param array $args array with attributes from parser
  127. * @param object $compiler compiler object
  128. * @return string compiled code
  129. */
  130. public function compile($args, $compiler)
  131. {
  132. $this->compiler = $compiler;
  133. // check and get attributes
  134. $_attr = $this->_get_attributes($args);
  135. // must endblock be nocache?
  136. if ($this->compiler->nocache) {
  137. $this->compiler->tag_nocache = true;
  138. }
  139. list($_open_tag, $this->compiler->nocache) = $this->_close_tag(array('section', 'sectionelse'));
  140. if ($_open_tag == 'sectionelse')
  141. return "<?php endif; ?>";
  142. else
  143. return "<?php endfor; endif; ?>";
  144. }
  145. }
  146. ?>