-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActiveField.php
executable file
·257 lines (251 loc) · 7.54 KB
/
ActiveField.php
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
namespace makroxyz\materializecss;
class ActiveField extends \yii\widgets\ActiveField
{
/**
* @inheritdoc
*/
public $template = "{icon}\n{input}\n{label}\n{hint}\n{error}";
/**
* @inheritdoc
*/
public $options = ['class' => 'input-field col s12'];
/**
* @inheritdoc
*/
public $inputOptions = ['class' => 'validate'];
/**
* @var array
*/
public $iconOptions = ['class' => 'prefix'];
/**
* @var string
*/
public $radioGapCssClass = 'with-gap';
/**
* @var string
*/
public $checkboxFilledCssClass = 'filled-in';
/**
* @inheritdoc
*/
public $labelOptions = [];
/**
* @var boolean
*/
public $row = true;
/**
* @inheritdoc
*/
public function init()
{
if (!isset($this->labelOptions['class']) && !empty($this->model->{$this->attribute})) {
Html::addCssClass($this->labelOptions, 'active');
}
parent::init();
}
/**
* Renders the opening tag of the field container.
* @return string the rendering result.
*/
public function begin()
{
if ($this->row) {
$out[] = Html::beginTag('div', ['class' => 'row']);
}
$out[] = parent::begin();
return implode("\n", $out);
}
// /**
// * Renders the closing tag of the field container.
// * @return string the rendering result.
// */
public function end()
{
$out[] = parent::end();
if ($this->row) {
$out[] = Html::endTag('div');
}
return implode("\n", $out);
}
/**
* @param null $content
* @return string
*/
public function render($content = null)
{
if ($content === null) {
if (!isset($this->parts['{icon}'])) {
$this->parts['{icon}'] = '';
}
}
return parent::render($content);
}
/**
* @param null $icon
* @param array $options
* @return $this
*/
public function icon($icon, $options = [])
{
if ($icon === false) {
$this->parts['{icon}'] = '';
return $this;
}
$options = array_merge($this->iconOptions, $options);
$this->parts['{icon}'] = Icon::widget(['name' => $icon, 'options' => $options]);
return $this;
}
/**
* @inheritdoc
*/
public function textarea($options = [])
{
Html::addCssClass($options, 'materialize-textarea');
return parent::textarea($options);
}
/**
* Renders default browser drop-down list
* @see http://materializecss.com/forms.html#select
* @param $items
* @param array $options
* @return $this
*/
public function dropDownListDefault($items, $options = [])
{
Html::addCssClass($options, 'browser-default');
return parent::dropDownList($items, $options);
}
/**
* @inheritdoc
*/
public function dropDownList($items, $options = [])
{
return $this->widget(Select::className(), [
'items' => $items,
'options' => $options
]);
}
/**
* Creates radio button with gap
* @see http://materializecss.com/forms.html#radio
* @param array $options
* @param bool|true $enclosedByLabel
* @return $this
*/
public function radioWithGap($options = [], $enclosedByLabel = true)
{
Html::addCssClass($options, $this->radioGapCssClass);
return self::radio($options, $enclosedByLabel);
}
/**
* Renders a list of radio buttons with gap
* @param $items
* @param array $options
* @return $this|ActiveField
*/
public function radioListWithGap($items, $options = [])
{
$this->addListInputCssClass($options, $this->radioGapCssClass);
return self::radioList($items, $options);
}
/**
* Renders filled in checkbox
* @see http://materializecss.com/forms.html#checkbox
* @param array $options
* @param bool|true $enclosedByLabel
* @return $this
*/
public function checkboxFilled($options = [], $enclosedByLabel = false)
{
Html::addCssClass($options, $this->checkboxFilledCssClass);
Html::removeCssClass($this->options, 'input-field');
Html::removeCssClass($this->labelOptions, 'active');
return parent::checkbox($options, $enclosedByLabel);
}
/**
* Renders filled in checkbox
* @see http://materializecss.com/forms.html#checkbox
* @param array $options
* @param bool|true $enclosedByLabel
* @return $this
*/
public function checkbox($options = [], $enclosedByLabel = false)
{
Html::removeCssClass($this->options, 'input-field');
Html::removeCssClass($this->labelOptions, 'active');
return parent::checkbox($options, $enclosedByLabel);
}
/**
* Renders switcher
* @see http://materializecss.com/forms.html#switches
* @param array $options
* @param array $flags
* @return $this
*/
public function switcher($options = [], $flags = null)
{
parent::checkbox($options, false);
if ($flags === null) {
$label = Html::encode($this->model->getAttributeLabel(Html::getAttributeName($this->attribute)));
$labelParts = explode(',', $label);
$flags = count($labelParts) >= 2 ? $labelParts : null;
}
if ($flags) {
Html::removeCssClass($this->options, 'input-field');
Html::addCssClass($this->options, 'switch');
$labelContent = $flags[0] . $this->parts['{input}'] . Html::tag('span', '', ['class' => 'lever']) . $flags[1];
$this->parts['{input}'] = Html::label($labelContent, Html::getInputId($this->model, $this->attribute));
}
return $this;
}
/**
* Renders a list of filled checkboxes
* @param $items
* @param array $options
* @return $this|ActiveField
*/
public function checkboxListFilled($items, $options = [])
{
$this->addListInputCssClass($options, $this->checkboxFilledCssClass);
return self::checkboxList($items, $options);
}
/**
* @inheritdoc
*/
public function checkboxList($items, $options = [])
{
$this->parts['{label}'] = '';
if (!isset($options['separator'])) {
$options['separator'] = Html::tag('br');
}
$this->parts['{input}'] = Html::activeCheckboxList($this->model, $this->attribute, $items, $options);
return $this;
}
public function datePicker($options = [])
{
return $this->widget(DatePicker::className(), [
'options' => $options
]);
}
/**
* @inheritDoc
*/
public function radioList($items, $options = [])
{
$this->parts['{label}'] = '';
if (!isset($options['separator'])) {
$options['separator'] = Html::tag('br');
}
$this->parts['{input}'] = Html::activeRadioList($this->model, $this->attribute, $items, $options);
return $this;
}
protected function addListInputCssClass(&$options, $class)
{
if (!isset($options['itemOptions'])) {
$options['itemOptions'] = ['class' => $class];
} else {
Html::addCssClass($options['itemOptions'], $class);
}
}
}