|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Itstructure\AdminModule\models; |
|
4
|
|
|
|
|
5
|
|
|
use Itstructure\AdminModule\Module; |
|
6
|
|
|
use Itstructure\AdminModule\interfaces\ModelInterface; |
|
7
|
|
|
use Itstructure\FieldWidgets\interfaces\{LanguageListInterface, LanguageFieldInterface}; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* This is the model class for table "language". |
|
11
|
|
|
* |
|
12
|
|
|
* @property int $id |
|
13
|
|
|
* @property string $shortName |
|
14
|
|
|
* @property string $name |
|
15
|
|
|
* @property string $locale |
|
16
|
|
|
* @property int $default |
|
17
|
|
|
* @property string $created_at |
|
18
|
|
|
* @property string $updated_at |
|
19
|
|
|
* |
|
20
|
|
|
* @package Itstructure\AdminModule\models |
|
21
|
|
|
* |
|
22
|
|
|
* @author Andrey Girnik <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class Language extends ActiveRecord implements LanguageListInterface, LanguageFieldInterface, ModelInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @inheritdoc |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function tableName() |
|
30
|
|
|
{ |
|
31
|
|
|
return 'language'; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @inheritdoc |
|
36
|
|
|
*/ |
|
37
|
|
|
public function rules() |
|
38
|
|
|
{ |
|
39
|
|
|
return [ |
|
40
|
|
|
[ |
|
41
|
|
|
['default'], |
|
42
|
|
|
'integer', |
|
43
|
|
|
], |
|
44
|
|
|
[ |
|
45
|
|
|
[ |
|
46
|
|
|
'created_at', |
|
47
|
|
|
'updated_at', |
|
48
|
|
|
], |
|
49
|
|
|
'safe', |
|
50
|
|
|
], |
|
51
|
|
|
[ |
|
52
|
|
|
[ |
|
53
|
|
|
'locale', |
|
54
|
|
|
'shortName', |
|
55
|
|
|
'name', |
|
56
|
|
|
], |
|
57
|
|
|
|
|
58
|
|
|
'required', |
|
59
|
|
|
], |
|
60
|
|
|
[ |
|
61
|
|
|
'locale', |
|
62
|
|
|
'string', |
|
63
|
|
|
'max' => 8, |
|
64
|
|
|
], |
|
65
|
|
|
[ |
|
66
|
|
|
'shortName', |
|
67
|
|
|
'string', |
|
68
|
|
|
'max' => 3, |
|
69
|
|
|
], |
|
70
|
|
|
[ |
|
71
|
|
|
'name', |
|
72
|
|
|
'string', |
|
73
|
|
|
'max' => 64, |
|
74
|
|
|
], |
|
75
|
|
|
[ |
|
76
|
|
|
'locale', |
|
77
|
|
|
'unique', |
|
78
|
|
|
'skipOnError' => true, |
|
79
|
|
|
'targetClass' => static::class, |
|
80
|
|
|
'filter' => $this->getScenario() == self::SCENARIO_UPDATE ? 'id != '.$this->id : '' |
|
81
|
|
|
], |
|
82
|
|
|
[ |
|
83
|
|
|
'shortName', |
|
84
|
|
|
'unique', |
|
85
|
|
|
'skipOnError' => true, |
|
86
|
|
|
'targetClass' => static::class, |
|
87
|
|
|
'filter' => $this->getScenario() == self::SCENARIO_UPDATE ? 'id != '.$this->id : '' |
|
88
|
|
|
], |
|
89
|
|
|
[ |
|
90
|
|
|
'name', |
|
91
|
|
|
'unique', |
|
92
|
|
|
'skipOnError' => true, |
|
93
|
|
|
'targetClass' => static::class, |
|
94
|
|
|
'filter' => $this->getScenario() == self::SCENARIO_UPDATE ? 'id != '.$this->id : '' |
|
95
|
|
|
], |
|
96
|
|
|
]; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @inheritdoc |
|
101
|
|
|
*/ |
|
102
|
|
|
public function attributeLabels() |
|
103
|
|
|
{ |
|
104
|
|
|
return [ |
|
105
|
|
|
'id' => Module::t('main', 'ID'), |
|
106
|
|
|
'shortName' => Module::t('languages', 'Short name'), |
|
107
|
|
|
'name' => Module::t('languages', 'Language name'), |
|
108
|
|
|
'locale' => Module::t('languages', 'Locale'), |
|
109
|
|
|
'default' => Module::t('languages', 'Set this language as default'), |
|
110
|
|
|
'created_at' => Module::t('main', 'Created date'), |
|
111
|
|
|
'updated_at' => Module::t('main', 'Updated date'), |
|
112
|
|
|
]; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Reset the default language. |
|
117
|
|
|
* |
|
118
|
|
|
* @param boolean $insert |
|
119
|
|
|
* |
|
120
|
|
|
* @return mixed |
|
121
|
|
|
*/ |
|
122
|
|
|
public function beforeSave($insert) |
|
123
|
|
|
{ |
|
124
|
|
|
if ($this->default == 1) { |
|
125
|
|
|
|
|
126
|
|
|
$default = static::findOne([ |
|
127
|
|
|
'default' => 1, |
|
128
|
|
|
]); |
|
129
|
|
|
|
|
130
|
|
|
if (null !== $default){ |
|
131
|
|
|
$default->default = 0; |
|
132
|
|
|
$default->save(); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return parent::beforeSave($insert); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Returns the default language. |
|
141
|
|
|
* |
|
142
|
|
|
* @return array|null|\yii\db\ActiveRecord |
|
143
|
|
|
*/ |
|
144
|
|
|
public static function getDefaultLanguage() |
|
145
|
|
|
{ |
|
146
|
|
|
return static::find() |
|
147
|
|
|
->where([ |
|
148
|
|
|
'default' => 1 |
|
149
|
|
|
]) |
|
150
|
|
|
->one(); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* List of available languages in short name format. |
|
155
|
|
|
* |
|
156
|
|
|
* @return array |
|
157
|
|
|
*/ |
|
158
|
|
|
public static function getShortLanguageList(): array |
|
159
|
|
|
{ |
|
160
|
|
|
$result = static::find()->all(); |
|
161
|
|
|
return array_map(function(Language $item) { |
|
162
|
|
|
return $item->shortName; |
|
163
|
|
|
}, $result); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Returns a list of available languages in the system. |
|
168
|
|
|
* |
|
169
|
|
|
* @return Language[] |
|
170
|
|
|
*/ |
|
171
|
|
|
public function getLanguageList(): array |
|
172
|
|
|
{ |
|
173
|
|
|
return static::find()->all(); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Returns the full name of the language. |
|
178
|
|
|
* |
|
179
|
|
|
* @return string |
|
180
|
|
|
*/ |
|
181
|
|
|
public function getName(): string |
|
182
|
|
|
{ |
|
183
|
|
|
return $this->name; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Returns the short name of the language. |
|
188
|
|
|
* |
|
189
|
|
|
* @return string |
|
190
|
|
|
*/ |
|
191
|
|
|
public function getShortName(): string |
|
192
|
|
|
{ |
|
193
|
|
|
return $this->shortName; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Returns default mode. |
|
198
|
|
|
* |
|
199
|
|
|
* @return int |
|
200
|
|
|
*/ |
|
201
|
|
|
public function getDefault(): int |
|
202
|
|
|
{ |
|
203
|
|
|
return $this->default; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Returns current model id. |
|
208
|
|
|
* |
|
209
|
|
|
* @return int |
|
210
|
|
|
*/ |
|
211
|
|
|
public function getId(): int |
|
212
|
|
|
{ |
|
213
|
|
|
return $this->id; |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|