14
14
use yii \db \ActiveRecord ;
15
15
use yii \helpers \ArrayHelper ;
16
16
use yii \helpers \Html ;
17
+ use yii \helpers \Inflector ;
17
18
18
19
/**
19
20
* Class MultipleInputColumn
@@ -28,6 +29,8 @@ class MultipleInputColumn extends Object
28
29
const TYPE_CHECKBOX_LIST = 'checkboxList ' ;
29
30
const TYPE_RADIO_LIST = 'radioList ' ;
30
31
const TYPE_STATIC = 'static ' ;
32
+ const TYPE_CHECKBOX = 'checkbox ' ;
33
+ const TYPE_RADIO = 'radio ' ;
31
34
32
35
/**
33
36
* @var string input name
@@ -174,38 +177,12 @@ public function prepareValue($data)
174
177
*/
175
178
public function renderCellContent ($ value , $ index )
176
179
{
177
- $ type = $ this ->type ;
178
180
$ name = $ this ->widget ->getElementName ($ this ->name , $ index );
179
181
180
182
$ options = $ this ->options ;
181
183
$ options ['id ' ] = $ this ->widget ->getElementId ($ this ->name , $ index );
182
- Html::addCssClass ($ options , 'form-control ' );
183
184
184
- switch ($ this ->type ) {
185
- case self ::TYPE_HIDDEN_INPUT :
186
- $ input = Html::hiddenInput ($ name , $ value , $ options );
187
- break ;
188
- case self ::TYPE_DROPDOWN :
189
- case self ::TYPE_LISTBOX :
190
- case self ::TYPE_CHECKBOX_LIST :
191
- case self ::TYPE_RADIO_LIST :
192
- $ input = Html::$ type ($ name , $ value , $ this ->items , $ options );
193
- break ;
194
- case self ::TYPE_STATIC :
195
- $ input = Html::tag ('p ' , $ value , ['class ' => 'form-control-static ' ]);
196
- break ;
197
- default :
198
- if (method_exists ('yii\helpers\Html ' , $ type )) {
199
- $ input = Html::$ type ($ name , $ value , $ options );
200
- } elseif (class_exists ($ type ) && method_exists ($ type , 'widget ' )) {
201
- $ input = $ type ::widget (array_merge ($ options , [
202
- 'name ' => $ name ,
203
- 'value ' => $ value ,
204
- ]));
205
- } else {
206
- throw new InvalidConfigException ("Invalid column type ' $ type' " );
207
- }
208
- }
185
+ $ input = $ this ->renderInput ($ name , $ value , $ options );
209
186
210
187
if ($ this ->isHiddenInput ()) {
211
188
return $ input ;
@@ -233,6 +210,100 @@ public function renderCellContent($value, $index)
233
210
]);
234
211
}
235
212
213
+ /**
214
+ * Renders the input.
215
+ *
216
+ * @param string $name name of the input
217
+ * @param mixed $value value of the input
218
+ * @param array $options the input options
219
+ * @return string
220
+ */
221
+ private function renderInput ($ name , $ value , $ options )
222
+ {
223
+ $ method = 'render ' . Inflector::camelize ($ this ->type );
224
+
225
+ if (method_exists ($ this , $ method )) {
226
+ $ input = call_user_func_array ([$ this , $ method ], [$ name , $ value , $ options ]);
227
+ } else {
228
+ $ input = $ this ->renderDefault ($ name , $ value , $ options );
229
+ }
230
+ return $ input ;
231
+ }
232
+
233
+
234
+ protected function renderDropDownList ($ name , $ value , $ options )
235
+ {
236
+ Html::addCssClass ($ options , 'form-control ' );
237
+ return Html::dropDownList ($ name , $ value , $ this ->items , $ options );
238
+ }
239
+
240
+ protected function renderListBox ($ name , $ value , $ options )
241
+ {
242
+ Html::addCssClass ($ options , 'form-control ' );
243
+ return Html::listBox ($ name , $ value , $ this ->items , $ options );
244
+ }
245
+
246
+ protected function renderHiddenInput ($ name , $ value , $ options )
247
+ {
248
+ return Html::hiddenInput ($ name , $ value , $ options );
249
+ }
250
+
251
+ protected function renderRadio ($ name , $ value , $ options )
252
+ {
253
+ if (!isset ($ options ['label ' ])) {
254
+ $ options ['label ' ] = '' ;
255
+ }
256
+ $ input = Html::radio ($ name , $ value , $ options );
257
+ return Html::tag ('div ' , $ input , ['class ' => 'radio ' ]);
258
+ }
259
+
260
+ protected function renderRadioList ($ name , $ value , $ options )
261
+ {
262
+ $ options ['item ' ] = function ($ index , $ label , $ name , $ checked , $ value ) {
263
+ return '<div class="radio"> ' . Html::radio ($ name , $ checked , ['label ' => $ label , 'value ' => $ value ]) . '</div> ' ;
264
+ };
265
+ $ input = Html::radioList ($ name , $ value , $ this ->items , $ options );
266
+ return Html::tag ('div ' , $ input , ['class ' => 'radio ' ]);
267
+ }
268
+
269
+ protected function renderCheckbox ($ name , $ value , $ options )
270
+ {
271
+ if (!isset ($ options ['label ' ])) {
272
+ $ options ['label ' ] = '' ;
273
+ }
274
+ $ input = Html::checkbox ($ name , $ value , $ options );
275
+ return Html::tag ('div ' , $ input , ['class ' => 'checkbox ' ]);
276
+ }
277
+
278
+ protected function renderCheckboxList ($ name , $ value , $ options )
279
+ {
280
+ $ options ['item ' ] = function ($ index , $ label , $ name , $ checked , $ value ) {
281
+ return '<div class="checkbox"> ' . Html::checkbox ($ name , $ checked , ['label ' => $ label , 'value ' => $ value ]) . '</div> ' ;
282
+ };
283
+ $ input = Html::checkboxList ($ name , $ value , $ this ->items , $ options );
284
+ return Html::tag ('div ' , $ input , ['class ' => 'checkbox ' ]);
285
+ }
286
+
287
+ protected function renderDefault ($ name , $ value , $ options )
288
+ {
289
+ $ type = $ this ->type ;
290
+
291
+ if ($ type == self ::TYPE_STATIC ) {
292
+ $ input = Html::tag ('p ' , $ value , ['class ' => 'form-control-static ' ]);
293
+ } elseif (method_exists ('yii\helpers\Html ' , $ type )) {
294
+ Html::addCssClass ($ options , 'form-control ' );
295
+ $ input = Html::$ type ($ name , $ value , $ options );
296
+ } elseif (class_exists ($ type ) && method_exists ($ type , 'widget ' )) {
297
+ $ input = $ type ::widget (array_merge ($ options , [
298
+ 'name ' => $ name ,
299
+ 'value ' => $ value ,
300
+ ]));
301
+ } else {
302
+ throw new InvalidConfigException ("Invalid column type ' $ type' " );
303
+ }
304
+ return $ input ;
305
+ }
306
+
236
307
/**
237
308
* @param string $error
238
309
* @return string
0 commit comments