1: <?php
2: 3: 4: 5: 6: 7:
8:
9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45:
46:
47: function smarty_function_cycle($params, $template)
48: {
49: static $cycle_vars;
50:
51: $name = (empty($params['name'])) ? 'default' : $params['name'];
52: $print = (isset($params['print'])) ? (bool) $params['print'] : true;
53: $advance = (isset($params['advance'])) ? (bool) $params['advance'] : true;
54: $reset = (isset($params['reset'])) ? (bool) $params['reset'] : false;
55:
56: if (!isset($params['values'])) {
57: if (!isset($cycle_vars[$name]['values'])) {
58: trigger_error("cycle: missing 'values' parameter");
59:
60: return;
61: }
62: } else {
63: if (isset($cycle_vars[$name]['values'])
64: && $cycle_vars[$name]['values'] != $params['values']
65: ) {
66: $cycle_vars[$name]['index'] = 0;
67: }
68: $cycle_vars[$name]['values'] = $params['values'];
69: }
70:
71: if (isset($params['delimiter'])) {
72: $cycle_vars[$name]['delimiter'] = $params['delimiter'];
73: } elseif (!isset($cycle_vars[$name]['delimiter'])) {
74: $cycle_vars[$name]['delimiter'] = ',';
75: }
76:
77: if (is_array($cycle_vars[$name]['values'])) {
78: $cycle_array = $cycle_vars[$name]['values'];
79: } else {
80: $cycle_array = explode($cycle_vars[$name]['delimiter'], $cycle_vars[$name]['values']);
81: }
82:
83: if (!isset($cycle_vars[$name]['index']) || $reset) {
84: $cycle_vars[$name]['index'] = 0;
85: }
86:
87: if (isset($params['assign'])) {
88: $print = false;
89: $template->assign($params['assign'], $cycle_array[$cycle_vars[$name]['index']]);
90: }
91:
92: if ($print) {
93: $retval = $cycle_array[$cycle_vars[$name]['index']];
94: } else {
95: $retval = null;
96: }
97:
98: if ($advance) {
99: if ($cycle_vars[$name]['index'] >= count($cycle_array) - 1) {
100: $cycle_vars[$name]['index'] = 0;
101: } else {
102: $cycle_vars[$name]['index'] ++;
103: }
104: }
105:
106: return $retval;
107: }
108: