summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticFormsSelect/src/SelectField.php
blob: 87adea0a04925cb360e14be3eaac31437beeb328 (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
<?php

/**
 * Represents a Select Field.
 * @license GNU GPL v2+
 * @since 3.0.0
 * @author: Alexander Gesinn
 */

namespace SFS;

use SMWQueryProcessor as QueryProcessor;
use Parser;
use MWDebug;

class SelectField {

	private $mParser = null;

	//private $mSelectField = array();
	private $mValues = null;
	private $mHasStaticValues = false;

	private $mData = [];    # array with all parameters
	private $mQuery = "";
	private $mFunction = "";
	private $mSelectIsMultiple = false;
	private $mSelectTemplate = "";
	private $mSelectField = "";
	private $mValueTemplate = "";
	private $mValueField = "";
	private $mSelectRemove = false;
	private $mLabel = false;
	private $mDelimiter = ",";

	public function __construct( & $parser ) {
		$this->mParser = $parser;
	}

	/**
	 * Convenience function to process all parameters at once
	 */
	public function processParameters( $input_name = "", $other_args ) {
		if ( array_key_exists( "query", $other_args ) ) {
			$this->setQuery( $other_args );
		} elseif ( array_key_exists( "function", $other_args ) ) {
			$this->setFunction( $other_args );
		}
	}

	/**
	 * getData
	 *
	 * @return array Array with all parameters
	 */
	public function getData() {
		return $this->mData;
	}

	public function setQuery( $other_args ) {
		$querystr = $other_args["query"];
		$querystr = str_replace( [ "~", "(", ")" ], [ "=", "[", "]" ], $querystr );

		//$this->mSelectField["query"] = $query;
		$this->mQuery = $querystr;
		$this->mData['selectquery'] = $querystr;

		// unparametrized query
		if ( strpos( $querystr, '@@@@' ) === false ) {
			$rawparams = explode( ";", $querystr );

			// there is no need to run the parser, $query has been parsed already
			//$params[0] = $wgParser->replaceVariables( $params[0] );

			list( $query, $params ) = QueryProcessor::getQueryAndParamsFromFunctionParams( $rawparams, SMW_OUTPUT_WIKI, QueryProcessor::INLINE_QUERY, false );
			
			$result = QueryProcessor::getResultFromQuery( $query, $params, SMW_OUTPUT_WIKI, QueryProcessor::INLINE_QUERY );
		
			$this->mValues = $this->getFormattedValuesFrom( $this->mDelimiter, $result );
		
			$this->setHasStaticValues( true );
		}
	}

	public function setFunction( $other_args ) {
		#global $wgParser;

		$function = $other_args["function"];
		$function = '{{#' . $function . '}}';
		$function = str_replace( [ "~", "(", ")" ], [ "=", "[", "]" ], $function );

		//$this->mSelectField["function"] = $function;
		$this->mFunction = $function;
		$this->mData['selectfunction'] = $function;

		// unparametrized function
		if ( strpos( $function, '@@@@' ) === false ) {
			$f = str_replace( ";", "|", $function );

			$this->setValues( $this->mParser->replaceVariables( $f ) );

			$this->setHasStaticValues( true );
		}
	}

	public function setSelectIsMultiple( Array $other_args ) {
		$this->mSelectIsMultiple = array_key_exists( "part_of_multiple", $other_args );
		$this->mData["selectismultiple"] = $this->mSelectIsMultiple;
	}

	public function setSelectTemplate( $input_name = "" ) {
		$index = strpos( $input_name, "[" );
		$this->mSelectTemplate = substr( $input_name, 0, $index );
		$this->mData['selecttemplate'] = $this->mSelectTemplate;
	}

	public function setSelectField( $input_name = "" ) {
		$index = strrpos( $input_name, "[" );
		$this->mSelectField = substr( $input_name, $index + 1, strlen( $input_name ) - $index - 2 );
		$this->mData['selectfield'] = $this->mSelectField;
	}

	public function setValueTemplate( Array $other_args ) {
		$this->mValueTemplate =
			array_key_exists( "sametemplate", $other_args ) ? $this->mSelectTemplate : $other_args["template"];
		$this->mData["valuetemplate"] = $this->mValueTemplate;
	}

	public function setValueField( Array $other_args ) {
		$this->mValueField = $other_args["field"];
		$this->mData["valuefield"] = $this->mValueField;

	}

	public function setSelectRemove( Array $other_args ) {
		$this->mSelectRemove = array_key_exists( 'rmdiv', $other_args );
		$this->mData['selectrm'] = $this->mSelectRemove;
	}

	public function setLabel( Array $other_args ) {
		$this->mLabel = array_key_exists( 'label', $other_args );
		$this->mData['label'] = $this->mLabel;
	}

	/**
	 * setDelimiter
	 * @param array $other_args
	 */
	public function setDelimiter( Array $other_args ) {
		
		$this->mDelimiter = $GLOBALS['wgPageFormsListSeparator'];
		
		if ( array_key_exists( 'sep', $other_args ) ) {
			$this->mDelimiter = $other_args['sep'];
		} else {
			// Adding Backcompatibility
			if ( array_key_exists( 'delimiter', $other_args ) ) {
				$this->mDelimiter = $other_args['delimiter'];
			}
		}

		$this->mData['sep'] = $this->mDelimiter;
	}

	public function getDelimiter() {
		return $this->mDelimiter;
	}

	public function getValues() {
		return $this->mValues;
	}

	/**
	 * setValues
	 * @param string $values (comma separated, fully parsed list of values)
	 */
	private function setValues( $values ) {
		$values = explode( $this->mDelimiter, $values );
		$values = array_map( "trim", $values );
		$values = array_unique( $values );
		$this->mValues = $values;
	}

	public function hasStaticValues() {
		return $this->mHasStaticValues;
	}

	private function setHasStaticValues( $StaticValues ) {
		$this->mHasStaticValues = $StaticValues;
	}
	
	/** Copied from ApiSemanticFormsSelectRequestProcessor */
	
	private function getFormattedValuesFrom( $sep, $values ) {

		if ( strpos( $values, $sep ) === false ) {
			return [ $values ];
		}

		$values = explode( $sep, $values );
		$values = array_map( "trim", $values );
		$values = array_unique( $values );

		// TODO: sorting here will destroy any sort defined in the query, e.g. in case sorting for labels (instead of mainlable)
		//sort( $values );
		// array_unshift( $values, "" ); Unshift no needed here

		return $values;
	}
}