Completed
Push — master ( 4e2c55...2c21d8 )
by Andrey
01:28
created

src/Fields.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Itstructure\FieldWidgets;
4
5
use Itstructure\FieldWidgets\interfaces\LanguageListInterface;
6
use yii\base\{Widget, Model};
7
use yii\widgets\ActiveForm;
8
9
/**
10
 * Class Fields
11
 * Widget class for displaying a list of form fields with given types.
12
 *
13
 * @property array $fields
14
 * @property LanguageListInterface|null $languageModel
15
 * @property Model $model
16
 * @property ActiveForm $form
17
 *
18
 * @package Itstructure\FieldWidgets
19
 */
20
class Fields extends Widget
21
{
22
    /**
23
     * List of form fields.
24
     * You must set the following parameters for each field:: name, type.
25
     * name (string) - field name, example: title, description.
26
     * type (string) - field type, may be: text, textarea, ckeditor, 'ckeditorAdmin', file, checkbox, dropdown,
27
     * password.
28
     * Optional:
29
     * label (string) - field label.
30
     * data (array) - data to be inserted in to the field.
31
     * options (array) - field options.
32
     * hide (bool) - to switch off the field. Default false.
33
     * Example:
34
     * $fields = [
35
     *      [
36
     *          'name' => 'title',
37
     *          'type' => 'text'
38
     *      ],
39
     *      [
40
     *          'name' => 'description',
41
     *          'type' => 'ckeditor',
42
     *          'label' => 'Description',
43
     *          'preset' => 'custom',
44
     *          'options' => [
45
     *              'toolbarGroups' => [
46
     *                  [
47
     *                      'name' => 'undo'
48
     *                  ],
49
     *                  [
50
     *                      'name' => 'basicstyles',
51
     *                      'groups' => ['basicstyles', 'cleanup']
52
     *                  ],
53
     *                  [
54
     *                      'name' => 'colors'
55
     *                  ],
56
     *                  [
57
     *                      'name' => 'links',
58
     *                      'groups' => ['links', 'insert']
59
     *                  ],
60
     *                  [
61
     *                      'name' => 'others',
62
     *                      'groups' => ['others', 'about']
63
     *                  ],
64
     *                  ],
65
     *              'filebrowserBrowseUrl' => '/ckfinder/ckfinder.html',
66
     *              'filebrowserImageBrowseUrl' => '/ckfinder/ckfinder.html?type=Images',
67
     *              'filebrowserUploadUrl' => '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files',
68
     *              'filebrowserImageUploadUrl' => '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
69
     *              'filebrowserWindowWidth' => '1000',
70
     *              'filebrowserWindowHeight' => '700',
71
     *          ]
72
     *      ],
73
     *      [
74
     *          'name' => 'roles',
75
     *          'type' => 'checkbox',
76
     *          'label' => 'Roles',
77
     *          'data' => ArrayHelper::map($roles, 'name', 'name'),
78
     *          'options' => ['style' => 'width: 50%;'],
79
     *      ],
80
     * ];
81
     *
82
     * @var array
83
     */
84
    public $fields = [];
85
86
    /**
87
     * Language model.
88
     * For multilanguage mode.
89
     *
90
     * @var LanguageListInterface
91
     */
92
    private $languageModel = null;
93
94
    /**
95
     * Model with fields.
96
     *
97
     * @var Model
98
     */
99
    private $model;
100
101
    /**
102
     * Form object.
103
     *
104
     * @var ActiveForm
105
     */
106
    private $form;
107
108
    /**
109
     * Set the language model.
110
     * For multilanguage mode.
111
     *
112
     * @param mixed $languageModel
113
     *
114
     * @return void
115
     */
116
    public function setLanguageModel(LanguageListInterface $languageModel): void
117
    {
118
        $this->languageModel = $languageModel;
119
    }
120
121
    /**
122
     * Set model.
123
     *
124
     * @param Model $model
125
     *
126
     * @return void
127
     */
128
    public function setModel(Model $model): void
129
    {
130
        $this->model = $model;
0 ignored issues
show
Documentation Bug introduced by
It seems like $model of type object<yii\base\Model> is incompatible with the declared type object<Model> of property $model.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
131
    }
132
133
    /**
134
     * Set form.
135
     *
136
     * @param mixed $form
137
     *
138
     * @return void
139
     */
140
    public function setForm(ActiveForm $form): void
141
    {
142
        $this->form = $form;
143
    }
144
145
    /**
146
     * Starts the output widget of the form fields list with the specified types.
147
     *
148
     * @return string
149
     */
150
    public function run(): string
151
    {
152
        $params = [
153
            'fields' => $this->fields,
154
            'model'  => $this->model,
155
            'form'   => $this->form,
156
        ];
157
158
        if ($this->languageModel === null) {
159
            $template = 'fieldsSimple';
160
        } else {
161
            $template = 'fieldsMultilanguage';
162
            $params['languageModel'] = $this->languageModel;
163
        }
164
165
        return $this->render($template, $params);
166
    }
167
}
168