204020d780488f505fdd639381bfda056819f092
[lhc/web/wiklou.git] / includes / htmlform / HTMLFormField.php
1 <?php
2
3 /**
4 * The parent class to generate form fields. Any field type should
5 * be a subclass of this.
6 */
7 abstract class HTMLFormField {
8 public $mParams;
9
10 protected $mValidationCallback;
11 protected $mFilterCallback;
12 protected $mName;
13 protected $mLabel; # String label. Set on construction
14 protected $mID;
15 protected $mClass = '';
16 protected $mDefault;
17 protected $mOptions = false;
18 protected $mOptionsLabelsNotFromMessage = false;
19 protected $mHideIf = null;
20
21 /**
22 * @var bool If true will generate an empty div element with no label
23 * @since 1.22
24 */
25 protected $mShowEmptyLabels = true;
26
27 /**
28 * @var HTMLForm
29 */
30 public $mParent;
31
32 /**
33 * This function must be implemented to return the HTML to generate
34 * the input object itself. It should not implement the surrounding
35 * table cells/rows, or labels/help messages.
36 *
37 * @param string $value the value to set the input to; eg a default
38 * text for a text input.
39 *
40 * @return string Valid HTML.
41 */
42 abstract function getInputHTML( $value );
43
44 /**
45 * Get a translated interface message
46 *
47 * This is a wrapper around $this->mParent->msg() if $this->mParent is set
48 * and wfMessage() otherwise.
49 *
50 * Parameters are the same as wfMessage().
51 *
52 * @return Message
53 */
54 function msg() {
55 $args = func_get_args();
56
57 if ( $this->mParent ) {
58 $callback = array( $this->mParent, 'msg' );
59 } else {
60 $callback = 'wfMessage';
61 }
62
63 return call_user_func_array( $callback, $args );
64 }
65
66
67 /**
68 * Fetch a field value from $alldata for the closest field matching a given
69 * name.
70 *
71 * This is complex because it needs to handle array fields like the user
72 * would expect. The general algorithm is to look for $name as a sibling
73 * of $this, then a sibling of $this's parent, and so on. Keeping in mind
74 * that $name itself might be referencing an array.
75 *
76 * @param array $alldata
77 * @param string $name
78 * @return string
79 */
80 protected function getNearestFieldByName( $alldata, $name ) {
81 $tmp = $this->mName;
82 $thisKeys = array();
83 while ( preg_match( '/^(.+)\[([^\]]+)\]$/', $tmp, $m ) ) {
84 array_unshift( $thisKeys, $m[2] );
85 $tmp = $m[1];
86 }
87 if ( substr( $tmp, 0, 2 ) == 'wp' &&
88 !isset( $alldata[$tmp] ) &&
89 isset( $alldata[substr( $tmp, 2 )] )
90 ) {
91 // Adjust for name mangling.
92 $tmp = substr( $tmp, 2 );
93 }
94 array_unshift( $thisKeys, $tmp );
95
96 $tmp = $name;
97 $nameKeys = array();
98 while ( preg_match( '/^(.+)\[([^\]]+)\]$/', $tmp, $m ) ) {
99 array_unshift( $nameKeys, $m[2] );
100 $tmp = $m[1];
101 }
102 array_unshift( $nameKeys, $tmp );
103
104 $testValue = '';
105 for ( $i = count( $thisKeys ) - 1; $i >= 0; $i-- ) {
106 $keys = array_merge( array_slice( $thisKeys, 0, $i ), $nameKeys );
107 $data = $alldata;
108 while ( $keys ) {
109 $key = array_shift( $keys );
110 if ( !is_array( $data ) || !isset( $data[$key] ) ) {
111 continue 2;
112 }
113 $data = $data[$key];
114 }
115 $testValue = $data;
116 break;
117 }
118
119 return $testValue;
120 }
121
122 /**
123 * Helper function for isHidden to handle recursive data structures.
124 *
125 * @param array $alldata
126 * @param array $params
127 * @return boolean
128 */
129 protected function isHiddenRecurse( array $alldata, array $params ) {
130 $origParams = $params;
131 $op = array_shift( $params );
132
133 try {
134 switch ( $op ) {
135 case 'AND':
136 foreach ( $params as $i => $p ) {
137 if ( !is_array( $p ) ) {
138 throw new MWException(
139 "Expected array, found " . gettype( $p ) . " at index $i"
140 );
141 }
142 if ( !$this->isHiddenRecurse( $alldata, $p ) ) {
143 return false;
144 }
145 }
146 return true;
147
148 case 'OR':
149 foreach ( $params as $p ) {
150 if ( !is_array( $p ) ) {
151 throw new MWException(
152 "Expected array, found " . gettype( $p ) . " at index $i"
153 );
154 }
155 if ( $this->isHiddenRecurse( $alldata, $p ) ) {
156 return true;
157 }
158 }
159 return false;
160
161 case 'NAND':
162 foreach ( $params as $i => $p ) {
163 if ( !is_array( $p ) ) {
164 throw new MWException(
165 "Expected array, found " . gettype( $p ) . " at index $i"
166 );
167 }
168 if ( !$this->isHiddenRecurse( $alldata, $p ) ) {
169 return true;
170 }
171 }
172 return false;
173
174 case 'NOR':
175 foreach ( $params as $p ) {
176 if ( !is_array( $p ) ) {
177 throw new MWException(
178 "Expected array, found " . gettype( $p ) . " at index $i"
179 );
180 }
181 if ( $this->isHiddenRecurse( $alldata, $p ) ) {
182 return false;
183 }
184 }
185 return true;
186
187 case 'NOT':
188 if ( count( $params ) !== 1 ) {
189 throw new MWException( "NOT takes exactly one parameter" );
190 }
191 $p = $params[0];
192 if ( !is_array( $p ) ) {
193 throw new MWException(
194 "Expected array, found " . gettype( $p ) . " at index 0"
195 );
196 }
197 return !$this->isHiddenRecurse( $alldata, $p );
198
199 case '===':
200 case '!==':
201 if ( count( $params ) !== 2 ) {
202 throw new MWException( "$op takes exactly two parameters" );
203 }
204 list( $field, $value ) = $params;
205 if ( !is_string( $field ) || !is_string( $value ) ) {
206 throw new MWException( "Parameters for $op must be strings" );
207 }
208 $testValue = $this->getNearestFieldByName( $alldata, $field );
209 switch ( $op ) {
210 case '===':
211 return ( $value === $testValue );
212 case '!==':
213 return ( $value !== $testValue );
214 }
215
216 default:
217 throw new MWException( "Unknown operation" );
218 }
219 } catch ( MWException $ex ) {
220 throw new MWException(
221 "Invalid hide-if specification for $this->mName: " .
222 $ex->getMessage() . " in " . var_export( $origParams, true ),
223 0, $ex
224 );
225 }
226 }
227
228 /**
229 * Test whether this field is supposed to be hidden, based on the values of
230 * the other form fields.
231 *
232 * @since 1.23
233 * @param array $alldata The data collected from the form
234 * @return bool
235 */
236 function isHidden( $alldata ) {
237 if ( !$this->mHideIf ) {
238 return false;
239 }
240
241 return $this->isHiddenRecurse( $alldata, $this->mHideIf );
242 }
243
244 /**
245 * Override this function to add specific validation checks on the
246 * field input. Don't forget to call parent::validate() to ensure
247 * that the user-defined callback mValidationCallback is still run
248 *
249 * @param string $value The value the field was submitted with
250 * @param array $alldata The data collected from the form
251 *
252 * @return bool|string true on success, or String error to display.
253 */
254 function validate( $value, $alldata ) {
255 if ( $this->isHidden( $alldata ) ) {
256 return true;
257 }
258
259 if ( isset( $this->mParams['required'] )
260 && $this->mParams['required'] !== false
261 && $value === ''
262 ) {
263 return $this->msg( 'htmlform-required' )->parse();
264 }
265
266 if ( isset( $this->mValidationCallback ) ) {
267 return call_user_func( $this->mValidationCallback, $value, $alldata, $this->mParent );
268 }
269
270 return true;
271 }
272
273 function filter( $value, $alldata ) {
274 if ( isset( $this->mFilterCallback ) ) {
275 $value = call_user_func( $this->mFilterCallback, $value, $alldata, $this->mParent );
276 }
277
278 return $value;
279 }
280
281 /**
282 * Should this field have a label, or is there no input element with the
283 * appropriate id for the label to point to?
284 *
285 * @return bool True to output a label, false to suppress
286 */
287 protected function needsLabel() {
288 return true;
289 }
290
291 /**
292 * Tell the field whether to generate a separate label element if its label
293 * is blank.
294 *
295 * @since 1.22
296 *
297 * @param bool $show Set to false to not generate a label.
298 * @return void
299 */
300 public function setShowEmptyLabel( $show ) {
301 $this->mShowEmptyLabels = $show;
302 }
303
304 /**
305 * Get the value that this input has been set to from a posted form,
306 * or the input's default value if it has not been set.
307 *
308 * @param WebRequest $request
309 * @return string The value
310 */
311 function loadDataFromRequest( $request ) {
312 if ( $request->getCheck( $this->mName ) ) {
313 return $request->getText( $this->mName );
314 } else {
315 return $this->getDefault();
316 }
317 }
318
319 /**
320 * Initialise the object
321 *
322 * @param array $params Associative Array. See HTMLForm doc for syntax.
323 *
324 * @since 1.22 The 'label' attribute no longer accepts raw HTML, use 'label-raw' instead
325 * @throws MWException
326 */
327 function __construct( $params ) {
328 $this->mParams = $params;
329
330 # Generate the label from a message, if possible
331 if ( isset( $params['label-message'] ) ) {
332 $msgInfo = $params['label-message'];
333
334 if ( is_array( $msgInfo ) ) {
335 $msg = array_shift( $msgInfo );
336 } else {
337 $msg = $msgInfo;
338 $msgInfo = array();
339 }
340
341 $this->mLabel = wfMessage( $msg, $msgInfo )->parse();
342 } elseif ( isset( $params['label'] ) ) {
343 if ( $params['label'] === '&#160;' ) {
344 // Apparently some things set &nbsp directly and in an odd format
345 $this->mLabel = '&#160;';
346 } else {
347 $this->mLabel = htmlspecialchars( $params['label'] );
348 }
349 } elseif ( isset( $params['label-raw'] ) ) {
350 $this->mLabel = $params['label-raw'];
351 }
352
353 $this->mName = "wp{$params['fieldname']}";
354 if ( isset( $params['name'] ) ) {
355 $this->mName = $params['name'];
356 }
357
358 $validName = Sanitizer::escapeId( $this->mName );
359 if ( $this->mName != $validName && !isset( $params['nodata'] ) ) {
360 throw new MWException( "Invalid name '{$this->mName}' passed to " . __METHOD__ );
361 }
362
363 $this->mID = "mw-input-{$this->mName}";
364
365 if ( isset( $params['default'] ) ) {
366 $this->mDefault = $params['default'];
367 }
368
369 if ( isset( $params['id'] ) ) {
370 $id = $params['id'];
371 $validId = Sanitizer::escapeId( $id );
372
373 if ( $id != $validId ) {
374 throw new MWException( "Invalid id '$id' passed to " . __METHOD__ );
375 }
376
377 $this->mID = $id;
378 }
379
380 if ( isset( $params['cssclass'] ) ) {
381 $this->mClass = $params['cssclass'];
382 }
383
384 if ( isset( $params['validation-callback'] ) ) {
385 $this->mValidationCallback = $params['validation-callback'];
386 }
387
388 if ( isset( $params['filter-callback'] ) ) {
389 $this->mFilterCallback = $params['filter-callback'];
390 }
391
392 if ( isset( $params['flatlist'] ) ) {
393 $this->mClass .= ' mw-htmlform-flatlist';
394 }
395
396 if ( isset( $params['hidelabel'] ) ) {
397 $this->mShowEmptyLabels = false;
398 }
399
400 if ( isset( $params['hide-if'] ) ) {
401 $this->mHideIf = $params['hide-if'];
402 }
403 }
404
405 /**
406 * Get the complete table row for the input, including help text,
407 * labels, and whatever.
408 *
409 * @param string $value The value to set the input to.
410 *
411 * @return string Complete HTML table row.
412 */
413 function getTableRow( $value ) {
414 list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
415 $inputHtml = $this->getInputHTML( $value );
416 $fieldType = get_class( $this );
417 $helptext = $this->getHelpTextHtmlTable( $this->getHelpText() );
418 $cellAttributes = array();
419 $rowAttributes = array();
420 $rowClasses = '';
421
422 if ( !empty( $this->mParams['vertical-label'] ) ) {
423 $cellAttributes['colspan'] = 2;
424 $verticalLabel = true;
425 } else {
426 $verticalLabel = false;
427 }
428
429 $label = $this->getLabelHtml( $cellAttributes );
430
431 $field = Html::rawElement(
432 'td',
433 array( 'class' => 'mw-input' ) + $cellAttributes,
434 $inputHtml . "\n$errors"
435 );
436
437 if ( $this->mHideIf ) {
438 $rowAttributes['data-hide-if'] = FormatJson::encode( $this->mHideIf );
439 $rowClasses .= ' mw-htmlform-hide-if';
440 }
441
442 if ( $verticalLabel ) {
443 $html = Html::rawElement( 'tr',
444 $rowAttributes + array( 'class' => "mw-htmlform-vertical-label $rowClasses" ), $label );
445 $html .= Html::rawElement( 'tr',
446 $rowAttributes + array(
447 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
448 ),
449 $field );
450 } else {
451 $html =
452 Html::rawElement( 'tr',
453 $rowAttributes + array(
454 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
455 ),
456 $label . $field );
457 }
458
459 return $html . $helptext;
460 }
461
462 /**
463 * Get the complete div for the input, including help text,
464 * labels, and whatever.
465 * @since 1.20
466 *
467 * @param string $value The value to set the input to.
468 *
469 * @return string Complete HTML table row.
470 */
471 public function getDiv( $value ) {
472 list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
473 $inputHtml = $this->getInputHTML( $value );
474 $fieldType = get_class( $this );
475 $helptext = $this->getHelpTextHtmlDiv( $this->getHelpText() );
476 $cellAttributes = array();
477 $label = $this->getLabelHtml( $cellAttributes );
478
479 $outerDivClass = array(
480 'mw-input',
481 'mw-htmlform-nolabel' => ( $label === '' )
482 );
483
484 $field = Html::rawElement(
485 'div',
486 array( 'class' => $outerDivClass ) + $cellAttributes,
487 $inputHtml . "\n$errors"
488 );
489 $divCssClasses = array( "mw-htmlform-field-$fieldType", $this->mClass, $errorClass );
490 if ( $this->mParent->isVForm() ) {
491 $divCssClasses[] = 'mw-ui-vform-div';
492 }
493
494 $wrapperAttributes = array(
495 'class' => $divCssClasses,
496 );
497 if ( $this->mHideIf ) {
498 $wrapperAttributes['data-hide-if'] = FormatJson::encode( $this->mHideIf );
499 $wrapperAttributes['class'][] = ' mw-htmlform-hide-if';
500 }
501 $html = Html::rawElement( 'div', $wrapperAttributes, $label . $field );
502 $html .= $helptext;
503
504 return $html;
505 }
506
507 /**
508 * Get the complete raw fields for the input, including help text,
509 * labels, and whatever.
510 * @since 1.20
511 *
512 * @param string $value The value to set the input to.
513 *
514 * @return string Complete HTML table row.
515 */
516 public function getRaw( $value ) {
517 list( $errors, ) = $this->getErrorsAndErrorClass( $value );
518 $inputHtml = $this->getInputHTML( $value );
519 $helptext = $this->getHelpTextHtmlRaw( $this->getHelpText() );
520 $cellAttributes = array();
521 $label = $this->getLabelHtml( $cellAttributes );
522
523 $html = "\n$errors";
524 $html .= $label;
525 $html .= $inputHtml;
526 $html .= $helptext;
527
528 return $html;
529 }
530
531 /**
532 * Generate help text HTML in table format
533 * @since 1.20
534 *
535 * @param string|null $helptext
536 * @return string
537 */
538 public function getHelpTextHtmlTable( $helptext ) {
539 if ( is_null( $helptext ) ) {
540 return '';
541 }
542
543 $rowAttributes = array();
544 if ( $this->mHideIf ) {
545 $rowAttributes['data-hide-if'] = FormatJson::encode( $this->mHideIf );
546 $rowAttributes['class'] = 'mw-htmlform-hide-if';
547 }
548
549 $row = Html::rawElement( 'td', array( 'colspan' => 2, 'class' => 'htmlform-tip' ), $helptext );
550 $row = Html::rawElement( 'tr', $rowAttributes, $row );
551
552 return $row;
553 }
554
555 /**
556 * Generate help text HTML in div format
557 * @since 1.20
558 *
559 * @param string|null $helptext
560 *
561 * @return string
562 */
563 public function getHelpTextHtmlDiv( $helptext ) {
564 if ( is_null( $helptext ) ) {
565 return '';
566 }
567
568 $wrapperAttributes = array(
569 'class' => 'htmlform-tip',
570 );
571 if ( $this->mHideIf ) {
572 $wrapperAttributes['data-hide-if'] = FormatJson::encode( $this->mHideIf );
573 $wrapperAttributes['class'] .= ' mw-htmlform-hide-if';
574 }
575 $div = Html::rawElement( 'div', $wrapperAttributes, $helptext );
576
577 return $div;
578 }
579
580 /**
581 * Generate help text HTML formatted for raw output
582 * @since 1.20
583 *
584 * @param string|null $helptext
585 * @return string
586 */
587 public function getHelpTextHtmlRaw( $helptext ) {
588 return $this->getHelpTextHtmlDiv( $helptext );
589 }
590
591 /**
592 * Determine the help text to display
593 * @since 1.20
594 * @return string
595 */
596 public function getHelpText() {
597 $helptext = null;
598
599 if ( isset( $this->mParams['help-message'] ) ) {
600 $this->mParams['help-messages'] = array( $this->mParams['help-message'] );
601 }
602
603 if ( isset( $this->mParams['help-messages'] ) ) {
604 foreach ( $this->mParams['help-messages'] as $name ) {
605 $helpMessage = (array)$name;
606 $msg = $this->msg( array_shift( $helpMessage ), $helpMessage );
607
608 if ( $msg->exists() ) {
609 if ( is_null( $helptext ) ) {
610 $helptext = '';
611 } else {
612 $helptext .= $this->msg( 'word-separator' )->escaped(); // some space
613 }
614 $helptext .= $msg->parse(); // Append message
615 }
616 }
617 } elseif ( isset( $this->mParams['help'] ) ) {
618 $helptext = $this->mParams['help'];
619 }
620
621 return $helptext;
622 }
623
624 /**
625 * Determine form errors to display and their classes
626 * @since 1.20
627 *
628 * @param string $value The value of the input
629 * @return array
630 */
631 public function getErrorsAndErrorClass( $value ) {
632 $errors = $this->validate( $value, $this->mParent->mFieldData );
633
634 if ( $errors === true ||
635 ( !$this->mParent->getRequest()->wasPosted() && $this->mParent->getMethod() === 'post' )
636 ) {
637 $errors = '';
638 $errorClass = '';
639 } else {
640 $errors = self::formatErrors( $errors );
641 $errorClass = 'mw-htmlform-invalid-input';
642 }
643
644 return array( $errors, $errorClass );
645 }
646
647 function getLabel() {
648 return is_null( $this->mLabel ) ? '' : $this->mLabel;
649 }
650
651 function getLabelHtml( $cellAttributes = array() ) {
652 # Don't output a for= attribute for labels with no associated input.
653 # Kind of hacky here, possibly we don't want these to be <label>s at all.
654 $for = array();
655
656 if ( $this->needsLabel() ) {
657 $for['for'] = $this->mID;
658 }
659
660 $labelValue = trim( $this->getLabel() );
661 $hasLabel = false;
662 if ( $labelValue !== '&#160;' && $labelValue !== '' ) {
663 $hasLabel = true;
664 }
665
666 $displayFormat = $this->mParent->getDisplayFormat();
667 $html = '';
668
669 if ( $displayFormat === 'table' ) {
670 $html =
671 Html::rawElement( 'td',
672 array( 'class' => 'mw-label' ) + $cellAttributes,
673 Html::rawElement( 'label', $for, $labelValue ) );
674 } elseif ( $hasLabel || $this->mShowEmptyLabels ) {
675 if ( $displayFormat === 'div' ) {
676 $html =
677 Html::rawElement( 'div',
678 array( 'class' => 'mw-label' ) + $cellAttributes,
679 Html::rawElement( 'label', $for, $labelValue ) );
680 } else {
681 $html = Html::rawElement( 'label', $for, $labelValue );
682 }
683 }
684
685 return $html;
686 }
687
688 function getDefault() {
689 if ( isset( $this->mDefault ) ) {
690 return $this->mDefault;
691 } else {
692 return null;
693 }
694 }
695
696 /**
697 * Returns the attributes required for the tooltip and accesskey.
698 *
699 * @return array Attributes
700 */
701 public function getTooltipAndAccessKey() {
702 if ( empty( $this->mParams['tooltip'] ) ) {
703 return array();
704 }
705
706 return Linker::tooltipAndAccesskeyAttribs( $this->mParams['tooltip'] );
707 }
708
709 /**
710 * Returns the given attributes from the parameters
711 *
712 * @param array $list List of attributes to get
713 * @return array Attributes
714 */
715 public function getAttributes( array $list ) {
716 static $boolAttribs = array( 'disabled', 'required', 'autofocus', 'multiple', 'readonly' );
717
718 $ret = array();
719
720 foreach ( $list as $key ) {
721 if ( in_array( $key, $boolAttribs ) ) {
722 if ( !empty( $this->mParams[$key] ) ) {
723 $ret[$key] = '';
724 }
725 } elseif ( isset( $this->mParams[$key] ) ) {
726 $ret[$key] = $this->mParams[$key];
727 }
728 }
729
730 return $ret;
731 }
732
733 /**
734 * Given an array of msg-key => value mappings, returns an array with keys
735 * being the message texts. It also forces values to strings.
736 *
737 * @param array $options
738 * @return array
739 */
740 private function lookupOptionsKeys( $options ) {
741 $ret = array();
742 foreach ( $options as $key => $value ) {
743 $key = $this->msg( $key )->plain();
744 $ret[$key] = is_array( $value )
745 ? $this->lookupOptionsKeys( $value )
746 : strval( $value );
747 }
748 return $ret;
749 }
750
751 /**
752 * Recursively forces values in an array to strings, because issues arise
753 * with integer 0 as a value.
754 *
755 * @param array $array
756 * @return array
757 */
758 static function forceToStringRecursive( $array ) {
759 if ( is_array( $array ) ) {
760 return array_map( array( __CLASS__, 'forceToStringRecursive' ), $array );
761 } else {
762 return strval( $array );
763 }
764 }
765
766 /**
767 * Fetch the array of options from the field's parameters. In order, this
768 * checks 'options-messages', 'options', then 'options-message'.
769 *
770 * @return array|null Options array
771 */
772 public function getOptions() {
773 if ( $this->mOptions === false ) {
774 if ( array_key_exists( 'options-messages', $this->mParams ) ) {
775 $this->mOptions = $this->lookupOptionsKeys( $this->mParams['options-messages'] );
776 } elseif ( array_key_exists( 'options', $this->mParams ) ) {
777 $this->mOptionsLabelsNotFromMessage = true;
778 $this->mOptions = self::forceToStringRecursive( $this->mParams['options'] );
779 } elseif ( array_key_exists( 'options-message', $this->mParams ) ) {
780 /** @todo This is copied from Xml::listDropDown(), deprecate/avoid duplication? */
781 $message = $this->msg( $this->mParams['options-message'] )->inContentLanguage()->plain();
782
783 $optgroup = false;
784 $this->mOptions = array();
785 foreach ( explode( "\n", $message ) as $option ) {
786 $value = trim( $option );
787 if ( $value == '' ) {
788 continue;
789 } elseif ( substr( $value, 0, 1 ) == '*' && substr( $value, 1, 1 ) != '*' ) {
790 # A new group is starting...
791 $value = trim( substr( $value, 1 ) );
792 $optgroup = $value;
793 } elseif ( substr( $value, 0, 2 ) == '**' ) {
794 # groupmember
795 $opt = trim( substr( $value, 2 ) );
796 if ( $optgroup === false ) {
797 $this->mOptions[$opt] = $opt;
798 } else {
799 $this->mOptions[$optgroup][$opt] = $opt;
800 }
801 } else {
802 # groupless reason list
803 $optgroup = false;
804 $this->mOptions[$option] = $option;
805 }
806 }
807 } else {
808 $this->mOptions = null;
809 }
810 }
811
812 return $this->mOptions;
813 }
814
815 /**
816 * flatten an array of options to a single array, for instance,
817 * a set of "<options>" inside "<optgroups>".
818 *
819 * @param array $options Associative Array with values either Strings or Arrays
820 * @return array Flattened input
821 */
822 public static function flattenOptions( $options ) {
823 $flatOpts = array();
824
825 foreach ( $options as $value ) {
826 if ( is_array( $value ) ) {
827 $flatOpts = array_merge( $flatOpts, self::flattenOptions( $value ) );
828 } else {
829 $flatOpts[] = $value;
830 }
831 }
832
833 return $flatOpts;
834 }
835
836 /**
837 * Formats one or more errors as accepted by field validation-callback.
838 *
839 * @param string|Message|array $errors Array of strings or Message instances
840 * @return string HTML
841 * @since 1.18
842 */
843 protected static function formatErrors( $errors ) {
844 if ( is_array( $errors ) && count( $errors ) === 1 ) {
845 $errors = array_shift( $errors );
846 }
847
848 if ( is_array( $errors ) ) {
849 $lines = array();
850 foreach ( $errors as $error ) {
851 if ( $error instanceof Message ) {
852 $lines[] = Html::rawElement( 'li', array(), $error->parse() );
853 } else {
854 $lines[] = Html::rawElement( 'li', array(), $error );
855 }
856 }
857
858 return Html::rawElement( 'ul', array( 'class' => 'error' ), implode( "\n", $lines ) );
859 } else {
860 if ( $errors instanceof Message ) {
861 $errors = $errors->parse();
862 }
863
864 return Html::rawElement( 'span', array( 'class' => 'error' ), $errors );
865 }
866 }
867 }