|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Contao Open Source CMS |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) Jan Karai |
|
6
|
|
|
* |
|
7
|
|
|
* @license LGPL-3.0+ |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace Mailwurm\Belegung; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class FormBelegungsplanCategorySelect |
|
13
|
|
|
* |
|
14
|
|
|
* @property integer $mSize |
|
15
|
|
|
* @property boolean $mandatory |
|
16
|
|
|
* @property boolean $multiple |
|
17
|
|
|
* @property array $options |
|
18
|
|
|
* @property boolean $chosen |
|
19
|
|
|
* |
|
20
|
|
|
* @author Jan Karai <https://www.sachsen-it.de> |
|
21
|
|
|
*/ |
|
22
|
|
|
class FormBelegungsplanCategorySelect extends \Widget |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Template |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $strTemplate = 'form_belegungsplancategoryselect'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The CSS class prefix |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $strPrefix = 'widget widget-select'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Add specific attributes |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $strKey The attribute name |
|
42
|
|
|
* @param mixed $varValue The attribute value |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __set($strKey, $varValue) |
|
45
|
|
|
{ |
|
46
|
|
|
switch ($strKey) |
|
47
|
|
|
{ |
|
48
|
|
|
case 'mandatory': |
|
49
|
|
|
if ($varValue) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->arrAttributes['required'] = 'required'; |
|
52
|
|
|
} |
|
53
|
|
|
else |
|
54
|
|
|
{ |
|
55
|
|
|
unset($this->arrAttributes['required']); |
|
56
|
|
|
} |
|
57
|
|
|
parent::__set($strKey, $varValue); |
|
58
|
|
|
break; |
|
59
|
|
|
case 'multiple': |
|
60
|
|
|
if ($varValue != '') |
|
61
|
|
|
{ |
|
62
|
|
|
$this->arrAttributes['multiple'] = 'multiple'; |
|
63
|
|
|
} |
|
64
|
|
|
break; |
|
65
|
|
|
case 'options': |
|
66
|
|
|
$this->arrOptions = \StringUtil::deserialize($varValue); |
|
|
|
|
|
|
67
|
|
|
break; |
|
68
|
|
|
case 'rgxp': |
|
69
|
|
|
case 'minlength': |
|
70
|
|
|
case 'maxlength': |
|
71
|
|
|
// Ignore |
|
72
|
|
|
break; |
|
73
|
|
|
default: |
|
74
|
|
|
parent::__set($strKey, $varValue); |
|
75
|
|
|
break; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Check options if the field is mandatory |
|
81
|
|
|
*/ |
|
82
|
|
|
public function validate() |
|
83
|
|
|
{ |
|
84
|
|
|
$mandatory = $this->mandatory; |
|
85
|
|
|
$options = $this->getPost($this->strName); |
|
86
|
|
|
// Check if there is at least one value |
|
87
|
|
|
if ($mandatory && is_array($options)) |
|
88
|
|
|
{ |
|
89
|
|
|
foreach ($options as $option) |
|
90
|
|
|
{ |
|
91
|
|
|
if (strlen($option)) |
|
92
|
|
|
{ |
|
93
|
|
|
$this->mandatory = false; |
|
94
|
|
|
break; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
$varInput = $this->validator($options); |
|
99
|
|
|
// Check for a valid option (see #4383) |
|
100
|
|
|
if (!empty($varInput) && !$this->isValidOption($varInput)) |
|
101
|
|
|
{ |
|
102
|
|
|
$this->addError($GLOBALS['TL_LANG']['ERR']['invalid']); |
|
103
|
|
|
} |
|
104
|
|
|
// Add class "error" |
|
105
|
|
|
if ($this->hasErrors()) |
|
106
|
|
|
{ |
|
107
|
|
|
$this->class = 'error'; |
|
108
|
|
|
} |
|
109
|
|
|
else |
|
110
|
|
|
{ |
|
111
|
|
|
$this->varValue = $varInput; |
|
112
|
|
|
} |
|
113
|
|
|
// Reset the property |
|
114
|
|
|
if ($mandatory) |
|
115
|
|
|
{ |
|
116
|
|
|
$this->mandatory = true; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Return a parameter |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $strKey The parameter name |
|
124
|
|
|
* |
|
125
|
|
|
* @return mixed The parameter value |
|
126
|
|
|
*/ |
|
127
|
|
|
public function __get($strKey) |
|
128
|
|
|
{ |
|
129
|
|
|
if ($strKey == 'options') |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->arrOptions; |
|
132
|
|
|
} |
|
133
|
|
|
return parent::__get($strKey); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Parse the template file and return it as string |
|
138
|
|
|
* |
|
139
|
|
|
* @param array $arrAttributes An optional attributes array |
|
140
|
|
|
* |
|
141
|
|
|
* @return string The template markup |
|
142
|
|
|
*/ |
|
143
|
|
|
public function parse($arrAttributes=null) |
|
144
|
|
|
{ |
|
145
|
|
|
$strClass = 'select'; |
|
146
|
|
View Code Duplication |
if ($this->multiple) |
|
|
|
|
|
|
147
|
|
|
{ |
|
148
|
|
|
$this->strName .= '[]'; |
|
149
|
|
|
$strClass = 'multiselect'; |
|
150
|
|
|
} |
|
151
|
|
|
// Make sure there are no multiple options in single mode |
|
152
|
|
|
elseif (is_array($this->varValue)) |
|
153
|
|
|
{ |
|
154
|
|
|
$this->varValue = $this->varValue[0]; |
|
155
|
|
|
} |
|
156
|
|
|
// Chosen |
|
157
|
|
|
if ($this->chosen) |
|
158
|
|
|
{ |
|
159
|
|
|
$strClass .= ' tl_chosen'; |
|
160
|
|
|
} |
|
161
|
|
|
// Custom class |
|
162
|
|
|
if ($this->strClass != '') |
|
163
|
|
|
{ |
|
164
|
|
|
$strClass .= ' ' . $this->strClass; |
|
165
|
|
|
} |
|
166
|
|
|
$this->strClass = $strClass; |
|
167
|
|
|
return parent::parse($arrAttributes); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Generate the options |
|
172
|
|
|
* |
|
173
|
|
|
* @return array The options array |
|
174
|
|
|
*/ |
|
175
|
|
|
protected function getOptions() |
|
176
|
|
|
{ |
|
177
|
|
|
$arrOptions = array(); |
|
178
|
|
|
// Add empty option if there are none |
|
179
|
|
View Code Duplication |
if (empty($this->arrOptions) || !is_array($this->arrOptions)) |
|
|
|
|
|
|
180
|
|
|
{ |
|
181
|
|
|
$this->arrOptions = array(array('value' => '', 'label' => '-')); |
|
182
|
|
|
} |
|
183
|
|
|
// Generate options |
|
184
|
|
|
foreach ($this->arrOptions as $arrOption) |
|
185
|
|
|
{ |
|
186
|
|
|
$arrOptions[] = array_replace |
|
187
|
|
|
( |
|
188
|
|
|
$arrOption, |
|
189
|
|
|
array |
|
190
|
|
|
( |
|
191
|
|
|
'type' => 'option', |
|
192
|
|
|
'value' => $arrOption['value'], |
|
193
|
|
|
'selected' => $this->isSelected($arrOption), |
|
194
|
|
|
'label' => $arrOption['label'], |
|
195
|
|
|
) |
|
196
|
|
|
); |
|
197
|
|
|
} |
|
198
|
|
|
return $arrOptions; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Generate the widget and return it as string |
|
203
|
|
|
* |
|
204
|
|
|
* @return string The widget markup |
|
205
|
|
|
*/ |
|
206
|
|
|
public function generate() |
|
207
|
|
|
{ |
|
208
|
|
|
$strOptions = ''; |
|
209
|
|
|
|
|
210
|
|
View Code Duplication |
if ($this->multiple) |
|
|
|
|
|
|
211
|
|
|
{ |
|
212
|
|
|
$this->strName .= '[]'; |
|
213
|
|
|
} |
|
214
|
|
|
// Make sure there are no multiple options in single mode |
|
215
|
|
|
elseif (is_array($this->varValue)) |
|
216
|
|
|
{ |
|
217
|
|
|
$this->varValue = $this->varValue[0]; |
|
218
|
|
|
} |
|
219
|
|
|
// Add empty option if there are none |
|
220
|
|
View Code Duplication |
if (empty($this->arrOptions) || !is_array($this->arrOptions)) |
|
|
|
|
|
|
221
|
|
|
{ |
|
222
|
|
|
$this->arrOptions = array(array('value'=>'', 'label'=>'-')); |
|
223
|
|
|
} |
|
224
|
|
|
foreach ($this->arrOptions as $arrOption) |
|
225
|
|
|
{ |
|
226
|
|
|
$strOptions .= sprintf('<option value="%s"%s>%s</option>', |
|
227
|
|
|
$arrOption['value'], |
|
228
|
|
|
$this->isSelected($arrOption), |
|
229
|
|
|
$arrOption['label']); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
return sprintf('<select name="%s" id="ctrl_%s" class="%s"%s>%s</select>', |
|
233
|
|
|
$this->strName, |
|
234
|
|
|
$this->strId, |
|
235
|
|
|
$this->class, |
|
236
|
|
|
$this->getAttributes(), |
|
237
|
|
|
$strOptions); |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.