summaryrefslogtreecommitdiff
path: root/vendor/oojs/oojs-ui/php/widgets/RadioSelectInputWidget.php
blob: 912c69172e7ca46e339423dd33350e62ba88736d (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
<?php

namespace OOUI;

/**
 * Multiple radio buttons input widget. Intended to be used within a OO.ui.FormLayout.
 */
class RadioSelectInputWidget extends InputWidget {

	/* Static Properties */

	public static $supportsSimpleLabel = false;

	/* Properties */

	/**
	 * @var string|null
	 */
	protected $name = null;

	/**
	 * @var FieldLayout[]
	 */
	protected $fields = array();

	/**
	 * @param array $config Configuration options
	 * @param array[] $config['options'] Array of menu options in the format
	 *   `array( 'data' => …, 'label' => … )`
	 */
	public function __construct( array $config = array() ) {
		// Parent constructor
		parent::__construct( $config );

		if ( isset( $config['name'] ) ) {
			$this->name = $config['name'];
		}

		// Initialization
		$this->setOptions( isset( $config['options'] ) ? $config['options'] : array() );
		$this->addClasses( array( 'oo-ui-radioSelectInputWidget' ) );
	}

	protected function getInputElement( $config ) {
		// Actually unused
		return new Tag( 'div' );
	}

	public function setValue( $value ) {
		$this->value = $this->cleanUpValue( $value );
		foreach ( $this->fields as &$field ) {
			$field->getField()->setSelected( $field->getField()->getValue() === $this->value );
		}
		return $this;
	}

	/**
	 * Set the options available for this input.
	 *
	 * @param array[] $options Array of menu options in the format
	 *   `array( 'data' => …, 'label' => … )`
	 * @chainable
	 */
	public function setOptions( $options ) {
		$value = $this->getValue();
		$isValueAvailable = false;
		$this->fields = array();

		// Rebuild the radio buttons
		$this->clearContent();
		// Need a unique name, otherwise more than one radio will be selectable
		$name = $this->name ?: 'oo-ui-radioSelectInputWidget' . mt_rand();
		foreach ( $options as $opt ) {
			$optValue = $this->cleanUpValue( $opt['data'] );
			$field = new FieldLayout(
				new RadioInputWidget( array(
					'name' => $name,
					'value' => $optValue,
					'disabled' => $this->isDisabled(),
				) ),
				array(
					'label' => isset( $opt['label'] ) ? $opt['label'] : $optValue,
					'align' => 'inline',
				)
			);

			if ( $value === $optValue ) {
				$isValueAvailable = true;
			}

			$this->fields[] = $field;
			$this->appendContent( $field );
		}

		// Restore the previous value, or reset to something sensible
		if ( $isValueAvailable ) {
			// Previous value is still available
			$this->setValue( $value );
		} else {
			// No longer valid, reset
			if ( count( $options ) ) {
				$this->setValue( $options[0]['data'] );
			}
		}

		return $this;
	}

	public function setDisabled( $state ) {
		parent::setDisabled( $state );
		foreach ( $this->fields as $field ) {
			$field->getField()->setDisabled( $this->isDisabled() );
		}
		return $this;
	}

	public function getConfig( &$config ) {
		$o = array();
		foreach ( $this->fields as $field ) {
			$label = $field->getLabel();
			$data = $field->getField()->getValue();
			$o[] = array( 'data' => $data, 'label' => $label );
		}
		$config['options'] = $o;
		return parent::getConfig( $config );
	}
}