1 | <?php |
||
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() |
||
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() |
||
83 | |||
84 | /** |
||
85 | * @return array |
||
86 | */ |
||
87 | public function getStatusList() |
||
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) |
||
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) |
||
129 | |||
130 | /** |
||
131 | * @inheritdoc |
||
132 | * @return LangQuery the active query used by this AR class. |
||
133 | */ |
||
134 | public static function find() |
||
138 | } |
||
139 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.