1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SFS; |
4
|
|
|
|
5
|
|
|
use SMWQueryProcessor as QueryProcessor; |
6
|
|
|
use Parser; |
7
|
|
|
use PFFormInput; |
8
|
|
|
use MWDebug; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @license GNU GPL v2+ |
12
|
|
|
* @since 1.3 |
13
|
|
|
* |
14
|
|
|
* @author Jason Zhang |
15
|
|
|
* @author Toni Hermoso Pulido |
16
|
|
|
* @author Alexander Gesinn |
17
|
|
|
*/ |
18
|
|
|
class SemanticFormsSelectInput extends PFFormInput { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Internal data container |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private static $data = array(); |
26
|
|
|
|
27
|
|
|
public function __construct( $inputNumber, $curValue, $inputName, $disabled, $otherArgs ) { |
28
|
|
|
parent::__construct( $inputNumber, $curValue, $inputName, $disabled, $otherArgs ); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public static function getName() { |
32
|
|
|
return 'SF_Select'; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public static function getParameters() { |
36
|
|
|
$params = parent::getParameters(); |
37
|
|
|
return $params; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getResourceModuleNames() { |
41
|
|
|
/** |
42
|
|
|
* Loading modules this way currently fails with: |
43
|
|
|
* "mw.loader.state({"ext.sf_select.scriptselect":"loading"});" |
44
|
|
|
*/ |
45
|
|
|
|
46
|
|
|
return array( |
47
|
|
|
'ext.sf_select.scriptselect' |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Returns the HTML code to be included in the output page for this input. |
53
|
|
|
* This is currently just a wrapper for getHTML(). |
54
|
|
|
*/ |
55
|
|
|
public function getHtmlText() { |
56
|
|
|
return self::getHTML( |
|
|
|
|
57
|
|
|
$this->mCurrentValue, |
58
|
|
|
$this->mInputName, |
59
|
|
|
$this->mIsMandatory, |
60
|
|
|
$this->mIsDisabled, |
61
|
|
|
$this->mOtherArgs ); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns the HTML code to be included in the output page for this input. |
66
|
|
|
* @deprecated use getHtmlText() instead |
67
|
|
|
* |
68
|
|
|
* @param $cur_value |
69
|
|
|
* @param $input_name |
70
|
|
|
* @param $is_mandatory |
71
|
|
|
* @param $is_disabled |
72
|
|
|
* @param $other_args |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function getHTML( $cur_value, $input_name, $is_mandatory, $is_disabled, $other_args ) { |
76
|
|
|
global # $wgScriptSelectCount, |
|
|
|
|
77
|
|
|
$wgScriptSelectCount, $sfgFieldNum, $wgUser, $wgParser; |
78
|
|
|
|
79
|
|
|
$selectField = array(); |
80
|
|
|
$values = null; |
81
|
|
|
$staticvalue = false; |
82
|
|
|
$data = array(); |
83
|
|
|
|
84
|
|
|
if ( array_key_exists( "query", $other_args ) ) { |
85
|
|
|
$query = $other_args["query"]; |
86
|
|
|
$query = str_replace( array( "~", "(", ")" ), array( "=", "[", "]" ), $query ); |
87
|
|
|
|
88
|
|
|
$selectField["query"] = $query; |
89
|
|
|
|
90
|
|
|
// unparametrized query |
91
|
|
|
if ( strpos( $query, '@@@@' ) === false ) { |
92
|
|
|
$params = explode( ";", $query ); |
93
|
|
|
|
94
|
|
|
// there is no need to run the parser, $query has been parsed already |
95
|
|
|
//$params[0] = $wgParser->replaceVariables( $params[0] ); |
|
|
|
|
96
|
|
|
|
97
|
|
|
$values = QueryProcessor::getResultFromFunctionParams( $params, SMW_OUTPUT_WIKI ); |
|
|
|
|
98
|
|
|
|
99
|
|
|
$staticvalue = true; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
} elseif ( array_key_exists( "function", $other_args ) ) { |
103
|
|
|
$query = $other_args["function"]; |
104
|
|
|
$query = '{{#' . $query . '}}'; |
105
|
|
|
$query = str_replace( array( "~", "(", ")" ), array( "=", "[", "]" ), $query ); |
106
|
|
|
|
107
|
|
|
$selectField["function"] = $query; |
108
|
|
|
|
109
|
|
|
// unparametrized function |
110
|
|
|
if ( strpos( $query, '@@@@' ) === false ) { |
111
|
|
|
$f = str_replace( ";", "|", $query ); |
112
|
|
|
|
113
|
|
|
$values = $wgParser->replaceVariables( $f ); |
114
|
|
|
|
115
|
|
|
$staticvalue = true; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if ( $staticvalue ) { |
120
|
|
|
$values = explode( ",", $values ); |
121
|
|
|
$values = array_map( "trim", $values ); |
122
|
|
|
$values = array_unique( $values ); |
123
|
|
|
} else { |
124
|
|
|
|
125
|
|
|
if ( $wgScriptSelectCount == 0 ) { |
|
|
|
|
126
|
|
|
// this has been moved to getResourceModuleNames() |
127
|
|
|
//Output::addModule( 'ext.sf_select.scriptselect' ); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
// $wgScriptSelectCount ++; |
130
|
|
|
|
131
|
|
|
$data["selectismultiple"] = array_key_exists( "part_of_multiple", $other_args ); |
132
|
|
|
|
133
|
|
|
$index = strpos( $input_name, "[" ); |
134
|
|
|
$data['selecttemplate'] = substr( $input_name, 0, $index ); |
135
|
|
|
|
136
|
|
|
// Does hit work for multiple template? |
137
|
|
|
$index = strrpos( $input_name, "[" ); |
138
|
|
|
$data['selectfield'] = substr( $input_name, $index + 1, strlen( $input_name ) - $index - 2 ); |
139
|
|
|
|
140
|
|
|
$valueField = array(); |
|
|
|
|
141
|
|
|
$data["valuetemplate"] = |
142
|
|
|
array_key_exists( "sametemplate", $other_args ) ? $data['selecttemplate'] : $other_args["template"]; |
143
|
|
|
$data["valuefield"] = $other_args["field"]; |
144
|
|
|
|
145
|
|
|
$data['selectrm'] = array_key_exists( 'rmdiv', $other_args ); |
146
|
|
|
$data['label'] = array_key_exists( 'label', $other_args ); |
147
|
|
|
$data['sep'] = array_key_exists( 'sep', $other_args ) ? $other_args["sep"] : ','; |
148
|
|
|
|
149
|
|
|
if ( array_key_exists( "query", $selectField ) ) { |
150
|
|
|
$data['selectquery'] = $selectField['query']; |
151
|
|
|
} else { |
152
|
|
|
$data['selectfunction'] = $selectField['function']; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
self::$data[] = $data; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$extraatt = ""; |
159
|
|
|
$is_list = false; |
160
|
|
|
|
161
|
|
|
// TODO This needs clean-up |
162
|
|
|
|
163
|
|
|
if ( array_key_exists( 'is_list', $other_args ) && $other_args['is_list'] == true ) { |
164
|
|
|
$is_list = true; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if ( $is_list ) { |
168
|
|
|
$extraatt = ' multiple="multiple" '; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
if ( array_key_exists( "size", $other_args ) ) { |
172
|
|
|
$extraatt .= " size=\"{$other_args['size']}\""; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$classes = array(); |
176
|
|
|
if ( $is_mandatory ) { |
177
|
|
|
$classes[] = "mandatoryField"; |
178
|
|
|
} |
179
|
|
|
if ( array_key_exists( "class", $other_args ) ) { |
180
|
|
|
$classes[] = $other_args['class']; |
181
|
|
|
} |
182
|
|
|
if ( $classes ) { |
|
|
|
|
183
|
|
|
$cstr = implode( " ", $classes ); |
184
|
|
|
$extraatt .= " class=\"$cstr\""; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$inname = $input_name; |
188
|
|
|
if ( $is_list ) { |
189
|
|
|
$inname .= '[]'; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
// TODO Use Html:: |
193
|
|
|
|
194
|
|
|
$spanextra = $is_mandatory ? 'mandatoryFieldSpan' : ''; |
195
|
|
|
$ret = "<span class=\"inputSpan $spanextra\"><select name='$inname' id='input_$sfgFieldNum' $extraatt>"; |
196
|
|
|
$curvalues = null; |
|
|
|
|
197
|
|
|
if ( $cur_value ) { |
198
|
|
|
if ( $cur_value === 'current user' ) { |
199
|
|
|
$cur_value = $wgUser->getName(); |
200
|
|
|
} |
201
|
|
|
if ( is_array( $cur_value ) ) { |
202
|
|
|
$curvalues = $cur_value; |
203
|
|
|
} else { |
204
|
|
|
$curvalues = array_map( "trim", explode( ",", $cur_value ) ); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
} else { |
208
|
|
|
$curvalues = array(); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
// TODO handle empty value case. |
212
|
|
|
$ret .= "<option></option>"; |
213
|
|
|
|
214
|
|
|
foreach ( $curvalues as $cur ) { |
215
|
|
|
$ret .= "<option selected='selected'>$cur</option>"; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
if ( $staticvalue ) { |
219
|
|
|
foreach ( $values as $val ) { |
220
|
|
|
if ( !in_array( $val, $curvalues ) ) { |
221
|
|
|
$ret .= "<option>$val</option>"; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$ret .= "</select></span>"; |
227
|
|
|
$ret .= "<span id=\"info_$sfgFieldNum\" class=\"errorMessage\"></span>"; |
228
|
|
|
|
229
|
|
|
if ( $other_args["is_list"] ) { |
230
|
|
|
$hiddenname = $input_name . '[is_list]'; |
231
|
|
|
$ret .= "<input type='hidden' name='$hiddenname' value='1' />"; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
if ( !$staticvalue ) { |
235
|
|
|
$item = Output::addToHeadItem( $data ); |
|
|
|
|
236
|
|
|
//$wgOut->addJsConfigVars('sf_select', array(json_encode( $data ))); |
|
|
|
|
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
Output::commitToParserOutput(); |
240
|
|
|
return $ret; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.