FieldType   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 281
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 76
c 1
b 0
f 0
dl 0
loc 281
rs 10
wmc 24

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getOptions() 0 3 1
A getFieldName() 0 7 2
A setOptions() 0 3 1
A getInstance() 0 18 2
A setData() 0 3 1
A getFieldId() 0 3 1
A getField() 0 3 1
A setLanguage() 0 3 1
A getLabel() 0 11 3
B run() 0 53 10
A getData() 0 3 1
1
<?php
2
3
namespace Itstructure\FieldWidgets;
4
5
use yii\widgets\ActiveForm;
6
use yii\helpers\Html;
7
use yii\base\{Model, InvalidConfigException, Widget};
8
use Itstructure\FieldWidgets\interfaces\LanguageFieldInterface;
9
10
/**
11
 * Class FieldType
12
 * Widget class for outputting the form field depending on the field type.
13
 *
14
 * @property string $name
15
 * @property string $label
16
 * @property string $type
17
 * @property string $preset
18
 * @property bool $ckeditor
19
 * @property ActiveForm $form
20
 * @property Model $model
21
 * @property array $data
22
 * @property array $options
23
 * @property bool $hide
24
 * @property LanguageFieldInterface|null $language
25
 *
26
 * @package Itstructure\FieldWidgets
27
 */
