Completed
Push — master ( e1f657...e57b78 )
by Igor
06:03
created

News   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 235
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 9
Bugs 0 Features 2
Metric Value
wmc 13
c 9
b 0
f 2
lcom 2
cbo 9
dl 0
loc 235
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A tableName() 0 4 1
B rules() 0 26 1
A attributeLabels() 0 18 1
A attributeHints() 0 6 1
B behaviors() 0 79 1
A transactions() 0 8 1
A beforeSave() 0 11 2
A getStatuses() 0 7 1
A getStatusName() 0 5 2
A getType() 0 4 1
A getTags() 0 6 1
1
<?php
2
3
namespace app\models;
4
5
use Intervention\Image\ImageManagerStatic as Image;
6
use app\components\BaseActive;
7
use app\helpers\Util;
8
use yii\behaviors\TimestampBehavior;
9
use Yii;
10
11
/**
12
 * This is the model class for table "news".
13
 *
14
 * @property integer $id
15
 * @property integer $type_id
16
 * @property string $title
17
 * @property string $text
18
 * @property string $preview
19
 * @property string $date_create
20
 * @property string $date_update
21
 * @property string $date_pub
22
 * @property string $reference
23
 * @property integer $status
24
 */
25
class News extends BaseActive
26
{
27
    const STATUS_BLOCKED = 0;
28
    const STATUS_ACTIVE  = 1;
29
30
    /**
31
     * @var array
32
     */
33
    public $tagValues;
34
    /**
35
     * @var array
36
     */
37
    public $gallery;
38
39
    /**
40
     * @inheritdoc
41
     */
42
    public static function tableName()
43
    {
44
        return 'news';
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50
    public function rules()
51
    {
52
        return [
53
            //[['title'], 'trim'],
54
            [['title', 'type_id', 'text', 'date_pub'], 'required'],
55
            [['title', 'type_id', 'text', 'date_pub',
56
            'preview', 'gallery', 'reference', 'status', 'tagValues'], 'safe'],
57
58
            ['type_id', 'integer'],
59
            ['type_id', 'exist', 'targetClass' => NewsType::className(), 'targetAttribute' => ['type_id' => 'id']],
60
61
            ['title', 'string', 'max' => 255],
62
63
            ['text', 'string'],
64
65
            ['preview', 'string', 'max' => 255],
66
67
            ['date_pub', 'date', 'format' => 'php:Y-m-d H:i:s'],
68
69
            ['reference', 'url'],
70
            ['reference', 'string', 'max' => 255],
71
72
            ['status', 'integer'],
73
            ['status', 'in', 'range' => array_keys(News::getStatuses())],
74
        ];
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80
    public function attributeLabels()
81
    {
82
        return [
83
            'id' => Yii::t('app', 'ID'),
84
            'type_id' => Yii::t('app', 'Type'),
85
            'title' => Yii::t('app', 'Title'),
86
            'text' => Yii::t('app', 'Text'),
87
            'preview' => Yii::t('app', 'Preview'),
88
            'gallery' => Yii::t('app', 'Gallery'),
89
            'date_create' => Yii::t('app', 'Date create'),
90
            'date_update' => Yii::t('app', 'Date update'),
91
            'date_pub' => Yii::t('app', 'Date publication'),
92
            'reference' => Yii::t('app', 'Reference'),
93
            'status' => Yii::t('app', 'Status'),
94
95
            'tagValues' => Yii::t('app', 'Tags'),
96
        ];
97
    }
98
99
    /**
100
     * @inheritdoc
101
     */
102
    public function attributeHints()
103
    {
104
        return [
105
106
        ];
107
    }
108
109
    /**
110
     * @inheritdoc
111
     */
112
    public function behaviors()
113
    {
114
        return [
115
            [
116
                'class' => TimestampBehavior::className(),
117
                'createdAtAttribute' => 'date_create',
118
                'updatedAtAttribute' => 'date_update',
119
                'value' => new \yii\db\Expression('NOW()'),
120
            ],
121
122
            [
123
                'class' => 'creocoder\taggable\TaggableBehavior',
124
                // 'tagValuesAsArray' => false,
125
                // 'tagRelation' => 'tags',
126
                 'tagValueAttribute' => 'title',
127
                 'tagFrequencyAttribute' => 'count',
128
            ],
129
130
            [
131
                'class' => 'rkit\filemanager\behaviors\FileBehavior',
132
                'attributes' => [
133
                    'text' => [
134
                        'storage' => 'rkit\filemanager\storages\LocalStorage',
135
                        'rules' => [
136
                            'mimeTypes' => ['image/png', 'image/jpg', 'image/jpeg'],
137
                            'extensions' => ['jpg', 'jpeg', 'png'],
138
                            'maxSize' => 1024 * 1024 * 1, // 1 MB
139
                            'tooBig' => Yii::t('app', 'File size must not exceed') . ' 1Mb'
140
                        ]
141
                    ],
142
                    'preview' => [
143
                        'storage' => 'rkit\filemanager\storages\LocalStorage',
144
                        'saveFilePath' => true,
145
                        'rules' => [
146
                            'imageSize' => ['minWidth' => 300, 'minHeight' => 300],
147
                            'mimeTypes' => ['image/png', 'image/jpg', 'image/jpeg'],
148
                            'extensions' => ['jpg', 'jpeg', 'png'],
149
                            'maxSize' => 1024 * 1024 * 1, // 1 MB
150
                            'tooBig' => Yii::t('app', 'File size must not exceed') . ' 1Mb'
151
                        ],
152
                        'preset' => [
153
                            '200x200' => function ($realPath, $publicPath, $thumbPath) {
154
                                Image::make($realPath . $publicPath)
155
                                    ->fit(200, 200)
156
                                    ->save($realPath . $thumbPath, 100);
157
                            },
158
                            '1000x1000' => function ($realPath, $publicPath, $thumbPath) {
159
                                Image::make($realPath . $publicPath)
160
                                    ->resize(1000, 1000, function ($constraint) {
161
                                        $constraint->aspectRatio();
162
                                        $constraint->upsize();
163
                                    })
164
                                    ->save(null, 100);
165
                            },
166
                        ],
167
                        'applyPresetAfterUpload' => '*'
168
                    ],
169
                    'gallery' => [
170
                        'storage' => 'rkit\filemanager\storages\LocalStorage',
171
                        'multiple' => true,
172
                        'rules' => [
173
                            'imageSize' => ['minWidth' => 300, 'minHeight' => 300],
174
                            'mimeTypes' => ['image/png', 'image/jpg', 'image/jpeg'],
175
                            'extensions' => ['jpg', 'jpeg', 'png'],
176
                            'maxSize' => 1024 * 1024 * 1, // 1 MB
177
                            'tooBig' => Yii::t('app', 'File size must not exceed') . ' 1Mb'
178
                        ],
179
                        'preset' => [
180
                            '80x80' => function ($realPath, $publicPath, $thumbPath) {
181
                                Image::make($realPath . $publicPath)
182
                                    ->fit(80, 80)
183
                                    ->save($realPath . $thumbPath, 100);
184
                            },
185
                        ],
186
                    ]
187
                ]
188
            ]
189
        ];
190
    }
191
192
    public function transactions()
193
    {
194
        return [
195
            'create' => self::OP_ALL,
196
            'update' => self::OP_ALL,
197
            'delete' => self::OP_ALL,
198
        ];
199
    }
200
201
    /**
202
     * @inheritdoc
203
     */
204
    public function beforeSave($insert)
205
    {
206
        if (parent::beforeSave($insert)) {
207
            $this->date_pub = Util::convertTz($this->date_pub, Yii::$app->params['mainTimeZone'], 'UTC');
208
            $this->setTagValues($this->tagValues);
209
210
            return true;
211
        }
212
213
        return false;
214
    }
215
216
    /**
217
     * Get all statuses
218
     *
219
     * @return string[]
220
     */
221
    public static function getStatuses()
222
    {
223
        return [
224
            self::STATUS_BLOCKED => Yii::t('app', 'Not published'),
225
            self::STATUS_ACTIVE  => Yii::t('app', 'Published'),
226
        ];
227
    }
228
229
    /**
230
     * Get statuse name
231
     *
232
     * @return string
233
     */
234
    public function getStatusName()
235
    {
236
        $statuses = $this->getStatuses();
237
        return isset($statuses[$this->status]) ? $statuses[$this->status] : '';
238
    }
239
240
    /**
241
     * @return \yii\db\ActiveQuery
242
     */
243
    public function getType()
244
    {
245
        return $this->hasOne(NewsType::className(), array('id' => 'type_id'));
246
    }
247
248
    /**
249
     * Get tags.
250
     *
251
     * @return \yii\db\ActiveQuery
252
     */
253
    public function getTags()
254
    {
255
        return $this->owner
256
            ->hasMany(Tag::className(), ['id' => 'tag_id'])
257
            ->viaTable('{{%news_tag_assn}}', ['news_id' => 'id']);
258
    }
259
}
260