Deutschland United States United Kingdom
ContentLion - Open Source CMS

Blame function.cycle.php

Details | Last modification | View Log

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