PageRenderTime 65ms CodeModel.GetById 55ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/vendor/smarty3/lib/libs/plugins/function.cycle.php

http://zoop.googlecode.com/
PHP | 101 lines | 51 code | 9 blank | 41 comment | 17 complexity | 086f7f65f2506b57f581af4c8f9b03a6 MD5 | raw file
  1<?php
  2/**
  3 * Smarty plugin
  4 * @package Smarty
  5 * @subpackage PluginsFunction
  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 *
 16 * Examples:<br>
 17 * <pre>
 18 * {cycle values="#eeeeee,#d0d0d0d"}
 19 * {cycle name=row values="one,two,three" reset=true}
 20 * {cycle name=row}
 21 * </pre>
 22 * @link http://smarty.php.net/manual/en/language.function.cycle.php {cycle}
 23 *       (Smarty online manual)
 24 * @author Monte Ohrt <monte at ohrt dot com>
 25 * @author credit to Mark Priatel <mpriatel@rogers.com>
 26 * @author credit to Gerard <gerard@interfold.com>
 27 * @author credit to Jason Sweat <jsweat_php@yahoo.com>
 28 * @param array $params parameters
 29 * Input:
 30 *         - name = name of cycle (optional)
 31 *         - values = comma separated list of values to cycle,
 32 *                    or an array of values to cycle
 33 *                    (this can be left out for subsequent calls)
 34 *         - reset = boolean - resets given var to true
 35 *         - print = boolean - print var or not. default is true
 36 *         - advance = boolean - whether or not to advance the cycle
 37 *         - delimiter = the value delimiter, default is ","
 38 *         - assign = boolean, assigns to template var instead of
 39 *                    printed.
 40 * @param object $smarty Smarty object
 41 * @param object $template template object
 42 * @return string|null
 43 */
 44function smarty_function_cycle($params, $smarty, $template)
 45{
 46    $name = (empty($params['name'])) ? 'default' : $params['name'];
 47    $print = (isset($params['print'])) ? (bool)$params['print'] : true;
 48    $advance = (isset($params['advance'])) ? (bool)$params['advance'] : true;
 49    $reset = (isset($params['reset'])) ? (bool)$params['reset'] : false;
 50            
 51    if (!in_array('values', array_keys($params))) {
 52        if(!isset($template->plugin_data['cycle'][$name]['values'])) {
 53            trigger_error("cycle: missing 'values' parameter",E_USER_WARNING);
 54            return;
 55        }
 56    } else {
 57        if(isset($template->plugin_data['cycle'][$name]['values'])
 58            && $template->plugin_data['cycle'][$name]['values'] != $params['values'] ) {
 59            $template->plugin_data['cycle'][$name]['index'] = 0;
 60        }
 61        $template->plugin_data['cycle'][$name]['values'] = $params['values'];
 62    }
 63
 64    if (isset($params['delimiter'])) {
 65      $template->plugin_data['cycle'][$name]['delimiter'] = $params['delimiter'];
 66    } elseif (!isset($template->plugin_data['cycle'][$name]['delimiter'])) {
 67      $template->plugin_data['cycle'][$name]['delimiter'] = ',';
 68    }
 69    
 70    if(is_array($template->plugin_data['cycle'][$name]['values'])) {
 71        $cycle_array = $template->plugin_data['cycle'][$name]['values'];
 72    } else {
 73        $cycle_array = explode($template->plugin_data['cycle'][$name]['delimiter'],$template->plugin_data['cycle'][$name]['values']);
 74    }
 75    
 76    if(!isset($template->plugin_data['cycle'][$name]['index']) || $reset ) {
 77        $template->plugin_data['cycle'][$name]['index'] = 0;
 78    }
 79    
 80    if (isset($params['assign'])) {
 81        $print = false;
 82        $template->assign($params['assign'], $cycle_array[$template->plugin_data['cycle'][$name]['index']]);
 83    }
 84        
 85    if($print) {
 86        $retval = $cycle_array[$template->plugin_data['cycle'][$name]['index']];
 87    } else {
 88        $retval = null;
 89    }
 90
 91    if($advance) {
 92        if ( $template->plugin_data['cycle'][$name]['index'] >= count($cycle_array) -1 ) {
 93            $template->plugin_data['cycle'][$name]['index'] = 0;
 94        } else {
 95            $template->plugin_data['cycle'][$name]['index']++;
 96        }
 97    }
 98    
 99    return $retval;
100}
101?>