]> git.mxchange.org Git - friendica.git/blob - library/Smarty/libs/plugins/function.cycle.php
Merge branch 'master' of ../save/merge/frio into frio_merge
[friendica.git] / library / Smarty / libs / plugins / function.cycle.php
1 <?php
2 /**
3  * Smarty plugin
4  *
5  * @package    Smarty
6  * @subpackage PluginsFunction
7  */
8
9 /**
10  * Smarty {cycle} function plugin
11  * Type:     function<br>
12  * Name:     cycle<br>
13  * Date:     May 3, 2002<br>
14  * Purpose:  cycle through given values<br>
15  * Params:
16  * <pre>
17  * - name      - name of cycle (optional)
18  * - values    - comma separated list of values to cycle, or an array of values to cycle
19  *               (this can be left out for subsequent calls)
20  * - reset     - boolean - resets given var to true
21  * - print     - boolean - print var or not. default is true
22  * - advance   - boolean - whether or not to advance the cycle
23  * - delimiter - the value delimiter, default is ","
24  * - assign    - boolean, assigns to template var instead of printed.
25  * </pre>
26  * Examples:<br>
27  * <pre>
28  * {cycle values="#eeeeee,#d0d0d0d"}
29  * {cycle name=row values="one,two,three" reset=true}
30  * {cycle name=row}
31  * </pre>
32  *
33  * @link     http://www.smarty.net/manual/en/language.function.cycle.php {cycle}
34  *           (Smarty online manual)
35  * @author   Monte Ohrt <monte at ohrt dot com>
36  * @author   credit to Mark Priatel <mpriatel@rogers.com>
37  * @author   credit to Gerard <gerard@interfold.com>
38  * @author   credit to Jason Sweat <jsweat_php@yahoo.com>
39  * @version  1.3
40  *
41  * @param array                    $params   parameters
42  * @param Smarty_Internal_Template $template template object
43  *
44  * @return string|null
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 }