diff options
Diffstat (limited to 'views/default/input/checkboxes.php')
| -rw-r--r-- | views/default/input/checkboxes.php | 116 |
1 files changed, 63 insertions, 53 deletions
diff --git a/views/default/input/checkboxes.php b/views/default/input/checkboxes.php index 4abeabf58..db4b06949 100644 --- a/views/default/input/checkboxes.php +++ b/views/default/input/checkboxes.php @@ -2,79 +2,89 @@ /** * Elgg checkbox input * Displays a checkbox input field - * NB: This also includes a hidden input with the same name as the checkboxes + * + * @note This also includes a hidden input with the same name as the checkboxes * to make sure something is sent to the server. The default value is 0. * If using JS, be specific to avoid selecting the hidden default value: - * $('input[type=checkbox][name=internalname]') + * $('input[type=checkbox][name=name]') + * + * @warning Passing integers as labels does not currently work due to a + * deprecated hack that will be removed in Elgg 1.9. To use integer labels, + * the labels must be character codes: 1 would be 1 * * @package Elgg * @subpackage Core * - * @uses string $vars['internalname'] The name of the input fields - * (Forced to an array by appending []) - * @uses array $vars['options'] An array of strings representing the - * label => option for the each checkbox field - * @uses string $vars['internalid'] The id for each input field. Optional - * (Only use this with a single value.) - * @uses string $vars['default'] The default value to send if nothing is checked. - * Optional, defaults to 0. Set to FALSE for no default. - * @uses bool $vars['disabled'] Make all input elements disabled. Optional. - * @uses string $vars['value'] The current value. Optional. - * @uses string $vars['class'] The class of each input element. Optional. - * @uses string $vars['js'] Any Javascript to enter into the input tag. Optional. + * @uses string $vars['name'] The name of the input fields + * (Forced to an array by appending []) + * @uses array $vars['options'] An array of strings representing the + * label => option for the each checkbox field + * @uses string $vars['default'] The default value to send if nothing is checked. + * Optional, defaults to 0. Set to FALSE for no default. + * @uses bool $vars['disabled'] Make all input elements disabled. Optional. + * @uses string $vars['value'] The current value. Single value or array. Optional. + * @uses string $vars['class'] Additional class of the list. Optional. + * @uses string $vars['align'] 'horizontal' or 'vertical' Default: 'vertical' * */ -if (!isset($vars['value']) || $vars['value'] === FALSE) { - $vars['value'] = elgg_get_sticky_value($vars['internalname']); +$defaults = array( + 'align' => 'vertical', + 'value' => array(), + 'default' => 0, + 'disabled' => false, + 'options' => array(), + 'name' => '', +); + +$vars = array_merge($defaults, $vars); + +$class = "elgg-input-checkboxes elgg-{$vars['align']}"; +if (isset($vars['class'])) { + $class .= " {$vars['class']}"; + unset($vars['class']); } -$class = (isset($vars['class'])) ? $vars['class'] : 'input-checkboxes'; -$value = (isset($vars['value'])) ? $vars['value'] : NULL; -$value_array = (is_array($value)) ? array_map('strtolower', $value) : array(strtolower($value)); -$internalname = (isset($vars['internalname'])) ? $vars['internalname'] : ''; -$options = (isset($vars['options']) && is_array($vars['options'])) ? $vars['options'] : array(); -$default = (isset($vars['default'])) ? $vars['default'] : 0; +$id = ''; +if (isset($vars['id'])) { + $id = "id=\"{$vars['id']}\""; + unset($vars['id']); +} + +if (is_array($vars['value'])) { + $values = array_map('elgg_strtolower', $vars['value']); +} else { + $values = array(elgg_strtolower($vars['value'])); +} -$id = (isset($vars['internalid'])) ? $vars['internalid'] : ''; -$disabled = (isset($vars['disabled'])) ? $vars['disabled'] : FALSE; -$js = (isset($vars['js'])) ? $vars['js'] : ''; +$input_vars = $vars; +$input_vars['default'] = false; +if ($vars['name']) { + $input_vars['name'] = "{$vars['name']}[]"; +} +unset($input_vars['align']); +unset($input_vars['options']); -if ($options) { +if (count($vars['options']) > 0) { // include a default value so if nothing is checked 0 will be passed. - if ($internalname && $default !== FALSE) { - echo "<input type=\"hidden\" name=\"$internalname\" value=\"$default\">"; + if ($vars['name'] && $vars['default'] !== false) { + echo "<input type=\"hidden\" name=\"{$vars['name']}\" value=\"{$vars['default']}\" />"; } - foreach ($options as $label => $option) { - // @hack - This sorta checks if options is not an assoc array and then - // ignores the label (because it's just the index) and sets the value ($option) - // as the label. - // Wow. - // @todo deprecate in Elgg 1.8 + echo "<ul class=\"$class\" $id>"; + foreach ($vars['options'] as $label => $value) { + // @deprecated 1.8 Remove in 1.9 if (is_integer($label)) { - $label = $option; - } - - $input_vars = array( - 'checked' => in_array(strtolower($option), $value_array), - 'value' => $option, - 'disabled' => $disabled, - 'id' => $id, - 'js' => $js, - 'default' => false, - ); - - if ($class) { - $input_vars['class'] = $class; + elgg_deprecated_notice('$vars[\'options\'] must be an associative array in input/checkboxes', 1.8); + $label = $value; } - if ($internalname) { - $input_vars['name'] = "{$internalname}[]"; - } + $input_vars['checked'] = in_array(elgg_strtolower($value), $values); + $input_vars['value'] = $value; $input = elgg_view('input/checkbox', $input_vars); - echo "<label>{$input}{$label}</label><br />"; + echo "<li><label>$input$label</label></li>"; } -}
\ No newline at end of file + echo '</ul>'; +} |
