Elgg  Version 4.3
checkboxes.php
Go to the documentation of this file.
1 <?php
24  'align' => 'vertical',
25  'value' => [],
26  'default' => 0,
27  'disabled' => false,
28  'options' => [],
29  'name' => '',
30 ];
31 
32 $vars = array_merge($defaults, $vars);
33 
34 $options_values = elgg_extract('options_values', $vars, []);
35 unset($vars['options_values']);
36 
38 
39 foreach ($options_values as $key => $value) {
40  if (is_array($value)) {
41  $key = elgg_extract('text', $value, $key);
42  } else {
43  $value = [
44  'text' => $value,
45  'value' => $key,
46  ];
47  }
49 }
50 
51 // turn options into options_values
52 $options = elgg_extract('options', $vars);
53 unset($vars['options']);
54 if (!empty($options) && empty($input_options)) {
55  foreach ($options as $label => $value) {
56  $input_options[$label] = ['value' => $value];
57  }
58 }
59 
60 if (empty($input_options)) {
61  return;
62 }
63 
65  'elgg-input-checkboxes',
66  "elgg-{$vars['align']}",
67 ]);
68 unset($vars['class']);
69 
70 $id = elgg_extract('id', $vars, '');
71 unset($vars['id']);
72 
73 if (is_array(elgg_extract('value', $vars))) {
74  $values = array_map('elgg_strtolower', elgg_extract('value', $vars));
75 } else {
76  $values = [elgg_strtolower((string) elgg_extract('value', $vars))];
77 }
78 
79 // include a default value so if nothing is checked 0 will be passed.
80 if ($vars['name'] && $vars['default'] !== false) {
81  echo elgg_view('input/hidden', [
82  'name' => elgg_extract('name', $vars),
83  'value' => elgg_extract('default', $vars),
84  'disabled' => elgg_extract('disabled', $vars),
85  ]);
86 }
87 
88 // prepare checkbox vars
90 $input_vars['default'] = false;
91 if (elgg_extract('name', $vars)) {
92  $input_vars['name'] = "{$vars['name']}[]";
93 }
94 unset($input_vars['align']);
95 
97 foreach ($input_options as $label => $option) {
98  $checkbox_input_options = array_merge($input_vars, $option);
99  $checkbox_input_options['checked'] = in_array(elgg_strtolower((string) elgg_extract('value', $option)), $values);
100  $checkbox_input_options['label'] = elgg_extract('text', $checkbox_input_options, $label);
101  unset($checkbox_input_options['text']);
102 
103  $input = elgg_view('input/checkbox', $checkbox_input_options);
104 
105  $checkboxes .= "<li>$input</li>";
106 }
107 
108 echo elgg_format_element('ul', ['class' => $list_class, 'id' => $id], $checkboxes);
if($view &&elgg_view_exists($view)) $label
Definition: field.php:26
$checkboxes
Definition: checkboxes.php:96
if(!empty($options)&&empty($input_options)) if(empty($input_options)) $list_class
Definition: checkboxes.php:64
$options_values
Definition: checkboxes.php:34
if($vars['name']&&$vars['default']!==false) $input_vars
Definition: checkboxes.php:89
elgg_strtolower()
Wrapper function for mb_strtolower().
Definition: mb_wrapper.php:154
elgg_extract_class(array $array, $existing=[], $extract_key= 'class')
Extract class names from an array, optionally merging into a preexisting set.
Definition: elgglib.php:569
$value
Definition: generic.php:51
$input_options
Definition: checkboxes.php:37
foreach($options_values as $key=> $value) $options
Definition: checkboxes.php:52
elgg_format_element($tag_name, array $attributes=[], $text= '', array $options=[])
Format an HTML element.
Definition: output.php:135
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:547
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
$defaults
Displays a checkbox input field.
Definition: checkboxes.php:23
$id
Definition: checkboxes.php:70
$input
Form field view.
Definition: field.php:13
elgg echo
Translates a string.
Definition: deprecated.js:530
$vars
Definition: checkboxes.php:32
elgg_view($view, $vars=[], $viewtype= '')
Return a parsed view.
Definition: views.php:179