Completed
Push — master ( b88251...ac7e0c )
by Loban
03:36
created

Lang::getStatusName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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