]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/oojs/oojs-ui/php/widgets/CheckboxMultiselectInputWidget.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / oojs / oojs-ui / php / widgets / CheckboxMultiselectInputWidget.php
1 <?php
2
3 namespace OOUI;
4
5 /**
6  * Multiple checkbox input widget. Intended to be used within a OO.ui.FormLayout.
7  */
8 class CheckboxMultiselectInputWidget extends InputWidget {
9
10         /* Properties */
11
12         /**
13          * @var string|null
14          */
15         protected $name = null;
16
17         /**
18          * Input value.
19          *
20          * @var string[]
21          */
22         protected $value = [];
23
24         /**
25          * Layouts for this input, as FieldLayouts.
26          *
27          * @var array
28          */
29         protected $fields = [];
30
31         /**
32          * @param array $config Configuration options
33          * @param array[] $config['options'] Array of menu options in the format
34          *   `[ 'data' => …, 'label' => …, 'disabled' => … ]`
35          */
36         public function __construct( array $config = [] ) {
37                 // Parent constructor
38                 parent::__construct( $config );
39
40                 if ( isset( $config['name'] ) ) {
41                         $this->name = $config['name'];
42                 }
43
44                 // Initialization
45                 $this->setOptions( isset( $config['options'] ) ? $config['options'] : [] );
46                 // Have to repeat this from parent, as we need options to be set up for this to make sense
47                 $this->setValue( isset( $config['value'] ) ? $config['value'] : null );
48                 $this->addClasses( [ 'oo-ui-checkboxMultiselectInputWidget' ] );
49         }
50
51         protected function getInputElement( $config ) {
52                 // Actually unused
53                 return new Tag( 'unused' );
54         }
55
56         /**
57          * Set the value of the input.
58          *
59          * @param string[] $value New value
60          * @return $this
61          */
62         public function setValue( $value ) {
63                 $this->value = $this->cleanUpValue( $value );
64                 // Deselect all options
65                 foreach ( $this->fields as $field ) {
66                         $field->getField()->setSelected( false );
67                 }
68                 // Select the requested ones
69                 foreach ( $this->value as $key ) {
70                         $this->fields[ $key ]->getField()->setSelected( true );
71                 }
72                 return $this;
73         }
74
75         /**
76          * Clean up incoming value.
77          *
78          * @param string[] $value Original value
79          * @return string[] Cleaned up value
80          */
81         protected function cleanUpValue( $value ) {
82                 $cleanValue = [];
83                 if ( !is_array( $value ) ) {
84                         return $cleanValue;
85                 }
86                 foreach ( $value as $singleValue ) {
87                         $singleValue = parent::cleanUpValue( $singleValue );
88                         // Remove options that we don't have here
89                         if ( !isset( $this->fields[ $singleValue ] ) ) {
90                                 continue;
91                         }
92                         $cleanValue[] = $singleValue;
93                 }
94                 return $cleanValue;
95         }
96
97         /**
98          * Set the options available for this input.
99          *
100          * @param array[] $options Array of menu options in the format
101          *   `[ 'data' => …, 'label' => …, 'disabled' => … ]`
102          * @return $this
103          */
104         public function setOptions( $options ) {
105                 $this->fields = [];
106
107                 // Rebuild the checkboxes
108                 $this->clearContent();
109                 $name = $this->name;
110                 foreach ( $options as $opt ) {
111                         $optValue = parent::cleanUpValue( $opt['data'] );
112                         $optDisabled = isset( $opt['disabled'] ) ? $opt['disabled'] : false;
113                         $field = new FieldLayout(
114                                 new CheckboxInputWidget( [
115                                         'name' => $name,
116                                         'value' => $optValue,
117                                         'disabled' => $this->isDisabled() || $optDisabled,
118                                 ] ),
119                                 [
120                                         'label' => isset( $opt['label'] ) ? $opt['label'] : $optValue,
121                                         'align' => 'inline',
122                                 ]
123                         );
124
125                         $this->fields[ $optValue ] = $field;
126                         $this->appendContent( $field );
127                 }
128
129                 // Re-set the value, checking the checkboxes as needed.
130                 // This will also get rid of any stale options that we just removed.
131                 $this->setValue( $this->getValue() );
132
133                 return $this;
134         }
135
136         public function setDisabled( $state ) {
137                 parent::setDisabled( $state );
138                 foreach ( $this->fields as $field ) {
139                         $field->getField()->setDisabled( $this->isDisabled() );
140                 }
141                 return $this;
142         }
143
144         public function getConfig( &$config ) {
145                 $o = [];
146                 foreach ( $this->fields as $field ) {
147                         $label = $field->getLabel();
148                         $data = $field->getField()->getValue();
149                         $disabled = $field->getField()->isDisabled();
150                         $o[] = [ 'data' => $data, 'label' => $label, 'disabled' => $disabled ];
151                 }
152                 $config['options'] = $o;
153                 return parent::getConfig( $config );
154         }
155 }