| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |  * Smarty plugin | 
					
						
							|  |  |  |  *  | 
					
						
							|  |  |  |  * @package Smarty | 
					
						
							|  |  |  |  * @subpackage PluginsFunction | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |  * Smarty {html_options} function plugin | 
					
						
							|  |  |  |  *  | 
					
						
							|  |  |  |  * Type:     function<br> | 
					
						
							|  |  |  |  * Name:     html_options<br> | 
					
						
							|  |  |  |  * Purpose:  Prints the list of <option> tags generated from | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |  *           the passed parameters<br> | 
					
						
							|  |  |  |  * Params: | 
					
						
							|  |  |  |  * <pre> | 
					
						
							|  |  |  |  * - name       (optional) - string default "select" | 
					
						
							|  |  |  |  * - values     (required) - if no options supplied) - array | 
					
						
							|  |  |  |  * - options    (required) - if no values supplied) - associative array | 
					
						
							|  |  |  |  * - selected   (optional) - string default not set | 
					
						
							|  |  |  |  * - output     (required) - if not options supplied) - array | 
					
						
							|  |  |  |  * - id         (optional) - string default not set | 
					
						
							|  |  |  |  * - class      (optional) - string default not set | 
					
						
							|  |  |  |  * </pre> | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |  *  | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |  * @link http://www.smarty.net/manual/en/language.function.html.options.php {html_image} | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |  *      (Smarty online manual) | 
					
						
							|  |  |  |  * @author Monte Ohrt <monte at ohrt dot com>  | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |  * @author Ralf Strehle (minor optimization) <ralf dot strehle at yahoo dot de> | 
					
						
							|  |  |  |  * @param array                    $params   parameters | 
					
						
							|  |  |  |  * @param Smarty_Internal_Template $template template object | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |  * @return string  | 
					
						
							|  |  |  |  * @uses smarty_function_escape_special_chars() | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2010-11-12 23:42:32 +00:00
										 |  |  | function smarty_function_html_options($params, $template) | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-09-19 13:22:32 +00:00
										 |  |  |     require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php'); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     $name = null; | 
					
						
							|  |  |  |     $values = null; | 
					
						
							|  |  |  |     $options = null; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     $selected = null; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     $output = null; | 
					
						
							| 
									
										
										
										
											2010-12-21 15:59:25 +00:00
										 |  |  |     $id = null; | 
					
						
							|  |  |  |     $class = null; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     $extra = ''; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     foreach ($params as $_key => $_val) { | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         switch ($_key) { | 
					
						
							|  |  |  |             case 'name': | 
					
						
							| 
									
										
										
										
											2010-12-21 15:59:25 +00:00
										 |  |  |             case 'class': | 
					
						
							|  |  |  |             case 'id': | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |                 $$_key = (string)$_val; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             case 'options': | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |                 $options = (array)$_val; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |                 break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             case 'values': | 
					
						
							|  |  |  |             case 'output': | 
					
						
							|  |  |  |                 $$_key = array_values((array)$_val); | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             case 'selected': | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |                 if (is_array($_val)) { | 
					
						
							|  |  |  |                     $selected = array_map('strval', array_values((array)$_val)); | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     $selected = $_val; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |                 break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             default: | 
					
						
							|  |  |  |                 if (!is_array($_val)) { | 
					
						
							|  |  |  |                     $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"'; | 
					
						
							|  |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2009-12-01 20:34:32 +00:00
										 |  |  |                     trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |                 }  | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |         }  | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!isset($options) && !isset($values)) | 
					
						
							|  |  |  |         return ''; | 
					
						
							|  |  |  |     /* raise error here? */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $_html_result = ''; | 
					
						
							| 
									
										
										
										
											2010-12-21 15:59:25 +00:00
										 |  |  |     $_idx = 0; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (isset($options)) { | 
					
						
							| 
									
										
										
										
											2010-12-21 15:59:25 +00:00
										 |  |  |         foreach ($options as $_key => $_val) { | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |             $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx); | 
					
						
							| 
									
										
										
										
											2010-12-21 15:59:25 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     } else { | 
					
						
							|  |  |  |         foreach ($values as $_i => $_key) { | 
					
						
							|  |  |  |             $_val = isset($output[$_i]) ? $output[$_i] : ''; | 
					
						
							| 
									
										
										
										
											2010-12-21 15:59:25 +00:00
										 |  |  |             $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         }  | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!empty($name)) { | 
					
						
							| 
									
										
										
										
											2010-12-21 15:59:25 +00:00
										 |  |  |         $_html_class = !empty($class) ? ' class="'.$class.'"' : ''; | 
					
						
							|  |  |  |         $_html_id = !empty($id) ? ' id="'.$id.'"' : ''; | 
					
						
							|  |  |  |         $_html_result = '<select name="' . $name . '"' . $_html_class . $_html_id . $extra . '>' . "\n" . $_html_result . '</select>' . "\n"; | 
					
						
							| 
									
										
										
										
											2010-04-16 13:49:46 +00:00
										 |  |  |     }  | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return $_html_result; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-21 15:59:25 +00:00
										 |  |  | function smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, &$idx) | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     if (!is_array($value)) { | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         $_html_result = '<option value="' . smarty_function_escape_special_chars($key) . '"'; | 
					
						
							|  |  |  |         if (is_array($selected)) { | 
					
						
							|  |  |  |             if (in_array((string)$key, $selected)) { | 
					
						
							|  |  |  |                 $_html_result .= ' selected="selected"'; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } elseif ($key == $selected) { | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |             $_html_result .= ' selected="selected"'; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-12-21 15:59:25 +00:00
										 |  |  |         $_html_class = !empty($class) ? ' class="'.$class.' option"' : ''; | 
					
						
							|  |  |  |         $_html_id = !empty($id) ? ' id="'.$id.'-'.$idx.'"' : ''; | 
					
						
							|  |  |  |         $_html_result .= $_html_class . $_html_id . '>' . smarty_function_escape_special_chars($value) . '</option>' . "\n"; | 
					
						
							|  |  |  |         $idx++; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2010-12-21 15:59:25 +00:00
										 |  |  |         $_idx = 0; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         $_html_result = smarty_function_html_options_optgroup($key, $value, $selected, !empty($id) ? ($id.'-'.$idx) : null, $class, $_idx); | 
					
						
							| 
									
										
										
										
											2010-12-21 15:59:25 +00:00
										 |  |  |         $idx++; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     return $_html_result; | 
					
						
							|  |  |  | }  | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-21 15:59:25 +00:00
										 |  |  | function smarty_function_html_options_optgroup($key, $values, $selected, $id, $class, &$idx) | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-04-16 13:49:46 +00:00
										 |  |  |     $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n"; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     foreach ($values as $key => $value) { | 
					
						
							| 
									
										
										
										
											2010-12-21 15:59:25 +00:00
										 |  |  |         $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, $idx); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     }  | 
					
						
							|  |  |  |     $optgroup_html .= "</optgroup>\n"; | 
					
						
							|  |  |  |     return $optgroup_html; | 
					
						
							|  |  |  | }  | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  | ?>
 |