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