summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/Search/Form/CustomForm.php
blob: 84bac0cd399d0512dbb018269bbfeb48d2976cbf (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
<?php

namespace SMW\MediaWiki\Search\Form;

use Html;
use SMW\DIProperty;
use Title;
use WebRequest;

/**
 * @private
 *
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class CustomForm {

	/**
	 * @var WebRequest
	 */
	private $request;

	/**
	 * @var Field
	 */
	private $field;

	/**
	 * @var boolean
	 */
	private $isActiveForm = false;

	/**
	 * @var []
	 */
	private $parameters = [];

	/**
	 * @var []
	 */
	private $fieldCounter = [];

	/**
	 * @var []
	 */
	private $html5TypeMap = [
		'_txt' => 'text',
		'_uri' => 'url',
		'_dat' => 'date',
		'_tel' => 'tel',
		'_ema' => 'email',
		'_num' => 'number'
	];

	/**
	 * @since 3.0
	 *
	 * @param WebRequest $request
	 */
	public function __construct( WebRequest $request ) {
		$this->request = $request;
		$this->field = new Field();
	}

	/**
	 * @since 3.0
	 *
	 * @return []
	 */
	public function getParameters() {
		return $this->parameters;
	}

	/**
	 * @since 3.0
	 *
	 * @param boolean $isActiveForm
	 */
	public function isActiveForm( $isActiveForm ) {
		$this->isActiveForm = (bool)$isActiveForm;
	}

	/**
	 * @since 3.0
	 *
	 * @param array $definition
	 */
	public function makeFields( $definition ) {

		$fields = [];
		$this->parameters = [];
		$nameList = [];

		foreach ( $definition as $property ) {
			$options = [];

			// Simple list, or does a property have some options?
			if ( is_array( $property ) ) {
				foreach ( $property as $p => $options ) {
					$property = $p;
				}
			}

			// Transforms (Foo bar -> foobar), better URL query conformity
			$name = FormsBuilder::toLowerCase( $property );
			$value = '';

			// Field with the same name should only appear once in a form
			if ( isset( $nameList[$name] ) ) {
				continue;
			}

			// Each form definition may contain properties that are also defined
			// in other forms therefore count its member position so that only
			// values for the active form are fetched. The counter is used as
			// positioning for the value array index.
			if ( !isset( $this->fieldCounter[$name] ) ) {
				$this->fieldCounter[$name] = 0;
			} else {
				$this->fieldCounter[$name]++;
			}

			// Find request related value for the active form
			if ( $this->isActiveForm ) {
				$vals = $this->request->getArray( $name );

				$i = $this->fieldCounter[$name];
				$value = isset( $vals[$i] ) ? $vals[$i] : $vals[0];
				$this->parameters[$name] = $value;
			}

			$nameList[$name] = true;
			$fields[] = $this->makeField( $name, $property, $value, $options );
		}

		return implode( '', $fields );
	}

	private function makeField( $name, $property, $value, $options ) {

		$display = $this->isActiveForm ? 'inline-block' : 'none';
		$options = !is_array( $options ) ? [] : $options;

		if ( !isset( $options['placeholder'] ) ) {
			$options['placeholder'] = "$property ...";
		}

		if ( !isset( $options['class'] ) ) {
			$options['class'] = "";
		}

		if ( isset( $options['autocomplete'] ) && $options['autocomplete'] ) {
			$options['class'] .= " smw-propertyvalue-input autocomplete-arrow";
		}

		if ( isset( $options['type'] ) ) {
			$type = $options['type'];
		} else {
			$typeID = DIProperty::newFromUserLabel( $property )->findPropertyTypeID();
			$type = 'text';

			if ( isset( $this->html5TypeMap[$typeID] ) ) {
				$type = $this->html5TypeMap[$typeID];
			}
		}

		// Numeric names are not useful and may have been caused by an invalid
		// JSON array/object definition
		if ( is_numeric( $name ) ) {
			$options[] = 'disabled';
		}

		$attributes = [
			'name' => $name,
			'value' => $value,
			'type'  => $type,
			'display' => $display,
			'placeholder' => $options['placeholder'],
			'data-property' => $property,
			'title' => $property,
			'multifield' => true,
		] + $options;

		return $this->field->create( 'input', $attributes );
	}

}