Passed
Push — master ( fe7b3f...22651e )
by Andrey
08:30
created

Page::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace app\models;
4
5
use yii\helpers\{ArrayHelper, Html};
6
use Itstructure\MultiLevelMenu\MenuWidget;
7
use Itstructure\AdminModule\models\{MultilanguageTrait, Language};
8
use Itstructure\MFUploader\Module as MFUModule;
9
use Itstructure\MFUploader\behaviors\{BehaviorMediafile, BehaviorAlbum};
10
use Itstructure\MFUploader\models\{Mediafile, OwnerAlbum, OwnerMediafile};
11
use Itstructure\MFUploader\models\album\Album;
12
use Itstructure\MFUploader\interfaces\UploadModelInterface;
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;
0 ignored issues
show
introduced by
The trait Itstructure\AdminModule\models\MultilanguageTrait requires some properties which are not provided by app\models\Page: $language, $shortName
Loading history...
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
     * @var array|null|\yii\db\ActiveRecord|Mediafile
53
     */
54
    protected $thumbnailModel;
55
56
    /**
57
     * Initialize.
58
     * Set albums, that page has.
59
     */
60
    public function init()
61
    {
62
        $this->albums = $this->getAlbums();
63
64
        parent::init();
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public static function tableName()
71
    {
72
        return 'pages';
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78
    public function rules()
79
    {
80
        return [
81
            [
82
                [
83
                    'created_at',
84
                    'updated_at',
85
                ],
86
                'safe',
87
            ],
88
            [
89
                [
90
                    'active'
91
                ],
92
                'required',
93
            ],
94
            [
95
                [
96
                    'parentId',
97
                    'newParentId',
98
                    'active'
99
                ],
100
                'integer',
101
            ],
102
            [
103
                'icon',
104
                'string',
105
                'max' => 64,
106
            ],
107
            [
108
                UploadModelInterface::FILE_TYPE_THUMB,
109
                function($attribute){
110
                    if (!is_numeric($this->{$attribute}) && !is_string($this->{$attribute})){
111
                        $this->addError($attribute, 'Tumbnail content must be a numeric or string.');
112
                    }
113
                },
114
                'skipOnError' => false,
115
            ],
116
            [
117
                'albums',
118
                'each',
119
                'rule' => ['integer'],
120
            ],
121
        ];
122
    }
123
124
    /**
125
     * @inheritdoc
126
     */
127
    public function behaviors()
128
    {
129
        return ArrayHelper::merge(parent::behaviors(), [
130
            'mediafile' => [
131
                'class' => BehaviorMediafile::class,
132
                'name' => static::tableName(),
133
                'attributes' => [
134
                    UploadModelInterface::FILE_TYPE_THUMB,
135
                ],
136
            ],
137
            'albums' => [
138
                'class' => BehaviorAlbum::class,
139
                'name' => static::tableName(),
140
                'attributes' => [
141
                    'albums',
142
                ],
143
            ],
144
        ]);
145
    }
146
147
    /**
148
     * @return array
149
     */
150
    public function attributes(): array
151
    {
152
        return [
153
            UploadModelInterface::FILE_TYPE_THUMB,
154
            'albums',
155
            'id',
156
            'parentId',
157
            'icon',
158
            'active',
159
            'newParentId',
160
            'created_at',
161
            'updated_at',
162
        ];
163
    }
164
165
    /**
166
     * @inheritdoc
167
     */
168
    public function attributeLabels()
169
    {
170
        return [
171
            'id' => 'ID',
172
            'parentId' => 'Parent Id',
173
            'icon' => 'Icon',
174
            'active' => 'Active',
175
            'created_at' => 'Created At',
176
            'updated_at' => 'Updated At',
177
        ];
178
    }
179
180
    /**
181
     * @param bool $insert
182
     *
183
     * @return bool
184
     */
185
    public function beforeSave($insert)
186
    {
187
        $this->parentId = empty($this->newParentId) ? null : (int)$this->newParentId;
188
189
        if (empty($this->newParentId)) {
190
            $this->parentId = null;
191
192
        } elseif (MenuWidget::checkNewParentId($this, $this->newParentId)) {
193
            $this->parentId = $this->newParentId;
194
        }
195
196
        return parent::beforeSave($insert);
197
    }
198
199
    /**
200
     * Reassigning child objects to their new parent after delete the main model record.
201
     */
202
    public function afterDelete()
203
    {
204
        MenuWidget::afterDeleteMainModel($this);
205
206
        parent::afterDelete();
207
    }
208
209
    /**
210
     * @return array|\yii\db\ActiveRecord[]
211
     */
212
    public static function getMenu()
213
    {
214
        return static::find()->select([
215
            'id', 'parentId'
216
        ])->all();
217
    }
218
219
    /**
220
     * @return array|\yii\db\ActiveRecord[]
221
     */
222
    public static function getActiveMenu()
223
    {
224
        return static::find()->select([
225
            'id', 'parentId'
226
        ])->where([
227
            'active' => 1
228
        ])->all();
229
    }
230
231
    /**
232
     * @return \yii\db\ActiveQuery
233
     */
234
    public function getPagesLanguages()
235
    {
236
        return $this->hasMany(PageLanguage::class, [
237
            'pages_id' => 'id'
238
        ]);
239
    }
240
241
    /**
242
     * @return \yii\db\ActiveQuery
243
     */
244
    public function getLanguages()
245
    {
246
        return $this->hasMany(Language::class, [
247
            'id' => 'language_id'
248
        ])->viaTable('pages_language', [
249
            'pages_id' => 'id'
250
        ]);
251
    }
252
253
    /**
254
     * Get albums, that catalog has.
255
     *
256
     * @return Album[]
257
     */
258
    public function getAlbums()
259
    {
260
        return OwnerAlbum::getAlbumsQuery([
261
            'owner' => $this->tableName(),
262
            'ownerId' => $this->id,
263
            'ownerAttribute' => 'albums',
264
        ])->all();
265
    }
266
267
    /**
268
     * Get album thumb image.
269
     *
270
     * @param array  $options
271
     *
272
     * @return mixed
273
     */
274
    public function getDefaultThumbImage(array $options = [])
275
    {
276
        $thumbnailModel = $this->getThumbnailModel();
277
278
        if (null === $thumbnailModel){
279
            return null;
280
        }
281
282
        $url = $thumbnailModel->getThumbUrl(MFUModule::THUMB_ALIAS_DEFAULT);
283
284
        if (empty($url)) {
285
            return null;
286
        }
287
288
        if (empty($options['alt'])) {
289
            $options['alt'] = $thumbnailModel->alt;
290
        }
291
292
        return Html::img($url, $options);
293
    }
294
295
    /**
296
     * Get page's thumbnail.
297
     *
298
     * @return array|null|\yii\db\ActiveRecord|Mediafile
299
     */
300
    public function getThumbnailModel()
301
    {
302
        if (null === $this->id) {
303
            return null;
304
        }
305
306
        if ($this->thumbnailModel === null) {
307
            $this->thumbnailModel = OwnerMediafile::getOwnerThumbnail($this->tableName(), $this->id);
308
        }
309
310
        return $this->thumbnailModel;
311
    }
312
}
313