1 <?php
 2 
 3 /**
 4  * @package redaxo\core\form
 5  */
 6 class rex_form_checkbox_element extends rex_form_options_element
 7 {
 8     // 1. Parameter nicht genutzt, muss aber hier stehen,
 9     // wg einheitlicher Konstrukturparameter
10     public function __construct($tag = '', rex_form_base $table = null, array $attributes = [])
11     {
12         parent::__construct('', $table, $attributes);
13         // Jede checkbox bekommt eingenes Label
14         $this->setLabel('');
15     }
16 
17     protected function formatLabel()
18     {
19         // Da Jedes Feld schon ein Label hat, hier nur eine "Ueberschrift" anbringen
20         $label = $this->getLabel();
21 
22         if ($label != '') {
23             $label = '<label class="control-label">' . $label . '</label>';
24         }
25 
26         return $label;
27     }
28 
29     public function formatElement()
30     {
31         $s = '';
32         $values = explode('|', trim($this->getValue(), '|'));
33         $options = $this->getOptions();
34         $name = $this->getAttribute('name');
35         $id = $this->getAttribute('id');
36 
37         $attr = '';
38         foreach ($this->getAttributes() as $attributeName => $attributeValue) {
39             if ($attributeName == 'name' || $attributeName == 'id') {
40                 continue;
41             }
42             $attr .= ' ' . rex_escape($attributeName, 'html_attr') . '="' . rex_escape($attributeValue) . '"';
43         }
44 
45         $formElements = [];
46 
47         foreach ($options as $opt_name => $opt_value) {
48             $opt_id = $id;
49             if ($opt_value != '') {
50                 $opt_id .= '-' . rex_string::normalize($opt_value, '-');
51             }
52             $opt_attr = $attr . ' id="' . rex_escape($opt_id) . '"';
53             $checked = in_array($opt_value, $values) ? ' checked="checked"' : '';
54 
55             $n = [];
56             $n['label'] = '<label class="control-label" for="' . rex_escape($opt_id) . '">' . rex_escape($opt_name) . '</label>';
57             $n['field'] = '<input type="checkbox" name="' . rex_escape($name) . '[' . rex_escape($opt_value) . ']" value="' . rex_escape($opt_value) . '"' . $opt_attr . $checked . ' />';
58             $formElements[] = $n;
59         }
60 
61         $fragment = new rex_fragment();
62         $fragment->setVar('elements', $formElements, false);
63         $fragment->setVar('grouped', true);
64         $s = $fragment->parse('core/form/checkbox.php');
65 
66         return $s;
67     }
68 }
69