Completed
Push — master ( 193986...cdf399 )
by Andrey
11:47
created

Page::getAlbums()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 8
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace app\models;
4
5
use yii\helpers\ArrayHelper;
6
use Itstructure\MultiLevelMenu\MenuWidget;
7
use Itstructure\MFUploader\behaviors\{BehaviorMediafile, BehaviorAlbum};
8
use Itstructure\MFUploader\models\OwnerAlbum;
9
use Itstructure\MFUploader\models\album\Album;
10
use Itstructure\MFUploader\interfaces\UploadModelInterface;
11
use app\traits\ThumbnailTrait;
12
13
/**
14
 * This is the model class for table "pages".
15
 *
16
 * @property int|string $thumbnail thumbnail(mediafile id or url).
17
 * @property array $albums Existing album ids.
18
 * @property int $id
19
 * @property string $title
20
 * @property string $description
21
 * @property string $content
22
 * @property string $metaKeys
23
 * @property string $metaDescription
24
 * @property string $created_at
25
 * @property string $updated_at
26
 * @property int $parentId
27
 * @property int $newParentId
28
 * @property string $icon
29
 * @property int $active
30
 *
31
 * @package app\models
32
 */
33
class Page extends ActiveRecord
34
{
35
    use ThumbnailTrait;
36
37
    /**
38
     * @var int|string thumbnail(mediafile id or url).
39
     */
40
    public $thumbnail;
41
42
    /**
43
     * @var array
44
     */
45
    public $albums = [];
46
47
    /**
48
     * @var int
49
     */
50
    public $newParentId;
51
52
    /**
53
     * Initialize.
54
     * Set albums, that page has.
55
     */
56
    public function init()
57
    {
58
        $this->albums = $this->getAlbums();
59
60
        parent::init();
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66
    public static function tableName()
67
    {
68
        return 'pages';
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74
    public function rules()
75
    {
76
        return [
77
            [
78
                [
79
                    'title',
80
                    'content',
81
                    'active'
82
                ],
83
                'required',
84
            ],
85
            [
86
                [
87
                    'description',
88
                    'content',
89
                ],
90
                'string',
91
            ],
92
            [
93
                [
94
                    'title',
95
                    'metaKeys',
96
                    'metaDescription',
97
                ],
98
                'string',
99
                'max' => 255,
100
            ],
101
            [
102
                [
103
                    'parentId',
104
                    'newParentId',
105
                    'active'
106
                ],
107
                'integer',
108
            ],
109
            [
110
                'icon',
111
                'string',
112
                'max' => 64,
113
            ],
114
            [
115
                UploadModelInterface::FILE_TYPE_THUMB,
116 View Code Duplication
                function($attribute){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
                    if (!is_numeric($this->{$attribute}) && !is_string($this->{$attribute})){
118
                        $this->addError($attribute, 'Tumbnail content must be a numeric or string.');
119
                    }
120
                },
121
                'skipOnError' => false,
122
            ],
123
            [
124
                'albums',
125
                'each',
126
                'rule' => ['integer'],
127
            ],
128
            [
129
                'title',
130
                'unique',
131
                'skipOnError'     => true,
132
                'filter' => $this->getScenario() == self::SCENARIO_UPDATE ? 'id != '.$this->id : ''
133
            ],
134
            [
135
                [
136
                    'created_at',
137
                    'updated_at',
138
                ],
139
                'safe',
140
            ],
141
        ];
142
    }
143
144
    /**
145
     * @inheritdoc
146
     */
147 View Code Duplication
    public function behaviors()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148
    {
149
        return ArrayHelper::merge(parent::behaviors(), [
150
            'mediafile' => [
151
                'class' => BehaviorMediafile::class,
152
                'name' => static::tableName(),
153
                'attributes' => [
154
                    UploadModelInterface::FILE_TYPE_THUMB,
155
                ],
156
            ],
157
            'albums' => [
158
                'class' => BehaviorAlbum::class,
159
                'name' => static::tableName(),
160
                'attributes' => [
161
                    'albums',
162
                ],
163
            ],
164
        ]);
165
    }
166
167
    /**
168
     * @return array
169
     */
170 View Code Duplication
    public function attributes(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
171
    {
172
        return [
173
            UploadModelInterface::FILE_TYPE_THUMB,
174
            'albums',
175
            'id',
176
            'parentId',
177
            'icon',
178
            'active',
179
            'newParentId',
180
            'title',
181
            'description',
182
            'content',
183
            'metaKeys',
184
            'metaDescription',
185
            'created_at',
186
            'updated_at',
187
        ];
188
    }
189
190
    /**
191
     * @inheritdoc
192
     */
193 View Code Duplication
    public function attributeLabels()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
194
    {
195
        return [
196
            'id' => 'ID',
197
            'parentId' => 'Parent Id',
198
            'icon' => 'Icon',
199
            'active' => 'Active',
200
            'title' => 'Title',
201
            'description' => 'Description',
202
            'content' => 'Content',
203
            'metaKeys' => 'Meta keys',
204
            'metaDescription' => 'Meta description',
205
            'created_at' => 'Created At',
206
            'updated_at' => 'Updated At',
207
        ];
208
    }
209
210
    /**
211
     * @param bool $insert
212
     *
213
     * @return bool
214
     */
215
    public function beforeSave($insert)
216
    {
217
        $this->parentId = empty($this->newParentId) ? null : (int)$this->newParentId;
218
219
        if (empty($this->newParentId)) {
220
            $this->parentId = null;
221
222
        } elseif (MenuWidget::checkNewParentId($this, $this->newParentId)) {
223
            $this->parentId = $this->newParentId;
224
        }
225
226
        return parent::beforeSave($insert);
227
    }
228
229
    /**
230
     * Reassigning child objects to their new parent after delete the main model record.
231
     */
232
    public function afterDelete()
233
    {
234
        MenuWidget::afterDeleteMainModel($this);
235
236
        parent::afterDelete();
237
    }
238
239
    /**
240
     * @return array|\yii\db\ActiveRecord[]
241
     */
242
    public static function getMenu()
243
    {
244
        return static::find()->select([
245
            'id', 'parentId'
246
        ])->all();
247
    }
248
249
    /**
250
     * @return array|\yii\db\ActiveRecord[]
251
     */
252
    public static function getActiveMenu()
253
    {
254
        return static::find()->select([
255
            'id', 'parentId'
256
        ])->where([
257
            'active' => 1
258
        ])->all();
259
    }
260
261
    /**
262
     * Get albums, that catalog has.
263
     *
264
     * @return Album[]
265
     */
266 View Code Duplication
    public function getAlbums()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
267
    {
268
        return OwnerAlbum::getAlbumsQuery([
269
            'owner' => $this->tableName(),
270
            'ownerId' => $this->id,
271
            'ownerAttribute' => 'albums',
272
        ])->all();
273
    }
274
}
275