Article::attributes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 18
rs 9.7333
1
<?php
2
3
namespace app\models;
4
5
use Yii;
6
use yii\helpers\ArrayHelper;
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 "articles".
15
 *
16
 * @property int|string $thumbnail thumbnail(mediafile id or url).
17
 * @property array $image image(array of 'mediafile id' or 'mediafile url').
18
 * @property array $albums Existing album ids.
19
 * @property int $id
20
 * @property string $title
21
 * @property string $description
22
 * @property string $content
23
 * @property string $metaKeys
24
 * @property string $metaDescription
25
 * @property string $created_at
26
 * @property string $updated_at
27
 * @property int $pageId
28
 * @property string $icon
29
 * @property string $alias
30
 * @property int $active
31
 *
32
 * @property Page $page
33
 *
34
 * @package app\models
35
 */
36
class Article extends ActiveRecord
37
{
38
    use ThumbnailTrait;
0 ignored issues
show
Bug introduced by
The trait app\traits\ThumbnailTrait requires the property $alt which is not provided by app\models\Article.
Loading history...
39
40
    /**
41
     * @var int|string thumbnail(mediafile id or url).
42
     */
43
    public $thumbnail;
44
45
    /**
46
     * @var array image(array of 'mediafile id' or 'mediafile url').
47
     */
48
    public $image;
49
50
    /**
51
     * @var array
52
     */
53
    public $albums = [];
54
55
    /**
56
     * Init albums.
57
     */
58
    public function afterFind()
59
    {
60
        $this->albums = $this->getAlbums();
61
62
        parent::afterFind();
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public static function tableName()
69
    {
70
        return 'articles';
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function rules()
77
    {
78
        return [
79
            [
80
                [
81
                    'title',
82
                    'description',
83
                    'content',
84
                    'pageId',
85
                    'active',
86
                    'alias',
87
                    'metaKeys',
88
                    'metaDescription',
89
                ],
90
                'required'
91
            ],
92
            [
93
                [
94
                    'description',
95
                    'content'
96
                ],
97
                'string'
98
            ],
99
            [
100
                [
101
                    'icon',
102
                    'title',
103
                    'metaKeys',
104
                    'alias',
105
                ],
106
                'string',
107
                'max' => 128,
108
            ],
109
            [
110
                [
111
                    'metaDescription',
112
                ],
113
                'string',
114
                'max' => 255,
115
            ],
116
            [
117
                [
118
                    'pageId',
119
                    'active'
120
                ],
121
                'integer'
122
            ],
123
            [
124
                'alias',
125
                'filter',
126
                'filter' => function ($value) {
127
                    return preg_replace( '/[^a-z0-9_]+/', '-', strtolower(trim($value)));
128
                }
129
            ],
130
            [
131
                'alias',
132
                'unique',
133
                'skipOnError'     => true,
134
                'targetClass'     => static::class,
135
                'filter' => $this->getScenario() == self::SCENARIO_UPDATE ? 'id != '.$this->id : ''
136
            ],
137
            [
138
                [
139
                    'pageId'
140
                ],
141
                'exist',
142
                'skipOnError' => true,
143
                'targetClass' => Page::class,
144
                'targetAttribute' => ['pageId' => 'id']
145
            ],
146
            [
147
                UploadModelInterface::FILE_TYPE_THUMB,
148
                function($attribute){
149
                    if (!is_numeric($this->{$attribute}) && !is_string($this->{$attribute})){
150
                        $this->addError($attribute, 'Tumbnail content must be a numeric or string.');
151
                    }
152
                },
153
                'skipOnError' => false,
154
            ],
155
            [
156
                UploadModelInterface::FILE_TYPE_IMAGE,
157
                function($attribute) {
158
                    if (!is_array($this->{$attribute})) {
159
                        $this->addError($attribute, 'Image field content must be an array.');
160
                    }
161
                },
162
                'skipOnError' => false,
163
            ],
164
            [
165
                'albums',
166
                'each',
167
                'rule' => ['integer'],
168
            ],
169
            [
170
                'title',
171
                'unique',
172
                'skipOnError'     => true,
173
                'targetClass'     => static::class,
174
                'filter' => $this->getScenario() == self::SCENARIO_UPDATE ? 'id != '.$this->id : ''
175
            ],
176
            [
177
                [
178
                    'created_at',
179
                    'updated_at'
180
                ],
181
                'safe'
182
            ],
183
        ];
184
    }
185
186
    /**
187
     * @inheritdoc
188
     */
189
    public function behaviors()
190
    {
191
        return ArrayHelper::merge(parent::behaviors(), [
192
            'mediafile' => [
193
                'class' => BehaviorMediafile::class,
194
                'name' => static::tableName(),
195
                'attributes' => [
196
                    UploadModelInterface::FILE_TYPE_THUMB,
197
                    UploadModelInterface::FILE_TYPE_IMAGE,
198
                ],
199
            ],
200
            'albums' => [
201
                'class' => BehaviorAlbum::class,
202
                'name' => static::tableName(),
203
                'attributes' => [
204
                    'albums',
205
                ],
206
            ],
207
        ]);
208
    }
209
210
    /**
211
     * @return array
212
     */
213
    public function attributes(): array
214
    {
215
        return [
216
            UploadModelInterface::FILE_TYPE_THUMB,
217
            UploadModelInterface::FILE_TYPE_IMAGE,
218
            'albums',
219
            'id',
220
            'pageId',
221
            'icon',
222
            'alias',
223
            'active',
224
            'title',
225
            'description',
226
            'content',
227
            'metaKeys',
228
            'metaDescription',
229
            'created_at',
230
            'updated_at'
231
        ];
232
    }
233
234
    /**
235
     * {@inheritdoc}
236
     */
237
    public function attributeLabels()
238
    {
239
        return [
240
            'id' => 'ID',
241
            'pageId' => Yii::t('articles', 'Parent page'),
242
            'icon' => Yii::t('app', 'Icon'),
243
            'active' => Yii::t('app', 'Active status'),
244
            'alias' => Yii::t('app', 'URL Alias'),
245
            'title' => Yii::t('app', 'Title'),
246
            'description' => Yii::t('app', 'Description'),
247
            'content' => Yii::t('app', 'Content'),
248
            'metaKeys' => Yii::t('app', 'Meta keys'),
249
            'metaDescription' => Yii::t('app', 'Meta description'),
250
            'created_at' => Yii::t('app', 'Created date'),
251
            'updated_at' => Yii::t('app', 'Updated date'),
252
        ];
253
    }
254
255
    /**
256
     * @return \yii\db\ActiveQuery
257
     */
258
    public function getPage()
259
    {
260
        return $this->hasOne(Page::class, [
261
            'id' => 'pageId'
262
        ]);
263
    }
264
265
    /**
266
     * Get albums, that article has.
267
     *
268
     * @return Album[]
269
     */
270
    public function getAlbums()
271
    {
272
        return OwnerAlbum::getAlbumsQuery([
273
            'owner' => static::tableName(),
274
            'ownerId' => $this->id,
275
            'ownerAttribute' => 'albums',
276
        ])->all();
277
    }
278
}
279