| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Smarty plugin | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |  * @package Smarty | 
					
						
							|  |  |  |  * @subpackage PluginsFunction | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Smarty {html_checkboxes} function plugin | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * File:       function.html_checkboxes.php<br> | 
					
						
							|  |  |  |  * Type:       function<br> | 
					
						
							|  |  |  |  * Name:       html_checkboxes<br> | 
					
						
							|  |  |  |  * Date:       24.Feb.2003<br> | 
					
						
							|  |  |  |  * Purpose:    Prints out a list of checkbox input types<br> | 
					
						
							|  |  |  |  * Examples: | 
					
						
							|  |  |  |  * <pre> | 
					
						
							|  |  |  |  * {html_checkboxes values=$ids output=$names} | 
					
						
							|  |  |  |  * {html_checkboxes values=$ids name='box' separator='<br>' output=$names} | 
					
						
							|  |  |  |  * {html_checkboxes values=$ids checked=$checked separator='<br>' output=$names} | 
					
						
							|  |  |  |  * </pre> | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |  * Params: | 
					
						
							|  |  |  |  * <pre> | 
					
						
							|  |  |  |  * - name       (optional) - string default "checkbox" | 
					
						
							|  |  |  |  * - values     (required) - array | 
					
						
							|  |  |  |  * - options    (optional) - associative array | 
					
						
							|  |  |  |  * - checked    (optional) - array default not set | 
					
						
							|  |  |  |  * - separator  (optional) - ie <br> or   | 
					
						
							|  |  |  |  * - output     (optional) - the output next to each checkbox | 
					
						
							|  |  |  |  * - assign     (optional) - assign the output as an array to this variable | 
					
						
							| 
									
										
										
										
											2011-11-16 11:28:35 +00:00
										 |  |  |  * - escape     (optional) - escape the content (not value), defaults to true | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |  * </pre> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @link http://www.smarty.net/manual/en/language.function.html.checkboxes.php {html_checkboxes} | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |  *      (Smarty online manual) | 
					
						
							|  |  |  |  * @author     Christopher Kvarme <christopher.kvarme@flashjab.com> | 
					
						
							|  |  |  |  * @author credits to Monte Ohrt <monte at ohrt dot com> | 
					
						
							|  |  |  |  * @version    1.0 | 
					
						
							|  |  |  |  * @param array $params parameters | 
					
						
							|  |  |  |  * @param object $template template object | 
					
						
							|  |  |  |  * @return string | 
					
						
							|  |  |  |  * @uses smarty_function_escape_special_chars() | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2010-11-12 23:42:32 +00:00
										 |  |  | function smarty_function_html_checkboxes($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 = 'checkbox'; | 
					
						
							|  |  |  |     $values = null; | 
					
						
							|  |  |  |     $options = null; | 
					
						
							| 
									
										
										
										
											2011-10-07 13:39:45 +00:00
										 |  |  |     $selected = array(); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     $separator = ''; | 
					
						
							| 
									
										
										
										
											2011-11-16 11:28:35 +00:00
										 |  |  |     $escape = true; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     $labels = true; | 
					
						
							| 
									
										
										
										
											2011-10-07 13:39:45 +00:00
										 |  |  |     $label_ids = false; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     $output = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $extra = ''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     foreach($params as $_key => $_val) { | 
					
						
							|  |  |  |         switch($_key) { | 
					
						
							|  |  |  |             case 'name': | 
					
						
							|  |  |  |             case 'separator': | 
					
						
							| 
									
										
										
										
											2011-10-07 13:39:45 +00:00
										 |  |  |                 $$_key = (string) $_val; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |                 break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-16 11:28:35 +00:00
										 |  |  |             case 'escape': | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |             case 'labels': | 
					
						
							| 
									
										
										
										
											2011-10-07 13:39:45 +00:00
										 |  |  |             case 'label_ids': | 
					
						
							|  |  |  |                 $$_key = (bool) $_val; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |                 break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             case 'options': | 
					
						
							| 
									
										
										
										
											2011-10-07 13:39:45 +00:00
										 |  |  |                 $$_key = (array) $_val; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |                 break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             case 'values': | 
					
						
							|  |  |  |             case 'output': | 
					
						
							| 
									
										
										
										
											2011-10-07 13:39:45 +00:00
										 |  |  |                 $$_key = array_values((array) $_val); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |                 break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             case 'checked': | 
					
						
							|  |  |  |             case 'selected': | 
					
						
							| 
									
										
										
										
											2011-10-07 13:39:45 +00:00
										 |  |  |                 if (is_array($_val)) { | 
					
						
							|  |  |  |                     $selected = array(); | 
					
						
							|  |  |  |                     foreach ($_val as $_sel) { | 
					
						
							|  |  |  |                         if (is_object($_sel)) { | 
					
						
							|  |  |  |                             if (method_exists($_sel, "__toString")) { | 
					
						
							| 
									
										
										
										
											2011-10-10 08:53:43 +00:00
										 |  |  |                                 $_sel = smarty_function_escape_special_chars((string) $_sel->__toString()); | 
					
						
							| 
									
										
										
										
											2011-10-07 13:39:45 +00:00
										 |  |  |                             } else { | 
					
						
							|  |  |  |                                 trigger_error("html_checkboxes: selected attribute contains an object of class '". get_class($_sel) ."' without __toString() method", E_USER_NOTICE); | 
					
						
							|  |  |  |                                 continue; | 
					
						
							|  |  |  |                             } | 
					
						
							|  |  |  |                         } else { | 
					
						
							|  |  |  |                             $_sel = smarty_function_escape_special_chars((string) $_sel); | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         $selected[$_sel] = true; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } elseif (is_object($_val)) { | 
					
						
							|  |  |  |                     if (method_exists($_val, "__toString")) { | 
					
						
							|  |  |  |                         $selected = smarty_function_escape_special_chars((string) $_val->__toString()); | 
					
						
							|  |  |  |                     } else { | 
					
						
							|  |  |  |                         trigger_error("html_checkboxes: selected attribute is an object of class '". get_class($_val) ."' without __toString() method", E_USER_NOTICE); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     $selected = smarty_function_escape_special_chars((string) $_val); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |                 break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             case 'checkboxes': | 
					
						
							| 
									
										
										
										
											2009-12-01 20:34:32 +00:00
										 |  |  |                 trigger_error('html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead', E_USER_WARNING); | 
					
						
							| 
									
										
										
										
											2011-10-07 13:39:45 +00:00
										 |  |  |                 $options = (array) $_val; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |                 break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             case 'assign': | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-13 18:31:48 +00:00
										 |  |  |             case 'strict': break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             case 'disabled': | 
					
						
							|  |  |  |             case 'readonly': | 
					
						
							|  |  |  |                 if (!empty($params['strict'])) { | 
					
						
							|  |  |  |                     if (!is_scalar($_val)) { | 
					
						
							|  |  |  |                         trigger_error("html_options: $_key attribute must be a scalar, only boolean true or string '$_key' will actually add the attribute", E_USER_NOTICE); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     if ($_val === true || $_val === $_key) { | 
					
						
							|  |  |  |                         $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"'; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     break; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 // omit break; to fall through!
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |             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_checkboxes: extra attribute '$_key' cannot be an array", E_USER_NOTICE); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!isset($options) && !isset($values)) | 
					
						
							|  |  |  |         return ''; /* raise error here? */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $_html_result = array(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (isset($options)) { | 
					
						
							| 
									
										
										
										
											2011-10-07 13:39:45 +00:00
										 |  |  |         foreach ($options as $_key=>$_val) { | 
					
						
							| 
									
										
										
										
											2011-11-16 11:28:35 +00:00
										 |  |  |             $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $escape); | 
					
						
							| 
									
										
										
										
											2011-10-07 13:39:45 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     } else { | 
					
						
							|  |  |  |         foreach ($values as $_i=>$_key) { | 
					
						
							|  |  |  |             $_val = isset($output[$_i]) ? $output[$_i] : ''; | 
					
						
							| 
									
										
										
										
											2011-11-16 11:28:35 +00:00
										 |  |  |             $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $escape); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if(!empty($params['assign'])) { | 
					
						
							|  |  |  |         $template->assign($params['assign'], $_html_result); | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2011-10-07 13:39:45 +00:00
										 |  |  |         return implode("\n", $_html_result); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-16 11:28:35 +00:00
										 |  |  | function smarty_function_html_checkboxes_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids, $escape=true) { | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     $_output = ''; | 
					
						
							| 
									
										
										
										
											2011-10-07 13:39:45 +00:00
										 |  |  |      | 
					
						
							|  |  |  |     if (is_object($value)) { | 
					
						
							|  |  |  |         if (method_exists($value, "__toString")) { | 
					
						
							|  |  |  |             $value = (string) $value->__toString(); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             trigger_error("html_options: value is an object of class '". get_class($value) ."' without __toString() method", E_USER_NOTICE); | 
					
						
							|  |  |  |             return ''; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         $value = (string) $value; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     if (is_object($output)) { | 
					
						
							|  |  |  |         if (method_exists($output, "__toString")) { | 
					
						
							|  |  |  |             $output = (string) $output->__toString(); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             trigger_error("html_options: output is an object of class '". get_class($output) ."' without __toString() method", E_USER_NOTICE); | 
					
						
							|  |  |  |             return ''; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         $output = (string) $output; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     if ($labels) { | 
					
						
							|  |  |  |         if ($label_ids) { | 
					
						
							| 
									
										
										
										
											2011-12-18 18:48:07 +00:00
										 |  |  |             $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!' . Smarty::$_UTF8_MODIFIER, '_', $name . '_' . $value)); | 
					
						
							| 
									
										
										
										
											2011-10-07 13:39:45 +00:00
										 |  |  |             $_output .= '<label for="' . $_id . '">'; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $_output .= '<label>'; | 
					
						
							|  |  |  |         }  | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     $name = smarty_function_escape_special_chars($name); | 
					
						
							|  |  |  |     $value = smarty_function_escape_special_chars($value); | 
					
						
							| 
									
										
										
										
											2011-11-16 11:28:35 +00:00
										 |  |  |     if ($escape) { | 
					
						
							|  |  |  |         $output = smarty_function_escape_special_chars($output); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-10-07 13:39:45 +00:00
										 |  |  |      | 
					
						
							|  |  |  |     $_output .= '<input type="checkbox" name="' . $name . '[]" value="' . $value . '"'; | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     if ($labels && $label_ids) { | 
					
						
							|  |  |  |         $_output .= ' id="' . $_id . '"'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     if (is_array($selected)) { | 
					
						
							|  |  |  |         if (isset($selected[$value])) { | 
					
						
							|  |  |  |             $_output .= ' checked="checked"'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } elseif ($value === $selected) { | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         $_output .= ' checked="checked"'; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-10-07 13:39:45 +00:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     $_output .= $extra . ' />' . $output; | 
					
						
							| 
									
										
										
										
											2011-10-07 13:39:45 +00:00
										 |  |  |     if ($labels) { | 
					
						
							|  |  |  |         $_output .= '</label>'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     $_output .=  $separator; | 
					
						
							|  |  |  |     return $_output; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  | ?>
 |