summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticFormsSelect/src/SemanticFormsSelectInput.php
blob: 3417f6d0e980e10f158a7e087b474cb0b0753698 (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php

/**
 * @license GNU GPL v2+
 * @since 1.3
 *
 * @author Jason Zhang
 * @author Toni Hermoso Pulido
 * @author Alexander Gesinn
 */

namespace SFS;

use SMWQueryProcessor as QueryProcessor;
use Parser;
use PFFormInput;
use MWDebug;

class SemanticFormsSelectInput extends PFFormInput {

	/**
	 * Internal data container
	 *
	 * @var array
	 */
	private static $data = [];

	private $mSelectField;

	public function __construct( $inputNumber, $curValue, $inputName, $disabled, $otherArgs ) {
		parent::__construct( $inputNumber, $curValue, $inputName, $disabled, $otherArgs );

		// SelectField is a simple value object - we accept creating it in the constructor
		$this->mSelectField = new SelectField( $GLOBALS['wgParser'] );
	}

	public static function getName() {
		return 'SF_Select';
	}

	public static function getParameters() {
		$params = parent::getParameters();
		return $params;
	}

	public function getResourceModuleNames() {
		/**
		 * Loading modules this way currently fails with:
		 * "mw.loader.state({"ext.sf_select.scriptselect":"loading"});"
		 */

		return [
			'ext.sf_select.scriptselect'
		];
	}

	/**
	 * Returns the HTML code to be included in the output page for this input.
	 * This is currently just a wrapper for getHTML().
	 */
	public function getHtmlText() {
		return self::getHTML( $this->mCurrentValue, $this->mInputName, $this->mIsMandatory, $this->mIsDisabled,
			$this->mOtherArgs );
	}

	/**
	 * Returns the HTML code to be included in the output page for this input.
	 * @deprecated use getHtmlText() instead
	 *
	 * @param    string $cur_value A single value or a list of values with separator
	 * @param    string $input_name Name of the input including the template, e.g. Building[Part Of Site]
	 * @param            $is_mandatory
	 * @param            $is_disabled
	 * @param    string[] $other_args Array of other field parameters
	 * @return string
	 */
	public function getHTML( $cur_value = "", $input_name = "", $is_mandatory, $is_disabled, Array $other_args ) {
		global $sfgFieldNum, $wgUser;

		// shortcut to the SelectField object
		$selectField = $this->mSelectField;

		// get 'delimiter' before 'query' or 'function'
		$selectField->setDelimiter( $other_args );

		if ( array_key_exists( "query", $other_args ) ) {
			$selectField->setQuery( $other_args );
		} elseif ( array_key_exists( "function", $other_args ) ) {
			$selectField->setFunction( $other_args );
		}

		if ( array_key_exists( "label", $other_args ) ) {
			$selectField->setLabel( $other_args );
		}

		// parameters are only required if values needs to be retrieved dynamically
		if ( !$selectField->hasStaticValues() ) {
			$selectField->setSelectIsMultiple( $other_args );
			$selectField->setSelectTemplate( $input_name );
			$selectField->setSelectField( $input_name );
			$selectField->setValueTemplate( $other_args );
			$selectField->setValueField( $other_args );
			$selectField->setSelectRemove( $other_args );

			$item = Output::addToHeadItem( $selectField->getData() );
		}

		Output::commitToParserOutput();

		// prepare the html input tag

		$extraatt = "";
		$is_list = false;

		if ( array_key_exists( 'is_list', $other_args ) && $other_args['is_list'] == true ) {
			$is_list = true;
		}

		if ( $is_list ) {
			$extraatt = ' multiple="multiple" ';
		}

		if ( array_key_exists( "size", $other_args ) ) {
			$extraatt .= " size=\"{$other_args['size']}\"";
		}

		$classes = [];
		if ( $is_mandatory ) {
			$classes[] = "mandatoryField";
		}
		if ( array_key_exists( "class", $other_args ) ) {
			$classes[] = $other_args['class'];
		}
		if ( $classes ) {
			$cstr = implode( " ", $classes );
			$extraatt .= " class=\"$cstr\"";
		}

		$inname = $input_name;
		if ( $is_list ) {
			$inname .= '[]';
		}

		// TODO Use Html::

		$spanextra = $is_mandatory ? 'mandatoryFieldSpan' : '';
		$is_single_select = (!$is_list) ? 'select-sfs-single' : '' ;
		$ret = "<span class=\"inputSpan select-sfs $is_single_select $spanextra\"><select name='$inname' id='input_$sfgFieldNum' $extraatt>";

		$curvalues = null;
		if ( $cur_value ) {
			if ( $cur_value === 'current user' ) {
				$cur_value = $wgUser->getName();
			}
			if ( is_array( $cur_value ) ) {
				$curvalues = $cur_value;
			} else {
				// delimiter for $cur_value is always ',' - PF seems to ignore $wgPageFormsListSeparator
				$curvalues = array_map( "trim", explode( $selectField->getDelimiter(), $cur_value ) );
			}

		} else {
			$curvalues = [];
		}


		$labelArray = [];
		if ( array_key_exists( "label", $other_args ) && $curvalues ) {
			// $labelArray = $this->getLabels( $curvalues );
		}

		// TODO handle empty value case.
		$ret .= "<option></option>";

		if ( $selectField->hasStaticValues() ) {

			$values = $selectField->getValues();

			if ( array_key_exists( "label", $other_args ) && $values ) {
				$labelArray = $this->getLabels( $values );
			}

			if ( is_array( $values ) ) {

				foreach ( $values as $val ) {

					$selected = "";

					if ( array_key_exists( $val, $labelArray ) ) {

						if ( in_array( $labelArray[ $val ][0], $curvalues ) ) {
							$selected = " selected='selected'";
						}

						$ret.="<option".$selected." value='".$labelArray[ $val ][0]."'>".$labelArray[ $val ][1]."</option>";

					} else {

						if ( in_array( $val, $curvalues ) ) {
							$selected = " selected='selected'";
						}

						$ret .= "<option".$selected.">$val</option>";
					}
				}
			}
		} else {

			foreach ( $curvalues as $cur ) {
				$selected = "";

				if ( array_key_exists( $cur, $labelArray ) ) {

					if ( in_array( $labelArray[ $cur ][0], $curvalues ) ) {
						$selected = " selected='selected'";
					}

					$ret.="<option".$selected." value='".$labelArray[ $cur ][0]."'>".$labelArray[ $cur ][1]."</option>";

				} else {
					if ( in_array( $cur, $curvalues ) ) {
						$selected = " selected='selected'";
					}
					$ret.="<option".$selected.">$cur</option>";
				}
			}

		}

		$ret .= "</select></span>";
		$ret .= "<span id=\"info_$sfgFieldNum\" class=\"errorMessage\"></span>";

		if ( $other_args["is_list"] ) {
			$hiddenname = $input_name . '[is_list]';
			$ret .= "<input type='hidden' name='$hiddenname' value='1' />";
		}

		return $ret;
	}


	private function getLabels( $labels ) {

		$labelArray = [ ];

		if ( is_array( $labels ) ) {
			foreach ( $labels as $label ) {

				$labelKey = $label;
				$labelValue = $label;

				// Tricky thing if ( ) already in name
				if ( strpos( $label, ")" ) && strpos( $label, "(" ) ) {

					// Check Break
					$openBr = 0;
					$doneBr = 0;
					$num = 0;

					$labelArr = str_split ( $label );

					$end = count( $labelArr ) - 1;
					$iter = $end;

					$endBr = $end;
					$startBr = 0;

					while ( $doneBr == 0 && $iter >= 0 ) {

						$char = $labelArr[ $iter ];

						if ( $char == ")" ) {
							$openBr = $openBr - 1;

							if ( $num == 0 ) {
								$endBr = $iter;
								$num = $num + 1;
							}
						}

						if ( $char == "(" ) {
							$openBr = $openBr + 1;

							if ( $num > 0 && $openBr == 0 ) {
								$startBr = $iter;
								$doneBr = 1;
							}
						}

						$iter = $iter - 1;

					}

					$labelValue = implode( "", array_slice( $labelArr, $startBr+1, $endBr-$startBr-1 ) );
					$labelKey = implode( "", array_slice( $labelArr, 0, $startBr-1 ) );

				}

				$labelArray[ $label ] = [ $labelKey, $labelValue ] ;
			}

		}

		return $labelArray;

	}
}