Elgg  Version master
checkboxes.php
Go to the documentation of this file.
1 <?php
24  'align' => 'vertical',
25  'value' => [],
26  'default' => 0,
27  'options' => [],
28  'name' => '',
29 ];
30 
31 $vars = array_merge($defaults, $vars);
32 
33 $options_values = elgg_extract('options_values', $vars, []);
34 unset($vars['options_values']);
35 
37 
38 foreach ($options_values as $key => $value) {
39  if (is_array($value)) {
40  $key = elgg_extract('text', $value, $key);
41  } else {
42  $value = [
43  'text' => $value,
44  'value' => $key,
45  ];
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 
95 unset($input_vars['align']);
96 
98 foreach ($input_options as $label => $option) {
99  $checkbox_input_options = array_merge($input_vars, $option);
100  $checkbox_input_options['checked'] = in_array(elgg_strtolower((string) elgg_extract('value', $option)), $values);
101  $checkbox_input_options['label'] = elgg_extract('text', $checkbox_input_options, $label);
102  unset($checkbox_input_options['text']);
103 
104  $input = elgg_view('input/checkbox', $checkbox_input_options);
105 
106  $checkboxes .= elgg_format_element('li', [], $input);
107 }
108 
109 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:97
if(!empty($options)&&empty($input_options)) if(empty($input_options)) $list_class
Definition: checkboxes.php:64
$options_values
Definition: checkboxes.php:33
if($vars['name']&&$vars['default']!==false) $input_vars
Definition: checkboxes.php:89
elgg_strtolower()
Wrapper function for mb_strtolower().
Definition: mb_wrapper.php:125
$value
Definition: generic.php:51
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:254
$input_options
Definition: checkboxes.php:36
elgg_view(string $view, array $vars=[], string $viewtype= '')
Return a parsed view.
Definition: views.php:156
foreach($options_values as $key=> $value) $options
Definition: checkboxes.php:52
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_format_element(string $tag_name, array $attributes=[], string $text= '', array $options=[])
Format an HTML element.
Definition: output.php:145
elgg_extract_class(array $array, array|string $existing=[], string $extract_key= 'class')
Extract class names from an array, optionally merging into a preexisting set.
Definition: elgglib.php:277
$vars
Definition: checkboxes.php:31