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 $local |
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
|
|
|
[['local'], 'trim'], |
51
|
|
|
[['local'], 'required'], |
52
|
|
|
[['local'], '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', 'local'], 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
|
|
|
'local' => 'Local', |
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 |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
public static function getList($active = false) |
100
|
|
|
{ |
101
|
|
|
$condition = $active ? ['status' => self::STATUS_ACTIVE] : []; |
102
|
|
|
|
103
|
|
|
return static::find() |
104
|
|
|
->select(['name', 'id']) |
105
|
|
|
->filterWhere($condition) |
106
|
|
|
->orderBy('id') |
107
|
|
|
->indexBy('id') |
108
|
|
|
->column(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
public static function getLocaleList() |
115
|
|
|
{ |
116
|
|
|
return static::find() |
117
|
|
|
->select(['local', 'id']) |
118
|
|
|
->where(['status' => self::STATUS_ACTIVE]) |
119
|
|
|
->indexBy('id') |
120
|
|
|
->column(); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|