Completed
Push — master ( c064eb...85769a )
by Igor
03:38
created

News::tableName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
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(
39 4
            'fileManager',
40 4
            require Yii::getAlias('@app/models/behaviors/news/filemanager.php')
41
        );
42 4
        parent::__construct($config);
43 4
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48 4
    public static function tableName()
49
    {
50 4
        return 'news';
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56 4
    public function attributeLabels()
57
    {
58
        return [
59 4
            'id' => Yii::t('app', 'ID'),
60 4
            'title' => Yii::t('app', 'Title'),
61 4
            'text' => Yii::t('app', 'Text'),
62 4
            'preview' => Yii::t('app', 'Preview'),
63 4
            'gallery' => Yii::t('app', 'Gallery'),
64 4
            'date_create' => Yii::t('app', 'Date create'),
65 4
            'date_update' => Yii::t('app', 'Date update'),
66 4
            'date_pub' => Yii::t('app', 'Date publication'),
67 4
            'status' => Yii::t('app', 'Status'),
68
        ];
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74 4
    public function behaviors()
75
    {
76
        return [
77
            [
78 4
                'class' => TimestampBehavior::class,
79 4
                'createdAtAttribute' => 'date_create',
80 4
                'updatedAtAttribute' => 'date_update',
81 4
                'value' => new \yii\db\Expression('NOW()'),
82
            ],
83
        ];
84
    }
85
86
    public function transactions()
87
    {
88
        return [
89
            'create' => self::OP_ALL,
90
            'update' => self::OP_ALL,
91
            'delete' => self::OP_ALL,
92
        ];
93
    }
94
95
    /**
96
     * @inheritdoc
97
     * @return NewsQuery
98
     */
99 4
    public static function find()
100
    {
101 4
        return new NewsQuery(get_called_class());
102
    }
103
104
    /**
105
     * Get all statuses
106
     *
107
     * @return string[]
108
     */
109 4
    public static function getStatuses(): array
110
    {
111
        return [
112 4
            self::STATUS_BLOCKED => Yii::t('app', 'Not published'),
113 4
            self::STATUS_ACTIVE  => Yii::t('app', 'Published'),
114
        ];
115
    }
116
117
    /**
118
     * Get statuse name
119
     *
120
     * @return string
121
     */
122 4
    public function getStatusName(): string
123
    {
124 4
        $statuses = self::getStatuses();
125 4
        return isset($statuses[$this->status]) ? $statuses[$this->status] : '';
126
    }
127
128
    /**
129
     * Is it blocked?
130
     *
131
     * @param bool
132
     */
133 4
    public function isBlocked(): bool
134
    {
135 4
        return $this->status == self::STATUS_BLOCKED;
136
    }
137
138
    /**
139
     * Is it active?
140
     *
141
     * @param bool
142
     */
143 4
    public function isActive(): bool
144
    {
145 4
        return $this->status == self::STATUS_ACTIVE;
146
    }
147
148 2
    public function getFiles($callable = null)
149
    {
150
        return $this
151 2
            ->hasMany(File::class, ['id' => 'file_id'])
152 2
            ->viaTable('news_files', ['news_id' => 'id'], $callable);
153
    }
154
}
155