radio.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Elgg radio input
  4. * Displays a radio input field
  5. *
  6. * @warning Passing integers as labels does not currently work due to a
  7. * deprecated hack that will be removed in Elgg 1.9. To use integer labels,
  8. * the labels must be character codes: 1 would be &#0049;
  9. *
  10. * @package Elgg
  11. * @subpackage Core
  12. *
  13. * @uses $vars['value'] The current value, if any
  14. * @uses $vars['name'] The name of the input field
  15. * @uses $vars['options'] An array of strings representing the options for the
  16. * radio field as "label" => option
  17. * @uses $vars['class'] Additional class of the list. Optional.
  18. * @uses $vars['align'] 'horizontal' or 'vertical' Default: 'vertical'
  19. */
  20. $defaults = array(
  21. 'align' => 'vertical',
  22. 'value' => array(),
  23. 'disabled' => false,
  24. 'options' => array(),
  25. 'name' => '',
  26. 'type' => 'radio'
  27. );
  28. $vars = array_merge($defaults, $vars);
  29. $options = elgg_extract('options', $vars);
  30. unset($vars['options']);
  31. if (empty($options)) {
  32. return;
  33. }
  34. $id = elgg_extract('id', $vars, '');
  35. unset($vars['id']);
  36. $list_class = (array) elgg_extract('class', $vars, []);
  37. $list_class[] = 'elgg-input-radios';
  38. $list_class[] = "elgg-{$vars['align']}";
  39. unset($vars['class']);
  40. unset($vars['align']);
  41. $vars['class'] = 'elgg-input-radio';
  42. if (is_array($vars['value'])) {
  43. $vars['value'] = array_map('elgg_strtolower', $vars['value']);
  44. } else {
  45. $vars['value'] = array(elgg_strtolower($vars['value']));
  46. }
  47. $value = $vars['value'];
  48. unset($vars['value']);
  49. $radios = '';
  50. foreach ($options as $label => $option) {
  51. $vars['checked'] = in_array(elgg_strtolower($option), $value);
  52. $vars['value'] = $option;
  53. // handle indexed array where label is not specified
  54. // @deprecated 1.8 Remove in 1.9
  55. if (is_integer($label)) {
  56. elgg_deprecated_notice('$vars[\'options\'] must be an associative array in input/radio', 1.8);
  57. $label = $option;
  58. }
  59. $radio = elgg_format_element('input', $vars);
  60. $radios .= "<li><label>{$radio}{$label}</label></li>";
  61. }
  62. echo elgg_format_element('ul', ['class' => $list_class, 'id' => $id], $radios);