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

Product::getAlbums()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace app\models;
4
5
use yii\helpers\{ArrayHelper, Html};
6
use Itstructure\AdminModule\models\{MultilanguageTrait, Language};
7
use Itstructure\MFUploader\Module as MFUModule;
8
use Itstructure\MFUploader\behaviors\{BehaviorMediafile, BehaviorAlbum};
9
use Itstructure\MFUploader\models\{Mediafile, OwnerAlbum, OwnerMediafile};
10
use Itstructure\MFUploader\models\album\Album;
11
use Itstructure\MFUploader\interfaces\UploadModelInterface;
12
13
/**
14
 * This is the model class for table "products".
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 $created_at
21
 * @property string $updated_at
22
 * @property int $pageId
23
 * @property string $icon
24
 * @property int $active
25
 *
26
 * @property Page $page
27
 * @property ProductLanguage[] $productsLanguages
28
 * @property Language[] $languages
29
 *
30
 * @package app\models
31
 */
32
class Product 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\Product: $language, $shortName
Loading history...
35
36
    /**
37
     * @var int|string thumbnail(mediafile id or url).
38
     */
39
    public $thumbnail;
40
41
    /**
42
     * @var array image(array of 'mediafile id' or 'mediafile url').
43
     */
44
    public $image;
45
46
    /**
47
     * @var array
48
     */
49
    public $albums = [];
50
51
    /**
52
     * @var array|null|\yii\db\ActiveRecord|Mediafile
53
     */
54
    protected $thumbnailModel;
55
56
    /**
57
     * Initialize.
58
     * Set albums, that product 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 'products';
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function rules()
79
    {
80
        return [
81
            [
82
                [
83
                    'pageId',
84
                    'active'
85
                ],
86
                'required'
87
            ],
88
            [
89
                [
90
                    'pageId',
91
                    'active'
92
                ],
93
                'integer'
94
            ],
95
            [
96
                [
97
                    'created_at',
98
                    'updated_at'
99
                ],
100
                'safe'
101
            ],
102
            [
103
                [
104
                    'icon'
105
                ],
106
                'string',
107
                'max' => 64
108
            ],
109
            [
110
                [
111
                    'pageId'
112
                ],
113
                'exist',
114
                'skipOnError' => true,
115
                'targetClass' => Page::class,
116
                'targetAttribute' => ['pageId' => 'id']
117
            ],
118
            [
119
                UploadModelInterface::FILE_TYPE_THUMB,
120
                function($attribute){
121
                    if (!is_numeric($this->{$attribute}) && !is_string($this->{$attribute})){
122
                        $this->addError($attribute, 'Tumbnail content must be a numeric or string.');
123
                    }
124
                },
125
                'skipOnError' => false,
126
            ],
127
            [
128
                UploadModelInterface::FILE_TYPE_IMAGE,
129
                function($attribute) {
130
                    if (!is_array($this->{$attribute})) {
131
                        $this->addError($attribute, 'Image field content must be an array.');
132
                    }
133
                },
134
                'skipOnError' => false,
135
            ],
136
            [
137
                'albums',
138
                'each',
139
                'rule' => ['integer'],
140
            ],
141
        ];
142
    }
143
144
    /**
145
     * @inheritdoc
146
     */
147
    public function behaviors()
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
                    UploadModelInterface::FILE_TYPE_IMAGE,
156
                ],
157
            ],
158
            'albums' => [
159
                'class' => BehaviorAlbum::class,
160
                'name' => static::tableName(),
161
                'attributes' => [
162
                    'albums',
163
                ],
164
            ],
165
        ]);
166
    }
167
168
    /**
169
     * @return array
170
     */
171
    public function attributes(): array
172
    {
173
        return [
174
            UploadModelInterface::FILE_TYPE_THUMB,
175
            UploadModelInterface::FILE_TYPE_IMAGE,
176
            'albums',
177
            'id',
178
            'pageId',
179
            'icon',
180
            'active',
181
            'created_at',
182
            'updated_at'
183
        ];
184
    }
185
186
    /**
187
     * {@inheritdoc}
188
     */
189
    public function attributeLabels()
190
    {
191
        return [
192
            'id' => 'ID',
193
            'pageId' => 'Page ID',
194
            'icon' => 'Icon',
195
            'active' => 'Active',
196
            'created_at' => 'Created At',
197
            'updated_at' => 'Updated At',
198
        ];
199
    }
200
201
    /**
202
     * @return \yii\db\ActiveQuery
203
     */
204
    public function getPage()
205
    {
206
        return $this->hasOne(Page::class, [
207
            'id' => 'pageId'
208
        ]);
209
    }
210
211
    /**
212
     * @return \yii\db\ActiveQuery
213
     */
214
    public function getProductsLanguages()
215
    {
216
        return $this->hasMany(ProductLanguage::class, [
217
            'products_id' => 'id'
218
        ]);
219
    }
220
221
    /**
222
     * @return \yii\db\ActiveQuery
223
     */
224
    public function getLanguages()
225
    {
226
        return $this->hasMany(Language::class, [
227
            'id' => 'language_id'
228
        ])->viaTable('products_language', [
229
            'products_id' => 'id'
230
        ]);
231
    }
232
233
    /**
234
     * Get albums, that product has.
235
     *
236
     * @return Album[]
237
     */
238
    public function getAlbums()
239
    {
240
        return OwnerAlbum::getAlbumsQuery([
241
            'owner' => $this->tableName(),
242
            'ownerId' => $this->id,
243
            'ownerAttribute' => 'albums',
244
        ])->all();
245
    }
246
247
    /**
248
     * Get album thumb image.
249
     *
250
     * @param array  $options
251
     *
252
     * @return mixed
253
     */
254
    public function getDefaultThumbImage(array $options = [])
255
    {
256
        $thumbnailModel = $this->getThumbnailModel();
257
258
        if (null === $thumbnailModel){
259
            return null;
260
        }
261
262
        $url = $thumbnailModel->getThumbUrl(MFUModule::THUMB_ALIAS_DEFAULT);
263
264
        if (empty($url)) {
265
            return null;
266
        }
267
268
        if (empty($options['alt'])) {
269
            $options['alt'] = $thumbnailModel->alt;
270
        }
271
272
        return Html::img($url, $options);
273
    }
274
275
    /**
276
     * Get product's thumbnail.
277
     *
278
     * @return array|null|\yii\db\ActiveRecord|Mediafile
279
     */
280
    public function getThumbnailModel()
281
    {
282
        if (null === $this->id) {
283
            return null;
284
        }
285
286
        if ($this->thumbnailModel === null) {
287
            $this->thumbnailModel = OwnerMediafile::getOwnerThumbnail($this->tableName(), $this->id);
288
        }
289
290
        return $this->thumbnailModel;
291
    }
292
}
293