Completed
Pull Request — master (#77)
by
unknown
20:00
created

SemanticFormsSelectInput::getHtmlText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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,
0 ignored issues
show
Deprecated Code introduced by
The method SFS\SemanticFormsSelectInput::getHTML() has been deprecated with message: use getHtmlText() instead

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.

Loading history...
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 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $is_disabled is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
78
		global $sfgFieldNum, $wgUser, $wgPageFormsShowOnSelect, $wgPageFormsFieldNum;
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() );
0 ignored issues
show
Unused Code introduced by
$item is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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 ) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $other_args['is_list'] of type string to the boolean true. If you are specifically checking for a non-empty string, consider using the more explicit !== '' instead.
Loading history...
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
		$sfgFieldNum = $sfgFieldNum ? $sfgFieldNum : $wgPageFormsFieldNum;
135
		$input_id = "input_$sfgFieldNum";
136
		if ( array_key_exists( 'show on select', $other_args ) ) {
137
			$classes[] = 'pfShowIfSelected';
138
			foreach ( $other_args['show on select'] as $div_id => $options ) {
0 ignored issues
show
Bug introduced by
The expression $other_args['show on select'] of type string is not traversable.
Loading history...
139
				if ( array_key_exists( $input_id, $wgPageFormsShowOnSelect ) ) {
140
					$wgPageFormsShowOnSelect[$input_id][] = array( $options, $div_id );
141
				} else {
142
					$wgPageFormsShowOnSelect[$input_id] = array( array( $options, $div_id ) );
143
				}
144
			}
145
		}
146
		if ( $classes ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $classes of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
147
			$cstr = implode( " ", $classes );
148
			$extraatt .= " class=\"$cstr\"";
149
		}
150
151
		$inname = $input_name;
152
		if ( $is_list ) {
153
			$inname .= '[]';
154
		}
155
156
		// TODO Use Html::
157
158
		$spanextra = $is_mandatory ? 'mandatoryFieldSpan' : '';
159
		$is_single_select = (!$is_list) ? 'select-sfs-single' : '' ;
160
		$ret = "<span class=\"inputSpan select-sfs $is_single_select $spanextra\"><select name='$inname' id='input_$sfgFieldNum' $extraatt>";
161
162
		$curvalues = null;
0 ignored issues
show
Unused Code introduced by
$curvalues is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
163
		if ( $cur_value ) {
164
			if ( $cur_value === 'current user' ) {
165
				$cur_value = $wgUser->getName();
166
			}
167
			if ( is_array( $cur_value ) ) {
168
				$curvalues = $cur_value;
169
			} else {
170
				// delimiter for $cur_value is always ',' - PF seems to ignore $wgPageFormsListSeparator
171
				$curvalues = array_map( "trim", explode( $selectField->getDelimiter(), $cur_value ) );
172
			}
173
174
		} else {
175
			$curvalues = [];
176
		}
177
178
		
179
		$labelArray = [];
180
		if ( array_key_exists( "label", $other_args ) && $curvalues ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $curvalues of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
181
			// $labelArray = $this->getLabels( $curvalues );
182
		}
183
184
		// TODO handle empty value case.
185
		$ret .= "<option></option>";
186
187
		if ( $selectField->hasStaticValues() ) {
188
			
189
			$values = $selectField->getValues();
190
			
191
			if ( array_key_exists( "label", $other_args ) && $values ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $values of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
192
				$labelArray = $this->getLabels( $values );
193
			}
194
			
195
			if ( is_array( $values ) ) {
196
				
197
				foreach ( $values as $val ) {
198
					
199
					$selected = "";
200
											
201
					if ( array_key_exists( $val, $labelArray ) ) {
202
					
203
						if ( in_array( $labelArray[ $val ][0], $curvalues ) ) {
204
							$selected = " selected='selected'";
205
						}
206
						
207
						$ret.="<option".$selected." value='".$labelArray[ $val ][0]."'>".$labelArray[ $val ][1]."</option>";
208
					
209
					} else {
210
						
211
						if ( in_array( $val, $curvalues ) ) {
212
							$selected = " selected='selected'";
213
						}
214
215
						$ret .= "<option".$selected.">$val</option>";
216
					}
217
				}
218
			}
219
		} else {
220
			
221
			foreach ( $curvalues as $cur ) {
222
				$selected = "";
223
				
224
				if ( array_key_exists( $cur, $labelArray ) ) {
225
					
226
					if ( in_array( $labelArray[ $cur ][0], $curvalues ) ) {
227
						$selected = " selected='selected'";
228
					}
229
					
230
					$ret.="<option".$selected." value='".$labelArray[ $cur ][0]."'>".$labelArray[ $cur ][1]."</option>";
231
					
232
				} else {
233
					if ( in_array( $cur, $curvalues ) ) {
234
						$selected = " selected='selected'";
235
					}
236
					$ret.="<option".$selected.">$cur</option>";
237
				}
238
			}
239
240
		}
241
242
		$ret .= "</select></span>";
243
		$ret .= "<span id=\"info_$sfgFieldNum\" class=\"errorMessage\"></span>";
244
245
		if ( $other_args["is_list"] ) {
246
			$hiddenname = $input_name . '[is_list]';
247
			$ret .= "<input type='hidden' name='$hiddenname' value='1' />";
248
		}
249
250
		return $ret;
251
	}
252
	
253
		
254
	private function getLabels( $labels ) {
255
		
256
		$labelArray = [ ];
257
258
		if ( is_array( $labels ) ) {
259
			foreach ( $labels as $label ) {
260
				
261
				$labelKey = $label;
262
				$labelValue = $label;
263
				
264
				// Tricky thing if ( ) already in name
265
				if ( strpos( $label, ")" ) && strpos( $label, "(" ) ) {
266
				
267
					// Check Break
268
					$openBr = 0;
269
					$doneBr = 0;
270
					$num = 0;
271
					
272
					$labelArr = str_split ( $label );
273
					
274
					$end = count( $labelArr ) - 1;
275
					$iter = $end;
276
					
277
					$endBr = $end;
278
					$startBr = 0;
279
					
280
					while ( $doneBr == 0 && $iter >= 0 ) {
281
					
282
						$char = $labelArr[ $iter ];
283
						
284
						if ( $char == ")" ) {
285
							$openBr = $openBr - 1;
286
							
287
							if ( $num == 0 ) {
288
								$endBr = $iter;
289
								$num = $num + 1;
290
							}
291
						}
292
						
293
						if ( $char == "(" ) {
294
							$openBr = $openBr + 1;
295
							
296
							if ( $num > 0 && $openBr == 0 ) {
297
								$startBr = $iter;
298
								$doneBr = 1;
299
							}
300
						}
301
						
302
						$iter = $iter - 1;
303
						
304
					}
305
					
306
					$labelValue = implode( "", array_slice( $labelArr, $startBr+1, $endBr-$startBr-1 ) );			
307
					$labelKey = implode( "", array_slice( $labelArr, 0, $startBr-1 ) );
308
				
309
				}
310
				
311
				$labelArray[ $label ] = [ $labelKey, $labelValue ] ;
312
			}
313
		
314
		}
315
		
316
		return $labelArray;
317
		
318
	}
319
}
320