summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/Specials/Ask/ParameterInput.php
blob: 5a505ed3cfd8cb4fb169fd55164a49eef14615fa (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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php

namespace SMW\MediaWiki\Specials\Ask;

use Html;
use ParamProcessor\ParamDefinition;
use Xml;

/**
 * Simple class to get a HTML input for the parameter.
 * Usable for when creating a GUI from a parameter list.
 *
 * Based on 'addOptionInput' from Special:Ask in SMW 1.5.6.
 *
 * TODO: nicify HTML
 *
 * @since 1.9
 *
 * @ingroup SMW
 *
 * @licence GNU GPL v2+
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 */
class ParameterInput {

	/**
	 * The parameter to print an input for.
	 *
	 * @since 1.9
	 *
	 * @var ParamDefinition
	 */
	protected $param;

	/**
	 * The current value for the parameter. When provided,
	 * it'll be used as value for the input, otherwise the
	 * parameters default value will be used.
	 *
	 * @since 1.9
	 *
	 * @var mixed: string or false
	 */
	protected $currentValue;

	/**
	 * Name for the input.
	 *
	 * @since 1.9
	 *
	 * @var string
	 */
	protected $inputName;

	/**
	 * @var array
	 */
	private $attributes = [];

	/**
	 * Constructor.
	 *
	 * @since 1.9
	 *
	 * @param ParamDefinition $param
	 * @param mixed $currentValue
	 */
	public function __construct( ParamDefinition $param, $currentValue = false ) {
		$this->currentValue = $currentValue;
		$this->inputName = $param->getName();
		$this->param = $param;
	}

	/**
	 * Sets the current value.
	 *
	 * @since 1.9
	 *
	 * @param mixed $currentValue
	 */
	public function setCurrentValue( $currentValue ) {
		$this->currentValue = $currentValue;
	}

	/**
	 * Sets the name for the input; defaults to the name of the parameter.
	 *
	 * @since 1.9
	 *
	 * @param string $name
	 */
	public function setInputName( $name ) {
		$this->inputName = $name;
	}

	/**
	 * @since 3.0
	 *
	 * @param array $attributes
	 */
	public function setAttributes( array $attributes ) {
		$this->attributes = $attributes;
	}

	/**
	 * Returns the HTML for the parameter input.
	 *
	 * @since 1.9
	 *
	 * @return string
	 */
	public function getHtml() {
		$valueList = [];

		if ( is_array( $this->param->getAllowedValues() ) ) {
			$valueList = $this->param->getAllowedValues();
		}

		if ( $valueList === [] ) {
			switch ( $this->param->getType() ) {
				case 'char':
				case 'float':
				case 'integer':
				case 'number':
					$html = $this->getNumberInput();
					break;
				case 'boolean':
					$html = $this->getBooleanInput();
					break;
				case 'string':
				default:
					$html = $this->getStrInput();
					break;
			}
		} else {
			$html = $this->param->isList() ? $this->getCheckboxListInput( $valueList ) : $this->getSelectInput( $valueList );
		}

		return $html;
	}

	/**
	 * Returns the value to initially display with the input.
	 *
	 * @since 1.9
	 *
	 * @return string
	 */
	protected function getValueToUse() {
		$value = $this->currentValue === false ? $this->param->getDefault() : $this->currentValue;

		if ( $this->param->isList() && is_array( $value ) ) {
			$value = implode( $this->param->getDelimiter(), $value );
		}

		// #1473
		if ( $value === [] ) {
		   $value = '';
		}

		return $value;
	}

	/**
	 * Gets a short text input suitable for numbers.
	 *
	 * @since 1.9
	 *
	 * @return string
	 */
	protected function getNumberInput() {

		$attributes = [
			'class' => 'parameter-number-input',
			'size' => 6,
			'style' => "width: 95%;"
		];

		if ( $this->attributes !==[] ) {
			$attributes = $this->attributes;
		}

		return Html::input(
			$this->inputName,
			$this->getValueToUse(),
			'text',
			$attributes
		);
	}

	/**
	 * Gets a text input for a string.
	 *
	 * @since 1.9
	 *
	 * @return string
	 */
	protected function getStrInput() {

		$attributes = [
			'class' => 'parameter-string-input',
			'size' => 20,
			'style' => "width: 95%;"
		];

		if ( $this->attributes !==[] ) {
			$attributes = $this->attributes;
		}

		return Html::input(
			$this->inputName,
			$this->getValueToUse(),
			'text',
			$attributes
		);
	}

	/**
	 * Gets a checkbox.
	 *
	 * @since 1.9
	 *
	 * @return string
	 */
	protected function getBooleanInput() {

		$attributes = [
			'class' => 'parameter-boolean-input'
		];

		if ( $this->attributes !==[] ) {
			$attributes = $this->attributes;
		}

		return Xml::check(
			$this->inputName,
			$this->getValueToUse(),
			$attributes
		);
	}

	/**
	 * Gets a select menu for the provided values.
	 *
	 * @since 1.9
	 *
	 * @param array $valueList
	 *
	 * @return string
	 */
	protected function getSelectInput( array $valueList ) {
		$options = [];
		$options[] = '<option value=""></option>';

		$currentValues = (array)$this->getValueToUse();
		if ( is_null( $currentValues ) ) {
			$currentValues = [];
		}

		foreach ( $valueList as $value ) {
			$options[] =
				'<option value="' . htmlspecialchars( $value ) . '"' .
					( in_array( $value, $currentValues ) ? ' selected="selected"' : '' ) . '>' . htmlspecialchars( $value ) .
				'</option>';
		}

		return Html::rawElement(
			'select',
			[
				'name' => $this->inputName,
				'class'=> 'parameter-select-input'
			],
			implode( "\n", $options )
		);
	}

	/**
	 * Gets a list of input boxes for the provided values.
	 *
	 * @since 1.9
	 *
	 * @param array $valueList
	 *
	 * @return string
	 */
	protected function getCheckboxListInput( array $valueList ) {
		$boxes = [];
		$currentValues = [];

		$values = $this->getValueToUse();

		// List of comma separated values, see ParametersProcessor::getParameterList
		if ( strpos( $values, ',' ) !== false ) {
			$currentValues = array_flip(
				array_map( 'trim', explode( ',', $values ) )
			);
		} elseif ( $values !== '' ) {
			$currentValues[$values] = true;
		}

		foreach ( $valueList as $value ) {

			// Use a value not a simple "true"
			$attr = [
				'type' => 'checkbox',
				'name' => $this->inputName . '[]',
				'value' => $value
			];

			$boxes[] = Html::rawElement(
				'span',
				[
					'class' => 'parameter-checkbox-input',
					'style' => 'white-space: nowrap; padding-right: 5px;'
				],
				Html::rawElement(
					'input',
					$attr + ( isset( $currentValues[$value] ) ? [ 'checked' ] : [] )
				) . Html::element( 'tt', [], $value )
			);
		}

		return implode( "\n", $boxes );
	}

}