1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace lav45\translate\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\db\ActiveRecord; |
7
|
|
|
use Locale; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This is the model class for table "lang". |
11
|
|
|
* |
12
|
|
|
* @property string $id |
13
|
|
|
* @property string $locale |
14
|
|
|
* @property string $name |
15
|
|
|
* @property integer $status |
16
|
|
|
*/ |
17
|
|
|
class Lang extends ActiveRecord |
18
|
|
|
{ |
19
|
|
|
const STATUS_DISABLE = 1; |
20
|
|
|
|
21
|
|
|
const STATUS_ACTIVE = 10; |
22
|
|
|
|
23
|
|
|
const PATTERN = '[a-z]{2}'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @inheritdoc |
27
|
|
|
*/ |
28
|
|
|
public static function tableName() |
29
|
|
|
{ |
30
|
|
|
return '{{%lang}}'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritdoc |
35
|
|
|
*/ |
36
|
|
|
public function rules() |
37
|
|
|
{ |
38
|
|
|
return [ |
39
|
|
|
[['id'], 'trim'], |
40
|
|
|
[['id'], 'required'], |
41
|
|
|
[['id'], 'string', 'min' => 2, 'max' => 2], |
42
|
|
|
[['id'], 'match', 'pattern' => '/^' . self::PATTERN . '$/'], |
43
|
|
|
[['id'], 'unique'], |
44
|
|
|
|
45
|
|
|
[['name'], 'trim'], |
46
|
|
|
[['name'], 'required'], |
47
|
|
|
[['name'], 'string', 'max' => 32], |
48
|
|
|
[['name'], 'unique'], |
49
|
|
|
|
50
|
|
|
[['locale'], 'trim'], |
51
|
|
|
[['locale'], 'required'], |
52
|
|
|
[['locale'], 'string', 'max' => 8], |
53
|
|
|
|
54
|
|
|
[['status'], 'integer'], |
55
|
|
|
[['status'], 'default', 'value' => self::STATUS_ACTIVE], |
56
|
|
|
[['status'], 'in', 'range' => array_keys($this->getStatusList())], |
57
|
|
|
|
58
|
|
|
[['id', 'status', 'locale'], function($attribute) { |
59
|
|
|
if ($this->isAttributeChanged($attribute, false) && $this->isSourceLanguage()) { |
60
|
|
|
$this->addError($attribute, 'This field is not editable.'); |
61
|
|
|
} |
62
|
|
|
}], |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritdoc |
68
|
|
|
*/ |
69
|
|
|
public function attributeLabels() |
70
|
|
|
{ |
71
|
|
|
return [ |
72
|
|
|
'id' => 'ID', |
73
|
|
|
'locale' => 'Locale', |
74
|
|
|
'name' => 'Name', |
75
|
|
|
'status' => 'Status', |
76
|
|
|
]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function isSourceLanguage() |
80
|
|
|
{ |
81
|
|
|
return $this->getOldAttribute('id') == Locale::getPrimaryLanguage(Yii::$app->sourceLanguage); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
public function getStatusList() |
88
|
|
|
{ |
89
|
|
|
return [ |
90
|
|
|
static::STATUS_ACTIVE => 'Active', |
91
|
|
|
static::STATUS_DISABLE => 'Disable', |
92
|
|
|
]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param bool $active default false so it is most often used in backend |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
public static function getList($active = false) |
100
|
|
|
{ |
101
|
|
|
$query = static::find() |
102
|
|
|
->select(['name', 'id']) |
103
|
|
|
->orderBy('id') |
104
|
|
|
->indexBy('id'); |
105
|
|
|
|
106
|
|
|
if ($active == true) { |
|
|
|
|
107
|
|
|
$query->active(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $query->column(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param bool $active default true so it is most often used in frontend |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
public static function getLocaleList($active = true) |
118
|
|
|
{ |
119
|
|
|
$query = static::find() |
120
|
|
|
->select(['locale', 'id']) |
121
|
|
|
->indexBy('id'); |
122
|
|
|
|
123
|
|
|
if ($active == true) { |
|
|
|
|
124
|
|
|
$query->active(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $query->column(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @inheritdoc |
132
|
|
|
* @return LangQuery the active query used by this AR class. |
133
|
|
|
*/ |
134
|
|
|
public static function find() |
135
|
|
|
{ |
136
|
|
|
return new LangQuery(get_called_class()); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.