| 1 | <?php |
| 2 | /** |
| 3 | * Smarty plugin |
| 4 | * @package Smarty |
| 5 | * @subpackage plugins |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Smarty {cycle} function plugin |
| 10 | * |
| 11 | * Type: function<br> |
| 12 | * Name: cycle<br> |
| 13 | * Date: May 3, 2002<br> |
| 14 | * Purpose: cycle through given values<br> |
| 15 | * Input: |
| 16 | * - name = name of cycle (optional) |
| 17 | * - values = comma separated list of values to cycle, |
| 18 | * 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 |
| 25 | * printed. |
| 26 | * |
| 27 | * Examples:<br> |
| 28 | * <pre> |
| 29 | * {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) |
| 45 | { |
| 46 | static $cycle_vars; |
| 47 | |
| 48 | extract($params); |
| 49 | |
| 50 | if (empty($name)) { |
| 51 | $name = 'default'; |
| 52 | } |
| 53 | |
| 54 | if (!isset($print)) { |
| 55 | $print = true; |
| 56 | } |
| 57 | |
| 58 | if (!isset($advance)) { |
| 59 | $advance = true; |
| 60 | } |
| 61 | |
| 62 | if (!isset($reset)) { |
| 63 | $reset = false; |
| 64 | } |
| 65 | |
| 66 | if (!in_array('values', array_keys($params))) { |
| 67 | if(!isset($cycle_vars[$name]['values'])) { |
| 68 | $smarty->trigger_error("cycle: missing 'values' parameter"); |
| 69 | return; |
| 70 | } |
| 71 | } else { |
| 72 | if(isset($cycle_vars[$name]['values']) |
| 73 | && $cycle_vars[$name]['values'] != $values ) { |
| 74 | $cycle_vars[$name]['index'] = 0; |
| 75 | } |
| 76 | $cycle_vars[$name]['values'] = $values; |
| 77 | } |
| 78 | |
| 79 | if (isset($delimiter)) { |
| 80 | $cycle_vars[$name]['delimiter'] = $delimiter; |
| 81 | } elseif (!isset($cycle_vars[$name]['delimiter'])) { |
| 82 | $cycle_vars[$name]['delimiter'] = ','; |
| 83 | } |
| 84 | |
| 85 | if(!is_array($cycle_vars[$name]['values'])) { |
| 86 | $cycle_array = explode($cycle_vars[$name]['delimiter'],$cycle_vars[$name]['values']); |
| 87 | } else { |
| 88 | $cycle_array = $cycle_vars[$name]['values']; |
| 89 | } |
| 90 | |
| 91 | if(!isset($cycle_vars[$name]['index']) || $reset ) { |
| 92 | $cycle_vars[$name]['index'] = 0; |
| 93 | } |
| 94 | |
| 95 | if (isset($assign)) { |
| 96 | $print = false; |
| 97 | $smarty->assign($assign, $cycle_array[$cycle_vars[$name]['index']]); |
| 98 | } |
| 99 | |
| 100 | if($print) { |
| 101 | $retval = $cycle_array[$cycle_vars[$name]['index']]; |
| 102 | } else { |
| 103 | $retval = null; |
| 104 | } |
| 105 | |
| 106 | if($advance) { |
| 107 | if ( $cycle_vars[$name]['index'] >= count($cycle_array) -1 ) { |
| 108 | $cycle_vars[$name]['index'] = 0; |
| 109 | } else { |
| 110 | $cycle_vars[$name]['index']++; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return $retval; |
| 115 | } |
| 116 | |
| 117 | /* vim: set expandtab: */ |
| 118 | |
| 119 | ?> |