summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/Specials/Ask/ParametersWidget.php
blob: b465d58877a33e5014ffb623226b887324d1b93f (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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php

namespace SMW\MediaWiki\Specials\Ask;

use Html;
use ParamProcessor\ParamDefinition;
use SMW\Message;
use SMW\Utils\HtmlDivTable;
use SMWQueryProcessor as QueryProcessor;
use Title;

/**
 * @private
 *
 * @license GNU GPL v2+
 * @since   1.8
 *
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 * @author mwjames
 */
class ParametersWidget {

	/**
	 * @var boolean
	 */
	private static $isTooltipDisplay = false;

	/**
	 * @var integer
	 */
	private static $defaultLimit = 50;

	/**
	 * @since 2.5
	 *
	 * @param boolean $isTooltipDisplay
	 */
	public static function setTooltipDisplay( $isTooltipDisplay ) {
		self::$isTooltipDisplay = (bool)$isTooltipDisplay;
	}

	/**
	 * @since 3.0
	 *
	 * @param integer $defaultLimit
	 */
	public static function setDefaultLimit( $defaultLimit ) {
		self::$defaultLimit = $defaultLimit;
	}

	/**
	 * @since 3.0
	 *
	 * @param Title $title
	 * @param array $parameters
	 *
	 * @return string
	 */
	public static function fieldset( Title $title, array $parameters ) {

		$toggle = Html::rawElement(
			'span',
			[
				'style' => 'margin-left:5px;'
			],
			'&#160;[' . Html::rawElement(
				'span',
				[
					'class' => 'options-toggle-action',
				],
				Html::rawElement(
					'label',
					[
						'for' => 'options-toggle',
						'title' => Message::get( 'smw-section-expand', Message::TEXT, Message::USER_LANGUAGE )
					],
					'+'
				)
			) . ']&#160;'
		);

		$options = Html::rawElement(
			'div',
			[
				'id' => 'parameter-title',
				'class' => 'strike'
			],
			Html::rawElement(
				'span',
				[],
				Message::get( 'smw-ask-parameters', Message::TEXT, Message::USER_LANGUAGE ) . $toggle
			)
		) . Html::rawElement(
			'div',
			[],
			'<input type="checkbox" id="options-toggle"/>' . Html::rawElement(
				'div',
				[
					'id' => 'options-list',
					'class' => 'options-list'
				],
				Html::rawElement(
					'div',
					[
						'class' => 'options-parameter-list'
					],
					self::parameterList( $parameters )
				)
			)
		);

		return Html::rawElement(
			'fieldset',
			[],
			Html::element(
				'legend',
				[],
				Message::get( 'smw-ask-options', Message::TEXT, Message::USER_LANGUAGE )
			). FormatListWidget::selectList(
				$title,
				$parameters
			) . $options . SortWidget::sortSection( $parameters )
		);
	}

	/**
	 * Display a form section showing the options for a given format,
	 * based on the getParameters() value for that format's query printer.
	 *
	 * @since 1.8
	 *
	 * @param string $format
	 * @param array $parameters The current values for the parameters (name => value)
	 *
	 * @return string
	 */
	public static function parameterList( array $values ) {

		$format = 'broadtable';

		if ( isset( $values['format'] ) ) {
			$format = $values['format'];
		}

		$optionList = self::optionList(
			QueryProcessor::getFormatParameters( $format ),
			$values
		);

		$i = 0;
		$n = 0;

		$rowHtml = '';
		$resultHtml = '';

		// Top info text for a collapsed option box
		if ( self::$isTooltipDisplay === true ){
			$resultHtml .= Html::element(
				'div',
				[
					'style' => 'margin-bottom:10px;'
				],
				Message::get( 'smw-ask-otheroptions-info', Message::TEXT, Message::USER_LANGUAGE )
			);
		}

		// Table
		$resultHtml = HtmlDivTable::open(
			[
				'class' => 'smw-ask-options-list',
				'width' => '100%'
			]
		);

		while ( $option = array_shift( $optionList ) ) {
			$i++;

			// Collect elements for a row
			$rowHtml .=  $option;

			// Create table row
			if ( $i % 3 == 0 ) {
				$resultHtml .= HtmlDivTable::row(
					$rowHtml,
					[
						'class' => $i % 6 == 0 ? 'smw-ask-options-row-even' : 'smw-ask-options-row-odd',
					]
				);
				$rowHtml = '';
				$n++;
			}
		}

		// Ensure left over elements are collected as well
		$resultHtml .= HtmlDivTable::row(
			$rowHtml,
			[
				'class' => $n % 2 == 0 ? 'smw-ask-options-row-odd' : 'smw-ask-options-row-even',
			]
		);

		$resultHtml .= HtmlDivTable::close();

		return $resultHtml;
	}

	private static function optionList( $definitions, $values ) {

		$html = [];

		/**
		 * @var \ParamProcessor\ParamDefinition $definition
		 */
		foreach ( $definitions as $name => $definition ) {

			// Ignore the format parameter, as we got a special control in the GUI for it already.
			if ( $name == 'format' ) {
				continue;
			}

			// Handle sort, order separate as the generated checkbox are suboptimal, and the single
			// field interferes with the GET request on multiple sort setters
			if ( in_array( $name, [ 'sort', 'order' ] ) ) {
				continue;
			}

			// Maybe there is a better way but somehow I couldn't find one therefore
			// 'source' display will be omitted where no alternative source was found or
			// a source that was marked as default but had no other available options
			$allowedValues = $definition->getAllowedValues();

			if ( $name == 'source' && (
					count( $allowedValues ) == 0 ||
					in_array( 'default', $allowedValues ) && count( $allowedValues ) < 2
				) ) {

				continue;
			}

			$currentValue = false;

			if ( array_key_exists( $name, $values ) ) {
				$currentValue = $values[$name];
			}

			// Set default values
			if ( $name === 'limit' && ( $currentValue === null || $currentValue === false ) ) {
				$currentValue = self::$defaultLimit;
			}

			if ( $name === 'offset' && ( $currentValue === null || $currentValue === false ) ) {
				$currentValue = 0;
			}

			$html[] = '<td>' . self::field( $definition, $name ) . '</td>' . self::input( $definition, $currentValue );
		}

		return $html;
	}

	private static function field( ParamDefinition $definition, $name ) {

		$info = '';
		$class = '';

		if ( self::$isTooltipDisplay === true ) {
			$class = 'smw-ask-info';
		}

		if ( $definition->getMessage() !== null  ) {
			$info = Message::get( $definition->getMessage(), Message::TEXT, Message::USER_LANGUAGE );
		}

		return HtmlDivTable::cell(
			Html::rawElement(
				'span',
				[
					'class'     =>  $class,
					'word-wrap' => 'break-word',
					'data-info' => $info
				],
				htmlspecialchars( $name ) . ': '
			),
			[
				'overflow' => 'hidden',
				'style' => 'border:none;'
			]
		);
	}

	private static function input( ParamDefinition $definition, $currentValue ) {

		$description = '';
		$info = '';

		$input = new ParameterInput( $definition );
		$input->setInputName( 'p[' . $definition->getName() . ']' );
		//$input->setInputClass( 'smw-ask-input-' . str_replace( ' ', '-', $definition->getName() ) );

		$opts = $definition->getOptions();
		$attributes = [];

		if ( isset( $opts['style'] ) ) {
			$attributes['style'] = $opts['style'];
		}

		if ( isset( $opts['size'] ) ) {
			$attributes['size'] = $opts['size'];
		}

		// [ 'data-props' => [
		//   'property' => Foo, 'value' => 'Bar', 'title-prefix' => 'false'
		// ] ]
		if ( isset( $opts['data-props'] ) && is_array( $opts['data-props'] ) ) {
			foreach ( $opts['data-props'] as $key => $value ) {
				if ( is_string( $key ) ) {
					$attributes["data-$key"] = $value;
				}
			}
		}

		if ( isset( $opts['class'] ) ) {
			$attributes['class'] = $opts['class'];
		}

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

		if ( $currentValue !== false ) {
			$input->setCurrentValue( $currentValue );
		}

		// Parameters description text
		if ( !self::$isTooltipDisplay ) {

			if ( $definition->getMessage() !== null ) {
				$info = Message::get( $definition->getMessage(), Message::PARSE, Message::USER_LANGUAGE );
			}

			$description =  Html::rawElement(
				'span',
				[
					'class' => 'smw-ask-parameter-description'
				],
				'<br />' . $info
			);
		}

		return HtmlDivTable::cell(
			$input->getHtml() . $description,
			[
				'overflow' => 'hidden',
				'style' => 'width:33%;border:none;'
			]
		);
	}

}