28
class FieldType extends Widget
29
{
30
    /**
31
     * Field type constants.
32
     *
33
     * @var string
34
     */
35
    const FIELD_TYPE_TEXT = 'text';
36
    const FIELD_TYPE_TEXT_AREA = 'textarea';
37
    const FIELD_TYPE_CKEDITOR = 'ckeditor';
38
    const FIELD_TYPE_CKEDITOR_ADMIN = 'ckeditorAdmin';
39
    const FIELD_TYPE_FILE = 'file';
40
    const FIELD_TYPE_CHECKBOX = 'checkbox';
41
    const FIELD_TYPE_PASSWORD = 'password';
42
    const FIELD_TYPE_DROPDOWN = 'dropdown';
43
44
    /**
45
     * Field name.
46
     *
47
     * @var string
48
     */
49
    public $name;
50
51
    /**
52
     * Field label.
53
     *
54
     * @var string
55
     */
56
    public $label;
57
58
    /**
59
     * Field type, text as default.
60
     *
61
     * @var string
62
     */
63
    public $type = self::FIELD_TYPE_TEXT;
64
65
    /**
66
     * CKEditor preset.
67
     *
68
     * @var string
69
     */
70
    public $preset = 'basic';
71
72
    /**
73
     * Form object, in which the field is placed.
74
     *
75
     * @var ActiveForm
76
     */
77
    public $form;
78
79
    /**
80
     * The model to which the field corresponds.
81
     *
82
     * @var Model
83
     */
84
    public $model;
85
86
    /**
87
     * Data to be inserted in to the field.
88
     *
89
     * @var array
90
     */
91
    public $data = [];
92
93
    /**
94
     * Field options.
95
     *
96
     * @var array
97
     */
98
    public $options = [];
99
100
    /**
101
     * Hide field.
102
     *
103
     * @var bool
104
     */
105
    public $hide = false;
106
107
    /**
108
     * The object of languages.
109
     * For multilanguage mode.
110
     *
111
     * @var LanguageFieldInterface
112
     */
113
    protected $language = null;
114
115
    /**
116
     * Set the language model.
117
     * For multilanguage mode.
118
     *
119
     * @param LanguageFieldInterface $language
120
     *
121
     * @return void
122
     */
123
    public function setLanguage(LanguageFieldInterface $language): void
124
    {
125
        $this->language = $language;
126
    }
127
128
    /**
129
     * Starts the output widget of the required form field, depending on the type of field.
130
     *
131
     * @throws InvalidConfigException
132
     *
133
     * @return mixed
134
     */
135
    public function run()
136
    {
137
        if ($this->hide) {
138
            return '';
139
        }
140
141
        switch ($this->type) {
142
            case self::FIELD_TYPE_TEXT: {
143
                $instance = $this->getInstance(FieldTypeText::class);
144
                break;
145
            }
146
147
            case self::FIELD_TYPE_TEXT_AREA: {
148
                $instance = $this->getInstance(FieldTypeTextArea::class);
149
                break;
150
            }
151
152
            case self::FIELD_TYPE_CKEDITOR: {
153
                $instance = $this->getInstance(FieldTypeCKEditor::class);
154
                break;
155
            }
156
157
            case self::FIELD_TYPE_CKEDITOR_ADMIN: {
158
                $instance = $this->getInstance(FieldTypeCKEditorAdmin::class);
159
                break;
160
            }
161
162
            case self::FIELD_TYPE_FILE: {
163
                $instance = $this->getInstance(FieldTypeFile::class);
164
                break;
165
            }
166
167
            case self::FIELD_TYPE_DROPDOWN: {
168
                $instance = $this->getInstance(FieldTypeDropdown::class);
169
                break;
170
            }
171
172
            case self::FIELD_TYPE_CHECKBOX: {
173
                $instance = $this->getInstance(FieldTypeCheckbox::class);
174
                break;
175
            }
176
177
            case self::FIELD_TYPE_PASSWORD: {
178
                $instance = $this->getInstance(FieldTypePassword::class);
179
                break;
180
            }
181
182
            default: {
183
                throw new InvalidConfigException('Unknown type of field');
184
            }
185
        }
186
187
        return $instance->run();
188
    }
189
190
    /**
191
     * Data to be inserted in to the field.
192
     *
193
     * @param array $data
194
     *
195
     * @return void
196
     */
197
    public function setData(array $data)
198
    {
199
        $this->data = $data;
200
    }
201
202
    /**
203
     * Set field options.
204
     *
205
     * @param array $options
206
     *
207
     * @return void
208
     */
209
    public function setOptions(array $options)
210
    {
211
        $this->options = $options;
212
    }
213
214
    /**
215
     * Data to be inserted in to the field.
216
     *
217
     * @return array
218
     */
219
    protected function getData(): array
220
    {
221
        return $this->data;
222
    }
223
224
    /**
225
     * Get field options.
226
     *
227
     * @return array
228
     */
229
    protected function getOptions(): array
230
    {
231
        return $this->options;
232
    }
233
234
    /**
235
     * Creates an object from the class of the selected field widget.
236
     *
237
     * @return object
238
     */
239
    protected function getInstance(string $className)
240
    {
241
        $config = [
242
            'class'   => $className,
243
            'name'    => $this->name,
244
            'form'    => $this->form,
245
            'model'   => $this->model,
246
            'label'   => $this->label,
247
            'data'    => $this->data,
248
            'options' => $this->options,
249
            'preset'  => $this->preset,
250
        ];
251
252
        if ($this->language !== null) {
253
            $config['language'] = $this->language;
254
        }
255
256
        return \Yii::createObject($config);
257
    }
258
259
    /**
260
     * Returns the form field object.
261
     *
262
     * @return mixed
263
     */
264
    protected function getField()
265
    {
266
        return $this->form->field($this->model, $this->getFieldName());
267
    }
268
269
    /**
270
     * Returns the label of the field.
271
     *
272
     * @return string
273
     */
274
    protected function getLabel(): string
275
    {
276
        if ($this->label != null) {
277
            return $this->label;
278
        }
279
280
        if (isset($this->model->attributeLabels()[$this->name])) {
281
            return $this->model->attributeLabels()[$this->name];
282
        }
283
284
        return ucfirst($this->name);
285
    }
286
287
    /**
288
     * Returns the name of the field tag with the language.
289
     *
290
     * @return string
291
     */
292
    protected function getFieldName(): string
293
    {
294
        if ($this->language === null) {
295
            return $this->name;
296
        }
297
298
        return $this->name . '_' . strtolower($this->language->getShortName());
299
    }
300
301
    /**
302
     * Returns ID of the field.
303
     *
304
     * @return string
305
     */
306
    protected function getFieldId()
307
    {
308
        return Html::getInputId($this->model, $this->getFieldName());
309
    }
310
}
311