Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 70
0.00% covered (danger)
0.00%
0 / 20
CRAP
0.00% covered (danger)
0.00%
0 / 1
PFFormInput
0.00% covered (danger)
0.00%
0 / 70
0.00% covered (danger)
0.00%
0 / 20
756
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getName
n/a
0 / 0
n/a
0 / 0
0
 getHandledPropertyTypes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getParameters
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
6
 updateFormInputJsFunctionData
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 getDefaultParameters
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHtmlText
n/a
0 / 0
n/a
0 / 0
0
 canHandleLists
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getJsInitFunctionData
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getJsValidationFunctionData
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getResourceModuleNames
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 addJsInitFunctionData
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 addJsValidationFunctionData
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDefaultPropTypes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDefaultPropTypeLists
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getOtherPropTypesHandled
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getOtherPropTypeListsHandled
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDefaultCargoTypes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDefaultCargoTypeLists
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getOtherCargoTypesHandled
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getOtherCargoTypeListsHandled
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 addJavaScript
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2/**
3 * The predecessor of this file held several subclasses of PFFormInput. The
4 * authors can not be sorted out with certainty anymore, thus are all listed
5 * here.
6 *
7 * @author Yaron Koren
8 * @author Jeffrey Stuckman
9 * @author Matt Williamson
10 * @author Patrick Nagel
11 * @author Sanyam Goyal
12 * @author Stephan Gambke
13 * @file
14 * @ingroup PF
15 */
16
17/**
18 * Parent class for all form input classes.
19 * @ingroup PFFormInput
20 */
21abstract class PFFormInput {
22
23    protected $mInputNumber;
24    protected $mCurrentValue;
25    protected $mInputName;
26    /**
27     * @deprecated, check for array_key_exists('mandatory', $this->mOtherArgs) instead
28     */
29    protected $mIsMandatory;
30    protected $mIsDisabled;
31    protected $mOtherArgs;
32
33    protected $mJsInitFunctionData = [];
34    protected $mJsValidationFunctionData = [];
35
36    /**
37     * @param string $input_number The number of the input in the form. For a simple HTML input
38     *  element this should end up in the id attribute in the format 'input_<number>'.
39     * @param string $cur_value The current value of the input field. For a simple HTML input
40     *  element this should end up in the value attribute.
41     * @param string $input_name The name of the input. For a simple HTML input element this should
42     *  end up in the name attribute.
43     * @param bool $disabled Is this input disabled?
44     * @param array $other_args An associative array of other parameters that were present in the
45     *  input definition.
46     */
47    public function __construct( $input_number, $cur_value, $input_name, $disabled, array $other_args ) {
48        $this->mInputNumber = $input_number;
49        $this->mCurrentValue = $cur_value;
50        $this->mInputName = $input_name;
51        $this->mOtherArgs = $other_args;
52        $this->mIsDisabled = $disabled;
53        $this->mIsMandatory = array_key_exists( 'mandatory', $other_args );
54    }
55
56    /**
57     * Returns the name of the input type this class handles.
58     *
59     * This is the name to be used in the field definition for the "input type"
60     * parameter. Used in PFFormPrinter::registerInputType().
61     *
62     * @return string The name of the input type this class handles.
63     */
64    abstract public static function getName();
65
66    /**
67     * Returns the set of SMW property types which this input can
68     * handle. See SMW's SMW_DataValueFactory.php
69     *
70     * @return string[]
71     */
72    public static function getHandledPropertyTypes() {
73        return null;
74    }
75
76    /**
77     * Returns the set of parameters for this form input.
78     * @return array[]
79     */
80    public static function getParameters() {
81        $params = [];
82        $params['mandatory'] = [
83            'name' => 'mandatory',
84            'type' => 'boolean',
85            'description' => wfMessage( 'pf_forminputs_mandatory' )->text()
86        ];
87        $params['restricted'] = [
88            'name' => 'restricted',
89            'type' => 'boolean',
90            'description' => wfMessage( 'pf_forminputs_restricted' )->text()
91        ];
92        $params['class'] = [
93            'name' => 'class',
94            'type' => 'string',
95            'description' => wfMessage( 'pf_forminputs_class' )->text()
96        ];
97        if ( defined( 'SMW_VERSION' ) ) {
98            $params['property'] = [
99                'name' => 'property',
100                'type' => 'string',
101                'description' => wfMessage( 'pf_forminputs_property' )->text()
102            ];
103        }
104        $params['default'] = [
105            'name' => 'default',
106            'type' => 'string',
107            'description' => wfMessage( 'pf_forminputs_default' )->text()
108        ];
109        return $params;
110    }
111
112    /**
113     * @param string $key
114     * @param array &$configVars
115     * @param array $functionData
116     * @param string $input_id
117     * @return array
118     */
119    private static function updateFormInputJsFunctionData( $key, &$configVars, $functionData, $input_id ) {
120        if ( array_key_exists( $key, $configVars ) ) {
121            $functionDataArray = $configVars[ $key ];
122        } else {
123            $functionDataArray = [];
124        }
125        $functionDataArray[ $input_id ] = $functionData;
126        return $functionDataArray;
127    }
128
129    /**
130     * Return an array of the default parameters for this input where the
131     * parameter name is the key while the parameter value is the value.
132     *
133     * @return string[]
134     */
135    public function getDefaultParameters() {
136        return null;
137    }
138
139    /**
140     * Returns the HTML code to be included in the output page for this input.
141     *
142     * Ideally this HTML code should provide a basic functionality even if the
143     * browser is not JavaScript capable. I.e. even without JavaScript the user
144     * should be able to input values.
145     * @return string
146     */
147    abstract public function getHtmlText();
148
149    /**
150     *
151     * @return bool True, if this input type can handle lists
152     */
153    public static function canHandleLists() {
154        return false;
155    }
156
157    /**
158     * Returns the name and parameters for the initialization JavaScript
159     * function for this input type, if any.
160     *
161     * This function is not used yet.
162     * @return array[]
163     */
164    public function getJsInitFunctionData() {
165        return $this->mJsInitFunctionData;
166    }
167
168    /**
169     * Returns the name and parameters for the validation JavaScript
170     * functions for this input type, if any.
171     *
172     * This function is not used yet.
173     * @return array[]
174     */
175    public function getJsValidationFunctionData() {
176        return $this->mJsValidationFunctionData;
177    }
178
179    /**
180     * Returns the names of the resource modules this input type uses.
181     *
182     * Returns the names of the modules as an array or - if there is only one
183     * module - as a string.
184     *
185     * @return null|string|array
186     */
187    public function getResourceModuleNames() {
188        return null;
189    }
190
191    /**
192     * For each input type one or more JavaScript initialization functions may
193     * be specified.
194     *
195     * <b>This function is not used yet.</b>
196     *
197     * They are called to initialize the input after the page html has loaded
198     * (or for "multiple" templates after the page fragment has loaded).
199     *
200     * The JavaScript function specified here must be in the top level scope of
201     * the document. When it is called it will get the input's id attribute as
202     * the first parameter and the specified param as the second.
203     *
204     *
205     * Examples:
206     *
207     * Adding initFoo like this: <code>addJsInitFunctionData( "initFoo", "'bar'" );</code> will result in this JavaScript call: <code>initFoo( inputID, 'bar' );</code>.
208     *
209     * Adding initFoo like this: <code>addJsInitFunctionData( "initFoo", "array('bar', 'baz'" );</code> will result in this JavaScript call: <code>initFoo( inputID, array('bar', 'baz') );</code>.
210     *
211     *
212     * @param string $name The name of the initialization function.
213     * @param string|array|null $param The parameter passed to the initialization function.
214     */
215    public function addJsInitFunctionData( $name, $param = null ) {
216        if ( is_string( $param ) ) {
217            $param = json_decode( $param );
218        }
219        $this->mJsInitFunctionData[] = [ 'name' => $name, 'param' => $param ];
220    }
221
222    /**
223     * For each input type one or more JavaScript validation functions may
224     * be specified.
225     *
226     * <b>Not used yet.</b>
227     *
228     * They are called to validate the input before the form is submitted for
229     * saving or preview.
230     *
231     * The JavaScript function specified here must be in the top level scope of
232     * the document. When it is called it will get the input's id attribute as
233     * the first parameter and the specified param as the second.
234     *
235     *
236     * Examples:
237     *
238     * Adding validateFoo like this: <code>addJsValidationFunctionData( "initFoo", "'bar'" );</code> will result in this JavaScript call: <code>validateFoo( inputID, 'bar' );</code>.
239     *
240     * Adding validateFoo like this: <code>addJsValidationFunctionData( "initFoo", "array('bar', 'baz'" );</code> will result in this JavaScript call: <code>validateFoo( inputID, array('bar', 'baz') );</code>.
241     *
242     *
243     * @param string $name The name of the initialization function.
244     * @param string|array $param The parameter passed to the initialization function.
245     */
246    public function addJsValidationFunctionData( $name, $param = 'null' ) {
247        $this->mJsValidationFunctionData[] = [ 'name' => $name, 'param' => $param ];
248    }
249
250    /**
251     * Returns the set of SMW property types for which this input is
252     * meant to be the default one - ideally, no more than one input
253     * should declare itself the default for any specific type.
254     *
255     * @deprecated
256     * @return array[] key is the property type, value is an array of
257     *  default args to be used for this input
258     */
259    public static function getDefaultPropTypes() {
260        return [];
261    }
262
263    /**
264     * Returns the set of SMW property types for which this input is
265     * meant to be the default one - ideally, no more than one input
266     * should declare itself the default for any specific type.
267     *
268     * @deprecated
269     * @return array[] key is the property type, value is an array of
270     *  default args to be used for this input
271     */
272    public static function getDefaultPropTypeLists() {
273        return [];
274    }
275
276    /**
277     * Returns the set of SMW property types which this input can
278     * handle, but for which it isn't the default input.
279     *
280     * @deprecated
281     * @return string[]
282     */
283    public static function getOtherPropTypesHandled() {
284        return [];
285    }
286
287    /**
288     * Returns the set of SMW property types which this input can
289     * handle, but for which it isn't the default input.
290     *
291     * @deprecated
292     * @return string[]
293     */
294    public static function getOtherPropTypeListsHandled() {
295        return [];
296    }
297
298    public static function getDefaultCargoTypes() {
299        return [];
300    }
301
302    public static function getDefaultCargoTypeLists() {
303        return [];
304    }
305
306    public static function getOtherCargoTypesHandled() {
307        return [];
308    }
309
310    public static function getOtherCargoTypeListsHandled() {
311        return [];
312    }
313
314    /**
315     * Add the necessary JavaScript for this input.
316     */
317    public function addJavaScript() {
318        global $wgOut;
319
320        // @TODO - the first works better for Special:RunQuery, and the
321        // second better for Special:FormEdit? Try to find some solution
322        // that always works correctly.
323        // $output = $wgParser->getOutput();
324        $output = $wgOut;
325        $modules = $this->getResourceModuleNames();
326
327        // Register modules for the input.
328        if ( $modules !== null ) {
329            $output->addModules( $modules );
330        }
331
332        if ( $this->getJsInitFunctionData() || $this->getJsValidationFunctionData() ) {
333            $input_id = $this->mInputName == 'pf_free_text' ? 'pf_free_text' : 'input_' . $this->mInputNumber;
334            $configVars = $output->getJsConfigVars();
335
336            $initFunctionData = self::updateFormInputJsFunctionData( 'ext.pf.initFunctionData', $configVars, $this->getJsInitFunctionData(), $input_id );
337            $validationFunctionData = self::updateFormInputJsFunctionData( 'ext.pf.validationFunctionData', $configVars, $this->getJsValidationFunctionData(), $input_id );
338
339            $output->addJsConfigVars( [
340                'ext.pf.initFunctionData' => $initFunctionData,
341                'ext.pf.validationFunctionData' => $validationFunctionData
342            ] );
343        }
344    }
345
346}