Completed
Push — master ( e4197d...f733ed )
by Igor
11:05 queued 09:33
created

News::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace app\models;
4
5
use Yii;
6
use yii\behaviors\TimestampBehavior;
7
use app\models\File;
8
use app\models\query\NewsQuery;
9
10
/**
11
 * This is the model class for table "news".
12
 *
13
 * @property integer $id
14
 * @property string $title
15
 * @property string $text
16
 * @property string $preview
17
 * @property string $date_create
18
 * @property string $date_update
19
 * @property string $date_pub
20
 * @property integer $status
21
 */
22
class News extends \yii\db\ActiveRecord
23
{
24
    const STATUS_BLOCKED = 0;
25
    const STATUS_ACTIVE  = 1;
26
27
    /**
28
     * @var array
29
     */
30
    public $gallery;
31
    /**
32
     * @var array
33
     */
34
    public $galleryTitles;
35
36 4
    public function __construct($config = [])
37
    {
38 4
        $this->attachBehavior('fileManager', require __DIR__ . '/behaviors/news/filemanager.php');
39 4
        parent::__construct($config);
40 4
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45 4
    public static function tableName()
46
    {
47 4
        return 'news';
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function rules()
54
    {
55
        return [
56
            [
57
                ['title', 'text', 'date_pub'], 'required'
58
            ],
59
            [
60
                [
61
                    'title', 'text', 'date_pub', 'preview',
62
                    'gallery', 'galleryTitles', 'status'
63
                ], 'safe'
64
            ],
65
66
            ['title', 'string', 'max' => 255],
67
            ['text', 'string'],
68
69
            ['date_pub', 'date', 'format' => 'php:Y-m-d H:i:s',
70
                'timestampAttribute' => 'date_pub',
71
                'timestampAttributeFormat' => 'php:Y-m-d H:i:s'
72
            ],
73
74
            ['status', 'integer'],
75
            ['status', 'in', 'range' => array_keys(News::getStatuses())],
76
        ];
77
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82 4
    public function attributeLabels()
83
    {
84
        return [
85 4
            'id' => Yii::t('app', 'ID'),
86 4
            'title' => Yii::t('app', 'Title'),
87 4
            'text' => Yii::t('app', 'Text'),
88 4
            'preview' => Yii::t('app', 'Preview'),
89 4
            'gallery' => Yii::t('app', 'Gallery'),
90 4
            'date_create' => Yii::t('app', 'Date create'),
91 4
            'date_update' => Yii::t('app', 'Date update'),
92 4
            'date_pub' => Yii::t('app', 'Date publication'),
93 4
            'status' => Yii::t('app', 'Status'),
94
        ];
95
    }
96
97
    /**
98
     * @inheritdoc
99
     */
100
    public function attributeHints()
101
    {
102
        return [
103
104
        ];
105
    }
106
107
    /**
108
     * @inheritdoc
109
     */
110 4
    public function behaviors()
111
    {
112
        return [
113
            [
114 4
                'class' => TimestampBehavior::class,
115 4
                'createdAtAttribute' => 'date_create',
116 4
                'updatedAtAttribute' => 'date_update',
117 4
                'value' => new \yii\db\Expression('NOW()'),
118
            ],
119
        ];
120
    }
121
122
    public function transactions()
123
    {
124
        return [
125
            'create' => self::OP_ALL,
126
            'update' => self::OP_ALL,
127
            'delete' => self::OP_ALL,
128
        ];
129
    }
130
131
    /**
132
     * @inheritdoc
133
     * @return NewsQuery
134
     */
135 4
    public static function find()
136
    {
137 4
        return new NewsQuery(get_called_class());
138
    }
139
140
    /**
141
     * Get all statuses
142
     *
143
     * @return string[]
144
     */
145 4
    public static function getStatuses(): array
146
    {
147
        return [
148 4
            self::STATUS_BLOCKED => Yii::t('app', 'Not published'),
149 4
            self::STATUS_ACTIVE  => Yii::t('app', 'Published'),
150
        ];
151
    }
152
153
    /**
154
     * Get statuse name
155
     *
156
     * @return string
157
     */
158 4
    public function getStatusName(): string
159
    {
160 4
        $statuses = self::getStatuses();
161 4
        return isset($statuses[$this->status]) ? $statuses[$this->status] : '';
162
    }
163
164
    /**
165
     * Is it blocked?
166
     *
167
     * @param bool
168
     */
169
    public function isBlocked(): bool
170
    {
171
        return $this->status == self::STATUS_BLOCKED;
172
    }
173
174
    /**
175
     * Is it active?
176
     *
177
     * @param bool
178
     */
179
    public function isActive(): bool
180
    {
181
        return $this->status == self::STATUS_ACTIVE;
182
    }
183
184
    public function getFiles($callable = null)
185
    {
186
        return $this
187
            ->hasMany(File::class, ['id' => 'file_id'])
188
            ->viaTable('news_files', ['news_id' => 'id'], $callable);
189
    }
190
}
191