Elgg  Version 3.0
checkboxes.php
Go to the documentation of this file.
1 <?php
33  'align' => 'vertical',
34  'value' => [],
35  'default' => 0,
36  'disabled' => false,
37  'options' => [],
38  'name' => '',
39 ];
40 
41 $vars = array_merge($defaults, $vars);
42 
43 $options_values = elgg_extract('options_values', $vars, []);
44 unset($vars['options_values']);
45 
47 
48 foreach ($options_values as $key => $value) {
49  if (is_array($value)) {
50  $key = elgg_extract('text', $value, $key);
51  } else {
52  $value = [
53  'text' => $value,
54  'value' => $key,
55  ];
56  }
58 }
59 
60 // turn options into options_values
61 $options = elgg_extract('options', $vars);
62 unset($vars['options']);
63 if (!empty($options) && empty($input_options)) {
64  foreach ($options as $label => $value) {
65  $input_options[$label] = ['value' => $value];
66  }
67 }
68 
69 if (empty($input_options)) {
70  return;
71 }
72 
74  'elgg-input-checkboxes',
75  "elgg-{$vars['align']}",
76 ]);
77 unset($vars['class']);
78 
79 $id = elgg_extract('id', $vars, '');
80 unset($vars['id']);
81 
82 if (is_array(elgg_extract('value', $vars))) {
83  $values = array_map('elgg_strtolower', elgg_extract('value', $vars));
84 } else {
85  $values = [elgg_strtolower(elgg_extract('value', $vars))];
86 }
87 
88 // include a default value so if nothing is checked 0 will be passed.
89 if ($vars['name'] && $vars['default'] !== false) {
90  echo elgg_view('input/hidden', [
91  'name' => elgg_extract('name', $vars),
92  'value' => elgg_extract('default', $vars),
93  'disabled' => elgg_extract('disabled', $vars),
94  ]);
95 }
96 
97 // prepare checkbox vars
99 $input_vars['default'] = false;
100 if (elgg_extract('name', $vars)) {
101  $input_vars['name'] = "{$vars['name']}[]";
102 }
103 unset($input_vars['align']);
104 
106 foreach ($input_options as $label => $option) {
107  $checkbox_input_options = array_merge($input_vars, $option);
108  $checkbox_input_options['checked'] = in_array(elgg_strtolower(elgg_extract('value', $option)), $values);
109  $checkbox_input_options['label'] = elgg_extract('text', $checkbox_input_options, $label);
110  unset($checkbox_input_options['text']);
111 
112  $input = elgg_view('input/checkbox', $checkbox_input_options);
113 
114  $checkboxes .= "<li>$input</li>";
115 }
116 
117 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:105
if(!empty($options)&&empty($input_options)) if(empty($input_options)) $list_class
Definition: checkboxes.php:73
$options_values
Definition: checkboxes.php:43
if($vars['name']&&$vars['default']!==false) $input_vars
Definition: checkboxes.php:98
elgg_strtolower()
Wrapper function for mb_strtolower().
Definition: mb_wrapper.php:174
elgg_extract_class(array $array, $existing=[], $extract_key= 'class')
Extract class names from an array, optionally merging into a preexisting set.
Definition: elgglib.php:1153
$input_options
Definition: checkboxes.php:46
foreach($options_values as $key=> $value) $options
Definition: checkboxes.php:61
elgg_format_element($tag_name, array $attributes=[], $text= '', array $options=[])
Format an HTML element.
Definition: output.php:168
elgg echo
Translates a string.
Definition: languages.js:43
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:1131
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:55
$value
Definition: debugging.php:7
$defaults
Definition: checkboxes.php:32
$id
Definition: checkboxes.php:79
$input
Form field view.
Definition: field.php:13
$vars
Definition: checkboxes.php:41
elgg_view($view, $vars=[], $viewtype= '')
Return a parsed view.
Definition: views.php:246