|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Author: metalguardian |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace metalguardian\formBuilder; |
|
7
|
|
|
|
|
8
|
|
|
use yii\base\InvalidConfigException; |
|
9
|
|
|
use yii\base\Model; |
|
10
|
|
|
use yii\helpers\ArrayHelper; |
|
11
|
|
|
use yii\widgets\ActiveField; |
|
12
|
|
|
use yii\widgets\ActiveForm; |
|
13
|
|
|
use yii\widgets\InputWidget; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class ActiveFormBuilder |
|
17
|
|
|
* @package metalguardian\formBuilder |
|
18
|
|
|
*/ |
|
19
|
|
|
class ActiveFormBuilder extends ActiveForm |
|
20
|
|
|
{ |
|
21
|
|
|
const INPUT_HIDDEN = 'hiddenInput'; |
|
22
|
|
|
const INPUT_TEXT = 'textInput'; |
|
23
|
|
|
const INPUT_TEXTAREA = 'textarea'; |
|
24
|
|
|
const INPUT_PASSWORD = 'passwordInput'; |
|
25
|
|
|
const INPUT_DROPDOWN_LIST = 'dropdownList'; |
|
26
|
|
|
const INPUT_LIST_BOX = 'listBox'; |
|
27
|
|
|
const INPUT_CHECKBOX = 'checkbox'; |
|
28
|
|
|
const INPUT_RADIO = 'radio'; |
|
29
|
|
|
const INPUT_CHECKBOX_LIST = 'checkboxList'; |
|
30
|
|
|
const INPUT_RADIO_LIST = 'radioList'; |
|
31
|
|
|
const INPUT_FILE = 'fileInput'; |
|
32
|
|
|
const INPUT_HTML5 = 'input'; |
|
33
|
|
|
const INPUT_WIDGET = 'widget'; |
|
34
|
|
|
const INPUT_RAW = 'raw'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param Model $model |
|
38
|
|
|
* @param array $config |
|
39
|
|
|
* |
|
40
|
|
|
* @return null|string |
|
41
|
|
|
*/ |
|
42
|
|
|
public function renderForm(Model $model, array $config) |
|
43
|
|
|
{ |
|
44
|
|
|
$form = null; |
|
45
|
|
|
foreach ($config as $attribute => $options) { |
|
46
|
|
|
$form .= $this->renderField($model, $attribute, $options); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $form; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $attribute |
|
55
|
|
|
* @param array $settings |
|
56
|
|
|
* @param Model $model |
|
57
|
|
|
* |
|
58
|
|
|
* @return ActiveField |
|
59
|
|
|
* @throws InvalidConfigException |
|
60
|
|
|
*/ |
|
61
|
|
|
public function renderField(Model $model, $attribute, array $settings = []) |
|
62
|
|
|
{ |
|
63
|
|
|
$fieldOptions = ArrayHelper::getValue($settings, 'fieldOptions', []); |
|
64
|
|
|
$field = $this->field($model, $attribute, $fieldOptions); |
|
65
|
|
|
|
|
66
|
|
|
if (($label = ArrayHelper::getValue($settings, 'label')) !== null) { |
|
67
|
|
|
$field->label($label, ArrayHelper::getValue($settings, 'labelOptions', [])); |
|
68
|
|
|
} |
|
69
|
|
|
if (($hint = ArrayHelper::getValue($settings, 'hint')) !== null) { |
|
70
|
|
|
$field->hint($hint, ArrayHelper::getValue($settings, 'hintOptions', [])); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$type = ArrayHelper::getValue($settings, 'type', static::INPUT_TEXT); |
|
74
|
|
|
$this->prepareField($field, $type, $settings); |
|
75
|
|
|
|
|
76
|
|
|
return $field; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param ActiveField $field |
|
81
|
|
|
* @param $type |
|
82
|
|
|
* @param array $settings |
|
83
|
|
|
* @throws InvalidConfigException |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function prepareField($field, $type, array $settings) |
|
86
|
|
|
{ |
|
87
|
|
|
$options = ArrayHelper::getValue($settings, 'options', []); |
|
88
|
|
|
switch ($type) { |
|
89
|
|
|
case static::INPUT_HIDDEN: |
|
90
|
|
|
case static::INPUT_TEXT: |
|
91
|
|
|
case static::INPUT_TEXTAREA: |
|
92
|
|
|
case static::INPUT_PASSWORD: |
|
93
|
|
|
case static::INPUT_FILE: |
|
94
|
|
|
$field->$type($options); |
|
95
|
|
|
break; |
|
96
|
|
|
|
|
97
|
|
|
case static::INPUT_DROPDOWN_LIST: |
|
98
|
|
|
case static::INPUT_LIST_BOX: |
|
99
|
|
|
case static::INPUT_CHECKBOX_LIST: |
|
100
|
|
|
case static::INPUT_RADIO_LIST: |
|
101
|
|
|
$items = ArrayHelper::getValue($settings, 'items', []); |
|
102
|
|
|
$field->$type($items, $options); |
|
103
|
|
|
break; |
|
104
|
|
|
|
|
105
|
|
|
case static::INPUT_CHECKBOX: |
|
106
|
|
|
case static::INPUT_RADIO: |
|
107
|
|
|
$enclosedByLabel = ArrayHelper::getValue($settings, 'enclosedByLabel', true); |
|
108
|
|
|
$field->$type($options, $enclosedByLabel); |
|
109
|
|
|
break; |
|
110
|
|
|
|
|
111
|
|
|
case static::INPUT_HTML5: |
|
112
|
|
|
$html5type = ArrayHelper::getValue($settings, 'html5type', 'text'); |
|
113
|
|
|
$field->$type($html5type, $options); |
|
114
|
|
|
break; |
|
115
|
|
|
|
|
116
|
|
|
case static::INPUT_WIDGET: |
|
117
|
|
|
$widgetClass = $this->getWidgetClass($settings); |
|
118
|
|
|
$field->$type($widgetClass, $options); |
|
119
|
|
|
break; |
|
120
|
|
|
|
|
121
|
|
|
case static::INPUT_RAW: |
|
122
|
|
|
$field->parts['{input}'] = $this->getValue($settings); |
|
123
|
|
|
break; |
|
124
|
|
|
|
|
125
|
|
|
default: |
|
126
|
|
|
throw new InvalidConfigException("Invalid input type '{$type}' configured for the attribute."); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param array $settings |
|
132
|
|
|
* @return mixed |
|
133
|
|
|
* @throws InvalidConfigException |
|
134
|
|
|
*/ |
|
135
|
|
|
protected function getWidgetClass(array $settings) |
|
136
|
|
|
{ |
|
137
|
|
|
$widgetClass = ArrayHelper::getValue($settings, 'widgetClass'); |
|
138
|
|
|
if (empty($widgetClass) && !$widgetClass instanceof InputWidget) { |
|
139
|
|
|
throw new InvalidConfigException( |
|
140
|
|
|
"A valid 'widgetClass' must be setup and extend from '\\yii\\widgets\\InputWidget'." |
|
141
|
|
|
); |
|
142
|
|
|
} |
|
143
|
|
|
return $widgetClass; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param array $settings |
|
148
|
|
|
* @return mixed|string |
|
149
|
|
|
*/ |
|
150
|
|
|
protected function getValue(array $settings) |
|
151
|
|
|
{ |
|
152
|
|
|
$value = ArrayHelper::getValue($settings, 'value', ''); |
|
153
|
|
|
if (is_callable($value)) { |
|
154
|
|
|
return call_user_func($value); |
|
155
|
|
|
} elseif (!is_string($value)) { |
|
156
|
|
|
return ''; |
|
157
|
|
|
} |
|
158
|
|
|
return $value; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|