| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * Smarty plugin |
|---|
| 4 | * @package Smarty |
|---|
| 5 | * @subpackage plugins |
|---|
| 6 | */ |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | /** |
|---|
| 10 | * Smarty {html_checkboxes} function plugin |
|---|
| 11 | * |
|---|
| 12 | * File: function.html_checkboxes.php<br> |
|---|
| 13 | * Type: function<br> |
|---|
| 14 | * Name: html_checkboxes<br> |
|---|
| 15 | * Date: 24.Feb.2003<br> |
|---|
| 16 | * Purpose: Prints out a list of checkbox input types<br> |
|---|
| 17 | * Input:<br> |
|---|
| 18 | * - name (optional) - string default "checkbox" |
|---|
| 19 | * - values (required) - array |
|---|
| 20 | * - options (optional) - associative array |
|---|
| 21 | * - checked (optional) - array default not set |
|---|
| 22 | * - separator (optional) - ie <br> or |
|---|
| 23 | * - output (optional) - the output next to each checkbox |
|---|
| 24 | * - assign (optional) - assign the output as an array to this variable |
|---|
| 25 | * Examples: |
|---|
| 26 | * <pre> |
|---|
| 27 | * {html_checkboxes values=$ids output=$names} |
|---|
| 28 | * {html_checkboxes values=$ids name='box' separator='<br>' output=$names} |
|---|
| 29 | * {html_checkboxes values=$ids checked=$checked separator='<br>' output=$names} |
|---|
| 30 | * </pre> |
|---|
| 31 | * @link http://smarty.php.net/manual/en/language.function.html.checkboxes.php {html_checkboxes} |
|---|
| 32 | * (Smarty online manual) |
|---|
| 33 | * @author Christopher Kvarme <[email protected]> |
|---|
| 34 | * @author credits to Monte Ohrt <monte at ohrt dot com> |
|---|
| 35 | * @version 1.0 |
|---|
| 36 | * @param array |
|---|
| 37 | * @param Smarty |
|---|
| 38 | * @return string |
|---|
| 39 | * @uses smarty_function_escape_special_chars() |
|---|
| 40 | */ |
|---|
| 41 | function smarty_function_html_checkboxes_ex($params, &$smarty) |
|---|
| 42 | { |
|---|
| 43 | require_once $smarty->_get_plugin_filepath('shared','escape_special_chars'); |
|---|
| 44 | |
|---|
| 45 | $name = 'checkbox'; |
|---|
| 46 | $values = null; |
|---|
| 47 | $options = null; |
|---|
| 48 | $selected = null; |
|---|
| 49 | $separator = ''; |
|---|
| 50 | $labels = true; |
|---|
| 51 | $label_ids = true; |
|---|
| 52 | $output = null; |
|---|
| 53 | |
|---|
| 54 | $extra = ''; |
|---|
| 55 | |
|---|
| 56 | foreach($params as $_key => $_val) { |
|---|
| 57 | switch($_key) { |
|---|
| 58 | case 'tags': |
|---|
| 59 | $$_key = split("\|", $_val); |
|---|
| 60 | break; |
|---|
| 61 | case 'name': |
|---|
| 62 | case 'separator': |
|---|
| 63 | $$_key = $_val; |
|---|
| 64 | break; |
|---|
| 65 | |
|---|
| 66 | case 'labels': |
|---|
| 67 | case 'label_ids': |
|---|
| 68 | $$_key = (bool)$_val; |
|---|
| 69 | break; |
|---|
| 70 | |
|---|
| 71 | case 'options': |
|---|
| 72 | $$_key = (array)$_val; |
|---|
| 73 | break; |
|---|
| 74 | |
|---|
| 75 | case 'values': |
|---|
| 76 | case 'output': |
|---|
| 77 | $$_key = array_values((array)$_val); |
|---|
| 78 | break; |
|---|
| 79 | |
|---|
| 80 | case 'checked': |
|---|
| 81 | case 'selected': |
|---|
| 82 | $selected = array_map('strval', array_values((array)$_val)); |
|---|
| 83 | break; |
|---|
| 84 | |
|---|
| 85 | case 'checkboxes': |
|---|
| 86 | $smarty->trigger_error('html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead', E_USER_WARNING); |
|---|
| 87 | $options = (array)$_val; |
|---|
| 88 | break; |
|---|
| 89 | |
|---|
| 90 | case 'assign': |
|---|
| 91 | break; |
|---|
| 92 | |
|---|
| 93 | default: |
|---|
| 94 | if(!is_array($_val)) { |
|---|
| 95 | $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"'; |
|---|
| 96 | } else { |
|---|
| 97 | $smarty->trigger_error("html_checkboxes: extra attribute '$_key' cannot be an array", E_USER_NOTICE); |
|---|
| 98 | } |
|---|
| 99 | break; |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | if (!isset($options) && !isset($values)) |
|---|
| 104 | return ''; /* raise error here? */ |
|---|
| 105 | |
|---|
| 106 | settype($selected, 'array'); |
|---|
| 107 | $_html_result = array(); |
|---|
| 108 | |
|---|
| 109 | if (isset($options)) { |
|---|
| 110 | |
|---|
| 111 | foreach ($options as $_key=>$_val) |
|---|
| 112 | $_html_result[] = smarty_function_html_checkboxes_output_ex($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $tags); |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | } else { |
|---|
| 116 | foreach ($values as $_i=>$_key) { |
|---|
| 117 | $_val = isset($output[$_i]) ? $output[$_i] : ''; |
|---|
| 118 | $_html_result[] = smarty_function_html_checkboxes_output_ex($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $tags); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | if(!empty($params['assign'])) { |
|---|
| 124 | $smarty->assign($params['assign'], $_html_result); |
|---|
| 125 | } else { |
|---|
| 126 | return implode("\n",$_html_result); |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | function smarty_function_html_checkboxes_output_ex($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids, $tags) { |
|---|
| 131 | $_output = ''; |
|---|
| 132 | |
|---|
| 133 | $_output .= '<input type="checkbox" name="' |
|---|
| 134 | . smarty_function_escape_special_chars($name) . '[]" value="' |
|---|
| 135 | . smarty_function_escape_special_chars($value) . '"'; |
|---|
| 136 | |
|---|
| 137 | if ($labels && $label_ids) { |
|---|
| 138 | $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!', '_', $name . '_' . $value)); |
|---|
| 139 | $_output .= ' id="' . $_id . '"'; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | if (in_array((string)$value, $selected)) { |
|---|
| 143 | $_output .= ' checked="checked"'; |
|---|
| 144 | } |
|---|
| 145 | $_output .= $extra . ' />'; |
|---|
| 146 | |
|---|
| 147 | $_output .= $tags[0]; |
|---|
| 148 | |
|---|
| 149 | if ($labels) { |
|---|
| 150 | if($label_ids) { |
|---|
| 151 | $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!', '_', $name . '_' . $value)); |
|---|
| 152 | $_output .= '<label for="' . $_id . '">'; |
|---|
| 153 | } else { |
|---|
| 154 | $_output .= '<label>'; |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | // ÃͤòÁÞÆþ |
|---|
| 159 | $_output .= $output; |
|---|
| 160 | |
|---|
| 161 | $_output .= $tags[1]; |
|---|
| 162 | |
|---|
| 163 | if ($labels) $_output .= '</label>'; |
|---|
| 164 | $_output .= $separator; |
|---|
| 165 | |
|---|
| 166 | return $_output; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | ?> |
|---|