1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\behaviors\TimestampBehavior; |
7
|
|
|
use Intervention\Image\ImageManagerStatic as Image; |
8
|
|
|
use app\helpers\Util; |
9
|
|
|
use app\models\query\NewsQuery; |
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 \yii\db\ActiveRecord |
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
|
|
|
[ |
54
|
|
|
['title', 'type_id', 'text', 'date_pub'], 'required' |
55
|
|
|
], |
56
|
|
|
[ |
57
|
|
|
[ |
58
|
|
|
'title', 'type_id', 'text', 'date_pub', 'preview', 'gallery', |
59
|
|
|
'reference', 'status', 'tagValues' |
60
|
|
|
], 'safe' |
61
|
|
|
], |
62
|
|
|
|
63
|
|
|
['type_id', 'integer'], |
64
|
|
|
['type_id', 'exist', 'targetClass' => NewsType::className(), 'targetAttribute' => ['type_id' => 'id']], |
65
|
|
|
|
66
|
|
|
['title', 'string', 'max' => 255], |
67
|
|
|
['text', 'string'], |
68
|
|
|
['date_pub', 'date', 'format' => 'php:Y-m-d H:i:s'], |
69
|
|
|
|
70
|
|
|
['reference', 'url'], |
71
|
|
|
['reference', 'string', 'max' => 255], |
72
|
|
|
|
73
|
|
|
['status', 'integer'], |
74
|
|
|
['status', 'in', 'range' => array_keys(News::getStatuses())], |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritdoc |
80
|
|
|
*/ |
81
|
|
|
public function attributeLabels() |
82
|
|
|
{ |
83
|
|
|
return [ |
84
|
|
|
'id' => Yii::t('app', 'ID'), |
85
|
|
|
'type_id' => Yii::t('app', 'Type'), |
86
|
|
|
'title' => Yii::t('app', 'Title'), |
87
|
|
|
'text' => Yii::t('app', 'Text'), |
88
|
|
|
'preview' => Yii::t('app', 'Preview'), |
89
|
|
|
'gallery' => Yii::t('app', 'Gallery'), |
90
|
|
|
'date_create' => Yii::t('app', 'Date create'), |
91
|
|
|
'date_update' => Yii::t('app', 'Date update'), |
92
|
|
|
'date_pub' => Yii::t('app', 'Date publication'), |
93
|
|
|
'reference' => Yii::t('app', 'Reference'), |
94
|
|
|
'status' => Yii::t('app', 'Status'), |
95
|
|
|
|
96
|
|
|
'tagValues' => Yii::t('app', 'Tags'), |
97
|
|
|
]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @inheritdoc |
102
|
|
|
*/ |
103
|
|
|
public function attributeHints() |
104
|
|
|
{ |
105
|
|
|
return [ |
106
|
|
|
|
107
|
|
|
]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @inheritdoc |
112
|
|
|
*/ |
113
|
|
|
public function behaviors() |
114
|
|
|
{ |
115
|
|
|
return [ |
116
|
|
|
[ |
117
|
|
|
'class' => TimestampBehavior::className(), |
118
|
|
|
'createdAtAttribute' => 'date_create', |
119
|
|
|
'updatedAtAttribute' => 'date_update', |
120
|
|
|
'value' => new \yii\db\Expression('NOW()'), |
121
|
|
|
], |
122
|
|
|
|
123
|
|
|
[ |
124
|
|
|
'class' => 'creocoder\taggable\TaggableBehavior', |
125
|
|
|
// 'tagValuesAsArray' => false, |
126
|
|
|
// 'tagRelation' => 'tags', |
127
|
|
|
'tagValueAttribute' => 'title', |
128
|
|
|
'tagFrequencyAttribute' => 'count', |
129
|
|
|
], |
130
|
|
|
|
131
|
|
|
[ |
132
|
|
|
'class' => 'rkit\filemanager\behaviors\FileBehavior', |
133
|
|
|
'attributes' => [ |
134
|
|
|
'text' => [ |
135
|
|
|
'storage' => 'rkit\filemanager\storages\LocalStorage', |
136
|
|
|
'rules' => [ |
137
|
|
|
'mimeTypes' => ['image/png', 'image/jpg', 'image/jpeg'], |
138
|
|
|
'extensions' => ['jpg', 'jpeg', 'png'], |
139
|
|
|
'maxSize' => 1024 * 1024 * 1, // 1 MB |
140
|
|
|
'tooBig' => Yii::t('app.messages', 'File size must not exceed') . ' 1Mb' |
141
|
|
|
] |
142
|
|
|
], |
143
|
|
|
'preview' => [ |
144
|
|
|
'storage' => 'rkit\filemanager\storages\LocalStorage', |
145
|
|
|
'saveFilePath' => true, |
146
|
|
|
'rules' => [ |
147
|
|
|
'imageSize' => ['minWidth' => 300, 'minHeight' => 300], |
148
|
|
|
'mimeTypes' => ['image/png', 'image/jpg', 'image/jpeg'], |
149
|
|
|
'extensions' => ['jpg', 'jpeg', 'png'], |
150
|
|
|
'maxSize' => 1024 * 1024 * 1, // 1 MB |
151
|
|
|
'tooBig' => Yii::t('app.messages', 'File size must not exceed') . ' 1Mb' |
152
|
|
|
], |
153
|
|
|
'preset' => [ |
154
|
|
|
'200x200' => function ($realPath, $publicPath, $thumbPath) { |
155
|
|
|
Image::make($realPath . $publicPath) |
156
|
|
|
->fit(200, 200) |
157
|
|
|
->save($realPath . $thumbPath, 100); |
158
|
|
|
}, |
159
|
|
|
'1000x1000' => function ($realPath, $publicPath, $thumbPath) { |
160
|
|
|
Image::make($realPath . $publicPath) |
161
|
|
|
->resize(1000, 1000, function ($constraint) { |
162
|
|
|
$constraint->aspectRatio(); |
163
|
|
|
$constraint->upsize(); |
164
|
|
|
}) |
165
|
|
|
->save(null, 100); |
166
|
|
|
}, |
167
|
|
|
], |
168
|
|
|
'applyPresetAfterUpload' => '*' |
169
|
|
|
], |
170
|
|
|
'gallery' => [ |
171
|
|
|
'storage' => 'rkit\filemanager\storages\LocalStorage', |
172
|
|
|
'multiple' => true, |
173
|
|
|
'rules' => [ |
174
|
|
|
'imageSize' => ['minWidth' => 300, 'minHeight' => 300], |
175
|
|
|
'mimeTypes' => ['image/png', 'image/jpg', 'image/jpeg'], |
176
|
|
|
'extensions' => ['jpg', 'jpeg', 'png'], |
177
|
|
|
'maxSize' => 1024 * 1024 * 1, // 1 MB |
178
|
|
|
'tooBig' => Yii::t('app.messages', 'File size must not exceed') . ' 1Mb' |
179
|
|
|
], |
180
|
|
|
'preset' => [ |
181
|
|
|
'80x80' => function ($realPath, $publicPath, $thumbPath) { |
182
|
|
|
Image::make($realPath . $publicPath) |
183
|
|
|
->fit(80, 80) |
184
|
|
|
->save($realPath . $thumbPath, 100); |
185
|
|
|
}, |
186
|
|
|
], |
187
|
|
|
] |
188
|
|
|
] |
189
|
|
|
] |
190
|
|
|
]; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function transactions() |
194
|
|
|
{ |
195
|
|
|
return [ |
196
|
|
|
'create' => self::OP_ALL, |
197
|
|
|
'update' => self::OP_ALL, |
198
|
|
|
'delete' => self::OP_ALL, |
199
|
|
|
]; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @inheritdoc |
204
|
|
|
*/ |
205
|
|
|
public function beforeSave($insert) |
206
|
|
|
{ |
207
|
|
|
if (parent::beforeSave($insert)) { |
208
|
|
|
$this->date_pub = Util::convertTz($this->date_pub, Yii::$app->params['mainTimeZone'], 'UTC'); |
209
|
|
|
$this->setTagValues($this->tagValues); |
210
|
|
|
|
211
|
|
|
return true; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return false; // @codeCoverageIgnore |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @inheritdoc |
219
|
|
|
* @return NewsQuery |
220
|
|
|
*/ |
221
|
|
|
public static function find() |
222
|
|
|
{ |
223
|
|
|
return new NewsQuery(get_called_class()); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Get all statuses |
228
|
|
|
* |
229
|
|
|
* @return string[] |
230
|
|
|
*/ |
231
|
|
|
public static function getStatuses() |
232
|
|
|
{ |
233
|
|
|
return [ |
234
|
|
|
self::STATUS_BLOCKED => Yii::t('app', 'Not published'), |
235
|
|
|
self::STATUS_ACTIVE => Yii::t('app', 'Published'), |
236
|
|
|
]; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Get statuse name |
241
|
|
|
* |
242
|
|
|
* @return string |
243
|
|
|
*/ |
244
|
|
|
public function getStatusName() |
245
|
|
|
{ |
246
|
|
|
$statuses = $this->getStatuses(); |
247
|
|
|
return isset($statuses[$this->status]) ? $statuses[$this->status] : ''; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Is it blocked? |
252
|
|
|
* |
253
|
|
|
* @param bool |
254
|
|
|
*/ |
255
|
|
|
public function isBlocked() |
256
|
|
|
{ |
257
|
|
|
return $this->status == self::STATUS_BLOCKED; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Is it active? |
262
|
|
|
* |
263
|
|
|
* @param bool |
264
|
|
|
*/ |
265
|
|
|
public function isActive() |
266
|
|
|
{ |
267
|
|
|
return $this->status == self::STATUS_ACTIVE; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @return \yii\db\ActiveQuery |
272
|
|
|
*/ |
273
|
|
|
public function getType() |
274
|
|
|
{ |
275
|
|
|
return $this->hasOne(NewsType::className(), array('id' => 'type_id')); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Get tags. |
280
|
|
|
* |
281
|
|
|
* @return \yii\db\ActiveQuery |
282
|
|
|
*/ |
283
|
|
|
public function getTags() |
284
|
|
|
{ |
285
|
|
|
return $this->owner |
286
|
|
|
->hasMany(Tag::className(), ['id' => 'tag_id']) |
287
|
|
|
->viaTable('{{%news_tag_assn}}', ['news_id' => 'id']); |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|