1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Itstructure\AdminModule\models; |
4
|
|
|
|
5
|
|
|
use yii\db\ActiveRecord as BaseActiveRecord; |
6
|
|
|
use yii\base\Model; |
7
|
|
|
use yii\helpers\ArrayHelper; |
8
|
|
|
use Itstructure\AdminModule\interfaces\ModelInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class MultilanguageValidateModel |
12
|
|
|
* General validation model together with multilingual fields. |
13
|
|
|
* |
14
|
|
|
* @property array $dynamicFields Dynamic fields from which the translated fields are formed. |
15
|
|
|
* @property BaseActiveRecord|MultilanguageTrait $mainModel Basic data model. |
16
|
|
|
* |
17
|
|
|
* @package Itstructure\AdminModule\models |
18
|
|
|
* |
19
|
|
|
* @author Andrey Girnik <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class MultilanguageValidateModel extends Model implements ModelInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Dynamic fields from which the translated fields are formed. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
public $dynamicFields = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Basic data model. |
32
|
|
|
* |
33
|
|
|
* @var BaseActiveRecord|MultilanguageTrait |
34
|
|
|
*/ |
35
|
|
|
private $mainModel; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Scripts Constants. |
39
|
|
|
* Required for certain validation rules to work for specific scenarios. |
40
|
|
|
*/ |
41
|
|
|
const SCENARIO_CREATE = 'create'; |
42
|
|
|
const SCENARIO_UPDATE = 'update'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Validation rules for all fields together with dynamic. |
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function rules(): array |
50
|
|
|
{ |
51
|
|
|
return ArrayHelper::merge( |
52
|
|
|
$this->getDynamicValidationRules(), |
53
|
|
|
$this->mainModel->rules() |
|
|
|
|
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Scenarios. |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
public function scenarios(): array |
63
|
|
|
{ |
64
|
|
|
return [ |
65
|
|
|
self::SCENARIO_CREATE => $this->attributes(), |
66
|
|
|
self::SCENARIO_UPDATE => $this->attributes(), |
67
|
|
|
self::SCENARIO_DEFAULT => $this->attributes(), |
68
|
|
|
]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Labels of all fields. |
73
|
|
|
* |
74
|
|
|
* @inheritdoc |
75
|
|
|
*/ |
76
|
|
|
public function attributeLabels() |
77
|
|
|
{ |
78
|
|
|
$dynamicAttributeLabels = []; |
79
|
|
|
$staticAttributeLabels = []; |
80
|
|
|
|
81
|
|
|
$translateAttributeLabels = call_user_func([ |
82
|
|
|
$this->mainModel->getTranslateModelName(), |
|
|
|
|
83
|
|
|
'attributeLabels', |
84
|
|
|
]); |
85
|
|
|
|
86
|
|
|
foreach ($this->dynamicFields as $fieldConditions) { |
87
|
|
|
|
88
|
|
|
$fieldName = $fieldConditions['name']; |
89
|
|
|
|
90
|
|
|
if (array_key_exists($fieldName, $translateAttributeLabels)) { |
91
|
|
|
|
92
|
|
|
$staticAttributeLabels[$fieldName] = $translateAttributeLabels[$fieldName]; |
93
|
|
|
|
94
|
|
|
foreach ($this->getShortLanguageList() as $language) { |
95
|
|
|
$dynamicAttributeLabels[$fieldName.'_'.$language] = $translateAttributeLabels[$fieldName]; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return ArrayHelper::merge( |
101
|
|
|
ArrayHelper::merge( |
102
|
|
|
$dynamicAttributeLabels, |
103
|
|
|
$staticAttributeLabels |
104
|
|
|
), |
105
|
|
|
$this->mainModel->attributeLabels() |
|
|
|
|
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Specifies the value of the field. |
111
|
|
|
* |
112
|
|
|
* @param string $name - name of field. |
113
|
|
|
* @param mixed $value - value to be stored in field. |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
public function __set($name, $value) |
118
|
|
|
{ |
119
|
|
|
$setter = 'set' . $name; |
120
|
|
|
if (method_exists($this, $setter)) { |
121
|
|
|
$this->$setter($value); |
122
|
|
|
} else { |
123
|
|
|
$this->{$name} = $value; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Gets the value of the field. |
129
|
|
|
* |
130
|
|
|
* @param string $name - field name. |
131
|
|
|
* |
132
|
|
|
* @return mixed |
133
|
|
|
*/ |
134
|
|
|
public function __get($name) |
135
|
|
|
{ |
136
|
|
|
$getter = 'get' . $name; |
137
|
|
|
if (method_exists($this, $getter)) { |
138
|
|
|
return $this->$getter(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if ($this->mainModel->isNewRecord) { |
142
|
|
|
return $this->{$name} ?? ''; |
143
|
|
|
} else { |
144
|
|
|
return $this->mainModel->{$name} ?? ''; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Setter for main model. |
150
|
|
|
* |
151
|
|
|
* @param BaseActiveRecord $mainModel |
152
|
|
|
*/ |
153
|
|
|
public function setMainModel(BaseActiveRecord $mainModel) |
154
|
|
|
{ |
155
|
|
|
$this->mainModel = $mainModel; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Getter for main model. |
160
|
|
|
* |
161
|
|
|
* @return mixed |
162
|
|
|
*/ |
163
|
|
|
public function getMainModel() |
164
|
|
|
{ |
165
|
|
|
return $this->mainModel; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Attributes along with dynamic and from the basic model. |
170
|
|
|
* |
171
|
|
|
* @return array |
172
|
|
|
*/ |
173
|
|
|
public function attributes(): array |
174
|
|
|
{ |
175
|
|
|
return ArrayHelper::merge( |
176
|
|
|
$this->getDynamicAttributes(), |
177
|
|
|
$this->mainModel->attributes() |
|
|
|
|
178
|
|
|
); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Saves data in the main model. |
183
|
|
|
* |
184
|
|
|
* @return bool |
185
|
|
|
*/ |
186
|
|
|
public function save(): bool |
187
|
|
|
{ |
188
|
|
|
if ($this->mainModel->isNewRecord) { |
189
|
|
|
$this->setScenario(self::SCENARIO_CREATE); |
190
|
|
|
} else { |
191
|
|
|
$this->setScenario(self::SCENARIO_UPDATE); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
if (!$this->validate()){ |
195
|
|
|
return false; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
// Transferring attribute values from this model to the main. |
199
|
|
|
foreach ($this->attributes() as $attribute) { |
200
|
|
|
|
201
|
|
|
$this->mainModel->{$attribute} = $this->{$attribute}; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $this->mainModel->save(); |
|
|
|
|
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Returns the id of the current model. |
209
|
|
|
* |
210
|
|
|
* @return int |
211
|
|
|
*/ |
212
|
|
|
public function getId() |
213
|
|
|
{ |
214
|
|
|
return $this->mainModel->id; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Returns an array of all multilanguage attributes. |
219
|
|
|
* |
220
|
|
|
* @return array |
221
|
|
|
*/ |
222
|
|
|
private function getDynamicAttributes(): array |
223
|
|
|
{ |
224
|
|
|
$languageList = $this->getShortLanguageList(); |
225
|
|
|
$dynamicAttributes = []; |
226
|
|
|
foreach ($languageList as $language) { |
227
|
|
|
foreach ($this->dynamicFields as $fieldConditions) { |
228
|
|
|
$dynamicAttributes[] = $fieldConditions['name'] . '_' . $language; |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
return array_values($dynamicAttributes); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Creates validation rules for dynamic fields for all languages. |
236
|
|
|
* |
237
|
|
|
* @return array |
238
|
|
|
*/ |
239
|
|
|
private function getDynamicValidationRules(): array |
240
|
|
|
{ |
241
|
|
|
$rules = []; |
242
|
|
|
foreach ($this->getShortLanguageList() as $language) { |
243
|
|
|
foreach ($this->dynamicFields as $fieldConditions) { |
244
|
|
|
|
245
|
|
|
$fieldName = $fieldConditions['name']; |
246
|
|
|
$fieldRules = isset($fieldConditions['rules']) ? $fieldConditions['rules'] : []; |
247
|
|
|
|
248
|
|
|
foreach ($fieldRules as $fieldRule) { |
249
|
|
|
|
250
|
|
|
if (in_array('required', $fieldRule) && $language != Language::getDefaultLanguage()->shortName) { |
251
|
|
|
continue; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
if (in_array('unique', $fieldRule)) { |
255
|
|
|
$fieldRule = ArrayHelper::merge( |
256
|
|
|
$fieldRule, |
257
|
|
|
[ |
258
|
|
|
'skipOnError' => true, |
259
|
|
|
'targetClass' => $this->mainModel->getTranslateModelName(), |
|
|
|
|
260
|
|
|
'targetAttribute' => [$fieldName . '_' . $language => $fieldName], |
261
|
|
|
|
262
|
|
|
'filter' => $this->getScenario() == self::SCENARIO_UPDATE ? |
263
|
|
|
$this->mainModel->getKeyToMainModel().' != '.$this->id : '', |
|
|
|
|
264
|
|
|
|
265
|
|
|
'message' => isset($fieldRule['message']) ? |
266
|
|
|
$fieldRule['message'] : 'Record with such attribute "{attribute}" already exists' |
267
|
|
|
] |
268
|
|
|
); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
$rules[] = ArrayHelper::merge( |
272
|
|
|
[$fieldName . '_' . $language], |
273
|
|
|
$fieldRule |
274
|
|
|
); |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
return $rules; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Returns the list of available languages in the short name format. |
284
|
|
|
* |
285
|
|
|
* @return array |
286
|
|
|
*/ |
287
|
|
|
private function getShortLanguageList(): array |
288
|
|
|
{ |
289
|
|
|
return Language::getShortLanguageList(); |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: