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