1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license GNU GPL v2+ |
5
|
|
|
* @since 1.3 |
6
|
|
|
* |
7
|
|
|
* @author Jason Zhang |
8
|
|
|
* @author Toni Hermoso Pulido |
9
|
|
|
* @author Alexander Gesinn |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace SFS; |
13
|
|
|
|
14
|
|
|
use SMWQueryProcessor as QueryProcessor; |
15
|
|
|
use Parser; |
16
|
|
|
use PFFormInput; |
17
|
|
|
use MWDebug; |
18
|
|
|
|
19
|
|
|
class SemanticFormsSelectInput extends PFFormInput { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Internal data container |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private static $data = []; |
|
|
|
|
27
|
|
|
|
28
|
|
|
private $mSelectField; |
29
|
|
|
|
30
|
|
|
public function __construct( $inputNumber, $curValue, $inputName, $disabled, $otherArgs ) { |
|
|
|
|
31
|
|
|
parent::__construct( $inputNumber, $curValue, $inputName, $disabled, $otherArgs ); |
32
|
|
|
|
33
|
|
|
// SelectField is a simple value object - we accept creating it in the constructor |
34
|
|
|
$this->mSelectField = new SelectField( $GLOBALS['wgParser'] ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public static function getName() { |
38
|
|
|
return 'SF_Select'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public static function getParameters() { |
42
|
|
|
$params = parent::getParameters(); |
43
|
|
|
return $params; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getResourceModuleNames() { |
47
|
|
|
/** |
48
|
|
|
* Loading modules this way currently fails with: |
49
|
|
|
* "mw.loader.state({"ext.sf_select.scriptselect":"loading"});" |
50
|
|
|
*/ |
51
|
|
|
|
52
|
|
|
return [ |
53
|
|
|
'ext.sf_select.scriptselect' |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns the HTML code to be included in the output page for this input. |
59
|
|
|
* This is currently just a wrapper for getHTML(). |
60
|
|
|
*/ |
61
|
|
|
public function getHtmlText() { |
62
|
|
|
return self::getHTML( $this->mCurrentValue, $this->mInputName, $this->mIsMandatory, $this->mIsDisabled, |
|
|
|
|
63
|
|
|
$this->mOtherArgs ); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns the HTML code to be included in the output page for this input. |
68
|
|
|
* @deprecated use getHtmlText() instead |
69
|
|
|
* |
70
|
|
|
* @param string $cur_value A single value or a list of values with separator |
71
|
|
|
* @param string $input_name Name of the input including the template, e.g. Building[Part Of Site] |
72
|
|
|
* @param $is_mandatory |
73
|
|
|
* @param $is_disabled |
74
|
|
|
* @param string[] $other_args Array of other field parameters |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function getHTML( $cur_value = "", $input_name = "", $is_mandatory, $is_disabled, Array $other_args ) { |
|
|
|
|
78
|
|
|
global $sfgFieldNum, $wgUser; |
|
|
|
|
79
|
|
|
|
80
|
|
|
// shortcut to the SelectField object |
81
|
|
|
$selectField = $this->mSelectField; |
82
|
|
|
|
83
|
|
|
// get 'delimiter' before 'query' or 'function' |
84
|
|
|
$selectField->setDelimiter( $other_args ); |
85
|
|
|
|
86
|
|
|
if ( array_key_exists( "query", $other_args ) ) { |
87
|
|
|
$selectField->setQuery( $other_args ); |
88
|
|
|
} elseif ( array_key_exists( "function", $other_args ) ) { |
89
|
|
|
$selectField->setFunction( $other_args ); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if ( array_key_exists( "label", $other_args ) ) { |
93
|
|
|
$selectField->setLabel( $other_args ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// parameters are only required if values needs to be retrieved dynamically |
97
|
|
|
if ( !$selectField->hasStaticValues() ) { |
98
|
|
|
$selectField->setSelectIsMultiple( $other_args ); |
99
|
|
|
$selectField->setSelectTemplate( $input_name ); |
100
|
|
|
$selectField->setSelectField( $input_name ); |
101
|
|
|
$selectField->setValueTemplate( $other_args ); |
102
|
|
|
$selectField->setValueField( $other_args ); |
103
|
|
|
$selectField->setSelectRemove( $other_args ); |
104
|
|
|
|
105
|
|
|
$item = Output::addToHeadItem( $selectField->getData() ); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
Output::commitToParserOutput(); |
109
|
|
|
|
110
|
|
|
// prepare the html input tag |
111
|
|
|
|
112
|
|
|
$extraatt = ""; |
113
|
|
|
$is_list = false; |
114
|
|
|
|
115
|
|
|
if ( array_key_exists( 'is_list', $other_args ) && $other_args['is_list'] == true ) { |
|
|
|
|
116
|
|
|
$is_list = true; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if ( $is_list ) { |
120
|
|
|
$extraatt = ' multiple="multiple" '; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if ( array_key_exists( "size", $other_args ) ) { |
124
|
|
|
$extraatt .= " size=\"{$other_args['size']}\""; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$classes = []; |
128
|
|
|
if ( $is_mandatory ) { |
129
|
|
|
$classes[] = "mandatoryField"; |
130
|
|
|
} |
131
|
|
|
if ( array_key_exists( "class", $other_args ) ) { |
132
|
|
|
$classes[] = $other_args['class']; |
133
|
|
|
} |
134
|
|
|
if ( $classes ) { |
|
|
|
|
135
|
|
|
$cstr = implode( " ", $classes ); |
136
|
|
|
$extraatt .= " class=\"$cstr\""; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$inname = $input_name; |
140
|
|
|
if ( $is_list ) { |
141
|
|
|
$inname .= '[]'; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// TODO Use Html:: |
145
|
|
|
|
146
|
|
|
$spanextra = $is_mandatory ? 'mandatoryFieldSpan' : ''; |
147
|
|
|
$is_single_select = (!$is_list) ? 'select-sfs-single' : '' ; |
148
|
|
|
$ret = "<span class=\"inputSpan select-sfs $is_single_select $spanextra\"><select name='$inname' id='input_$sfgFieldNum' $extraatt>"; |
149
|
|
|
|
150
|
|
|
$curvalues = null; |
|
|
|
|
151
|
|
|
if ( $cur_value ) { |
152
|
|
|
if ( $cur_value === 'current user' ) { |
153
|
|
|
$cur_value = $wgUser->getName(); |
154
|
|
|
} |
155
|
|
|
if ( is_array( $cur_value ) ) { |
156
|
|
|
$curvalues = $cur_value; |
157
|
|
|
} else { |
158
|
|
|
// delimiter for $cur_value is always ',' - PF seems to ignore $wgPageFormsListSeparator |
159
|
|
|
$curvalues = array_map( "trim", explode( $selectField->getDelimiter(), $cur_value ) ); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
} else { |
163
|
|
|
$curvalues = []; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
$labelArray = []; |
168
|
|
|
if ( array_key_exists( "label", $other_args ) && $curvalues ) { |
|
|
|
|
169
|
|
|
// $labelArray = $this->getLabels( $curvalues ); |
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
// TODO handle empty value case. |
173
|
|
|
$ret .= "<option></option>"; |
174
|
|
|
|
175
|
|
|
if ( $selectField->hasStaticValues() ) { |
176
|
|
|
|
177
|
|
|
$values = $selectField->getValues(); |
178
|
|
|
|
179
|
|
|
if ( array_key_exists( "label", $other_args ) && $values ) { |
180
|
|
|
$labelArray = $this->getLabels( $values ); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
if ( is_array( $values ) ) { |
184
|
|
|
|
185
|
|
|
foreach ( $values as $val ) { |
186
|
|
|
|
187
|
|
|
$selected = ""; |
188
|
|
|
|
189
|
|
|
if ( array_key_exists( $val, $labelArray ) ) { |
190
|
|
|
|
191
|
|
|
if ( in_array( $labelArray[ $val ][0], $curvalues ) ) { |
192
|
|
|
$selected = " selected='selected'"; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$ret.="<option".$selected." value='".$labelArray[ $val ][0]."'>".$labelArray[ $val ][1]."</option>"; |
196
|
|
|
|
197
|
|
|
} else { |
198
|
|
|
|
199
|
|
|
if ( in_array( $val, $curvalues ) ) { |
200
|
|
|
$selected = " selected='selected'"; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$ret .= "<option".$selected.">$val</option>"; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
} else { |
208
|
|
|
|
209
|
|
|
foreach ( $curvalues as $cur ) { |
210
|
|
|
$selected = ""; |
211
|
|
|
|
212
|
|
|
if ( array_key_exists( $cur, $labelArray ) ) { |
213
|
|
|
|
214
|
|
|
if ( in_array( $labelArray[ $cur ][0], $curvalues ) ) { |
215
|
|
|
$selected = " selected='selected'"; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$ret.="<option".$selected." value='".$labelArray[ $cur ][0]."'>".$labelArray[ $cur ][1]."</option>"; |
219
|
|
|
|
220
|
|
|
} else { |
221
|
|
|
if ( in_array( $cur, $curvalues ) ) { |
222
|
|
|
$selected = " selected='selected'"; |
223
|
|
|
} |
224
|
|
|
$ret.="<option".$selected.">$cur</option>"; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
$ret .= "</select></span>"; |
231
|
|
|
$ret .= "<span id=\"info_$sfgFieldNum\" class=\"errorMessage\"></span>"; |
232
|
|
|
|
233
|
|
|
if ( $other_args["is_list"] ) { |
234
|
|
|
$hiddenname = $input_name . '[is_list]'; |
235
|
|
|
$ret .= "<input type='hidden' name='$hiddenname' value='1' />"; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return $ret; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
|
242
|
|
|
private function getLabels( $labels ) { |
243
|
|
|
|
244
|
|
|
$labelArray = [ ]; |
245
|
|
|
|
246
|
|
|
if ( is_array( $labels ) ) { |
247
|
|
|
foreach ( $labels as $label ) { |
248
|
|
|
|
249
|
|
|
$labelKey = $label; |
250
|
|
|
$labelValue = $label; |
251
|
|
|
|
252
|
|
|
// Tricky thing if ( ) already in name |
253
|
|
|
if ( strpos( $label, ")" ) && strpos( $label, "(" ) ) { |
254
|
|
|
|
255
|
|
|
// Check Break |
256
|
|
|
$openBr = 0; |
257
|
|
|
$doneBr = 0; |
258
|
|
|
$num = 0; |
259
|
|
|
|
260
|
|
|
$labelArr = str_split ( $label ); |
261
|
|
|
|
262
|
|
|
$end = count( $labelArr ) - 1; |
263
|
|
|
$iter = $end; |
264
|
|
|
|
265
|
|
|
$endBr = $end; |
266
|
|
|
$startBr = 0; |
267
|
|
|
|
268
|
|
|
while ( $doneBr == 0 && $iter >= 0 ) { |
269
|
|
|
|
270
|
|
|
$char = $labelArr[ $iter ]; |
271
|
|
|
|
272
|
|
|
if ( $char == ")" ) { |
273
|
|
|
$openBr = $openBr - 1; |
274
|
|
|
|
275
|
|
|
if ( $num == 0 ) { |
276
|
|
|
$endBr = $iter; |
277
|
|
|
$num = $num + 1; |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
if ( $char == "(" ) { |
282
|
|
|
$openBr = $openBr + 1; |
283
|
|
|
|
284
|
|
|
if ( $num > 0 && $openBr == 0 ) { |
285
|
|
|
$startBr = $iter; |
286
|
|
|
$doneBr = 1; |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
$iter = $iter - 1; |
291
|
|
|
|
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
$labelValue = implode( "", array_slice( $labelArr, $startBr+1, $endBr-$startBr-1 ) ); |
295
|
|
|
$labelKey = implode( "", array_slice( $labelArr, 0, $startBr-1 ) ); |
296
|
|
|
|
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
$labelArray[ $label ] = [ $labelKey, $labelValue ] ; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
return $labelArray; |
305
|
|
|
|
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.