-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLang.php
158 lines (135 loc) · 3.7 KB
/
Lang.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* @link https://github.com/LAV45/yii2-translated-behavior
* @copyright Copyright (c) 2015 LAV45!
* @author Alexey Loban <[email protected]>
* @license http://opensource.org/licenses/BSD-3-Clause
*/
namespace lav45\translate\models;
use Yii;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use lav45\translate\LocaleHelperTrait;
/**
* This is the model class for table "lang".
*
* @property string $id
* @property string $locale
* @property string $name
* @property integer $status
*
* @property array $statusList
* @property string $statusName
*/
class Lang extends ActiveRecord
{
use LocaleHelperTrait;
const STATUS_DISABLE = 1;
const STATUS_ACTIVE = 10;
const PATTERN = '[a-z]{2}';
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%lang}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id'], 'trim'],
[['id'], 'required'],
[['id'], 'string', 'min' => 2, 'max' => 2],
[['id'], 'match', 'pattern' => '/^' . self::PATTERN . '$/'],
[['id'], 'unique'],
[['name'], 'trim'],
[['name'], 'required'],
[['name'], 'string', 'max' => 32],
[['name'], 'unique'],
[['locale'], 'trim'],
[['locale'], 'required'],
[['locale'], 'string', 'max' => 8],
[['status'], 'integer'],
[['status'], 'default', 'value' => self::STATUS_ACTIVE],
[['status'], 'in', 'range' => array_keys($this->getStatusList())],
[['id', 'status', 'locale'], function($attribute) {
if ($this->isAttributeChanged($attribute, false) && $this->isSourceLanguage()) {
$this->addError($attribute, Yii::t('app', 'This field is not editable.'));
}
}],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'locale' => Yii::t('app', 'Locale'),
'name' => Yii::t('app', 'Name'),
'status' => Yii::t('app', 'Status'),
];
}
public function isSourceLanguage()
{
return $this->getOldAttribute('id') == $this->getPrimaryLanguage(Yii::$app->sourceLanguage);
}
/**
* @return string[]
*/
public function getStatusList()
{
return [
static::STATUS_ACTIVE => Yii::t('app', 'Active'),
static::STATUS_DISABLE => Yii::t('app', 'Disable'),
];
}
/**
* @return string
*/
public function getStatusName()
{
return ArrayHelper::getValue($this->getStatusList(), $this->status);
}
/**
* @param bool $active default false so it is most often used in backend
* @return array
*/
public static function getList($active = false)
{
$query = static::find()
->select(['name', 'id'])
->orderBy('id')
->indexBy('id');
if ($active === true) {
$query->active();
}
return $query->column();
}
/**
* @param bool $active default true so it is most often used in frontend
* @return array
*/
public static function getLocaleList($active = true)
{
$query = static::find()
->select(['locale', 'id'])
->indexBy('id');
if ($active === true) {
$query->active();
}
return $query->column();
}
/**
* @inheritdoc
* @return LangQuery the active query used by this AR class.
*/
public static function find()
{
return new LangQuery(get_called_class());
}
}