-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathradio.php
171 lines (144 loc) · 3.95 KB
/
radio.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* yform.
*
* @author jan.kristinus[at]redaxo[dot]org Jan Kristinus
* @author <a href="http://www.yakamara.de">www.yakamara.de</a>
*/
class rex_radio
{
public $attributes;
public $options;
public $option_selected;
public function __construct()
{
$this->init();
}
public function init()
{
$this->attributes = [];
$this->resetSelected();
$this->setName('standard');
$this->setDisabled(false);
}
public function setAttribute($name, $value)
{
$this->attributes[$name] = $value;
}
public function delAttribute($name)
{
if ($this->hasAttribute($name)) {
unset($this->attributes[$name]);
return true;
}
return false;
}
public function hasAttribute($name)
{
return isset($this->attributes[$name]);
}
public function getAttribute($name, $default = '')
{
if ($this->hasAttribute($name)) {
return $this->attributes[$name];
}
return $default;
}
public function setDisabled($disabled = true)
{
if ($disabled) {
$this->setAttribute('disabled', 'disabled');
} else {
$this->delAttribute('disabled');
}
}
public function setName($name)
{
$this->setAttribute('name', $name);
}
public function setId($id)
{
$this->setAttribute('id', $id);
}
/**
* select style
* Es ist moeglich sowohl eine Styleklasse als auch einen Style zu uebergeben.
*
* Aufrufbeispiel:
* $sel_media->setStyle('class="inp100"');
* und/oder
* $sel_media->setStyle("width:150px;");
*/
public function setStyle($style)
{
if (str_contains($style, 'class=')) {
if (preg_match('/class=["\']?([^"\']*)["\']?/i', $style, $matches)) {
$this->setAttribute('class', $matches[1]);
}
} else {
$this->setAttribute('style', $style);
}
}
public function setSize($size)
{
$this->setAttribute('size', $size);
}
public function setSelected($selected)
{
$this->option_selected = rex_escape($selected);
}
public function resetSelected()
{
$this->option_selected = '';
}
public function addOption($name, $value, $attributes = [])
{
$this->options[] = ['name' => $name, 'value' => $value, 'attributes' => $attributes];
}
public function get()
{
$attr = '';
foreach ($this->attributes as $name => $value) {
$attr .= ' ' . $name . '="' . $value . '"';
}
$ausgabe = "\n";
$ausgabe .= '<div class="radios">';
if (is_array($this->options)) {
$ausgabe .= $this->_outOptions();
}
$ausgabe .= '</div>' . "\n";
return $ausgabe;
}
public function show()
{
echo $this->get();
}
private function _outOptions()
{
$return = '';
$selected = '';
foreach ($this->options as $option) {
if ('' == $selected) {
$selected = $option['value'];
}
if ($this->option_selected == $option['value']) {
$selected = $option['value'];
}
}
$id = $this->getAttribute('id');
$counter = 0;
foreach ($this->options as $option) {
++$counter;
$oid = $id . '-' . $counter;
$return .= '<p class="radio">';
$return .= '<input type="radio" class="radio" id="' . $oid . '" name="' . $this->getAttribute('name') . '" value="' . $option['value'] . '" ';
if ($selected == $option['value']) {
$return .= ' checked="checked" ';
}
$return .= '/>';
$return .= '<label for="' . $oid . '">' . $option['name'] . '</label>' . "\n";
$return .= '</p>';
}
return $return;
}
}