XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
function.cycle.php
Go to the documentation of this file.
1 <?php
44 function smarty_function_cycle($params, &$smarty)
45 {
46  static $cycle_vars;
47 
48  $name = (empty($params['name'])) ? 'default' : $params['name'];
49  $print = (isset($params['print'])) ? (bool)$params['print'] : true;
50  $advance = (isset($params['advance'])) ? (bool)$params['advance'] : true;
51  $reset = (isset($params['reset'])) ? (bool)$params['reset'] : false;
52 
53  if (!in_array('values', array_keys($params))) {
54  if(!isset($cycle_vars[$name]['values'])) {
55  $smarty->trigger_error("cycle: missing 'values' parameter");
56  return;
57  }
58  } else {
59  if(isset($cycle_vars[$name]['values'])
60  && $cycle_vars[$name]['values'] != $params['values'] ) {
61  $cycle_vars[$name]['index'] = 0;
62  }
63  $cycle_vars[$name]['values'] = $params['values'];
64  }
65 
66  if (isset($params['delimiter'])) {
67  $cycle_vars[$name]['delimiter'] = $params['delimiter'];
68  } elseif (!isset($cycle_vars[$name]['delimiter'])) {
69  $cycle_vars[$name]['delimiter'] = ',';
70  }
71 
72  if(is_array($cycle_vars[$name]['values'])) {
73  $cycle_array = $cycle_vars[$name]['values'];
74  } else {
75  $cycle_array = explode($cycle_vars[$name]['delimiter'],$cycle_vars[$name]['values']);
76  }
77 
78  if(!isset($cycle_vars[$name]['index']) || $reset ) {
79  $cycle_vars[$name]['index'] = 0;
80  }
81 
82  if (isset($params['assign'])) {
83  $print = false;
84  $smarty->assign($params['assign'], $cycle_array[$cycle_vars[$name]['index']]);
85  }
86 
87  if($print) {
88  $retval = $cycle_array[$cycle_vars[$name]['index']];
89  } else {
90  $retval = null;
91  }
92 
93  if($advance) {
94  if ( $cycle_vars[$name]['index'] >= count($cycle_array) -1 ) {
95  $cycle_vars[$name]['index'] = 0;
96  } else {
97  $cycle_vars[$name]['index']++;
98  }
99  }
100 
101  return $retval;
102 }
103 
104 /* vim: set expandtab: */
105 
106 ?>