checkboxes.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * Elgg checkbox input
  4. * Displays a checkbox input field
  5. *
  6. * @note This also includes a hidden input with the same name as the checkboxes
  7. * to make sure something is sent to the server. The default value is 0.
  8. * If using JS, be specific to avoid selecting the hidden default value:
  9. * $('input[type=checkbox][name=name]')
  10. *
  11. * @warning Passing integers as labels does not currently work due to a
  12. * deprecated hack that will be removed in Elgg 1.9. To use integer labels,
  13. * the labels must be character codes: 1 would be &#0049;
  14. *
  15. * @package Elgg
  16. * @subpackage Core
  17. *
  18. * @uses string $vars['name'] The name of the input fields
  19. * (Forced to an array by appending [])
  20. * @uses array $vars['options'] An array of strings representing the
  21. * label => option for the each checkbox field
  22. * @uses string $vars['default'] The default value to send if nothing is checked.
  23. * Optional, defaults to 0. Set to FALSE for no default.
  24. * @uses bool $vars['disabled'] Make all input elements disabled. Optional.
  25. * @uses string $vars['value'] The current value. Single value or array. Optional.
  26. * @uses string $vars['class'] Additional class of the list. Optional.
  27. * @uses string $vars['align'] 'horizontal' or 'vertical' Default: 'vertical'
  28. *
  29. */
  30. $defaults = array(
  31. 'align' => 'vertical',
  32. 'value' => array(),
  33. 'default' => 0,
  34. 'disabled' => false,
  35. 'options' => array(),
  36. 'name' => '',
  37. );
  38. $vars = array_merge($defaults, $vars);
  39. if (empty($vars['options'])) {
  40. return;
  41. }
  42. $list_class = (array) elgg_extract('class', $vars, []);
  43. unset($vars['class']);
  44. $list_class[] = 'elgg-input-checkboxes';
  45. $list_class[] = "elgg-{$vars['align']}";
  46. $id = elgg_extract('id', $vars, '');
  47. unset($vars['id']);
  48. if (is_array($vars['value'])) {
  49. $values = array_map('elgg_strtolower', $vars['value']);
  50. } else {
  51. $values = array(elgg_strtolower($vars['value']));
  52. }
  53. $input_vars = $vars;
  54. $input_vars['default'] = false;
  55. if ($vars['name']) {
  56. $input_vars['name'] = "{$vars['name']}[]";
  57. }
  58. unset($input_vars['align']);
  59. unset($input_vars['options']);
  60. // include a default value so if nothing is checked 0 will be passed.
  61. if ($vars['name'] && $vars['default'] !== false) {
  62. echo elgg_view('input/hidden', ['name' => $vars['name'], 'value' => $default]);
  63. }
  64. $checkboxes = '';
  65. foreach ($vars['options'] as $label => $value) {
  66. // @deprecated 1.8 Remove in 1.9
  67. if (is_integer($label)) {
  68. elgg_deprecated_notice('$vars[\'options\'] must be an associative array in input/checkboxes', 1.8);
  69. $label = $value;
  70. }
  71. $input_vars['checked'] = in_array(elgg_strtolower($value), $values);
  72. $input_vars['value'] = $value;
  73. $input_vars['label'] = $label;
  74. $input = elgg_view('input/checkbox', $input_vars);
  75. $checkboxes .= "<li>$input</li>";
  76. }
  77. echo elgg_format_element('ul', ['class' => $list_class, 'id' => $id], $checkboxes);