1 <?php
 2 
 3 /**
 4  * @package redaxo\core\form
 5  */
 6 class rex_form_radio_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         // Jedes radio bekommt eingenes Label
14     }
15 
16     protected function formatLabel()
17     {
18         // Da Jedes Feld schon ein Label hat, hier nur eine "Ueberschrift" anbringen
19         return '<label class="control-label">' . $this->getLabel() . '</label>';
20     }
21 
22     public function formatElement()
23     {
24         $s = '';
25         $value = $this->getValue();
26         $options = $this->getOptions();
27         $id = $this->getAttribute('id');
28 
29         $attr = '';
30         foreach ($this->getAttributes() as $attributeName => $attributeValue) {
31             if ($attributeName == 'id') {
32                 continue;
33             }
34             $attr .= ' ' . rex_escape($attributeName, 'html_attr') . '="' . rex_escape($attributeValue) . '"';
35         }
36 
37         $formElements = [];
38 
39         foreach ($options as $opt_name => $opt_value) {
40             $checked = $opt_value == $value ? ' checked="checked"' : '';
41             $opt_id = $id . '-' . rex_string::normalize($opt_value, '-');
42             $opt_attr = $attr . ' id="' . $opt_id . '"';
43 
44             $n = [];
45             $n['label'] = '<label class="control-label" for="' . $opt_id . '">' . rex_escape($opt_name) . '</label>';
46             $n['field'] = '<input type="radio" value="' . rex_escape($opt_value) . '"' . $opt_attr . $checked . ' />';
47             $formElements[] = $n;
48         }
49 
50         $fragment = new rex_fragment();
51         $fragment->setVar('elements', $formElements, false);
52         $s = $fragment->parse('core/form/radio.php');
53 
54         return $s;
55     }
56 }
57