Completed
Push — master ( f45cc5...2edd5b )
by Igor
05:32
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 integer $type_id
15
 * @property string $title
16
 * @property string $text
17
 * @property string $preview
18
 * @property string $date_create
19
 * @property string $date_update
20
 * @property string $date_pub
21
 * @property string $reference
22
 * @property integer $status
23
 */
24
class News extends \yii\db\ActiveRecord
25
{
26
    const STATUS_BLOCKED = 0;
27
    const STATUS_ACTIVE  = 1;
28
29
    /**
30
     * @var array
31
     */
32
    public $tagValues;
33
    /**
34
     * @var array
35
     */
36
    public $gallery;
37
    /**
38
     * @var array
39
     */
40
    public $galleryTitles;
41
42 28
    public function __construct($config = [])
43
    {
44 28
        $this->attachBehavior('fileManager', require __DIR__ . '/behaviors/news/filemanager.php');
45 28
        parent::__construct($config);
46 28
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51 28
    public static function tableName()
52
    {
53 28
        return 'news';
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59 7
    public function rules()
60
    {
61
        return [
62
            [
63
                ['title', 'type_id', 'text', 'date_pub'], 'required'
64 7
            ],
65
            [
66
                [
67
                    'title', 'type_id', 'text', 'date_pub', 'preview', 'gallery', 'galleryTitles',
68
                    'reference', 'status', 'tagValues'
69
                ], 'safe'
70
            ],
71
72
            ['type_id', 'integer'],
73 7
            ['type_id', 'exist', 'targetClass' => NewsType::className(), 'targetAttribute' => ['type_id' => 'id']],
74
75
            ['title', 'string', 'max' => 255],
76
            ['text', 'string'],
77
78
            ['date_pub', 'date', 'format' => 'php:Y-m-d H:i:s',
79
                'timestampAttribute' => 'date_pub',
80
                'timestampAttributeFormat' => 'php:Y-m-d H:i:s'
81
            ],
82
83
            ['reference', 'url'],
84
            ['reference', 'string', 'max' => 255],
85
86
            ['status', 'integer'],
87 7
            ['status', 'in', 'range' => array_keys(News::getStatuses())],
88
        ];
89
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94 26
    public function attributeLabels()
95
    {
96
        return [
97 26
            'id' => Yii::t('app', 'ID'),
98 26
            'type_id' => Yii::t('app', 'Type'),
99 26
            'title' => Yii::t('app', 'Title'),
100 26
            'text' => Yii::t('app', 'Text'),
101 26
            'preview' => Yii::t('app', 'Preview'),
102 26
            'gallery' => Yii::t('app', 'Gallery'),
103 26
            'date_create' => Yii::t('app', 'Date create'),
104 26
            'date_update' => Yii::t('app', 'Date update'),
105 26
            'date_pub' => Yii::t('app', 'Date publication'),
106 26
            'reference' => Yii::t('app', 'Reference'),
107 26
            'status' => Yii::t('app', 'Status'),
108
109 26
            'tagValues' => Yii::t('app', 'Tags'),
110
        ];
111
    }
112
113
    /**
114
     * @inheritdoc
115
     */
116 5
    public function attributeHints()
117
    {
118
        return [
119
120 5
        ];
121
    }
122
123
    /**
124
     * @inheritdoc
125
     */
126 28
    public function behaviors()
127
    {
128
        return [
129
            [
130 28
                'class' => TimestampBehavior::className(),
131 28
                'createdAtAttribute' => 'date_create',
132 28
                'updatedAtAttribute' => 'date_update',
133 28
                'value' => new \yii\db\Expression('NOW()'),
134 28
            ],
135
136
            [
137
                'class' => 'creocoder\taggable\TaggableBehavior',
138
                // 'tagValuesAsArray' => false,
139
                // 'tagRelation' => 'tags',
140
                 'tagValueAttribute' => 'title',
141
                 'tagFrequencyAttribute' => 'count',
142
            ],
143
        ];
144
    }
145
146 3
    public function transactions()
147
    {
148
        return [
149 3
            'create' => self::OP_ALL,
150 3
            'update' => self::OP_ALL,
151 3
            'delete' => self::OP_ALL,
152
        ];
153
    }
154
155
    /**
156
     * @inheritdoc
157
     */
158
    public function beforeSave($insert)
159
    {
160
        if (parent::beforeSave($insert)) {
161
            $this->setTagValues($this->tagValues);
162
163
            return true;
164
        }
165
166
        return false; // @codeCoverageIgnore
167
    }
168
169
    /**
170
     * @inheritdoc
171
     * @return NewsQuery
172
     */
173 27
    public static function find()
174
    {
175 27
        return new NewsQuery(get_called_class());
176
    }
177
178
    /**
179
     * Get all statuses
180
     *
181
     * @return string[]
182
     */
183 26
    public static function getStatuses()
184
    {
185
        return [
186 26
            self::STATUS_BLOCKED => Yii::t('app', 'Not published'),
187 26
            self::STATUS_ACTIVE  => Yii::t('app', 'Published'),
188
        ];
189
    }
190
191
    /**
192
     * Get statuse name
193
     *
194
     * @return string
195
     */
196 25
    public function getStatusName()
197
    {
198 25
        $statuses = self::getStatuses();
199 25
        return isset($statuses[$this->status]) ? $statuses[$this->status] : '';
200
    }
201
202
    /**
203
     * Is it blocked?
204
     *
205
     * @param bool
206
     */
207 1
    public function isBlocked()
208
    {
209 1
        return $this->status == self::STATUS_BLOCKED;
210
    }
211
212
    /**
213
     * Is it active?
214
     *
215
     * @param bool
216
     */
217 1
    public function isActive()
218
    {
219 1
        return $this->status == self::STATUS_ACTIVE;
220
    }
221
222
    /**
223
     * @return \yii\db\ActiveQuery
224
     */
225 25
    public function getType()
226
    {
227 25
        return $this->hasOne(NewsType::className(), array('id' => 'type_id'));
228
    }
229
230
    /**
231
     * Get tags.
232
     *
233
     * @return \yii\db\ActiveQuery
234
     */
235 3
    public function getTags()
236
    {
237 3
        return $this->owner
238 3
            ->hasMany(Tag::className(), ['id' => 'tag_id'])
239 3
            ->viaTable('{{%news_tags}}', ['news_id' => 'id']);
240
    }
241
242
    public function getFiles($callable = null)
243
    {
244
        return $this
245
            ->hasMany(File::className(), ['id' => 'file_id'])
246
            ->viaTable('news_files', ['news_id' => 'id'], $callable);
247
    }
248
}
249