]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/Smarty/plugins/function.cycle.php
Release 6.5.0
[Github/sugarcrm.git] / include / Smarty / plugins / function.cycle.php
1 <?php
2
3 /*
4
5 Modification information for LGPL compliance
6
7 r56990 - 2010-06-16 13:05:36 -0700 (Wed, 16 Jun 2010) - kjing - snapshot "Mango" svn branch to a new one for GitHub sync
8
9 r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
10
11 r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
12
13 r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3  tags and updated the build system 
14
15 r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
16
17 r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
18
19 r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
20
21 r8230 - 2005-10-03 17:47:19 -0700 (Mon, 03 Oct 2005) - majed - Added Sugar_Smarty to the code tree.
22
23
24 */
25
26
27 /**
28  * Smarty plugin
29  * @package Smarty
30  * @subpackage plugins
31  */
32
33 /**
34  * Smarty {cycle} function plugin
35  *
36  * Type:     function<br>
37  * Name:     cycle<br>
38  * Date:     May 3, 2002<br>
39  * Purpose:  cycle through given values<br>
40  * Input:
41  *         - name = name of cycle (optional)
42  *         - values = comma separated list of values to cycle,
43  *                    or an array of values to cycle
44  *                    (this can be left out for subsequent calls)
45  *         - reset = boolean - resets given var to true
46  *         - print = boolean - print var or not. default is true
47  *         - advance = boolean - whether or not to advance the cycle
48  *         - delimiter = the value delimiter, default is ","
49  *         - assign = boolean, assigns to template var instead of
50  *                    printed.
51  *
52  * Examples:<br>
53  * <pre>
54  * {cycle values="#eeeeee,#d0d0d0d"}
55  * {cycle name=row values="one,two,three" reset=true}
56  * {cycle name=row}
57  * </pre>
58  * @link http://smarty.php.net/manual/en/language.function.cycle.php {cycle}
59  *       (Smarty online manual)
60  * @author Monte Ohrt <monte at ohrt dot com>
61  * @author credit to Mark Priatel <mpriatel@rogers.com>
62  * @author credit to Gerard <gerard@interfold.com>
63  * @author credit to Jason Sweat <jsweat_php@yahoo.com>
64  * @version  1.3
65  * @param array
66  * @param Smarty
67  * @return string|null
68  */
69 function smarty_function_cycle($params, &$smarty)
70 {
71     static $cycle_vars;
72     
73     $name = (empty($params['name'])) ? 'default' : $params['name'];
74     $print = (isset($params['print'])) ? (bool)$params['print'] : true;
75     $advance = (isset($params['advance'])) ? (bool)$params['advance'] : true;
76     $reset = (isset($params['reset'])) ? (bool)$params['reset'] : false;
77             
78     if (!in_array('values', array_keys($params))) {
79         if(!isset($cycle_vars[$name]['values'])) {
80             $smarty->trigger_error("cycle: missing 'values' parameter");
81             return;
82         }
83     } else {
84         if(isset($cycle_vars[$name]['values'])
85             && $cycle_vars[$name]['values'] != $params['values'] ) {
86             $cycle_vars[$name]['index'] = 0;
87         }
88         $cycle_vars[$name]['values'] = $params['values'];
89     }
90
91     $cycle_vars[$name]['delimiter'] = (isset($params['delimiter'])) ? $params['delimiter'] : ',';
92     
93     if(is_array($cycle_vars[$name]['values'])) {
94         $cycle_array = $cycle_vars[$name]['values'];
95     } else {
96         $cycle_array = explode($cycle_vars[$name]['delimiter'],$cycle_vars[$name]['values']);
97     }
98     
99     if(!isset($cycle_vars[$name]['index']) || $reset ) {
100         $cycle_vars[$name]['index'] = 0;
101     }
102     
103     if (isset($params['assign'])) {
104         $print = false;
105         $smarty->assign($params['assign'], $cycle_array[$cycle_vars[$name]['index']]);
106     }
107         
108     if($print) {
109         $retval = $cycle_array[$cycle_vars[$name]['index']];
110     } else {
111         $retval = null;
112     }
113
114     if($advance) {
115         if ( $cycle_vars[$name]['index'] >= count($cycle_array) -1 ) {
116             $cycle_vars[$name]['index'] = 0;
117         } else {
118             $cycle_vars[$name]['index']++;
119         }
120     }
121     
122     return $retval;
123 }
124
125 /* vim: set expandtab: */
126
127 ?>