1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\models; |
4
|
|
|
|
5
|
|
|
use yii\helpers\ArrayHelper; |
6
|
|
|
use Itstructure\MultiLevelMenu\MenuWidget; |
7
|
|
|
use Itstructure\AdminModule\models\{MultilanguageTrait, Language}; |
8
|
|
|
use Itstructure\MFUploader\behaviors\{BehaviorMediafile, BehaviorAlbum}; |
9
|
|
|
use Itstructure\MFUploader\models\OwnerAlbum; |
10
|
|
|
use Itstructure\MFUploader\models\album\Album; |
11
|
|
|
use Itstructure\MFUploader\interfaces\UploadModelInterface; |
12
|
|
|
use app\traits\ThumbnailTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* This is the model class for table "pages". |
16
|
|
|
* |
17
|
|
|
* @property int|string $thumbnail thumbnail(mediafile id or url). |
18
|
|
|
* @property array $albums Existing album ids. |
19
|
|
|
* @property int $id |
20
|
|
|
* @property string $created_at |
21
|
|
|
* @property string $updated_at |
22
|
|
|
* @property int $parentId |
23
|
|
|
* @property int $newParentId |
24
|
|
|
* @property string $icon |
25
|
|
|
* @property int $active |
26
|
|
|
* |
27
|
|
|
* @property PageLanguage[] $pagesLanguages |
28
|
|
|
* @property Language[] $languages |
29
|
|
|
* |
30
|
|
|
* @package app\models |
31
|
|
|
*/ |
32
|
|
|
class Page extends ActiveRecord |
33
|
|
|
{ |
34
|
|
|
use MultilanguageTrait, ThumbnailTrait; |
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int|string thumbnail(mediafile id or url). |
38
|
|
|
*/ |
39
|
|
|
public $thumbnail; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
public $albums = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
public $newParentId; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Initialize. |
53
|
|
|
* Set albums, that page has. |
54
|
|
|
*/ |
55
|
|
|
public function init() |
56
|
|
|
{ |
57
|
|
|
$this->albums = $this->getAlbums(); |
58
|
|
|
|
59
|
|
|
parent::init(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritdoc |
64
|
|
|
*/ |
65
|
|
|
public static function tableName() |
66
|
|
|
{ |
67
|
|
|
return 'pages'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @inheritdoc |
72
|
|
|
*/ |
73
|
|
|
public function rules() |
74
|
|
|
{ |
75
|
|
|
return [ |
76
|
|
|
[ |
77
|
|
|
[ |
78
|
|
|
'created_at', |
79
|
|
|
'updated_at', |
80
|
|
|
], |
81
|
|
|
'safe', |
82
|
|
|
], |
83
|
|
|
[ |
84
|
|
|
[ |
85
|
|
|
'active' |
86
|
|
|
], |
87
|
|
|
'required', |
88
|
|
|
], |
89
|
|
|
[ |
90
|
|
|
[ |
91
|
|
|
'parentId', |
92
|
|
|
'newParentId', |
93
|
|
|
'active' |
94
|
|
|
], |
95
|
|
|
'integer', |
96
|
|
|
], |
97
|
|
|
[ |
98
|
|
|
'icon', |
99
|
|
|
'string', |
100
|
|
|
'max' => 64, |
101
|
|
|
], |
102
|
|
|
[ |
103
|
|
|
UploadModelInterface::FILE_TYPE_THUMB, |
104
|
|
|
function($attribute){ |
105
|
|
|
if (!is_numeric($this->{$attribute}) && !is_string($this->{$attribute})){ |
106
|
|
|
$this->addError($attribute, 'Tumbnail content must be a numeric or string.'); |
107
|
|
|
} |
108
|
|
|
}, |
109
|
|
|
'skipOnError' => false, |
110
|
|
|
], |
111
|
|
|
[ |
112
|
|
|
'albums', |
113
|
|
|
'each', |
114
|
|
|
'rule' => ['integer'], |
115
|
|
|
], |
116
|
|
|
]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @inheritdoc |
121
|
|
|
*/ |
122
|
|
|
public function behaviors() |
123
|
|
|
{ |
124
|
|
|
return ArrayHelper::merge(parent::behaviors(), [ |
125
|
|
|
'mediafile' => [ |
126
|
|
|
'class' => BehaviorMediafile::class, |
127
|
|
|
'name' => static::tableName(), |
128
|
|
|
'attributes' => [ |
129
|
|
|
UploadModelInterface::FILE_TYPE_THUMB, |
130
|
|
|
], |
131
|
|
|
], |
132
|
|
|
'albums' => [ |
133
|
|
|
'class' => BehaviorAlbum::class, |
134
|
|
|
'name' => static::tableName(), |
135
|
|
|
'attributes' => [ |
136
|
|
|
'albums', |
137
|
|
|
], |
138
|
|
|
], |
139
|
|
|
]); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
public function attributes(): array |
146
|
|
|
{ |
147
|
|
|
return [ |
148
|
|
|
UploadModelInterface::FILE_TYPE_THUMB, |
149
|
|
|
'albums', |
150
|
|
|
'id', |
151
|
|
|
'parentId', |
152
|
|
|
'icon', |
153
|
|
|
'active', |
154
|
|
|
'newParentId', |
155
|
|
|
'created_at', |
156
|
|
|
'updated_at', |
157
|
|
|
]; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @inheritdoc |
162
|
|
|
*/ |
163
|
|
|
public function attributeLabels() |
164
|
|
|
{ |
165
|
|
|
return [ |
166
|
|
|
'id' => 'ID', |
167
|
|
|
'parentId' => 'Parent Id', |
168
|
|
|
'icon' => 'Icon', |
169
|
|
|
'active' => 'Active', |
170
|
|
|
'created_at' => 'Created At', |
171
|
|
|
'updated_at' => 'Updated At', |
172
|
|
|
]; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param bool $insert |
177
|
|
|
* |
178
|
|
|
* @return bool |
179
|
|
|
*/ |
180
|
|
|
public function beforeSave($insert) |
181
|
|
|
{ |
182
|
|
|
$this->parentId = empty($this->newParentId) ? null : (int)$this->newParentId; |
183
|
|
|
|
184
|
|
|
if (empty($this->newParentId)) { |
185
|
|
|
$this->parentId = null; |
186
|
|
|
|
187
|
|
|
} elseif (MenuWidget::checkNewParentId($this, $this->newParentId)) { |
188
|
|
|
$this->parentId = $this->newParentId; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return parent::beforeSave($insert); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Reassigning child objects to their new parent after delete the main model record. |
196
|
|
|
*/ |
197
|
|
|
public function afterDelete() |
198
|
|
|
{ |
199
|
|
|
MenuWidget::afterDeleteMainModel($this); |
200
|
|
|
|
201
|
|
|
parent::afterDelete(); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @return array|\yii\db\ActiveRecord[] |
206
|
|
|
*/ |
207
|
|
|
public static function getMenu() |
208
|
|
|
{ |
209
|
|
|
return static::find()->select([ |
210
|
|
|
'id', 'parentId' |
211
|
|
|
])->all(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return array|\yii\db\ActiveRecord[] |
216
|
|
|
*/ |
217
|
|
|
public static function getActiveMenu() |
218
|
|
|
{ |
219
|
|
|
return static::find()->select([ |
220
|
|
|
'id', 'parentId' |
221
|
|
|
])->where([ |
222
|
|
|
'active' => 1 |
223
|
|
|
])->all(); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @return \yii\db\ActiveQuery |
228
|
|
|
*/ |
229
|
|
|
public function getPagesLanguages() |
230
|
|
|
{ |
231
|
|
|
return $this->hasMany(PageLanguage::class, [ |
232
|
|
|
'pages_id' => 'id' |
233
|
|
|
]); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @return \yii\db\ActiveQuery |
238
|
|
|
*/ |
239
|
|
|
public function getLanguages() |
240
|
|
|
{ |
241
|
|
|
return $this->hasMany(Language::class, [ |
242
|
|
|
'id' => 'language_id' |
243
|
|
|
])->viaTable('pages_language', [ |
244
|
|
|
'pages_id' => 'id' |
245
|
|
|
]); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Get albums, that catalog has. |
250
|
|
|
* |
251
|
|
|
* @return Album[] |
252
|
|
|
*/ |
253
|
|
|
public function getAlbums() |
254
|
|
|
{ |
255
|
|
|
return OwnerAlbum::getAlbumsQuery([ |
256
|
|
|
'owner' => $this->tableName(), |
257
|
|
|
'ownerId' => $this->id, |
258
|
|
|
'ownerAttribute' => 'albums', |
259
|
|
|
])->all(); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|