1 | <?php |
||
2 | |||
3 | namespace execut\import\models; |
||
4 | use execut\crudFields\Behavior; |
||
5 | use execut\crudFields\BehaviorStub; |
||
6 | use execut\crudFields\fields\DropDown; |
||
7 | use execut\crudFields\fields\HasOneSelect2; |
||
8 | use execut\crudFields\fields\Hidden; |
||
9 | use execut\crudFields\fields\NumberField; |
||
10 | use execut\crudFields\ModelsHelperTrait; |
||
11 | use yii\behaviors\TimestampBehavior; |
||
12 | use yii\db\Expression; |
||
13 | use yii\helpers\ArrayHelper; |
||
14 | use yii\helpers\Url; |
||
15 | use yii\web\JsExpression; |
||
16 | |||
17 | /** |
||
18 | * This is the model class for table "import_settings_values". |
||
19 | * |
||
20 | * @property integer $id |
||
21 | * @property string $created |
||
22 | * @property string $updated |
||
23 | * @property string $name |
||
24 | * @property string $type |
||
25 | * @property string $column_nbr |
||
26 | * @property string $format |
||
27 | * @property string $value_string |
||
28 | * @property string $value_option |
||
29 | * @property string $import_settings_set_id |
||
30 | * |
||
31 | * @property \execut\import\models\SettingsSet $settingsSet |
||
32 | */ |
||
33 | class SettingsValue extends base\SettingsValue |
||
34 | { |
||
35 | use BehaviorStub, ModelsHelperTrait; |
||
36 | |||
37 | /** |
||
38 | * @return \execut\import\models\queries\SettingsValue |
||
39 | */ |
||
40 | public static function find() |
||
41 | { |
||
42 | return new \execut\import\models\queries\SettingsValue(static::class); |
||
43 | } |
||
44 | |||
45 | public function behaviors() |
||
46 | { |
||
47 | return [ |
||
48 | 'fields' => [ |
||
49 | 'class' => Behavior::class, |
||
50 | 'module' => 'import', |
||
51 | 'fields' => [ |
||
52 | 'import_settings_set_id' => [ |
||
53 | 'class' => Hidden::class, |
||
54 | 'attribute' => 'import_settings_set_id', |
||
55 | ], |
||
56 | 'type' => [ |
||
57 | 'class' => DropDown::class, |
||
58 | 'attribute' => 'type', |
||
59 | 'data' => function () { |
||
60 | return self::getAttributesValuesTypesList(); |
||
61 | }, |
||
62 | ], |
||
63 | 'column_nbr' => [ |
||
64 | 'class' => NumberField::class, |
||
65 | 'attribute' => 'column_nbr', |
||
66 | ], |
||
67 | 'number_delimiter' => [ |
||
68 | 'attribute' => 'number_delimiter', |
||
69 | 'required' => true, |
||
70 | 'defaultValue' => ',', |
||
71 | ], |
||
72 | 'value_string' => [ |
||
73 | 'attribute' => 'value_string', |
||
74 | ], |
||
75 | 'value_option' => [ |
||
76 | 'class' => HasOneSelect2::class, |
||
77 | 'attribute' => 'value_option', |
||
78 | 'nameParam' => 'name', |
||
79 | // 'relation' => 'dictionariesData', |
||
80 | 'data' => function () { |
||
81 | if (!$this->settingsSet || !$this->settingsSet->settingsSheet || !$this->settingsSet->settingsSheet->settingsSets) { |
||
82 | return []; |
||
83 | } |
||
84 | $dictionaryOptions = ['' => '']; |
||
85 | $types = SettingsSheet::getDictionaries(); |
||
86 | // return []; |
||
87 | foreach ($this->settingsSet->settingsSheet->settingsSets as $settingsSet) { |
||
88 | foreach ($settingsSet->settingsValues as $value) { |
||
89 | $type = explode('.', $value->type)[0]; |
||
90 | if (!empty($types[$type]) && !empty($value->value_option)) { |
||
91 | if ($model = $types[$type]->byId($value->value_option)->one()) { |
||
92 | $dictionaryOptions[$model->id] = $model->name; |
||
93 | } |
||
94 | } |
||
95 | } |
||
96 | } |
||
97 | |||
98 | return $dictionaryOptions; |
||
99 | }, |
||
100 | 'url' => ['get-dictionaries'], |
||
101 | 'isNoRenderRelationLink' => true, |
||
102 | 'widgetOptions' => [ |
||
103 | 'pluginOptions' => [ |
||
104 | 'ajax' => [ |
||
105 | 'data' => new JsExpression(<<<JS |
||
106 | function(params) { |
||
107 | var currentType = $(this).parents('.multiple-input-list__item:first').find('.list-cell__type select').val(); |
||
108 | return { |
||
109 | name: params.term, |
||
110 | type: currentType, |
||
111 | page: params.page |
||
112 | }; |
||
113 | } |
||
114 | JS |
||
115 | ), |
||
116 | ], |
||
117 | ], |
||
118 | ], |
||
119 | // 'data' => [], |
||
120 | // 'options' => [ |
||
121 | // 'initValueText' => $dictionaryOptions, |
||
122 | // 'options' => [ |
||
123 | // 'placeholder' => 'Dictionary' |
||
124 | // ], |
||
125 | // 'pluginOptions' => [ |
||
126 | // 'allowClear' => true, |
||
127 | // 'ajax' => [ |
||
128 | // 'url' => $getDictionariesUrl, |
||
129 | // 'dataType' => 'json', |
||
130 | // 'data' => new JsExpression(<<<JS |
||
131 | //function(params) { |
||
132 | // var currentType = $(this).parents('.multiple-input-list__item:first').find('.list-cell__type select').val(); |
||
133 | // return { |
||
134 | // name: params.term, |
||
135 | // type: currentType |
||
136 | // }; |
||
137 | //} |
||
138 | //JS |
||
139 | // ) |
||
140 | // ], |
||
141 | // ], |
||
142 | // ], |
||
143 | // 'enableError' => true, |
||
144 | ], |
||
145 | ], |
||
146 | // 'plugins' => \yii::$app->getModule('import')->getSettingsSetsCrudFieldsPlugins(), |
||
147 | ], |
||
148 | 'date' => [ |
||
149 | 'class' => TimestampBehavior::class, |
||
150 | 'createdAtAttribute' => 'created', |
||
151 | 'updatedAtAttribute' => 'updated', |
||
152 | 'value' => new Expression('NOW()'), |
||
153 | ], |
||
154 | ]; |
||
155 | } |
||
156 | |||
157 | public static function getAttributesValuesTypesList() { |
||
158 | return \yii::$app->getModule('import')->getAttributesValuesTypesList(); |
||
159 | } |
||
160 | |||
161 | public function delete() |
||
162 | { |
||
163 | foreach ($this->logs as $importLog) { |
||
164 | $importLog->delete(); |
||
165 | } |
||
166 | |||
167 | return parent::delete(); // TODO: Change the autogenerated stub |
||
168 | } |
||
169 | |||
170 | public function rules() |
||
171 | { |
||
172 | $rules = $this->getBehavior('fields')->rules(); |
||
173 | |||
174 | return ArrayHelper::merge([ |
||
175 | [['type'], 'validateValuesFields'], |
||
176 | ], $rules); |
||
177 | } |
||
178 | |||
179 | public function validateValuesFields() { |
||
180 | $variants = [ |
||
181 | 'column_nbr', |
||
182 | 'value_option', |
||
183 | 'value_string' |
||
184 | ]; |
||
185 | $notEmptyCount = 0; |
||
186 | foreach ($variants as $variant) { |
||
187 | if ($variant === 'column_nbr' && $this->$variant === '0' || !empty($this->$variant)) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
188 | $notEmptyCount++; |
||
189 | } |
||
190 | } |
||
191 | |||
192 | if ($notEmptyCount != 1) { |
||
193 | $this->addError('type', 'Enter one of fields: column nbr, text value or dictionary option for attribute value'); |
||
194 | } |
||
195 | } |
||
196 | } |
||
197 |