1 | <?php |
||||
2 | /** |
||||
3 | * Admin Page Framework |
||||
4 | * |
||||
5 | * http://admin-page-framework.michaeluno.jp/ |
||||
6 | * Copyright (c) 2013-2022, Michael Uno; Licensed MIT |
||||
7 | * |
||||
8 | */ |
||||
9 | |||||
10 | /** |
||||
11 | * Provides methods to output form input element of drop-down list. |
||||
12 | * |
||||
13 | * @package AdminPageFramework/Common/Form/Input |
||||
14 | * @since 3.4.0 |
||||
15 | * @internal |
||||
16 | */ |
||||
17 | class AdminPageFramework_Input_select extends AdminPageFramework_Input_Base { |
||||
18 | |||||
19 | /** |
||||
20 | * Represents the structure of the options array. |
||||
21 | * |
||||
22 | * @since 3.4.0 |
||||
23 | */ |
||||
24 | public $aStructureOptions = array( |
||||
25 | 'input_container_tag' => 'span', |
||||
26 | 'input_container_attributes' => array( |
||||
27 | 'class' => 'admin-page-framework-input-container', |
||||
28 | ), |
||||
29 | 'label_container_tag' => 'span', |
||||
30 | 'label_container_attributes' => array( |
||||
31 | 'class' => 'admin-page-framework-input-label-string', |
||||
32 | ), |
||||
33 | ); |
||||
34 | |||||
35 | /** |
||||
36 | * A user constructor. |
||||
37 | * |
||||
38 | * @since 3.5.3 |
||||
39 | * @return void |
||||
40 | */ |
||||
41 | protected function construct() { |
||||
42 | |||||
43 | // For backward compatibility. |
||||
44 | |||||
45 | // If the $aField property is set, extract certain elements from it and set them to the attribute array. |
||||
46 | if ( isset( $this->aField[ 'is_multiple' ] ) ) { |
||||
0 ignored issues
–
show
|
|||||
47 | $this->aAttributes[ 'select' ][ 'multiple' ] = $this->aField[ 'is_multiple' ] |
||||
48 | ? 'multiple' |
||||
49 | : $this->getElement( $this->aAttributes, array( 'select', 'multiple' ) ); |
||||
50 | } |
||||
51 | |||||
52 | } |
||||
53 | |||||
54 | /** |
||||
55 | * Returns the output of the input element. |
||||
56 | * |
||||
57 | * @remark This method should be overridden in each extended class. |
||||
58 | * @since 3.4.0 |
||||
59 | */ |
||||
60 | public function get( /* $aLabels, $aAttributes=array() */ ) { |
||||
61 | |||||
62 | // Parameters |
||||
63 | $_aParams = func_get_args() + array( 0 => null, 1 => array() ); |
||||
64 | $_aLabels = $_aParams[ 0 ]; |
||||
65 | $_aAttributes = $this->uniteArrays( |
||||
66 | $this->getElementAsArray( $_aParams, 1, array() ), |
||||
67 | $this->aAttributes |
||||
68 | ); |
||||
69 | |||||
70 | return |
||||
71 | "<{$this->aOptions[ 'input_container_tag' ]} " . $this->getAttributes( $this->aOptions[ 'input_container_attributes' ] ) . ">" |
||||
72 | . "<select " . $this->getAttributes( $this->_getSelectAttributes( $_aAttributes ) ) . " >" |
||||
73 | . $this->_getDropDownList( |
||||
74 | $this->getAttribute( 'id' ), |
||||
75 | $this->getAsArray( |
||||
76 | isset( $_aLabels ) |
||||
77 | ? $_aLabels |
||||
78 | : $this->aField[ 'label' ], // backward compatibility |
||||
0 ignored issues
–
show
The property
AdminPageFramework_Input_Base::$aField has been deprecated: 3.5.3
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This property has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.
Loading history...
|
|||||
79 | true |
||||
80 | ), |
||||
81 | $_aAttributes |
||||
82 | ) |
||||
83 | . "</select>" |
||||
84 | . "</{$this->aOptions[ 'input_container_tag' ]}>" |
||||
85 | ; |
||||
86 | |||||
87 | } |
||||
88 | /** |
||||
89 | * Retrusn an HTML select attribute array. |
||||
90 | * @since 3.5.3 |
||||
91 | * @return array The generated attribute array for the `select` tag. |
||||
92 | */ |
||||
93 | private function _getSelectAttributes( array $aBaseAttributes ) { |
||||
94 | $_bIsMultiple = $this->getElement( $aBaseAttributes, 'multiple' ) |
||||
95 | ? true |
||||
96 | : ( ( bool ) $this->getElement( $aBaseAttributes, array( 'select', 'multiple' ) ) ); |
||||
97 | return $this->uniteArrays( |
||||
98 | // allowing the user set attributes override the system set attributes. |
||||
99 | $this->getElementAsArray( $aBaseAttributes, 'select', array() ), |
||||
100 | array( |
||||
101 | 'id' => $this->getAttribute( 'id' ), |
||||
102 | 'multiple' => $_bIsMultiple |
||||
103 | ? 'multiple' |
||||
104 | : null, |
||||
105 | 'name' => $_bIsMultiple |
||||
106 | ? $this->getAttribute( 'name' ) . '[]' |
||||
107 | : $this->getAttribute( 'name' ), |
||||
108 | 'data-id' => $this->getAttribute( 'id' ), // referenced by the JavaScript scripts such as the revealer script. |
||||
109 | ) |
||||
110 | ); |
||||
111 | |||||
112 | } |
||||
113 | /** |
||||
114 | * Returns the option tags of the select field. |
||||
115 | * |
||||
116 | * @since 2.0.0 |
||||
117 | * @since 2.0.1 Added the $vValue parameter to the second parameter. This is the result of supporting the size field type. |
||||
118 | * @since 2.1.5 Added the $tag_id parameter. |
||||
119 | * @since 3.0.0 Reconstructed entirely. |
||||
120 | * @since 3.4.0 |
||||
121 | * @internal |
||||
122 | * @param string $sInputID The input ID that will be the base of each generated option tag ID. |
||||
123 | * @param array $aLabels The array holding labels. |
||||
124 | * @param array $aAttributes The attribute arrays. Accepts the following arguments. |
||||
125 | * - optgroup |
||||
126 | * - option |
||||
127 | */ |
||||
128 | private function _getDropDownList( $sInputID, array $aLabels, array $aBaseAttributes ) { |
||||
129 | |||||
130 | $_aOutput = array(); |
||||
131 | foreach( $aLabels as $__sKey => $__asLabel ) { |
||||
132 | |||||
133 | // For an optgroup tag, |
||||
134 | if ( is_array( $__asLabel ) ) { |
||||
135 | $_aOutput[] = $this->_getOptGroup( |
||||
136 | $aBaseAttributes, |
||||
137 | $sInputID, |
||||
138 | $__sKey, |
||||
139 | $__asLabel |
||||
140 | ); |
||||
141 | continue; |
||||
142 | } |
||||
143 | |||||
144 | // A normal option tag, |
||||
145 | $_aOutput[] = $this->_getOptionTag( |
||||
146 | $__asLabel, // the text label the user sees to be selected |
||||
147 | $this->_getOptionTagAttributes( |
||||
148 | $aBaseAttributes, |
||||
149 | $sInputID, |
||||
150 | $__sKey, |
||||
151 | $this->getAsArray( $aBaseAttributes[ 'value' ], true ) |
||||
152 | ) |
||||
153 | ); |
||||
154 | |||||
155 | } |
||||
156 | return implode( PHP_EOL, $_aOutput ); |
||||
157 | |||||
158 | } |
||||
159 | /** |
||||
160 | * Returns an HTML output of optgroup tag. |
||||
161 | * @since 3.5.3 |
||||
162 | * @return string an HTML output of optgroup tag. |
||||
163 | */ |
||||
164 | private function _getOptGroup( array $aBaseAttributes, $sInputID, $sKey, $asLabel ) { |
||||
165 | |||||
166 | $_aOptGroupAttributes = isset( $aBaseAttributes[ 'optgroup' ][ $sKey ] ) && is_array( $aBaseAttributes[ 'optgroup' ][ $sKey ] ) |
||||
167 | ? $aBaseAttributes[ 'optgroup' ][ $sKey ] + $aBaseAttributes[ 'optgroup' ] |
||||
168 | : $aBaseAttributes[ 'optgroup' ]; |
||||
169 | $_aOptGroupAttributes = array( |
||||
170 | 'label' => $sKey, |
||||
171 | ) + $_aOptGroupAttributes; |
||||
172 | return "<optgroup " . $this->getAttributes( $_aOptGroupAttributes ) . ">" |
||||
173 | . $this->_getDropDownList( $sInputID, $asLabel, $aBaseAttributes ) |
||||
174 | . "</optgroup>"; |
||||
175 | |||||
176 | } |
||||
177 | |||||
178 | /** |
||||
179 | * |
||||
180 | * @since 3.5.3 |
||||
181 | */ |
||||
182 | private function _getOptionTagAttributes( array $aBaseAttributes, $sInputID, $sKey, $aValues ) { |
||||
183 | |||||
184 | $aValues = $this->getElementAsArray( |
||||
185 | $aBaseAttributes, |
||||
186 | array( 'option', $sKey, 'value' ), |
||||
187 | $aValues |
||||
188 | ); |
||||
189 | return array( |
||||
190 | 'id' => $sInputID . '_' . $sKey, |
||||
191 | 'value' => $sKey, |
||||
192 | 'selected' => in_array( ( string ) $sKey, $aValues ) |
||||
193 | ? 'selected' |
||||
194 | : null, |
||||
195 | ) + ( isset( $aBaseAttributes[ 'option' ][ $sKey ] ) && is_array( $aBaseAttributes[ 'option' ][ $sKey ] ) |
||||
196 | ? $aBaseAttributes[ 'option' ][ $sKey ] + $aBaseAttributes[ 'option' ] |
||||
197 | : $aBaseAttributes[ 'option' ] ); |
||||
198 | |||||
199 | } |
||||
200 | |||||
201 | /** |
||||
202 | * Returns an HTML option tag output. |
||||
203 | * @sicne 3.4.0 |
||||
204 | * @return string The generated option tag HTML output. |
||||
205 | */ |
||||
206 | private function _getOptionTag( $sLabel, array $aOptionTagAttributes=array() ) { |
||||
207 | return "<option " . $this->getAttributes( $aOptionTagAttributes ) . " >" |
||||
208 | . $sLabel |
||||
209 | . "</option>"; |
||||
210 | } |
||||
211 | |||||
212 | } |
||||
213 |
This property has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.