1
|
|
|
<?php |
2
|
|
|
namespace Itstructure\MFUploader\models\album; |
3
|
|
|
|
4
|
|
|
use yii\db\ActiveQuery; |
5
|
|
|
use yii\helpers\Html; |
6
|
|
|
use Itstructure\MFUploader\Module; |
7
|
|
|
use Itstructure\MFUploader\models\{ActiveRecord, OwnerAlbum, OwnerMediafile, Mediafile}; |
8
|
|
|
use Itstructure\MFUploader\interfaces\UploadModelInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This is the model class for table "albums". |
12
|
|
|
* |
13
|
|
|
* @property int|string $thumbnail Thumbnail field. Corresponds to the file type of thumbnail. |
14
|
|
|
* In the thml form field should also be called. |
15
|
|
|
* Can have the values according with the selected type of: |
16
|
|
|
* FileSetter::INSERTED_DATA_ID |
17
|
|
|
* FileSetter::INSERTED_DATA_URL |
18
|
|
|
* @property int $id |
19
|
|
|
* @property string $title |
20
|
|
|
* @property string $description |
21
|
|
|
* @property string $type |
22
|
|
|
* @property int $created_at |
23
|
|
|
* @property int $updated_at |
24
|
|
|
* |
25
|
|
|
* @package Itstructure\MFUploader\models\album |
26
|
|
|
* |
27
|
|
|
* @author Andrey Girnik <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class Album extends ActiveRecord |
30
|
|
|
{ |
31
|
|
|
const ALBUM_TYPE_IMAGE = UploadModelInterface::FILE_TYPE_IMAGE . 'Album'; |
32
|
|
|
const ALBUM_TYPE_AUDIO = UploadModelInterface::FILE_TYPE_AUDIO . 'Album'; |
33
|
|
|
const ALBUM_TYPE_VIDEO = UploadModelInterface::FILE_TYPE_VIDEO . 'Album'; |
34
|
|
|
const ALBUM_TYPE_APP = UploadModelInterface::FILE_TYPE_APP . 'Album'; |
35
|
|
|
const ALBUM_TYPE_TEXT = UploadModelInterface::FILE_TYPE_TEXT . 'Album'; |
36
|
|
|
const ALBUM_TYPE_OTHER = UploadModelInterface::FILE_TYPE_OTHER . 'Album'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Thumbnail field. Corresponds to the file type of thumbnail. |
40
|
|
|
* In the thml form field should also be called. |
41
|
|
|
* Can have the values according with the selected type of: |
42
|
|
|
* FileSetter::INSERTED_DATA_ID |
43
|
|
|
* FileSetter::INSERTED_DATA_URL |
44
|
|
|
* |
45
|
|
|
* @var int|string thumbnail(mediafile id or url). |
46
|
|
|
*/ |
47
|
|
|
public $thumbnail; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public static function tableName() |
53
|
|
|
{ |
54
|
|
|
return 'albums'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function rules() |
61
|
|
|
{ |
62
|
|
|
return [ |
63
|
|
|
[ |
64
|
|
|
[ |
65
|
|
|
'title', |
66
|
|
|
'type', |
67
|
|
|
], |
68
|
|
|
'required', |
69
|
|
|
], |
70
|
|
|
[ |
71
|
|
|
[ |
72
|
|
|
'description' |
73
|
|
|
], |
74
|
|
|
'string', |
75
|
|
|
], |
76
|
|
|
[ |
77
|
|
|
[ |
78
|
|
|
'title', |
79
|
|
|
'type', |
80
|
|
|
], |
81
|
|
|
'string', |
82
|
|
|
'max' => 64, |
83
|
|
|
], |
84
|
|
|
[ |
85
|
|
|
UploadModelInterface::FILE_TYPE_THUMB, |
86
|
|
|
function($attribute) { |
87
|
|
|
if (!is_numeric($this->{$attribute}) && !is_string($this->{$attribute})) { |
88
|
|
|
$this->addError($attribute, 'Thumbnail content must be numeric or string.'); |
89
|
|
|
} |
90
|
|
|
}, |
91
|
|
|
'skipOnError' => false, |
92
|
|
|
], |
93
|
|
|
[ |
94
|
|
|
[ |
95
|
|
|
'created_at', |
96
|
|
|
'updated_at', |
97
|
|
|
], |
98
|
|
|
'safe', |
99
|
|
|
], |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
|
|
public function attributeLabels() |
107
|
|
|
{ |
108
|
|
|
return [ |
109
|
|
|
'id' => Module::t('main', 'ID'), |
110
|
|
|
'title' => Module::t('album', 'Title'), |
111
|
|
|
'description' => Module::t('album', 'Description'), |
112
|
|
|
'type' => Module::t('album', 'Type'), |
113
|
|
|
'created_at' => Module::t('main', 'Created date'), |
114
|
|
|
'updated_at' => Module::t('main', 'Updated date'), |
115
|
|
|
]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get album types or selected type. |
120
|
|
|
* |
121
|
|
|
* @param string|null $key |
122
|
|
|
* |
123
|
|
|
* @return mixed |
124
|
|
|
*/ |
125
|
|
|
public static function getAlbumTypes(string $key = null) |
126
|
|
|
{ |
127
|
|
|
$types = [ |
128
|
|
|
self::ALBUM_TYPE_IMAGE => Module::t('album', 'Image album'), |
129
|
|
|
self::ALBUM_TYPE_AUDIO => Module::t('album', 'Audio album'), |
130
|
|
|
self::ALBUM_TYPE_VIDEO => Module::t('album', 'Video album'), |
131
|
|
|
self::ALBUM_TYPE_APP => Module::t('album', 'Applications'), |
132
|
|
|
self::ALBUM_TYPE_TEXT => Module::t('album', 'Documents'), |
133
|
|
|
self::ALBUM_TYPE_OTHER => Module::t('album', 'Other files'), |
134
|
|
|
]; |
135
|
|
|
|
136
|
|
|
if (null !== $key) { |
137
|
|
|
return array_key_exists($key, $types) ? $types[$key] : []; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $types; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get file type by album type. |
145
|
|
|
* |
146
|
|
|
* @param string $albumType |
147
|
|
|
* |
148
|
|
|
* @return mixed|null |
149
|
|
|
*/ |
150
|
|
|
public static function getFileType(string $albumType) |
151
|
|
|
{ |
152
|
|
|
$albumTypes = [ |
153
|
|
|
self::ALBUM_TYPE_IMAGE => UploadModelInterface::FILE_TYPE_IMAGE, |
154
|
|
|
self::ALBUM_TYPE_AUDIO => UploadModelInterface::FILE_TYPE_AUDIO, |
155
|
|
|
self::ALBUM_TYPE_VIDEO => UploadModelInterface::FILE_TYPE_VIDEO, |
156
|
|
|
self::ALBUM_TYPE_APP => UploadModelInterface::FILE_TYPE_APP, |
157
|
|
|
self::ALBUM_TYPE_TEXT => UploadModelInterface::FILE_TYPE_TEXT, |
158
|
|
|
self::ALBUM_TYPE_OTHER => UploadModelInterface::FILE_TYPE_OTHER, |
159
|
|
|
]; |
160
|
|
|
|
161
|
|
|
return array_key_exists($albumType, $albumTypes) ? $albumTypes[$albumType] : null; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Search models by file types. |
166
|
|
|
* |
167
|
|
|
* @param array $types |
168
|
|
|
* |
169
|
|
|
* @return ActiveRecord|array |
170
|
|
|
*/ |
171
|
|
|
public static function findByTypes(array $types): ActiveRecord |
172
|
|
|
{ |
173
|
|
|
return static::find()->filterWhere(['in', 'type', $types])->all(); |
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Add owner to mediafiles table. |
178
|
|
|
* |
179
|
|
|
* @param int $ownerId |
180
|
|
|
* @param string $owner |
181
|
|
|
* @param string $ownerAttribute |
182
|
|
|
* |
183
|
|
|
* @return bool |
184
|
|
|
*/ |
185
|
|
|
public function addOwner(int $ownerId, string $owner, string $ownerAttribute): bool |
186
|
|
|
{ |
187
|
|
|
return OwnerAlbum::addOwner($this->id, $ownerId, $owner, $ownerAttribute); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Get album's owners. |
192
|
|
|
* |
193
|
|
|
* @return ActiveQuery |
194
|
|
|
*/ |
195
|
|
|
public function getOwners() |
196
|
|
|
{ |
197
|
|
|
return $this->hasMany(OwnerAlbum::class, ['albumId' => 'id']); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Get album's mediafiles. |
202
|
|
|
* |
203
|
|
|
* @param string|null $ownerAttribute |
204
|
|
|
* |
205
|
|
|
* @return \Itstructure\MFUploader\models\ActiveRecord[] |
206
|
|
|
*/ |
207
|
|
|
public function getMediaFiles(string $ownerAttribute = null) |
208
|
|
|
{ |
209
|
|
|
return OwnerMediafile::getMediaFiles($this->type, $this->id, $ownerAttribute); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Get album's mediafiles query. |
214
|
|
|
* |
215
|
|
|
* @param string|null $ownerAttribute |
216
|
|
|
* |
217
|
|
|
* @return ActiveQuery |
218
|
|
|
*/ |
219
|
|
|
public function getMediaFilesQuery(string $ownerAttribute = null) |
220
|
|
|
{ |
221
|
|
|
return OwnerMediafile::getMediaFilesQuery([ |
222
|
|
|
'owner' => $this->type, |
223
|
|
|
'ownerId' => $this->id, |
224
|
|
|
'ownerAttribute' => $ownerAttribute, |
225
|
|
|
]); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Get album thumb image. |
230
|
|
|
* |
231
|
|
|
* @param array $options |
232
|
|
|
* |
233
|
|
|
* @return mixed |
234
|
|
|
*/ |
235
|
|
|
public function getDefaultThumbImage(array $options = []) |
236
|
|
|
{ |
237
|
|
|
/** @var Mediafile $thumbnailModel */ |
238
|
|
|
$thumbnailModel = $this->getThumbnailModel(); |
239
|
|
|
|
240
|
|
|
if (null === $thumbnailModel) { |
241
|
|
|
return null; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$url = $thumbnailModel->getThumbUrl(Module::THUMB_ALIAS_DEFAULT); |
245
|
|
|
|
246
|
|
|
if (empty($url)) { |
247
|
|
|
return null; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
if (empty($options['alt'])) { |
251
|
|
|
$options['alt'] = $thumbnailModel->alt; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
return Html::img($url, $options); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Get album's thumbnail. |
259
|
|
|
* |
260
|
|
|
* @return array|null|\yii\db\ActiveRecord|Mediafile |
261
|
|
|
*/ |
262
|
|
|
public function getThumbnailModel() |
263
|
|
|
{ |
264
|
|
|
if (null === $this->type || null === $this->id) { |
265
|
|
|
return null; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
return OwnerMediafile::getOwnerThumbnail($this->type, $this->id); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|