1<?php
2/*
3 * template_lite plugin
4 *
5 * Type:     function
6 * Name:     cycle
7 * Version:  1.3
8 * Date:     May 3, 2002
9 * Author:   Monte Ohrt <monte@ispi.net>
10 * Credits:  Mark Priatel <mpriatel@rogers.com>
11 *           Gerard <gerard@interfold.com>
12 *           Jason Sweat <jsweat_php@yahoo.com>
13 * Purpose:  cycle through given values
14 * Input:    name = name of cycle (optional)
15 *           values = comma separated list of values to cycle,
16 *                    or an array of values to cycle
17 *                    (this can be left out for subsequent calls)
18 *           reset = boolean - resets given var to true
19 *           print = boolean - print var or not. default is true
20 *           advance = boolean - whether or not to advance the cycle
21 *           delimiter = the value delimiter, default is ","
22 *           assign = boolean, assigns to template var instead of
23 *                    printed.
24 * Examples: {cycle values="#eeeeee,#d0d0d0d"}
25 *           {cycle name=row values="one,two,three" reset=true}
26 *           {cycle name=row}
27 * Credit:   Taken from the original Smarty
28 *           http://smarty.php.net
29 */
30function tpl_function_cycle($params, &$tpl)
31{
32	static $cycle_vars;
33
34	$name    = (empty($params['name']))    ? 'default' : $params['name'];
35	$print   = (isset($params['print']))   ? (bool)$params['print'] : true;
36	$advance = (isset($params['advance'])) ? (bool)$params['advance'] : true;
37	$reset   = (isset($params['reset']))   ? (bool)$params['reset'] : false;
38
39	if (!in_array('values', array_keys($params)))
40	{
41		if(!isset($cycle_vars[$name]['values']))
42		{
43			$tpl->trigger_error("cycle: missing 'values' parameter");
44			return;
45		}
46	}
47	else
48	{
49		if(isset($cycle_vars[$name]['values']) && $cycle_vars[$name]['values'] != $params['values'] )
50		{
51			$cycle_vars[$name]['index'] = 0;
52		}
53		$cycle_vars[$name]['values'] = $params['values'];
54	}
55
56	$cycle_vars[$name]['delimiter'] = (isset($params['delimiter'])) ? $params['delimiter'] : ',';
57
58	if(is_array($cycle_vars[$name]['values']))
59	{
60		$cycle_array = $cycle_vars[$name]['values'];
61	}
62	else
63	{
64		$cycle_array = explode($cycle_vars[$name]['delimiter'],$cycle_vars[$name]['values']);
65	}
66
67	if(!isset($cycle_vars[$name]['index']) || $reset )
68	{
69		$cycle_vars[$name]['index'] = 0;
70	}
71
72	if (isset($params['assign']))
73	{
74		$print = false;
75		$tpl->assign($params['assign'], $cycle_array[$cycle_vars[$name]['index']]);
76	}
77
78	if($print)
79	{
80		$retval = $cycle_array[$cycle_vars[$name]['index']];
81	}
82	else
83	{
84		$retval = null;
85	}
86
87	if($advance)
88	{
89		if ( $cycle_vars[$name]['index'] >= count($cycle_array) -1 )
90		{
91			$cycle_vars[$name]['index'] = 0;
92		}
93		else
94		{
95			$cycle_vars[$name]['index']++;
96		}
97	}
98
99	return $retval;
100}
101?>