Lang::attributeLabels()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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