Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Smarty plugin
Chris PeBenito 696b41
 * @package Smarty
Chris PeBenito 696b41
 * @subpackage plugins
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Smarty {cycle} function plugin
Chris PeBenito 696b41
 *
Chris PeBenito 696b41
 * Type:     function
Chris PeBenito 696b41
 * Name:     cycle
Chris PeBenito 696b41
 * Date:     May 3, 2002
Chris PeBenito 696b41
 * Purpose:  cycle through given values
Chris PeBenito 696b41
 * Input:
Chris PeBenito 696b41
 *         - name = name of cycle (optional)
Chris PeBenito 696b41
 *         - values = comma separated list of values to cycle,
Chris PeBenito 696b41
 *                    or an array of values to cycle
Chris PeBenito 696b41
 *                    (this can be left out for subsequent calls)
Chris PeBenito 696b41
 *         - reset = boolean - resets given var to true
Chris PeBenito 696b41
 *         - print = boolean - print var or not. default is true
Chris PeBenito 696b41
 *         - advance = boolean - whether or not to advance the cycle
Chris PeBenito 696b41
 *         - delimiter = the value delimiter, default is ","
Chris PeBenito 696b41
 *         - assign = boolean, assigns to template var instead of
Chris PeBenito 696b41
 *                    printed.
Chris PeBenito 696b41
 *
Chris PeBenito 696b41
 * Examples:
Chris PeBenito 696b41
 * 
Chris PeBenito 696b41
 * {cycle values="#eeeeee,#d0d0d0d"}
Chris PeBenito 696b41
 * {cycle name=row values="one,two,three" reset=true}
Chris PeBenito 696b41
 * {cycle name=row}
Chris PeBenito 696b41
 * 
Chris PeBenito 696b41
 * @link http://smarty.php.net/manual/en/language.function.cycle.php {cycle}
Chris PeBenito 696b41
 *       (Smarty online manual)
Chris PeBenito 696b41
 * @author Monte Ohrt <monte at ohrt dot com>
Chris PeBenito 696b41
 * @author credit to Mark Priatel <mpriatel@rogers.com>
Chris PeBenito 696b41
 * @author credit to Gerard <gerard@interfold.com>
Chris PeBenito 696b41
 * @author credit to Jason Sweat <jsweat_php@yahoo.com>
Chris PeBenito 696b41
 * @version  1.3
Chris PeBenito 696b41
 * @param array
Chris PeBenito 696b41
 * @param Smarty
Chris PeBenito 696b41
 * @return string|null
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
function smarty_function_cycle($params, &$smarty)
Chris PeBenito 696b41
{
Chris PeBenito 696b41
    static $cycle_vars;
Chris PeBenito 696b41
    
Chris PeBenito 696b41
    $name = (empty($params['name'])) ? 'default' : $params['name'];
Chris PeBenito 696b41
    $print = (isset($params['print'])) ? (bool)$params['print'] : true;
Chris PeBenito 696b41
    $advance = (isset($params['advance'])) ? (bool)$params['advance'] : true;
Chris PeBenito 696b41
    $reset = (isset($params['reset'])) ? (bool)$params['reset'] : false;
Chris PeBenito 696b41
            
Chris PeBenito 696b41
    if (!in_array('values', array_keys($params))) {
Chris PeBenito 696b41
        if(!isset($cycle_vars[$name]['values'])) {
Chris PeBenito 696b41
            $smarty->trigger_error("cycle: missing 'values' parameter");
Chris PeBenito 696b41
            return;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
    } else {
Chris PeBenito 696b41
        if(isset($cycle_vars[$name]['values'])
Chris PeBenito 696b41
            && $cycle_vars[$name]['values'] != $params['values'] ) {
Chris PeBenito 696b41
            $cycle_vars[$name]['index'] = 0;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        $cycle_vars[$name]['values'] = $params['values'];
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    $cycle_vars[$name]['delimiter'] = (isset($params['delimiter'])) ? $params['delimiter'] : ',';
Chris PeBenito 696b41
    
Chris PeBenito 696b41
    if(is_array($cycle_vars[$name]['values'])) {
Chris PeBenito 696b41
        $cycle_array = $cycle_vars[$name]['values'];
Chris PeBenito 696b41
    } else {
Chris PeBenito 696b41
        $cycle_array = explode($cycle_vars[$name]['delimiter'],$cycle_vars[$name]['values']);
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
    
Chris PeBenito 696b41
    if(!isset($cycle_vars[$name]['index']) || $reset ) {
Chris PeBenito 696b41
        $cycle_vars[$name]['index'] = 0;
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
    
Chris PeBenito 696b41
    if (isset($params['assign'])) {
Chris PeBenito 696b41
        $print = false;
Chris PeBenito 696b41
        $smarty->assign($params['assign'], $cycle_array[$cycle_vars[$name]['index']]);
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
        
Chris PeBenito 696b41
    if($print) {
Chris PeBenito 696b41
        $retval = $cycle_array[$cycle_vars[$name]['index']];
Chris PeBenito 696b41
    } else {
Chris PeBenito 696b41
        $retval = null;
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    if($advance) {
Chris PeBenito 696b41
        if ( $cycle_vars[$name]['index'] >= count($cycle_array) -1 ) {
Chris PeBenito 696b41
            $cycle_vars[$name]['index'] = 0;
Chris PeBenito 696b41
        } else {
Chris PeBenito 696b41
            $cycle_vars[$name]['index']++;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
    
Chris PeBenito 696b41
    return $retval;
Chris PeBenito 696b41
}
Chris PeBenito 696b41
Chris PeBenito 696b41
/* vim: set expandtab: */
Chris PeBenito 696b41
Chris PeBenito 696b41
?>