| 
									
										
										
										
											2002-05-02 22:05:58 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  |  * Smarty plugin | 
					
						
							|  |  |  |  * ------------------------------------------------------------- | 
					
						
							|  |  |  |  * Type:     function
 | 
					
						
							|  |  |  |  * Name:     cycle | 
					
						
							| 
									
										
										
										
											2002-06-03 16:53:53 +00:00
										 |  |  |  * Version:  1.3 | 
					
						
							| 
									
										
										
										
											2002-05-03 14:39:49 +00:00
										 |  |  |  * Date:     May 3, 2002 | 
					
						
							| 
									
										
										
										
											2002-05-02 22:05:58 +00:00
										 |  |  |  * Author:	 Monte Ohrt <monte@ispi.net> | 
					
						
							|  |  |  |  * Credits:  Mark Priatel <mpriatel@rogers.com> | 
					
						
							|  |  |  |  *           Gerard <gerard@interfold.com> | 
					
						
							| 
									
										
										
										
											2002-05-03 18:14:24 +00:00
										 |  |  |  *           Jason Sweat <jsweat_php@yahoo.com> | 
					
						
							| 
									
										
										
										
											2002-05-02 22:10:50 +00:00
										 |  |  |  * Purpose:  cycle through given values | 
					
						
							| 
									
										
										
										
											2002-05-03 18:14:24 +00:00
										 |  |  |  * Input:    name = name of cycle (optional) | 
					
						
							| 
									
										
										
										
											2002-05-07 14:16:34 +00:00
										 |  |  |  *           values = comma separated list of values to cycle, | 
					
						
							|  |  |  |  *                    or an array of values to cycle | 
					
						
							| 
									
										
										
										
											2002-05-03 14:39:49 +00:00
										 |  |  |  *                    (this can be left out for subsequent calls) | 
					
						
							| 
									
										
										
										
											2002-05-07 14:16:34 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2002-05-02 22:05:58 +00:00
										 |  |  |  *           reset = boolean - resets given var to true | 
					
						
							|  |  |  |  *			 print = boolean - print var or not. default is true | 
					
						
							|  |  |  |  *           advance = boolean - whether or not to advance the cycle | 
					
						
							|  |  |  |  *           delimiter = the value delimiter, default is "," | 
					
						
							|  |  |  |  *           assign = boolean, assigns to template var instead of | 
					
						
							|  |  |  |  *                    printed. | 
					
						
							|  |  |  |  *  | 
					
						
							|  |  |  |  * Examples: {cycle values="#eeeeee,#d0d0d0d"} | 
					
						
							| 
									
										
										
										
											2002-05-03 18:14:24 +00:00
										 |  |  |  *           {cycle name=row values="one,two,three" reset=true} | 
					
						
							|  |  |  |  *           {cycle name=row} | 
					
						
							| 
									
										
										
										
											2002-05-02 22:05:58 +00:00
										 |  |  |  * ------------------------------------------------------------- | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function smarty_function_cycle($params, &$smarty) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2002-05-03 14:39:49 +00:00
										 |  |  | 	static $cycle_vars; | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2002-05-02 22:05:58 +00:00
										 |  |  |     extract($params); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-05-03 18:14:24 +00:00
										 |  |  |     if (empty($name)) { | 
					
						
							|  |  |  |         $name = 'default'; | 
					
						
							| 
									
										
										
										
											2002-05-02 22:05:58 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!isset($print)) { | 
					
						
							|  |  |  |         $print = true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!isset($advance)) { | 
					
						
							|  |  |  |         $advance = true;		 | 
					
						
							|  |  |  |     }	 | 
					
						
							| 
									
										
										
										
											2002-06-18 18:20:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!isset($reset)) { | 
					
						
							|  |  |  |         $reset = false;		 | 
					
						
							|  |  |  |     }		 | 
					
						
							|  |  |  | 			 | 
					
						
							| 
									
										
										
										
											2002-05-02 22:05:58 +00:00
										 |  |  |     if (!in_array('values', array_keys($params))) { | 
					
						
							| 
									
										
										
										
											2002-05-03 18:14:24 +00:00
										 |  |  | 		if(!isset($cycle_vars[$name]['values'])) { | 
					
						
							| 
									
										
										
										
											2002-05-03 14:39:49 +00:00
										 |  |  |         	$smarty->trigger_error("cycle: missing 'values' parameter"); | 
					
						
							|  |  |  |         	return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2002-05-03 18:14:24 +00:00
										 |  |  | 		if(isset($cycle_vars[$name]['values']) | 
					
						
							|  |  |  | 			&& $cycle_vars[$name]['values'] != $values ) { | 
					
						
							|  |  |  | 			$cycle_vars[$name]['index'] = 0; | 
					
						
							| 
									
										
										
										
											2002-05-03 14:39:49 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2002-05-03 18:14:24 +00:00
										 |  |  | 		$cycle_vars[$name]['values'] = $values; | 
					
						
							| 
									
										
										
										
											2002-05-03 14:39:49 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2002-05-02 22:05:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-03 16:53:26 +00:00
										 |  |  |     if (isset($delimiter)) { | 
					
						
							|  |  |  | 		$cycle_vars[$name]['delimiter'] = $delimiter; | 
					
						
							|  |  |  |     } elseif (!isset($cycle_vars[$name]['delimiter'])) { | 
					
						
							|  |  |  | 		$cycle_vars[$name]['delimiter'] = ',';		 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2002-05-07 14:16:34 +00:00
										 |  |  | 	if(!is_array($cycle_vars[$name]['values'])) { | 
					
						
							| 
									
										
										
										
											2002-06-03 16:53:26 +00:00
										 |  |  | 		$cycle_array = explode($cycle_vars[$name]['delimiter'],$cycle_vars[$name]['values']); | 
					
						
							| 
									
										
										
										
											2002-05-07 14:16:34 +00:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		$cycle_array = $cycle_vars[$name]['values'];	 | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2002-05-02 22:05:58 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2002-05-03 18:14:24 +00:00
										 |  |  | 	if(!isset($cycle_vars[$name]['index']) || $reset ) { | 
					
						
							|  |  |  | 		$cycle_vars[$name]['index'] = 0; | 
					
						
							| 
									
										
										
										
											2002-05-02 22:05:58 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2002-06-03 16:53:26 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2002-05-02 22:05:58 +00:00
										 |  |  |     if (isset($assign)) { | 
					
						
							|  |  |  |         $print = false; | 
					
						
							| 
									
										
										
										
											2002-05-03 18:14:24 +00:00
										 |  |  |         $smarty->assign($assign, $cycle_array[$cycle_vars[$name]['index']]); | 
					
						
							| 
									
										
										
										
											2002-05-02 22:05:58 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 	if($print) { | 
					
						
							| 
									
										
										
										
											2003-01-08 17:34:45 +00:00
										 |  |  | 		$retval = $cycle_array[$cycle_vars[$name]['index']]; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		$retval = null; | 
					
						
							| 
									
										
										
										
											2002-05-02 22:05:58 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if($advance) { | 
					
						
							| 
									
										
										
										
											2002-05-03 18:14:24 +00:00
										 |  |  | 		if ( $cycle_vars[$name]['index'] >= count($cycle_array) -1 ) { | 
					
						
							|  |  |  | 			$cycle_vars[$name]['index'] = 0;			 | 
					
						
							| 
									
										
										
										
											2002-05-02 22:05:58 +00:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2002-05-03 18:14:24 +00:00
										 |  |  | 			$cycle_vars[$name]['index']++; | 
					
						
							| 
									
										
										
										
											2002-05-02 22:05:58 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2003-01-08 17:34:45 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	return $retval; | 
					
						
							| 
									
										
										
										
											2002-05-02 22:05:58 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* vim: set expandtab: */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ?>
 |