1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Itstructure\MFUploader\models; |
4
|
|
|
|
5
|
|
|
use yii\helpers\ArrayHelper; |
6
|
|
|
use yii\base\InvalidConfigException; |
7
|
|
|
use Itstructure\MFUploader\Module; |
8
|
|
|
use Itstructure\MFUploader\helpers\Html; |
9
|
|
|
use Itstructure\MFUploader\interfaces\UploadModelInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* This is the model class for table "mediafiles". |
13
|
|
|
* |
14
|
|
|
* @property int $id |
15
|
|
|
* @property string $filename |
16
|
|
|
* @property string $type |
17
|
|
|
* @property string $url |
18
|
|
|
* @property string $alt |
19
|
|
|
* @property int $size |
20
|
|
|
* @property string $title |
21
|
|
|
* @property string $description |
22
|
|
|
* @property string $thumbs |
23
|
|
|
* @property string $storage |
24
|
|
|
* @property int $created_at |
25
|
|
|
* @property int $updated_at |
26
|
|
|
* @property Module $_module |
27
|
|
|
* |
28
|
|
|
* @package Itstructure\MFUploader\models |
29
|
|
|
* |
30
|
|
|
* @author Andrey Girnik <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class Mediafile extends ActiveRecord |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var Module |
36
|
|
|
*/ |
37
|
|
|
private $_module; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public static function tableName() |
43
|
|
|
{ |
44
|
|
|
return 'mediafiles'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function rules() |
51
|
|
|
{ |
52
|
|
|
return [ |
53
|
|
|
[ |
54
|
|
|
[ |
55
|
|
|
'filename', |
56
|
|
|
'type', |
57
|
|
|
'url', |
58
|
|
|
'size', |
59
|
|
|
'storage', |
60
|
|
|
], |
61
|
|
|
'required', |
62
|
|
|
], |
63
|
|
|
[ |
64
|
|
|
[ |
65
|
|
|
'alt', |
66
|
|
|
'title', |
67
|
|
|
'description', |
68
|
|
|
'thumbs', |
69
|
|
|
'storage', |
70
|
|
|
], |
71
|
|
|
'string', |
72
|
|
|
], |
73
|
|
|
[ |
74
|
|
|
[ |
75
|
|
|
'size', |
76
|
|
|
], |
77
|
|
|
'integer', |
78
|
|
|
], |
79
|
|
|
[ |
80
|
|
|
[ |
81
|
|
|
'filename', |
82
|
|
|
'type', |
83
|
|
|
'url', |
84
|
|
|
], |
85
|
|
|
'string', |
86
|
|
|
'max' => 255, |
87
|
|
|
], |
88
|
|
|
[ |
89
|
|
|
[ |
90
|
|
|
'created_at', |
91
|
|
|
'updated_at', |
92
|
|
|
], |
93
|
|
|
'safe', |
94
|
|
|
], |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function attributeLabels() |
102
|
|
|
{ |
103
|
|
|
return [ |
104
|
|
|
'id' => 'ID', |
105
|
|
|
'filename' => 'Filename', |
106
|
|
|
'type' => 'Type', |
107
|
|
|
'url' => 'Url', |
108
|
|
|
'alt' => 'Alt', |
109
|
|
|
'size' => 'Size', |
110
|
|
|
'title' => 'Title', |
111
|
|
|
'description' => 'Description', |
112
|
|
|
'thumbs' => 'Thumbs', |
113
|
|
|
'storage' => 'Storage', |
114
|
|
|
'created_at' => 'Created At', |
115
|
|
|
'updated_at' => 'Updated At', |
116
|
|
|
]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return Module |
121
|
|
|
*/ |
122
|
|
|
public function getModule(): Module |
123
|
|
|
{ |
124
|
|
|
|
125
|
|
|
if ($this->_module === null) { |
126
|
|
|
$this->_module = \Yii::$app->getModule(Module::MODULE_NAME); |
127
|
|
|
|
128
|
|
|
if (empty($this->_module)) { |
129
|
|
|
$this->_module = Module::getInstance(); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $this->_module; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Find model by url. |
138
|
|
|
* |
139
|
|
|
* @param string $url |
140
|
|
|
* |
141
|
|
|
* @return mixed |
142
|
|
|
*/ |
143
|
|
|
public static function findByUrl(string $url) |
144
|
|
|
{ |
145
|
|
|
return self::findOne(['url' => $url]); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Search models by file types. |
150
|
|
|
* |
151
|
|
|
* @param array $types |
152
|
|
|
* |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
|
|
public static function findByTypes(array $types): array |
156
|
|
|
{ |
157
|
|
|
return self::find()->filterWhere(['in', 'type', $types])->all(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Add owner to mediafiles table. |
162
|
|
|
* |
163
|
|
|
* @param int $ownerId |
164
|
|
|
* @param string $owner |
165
|
|
|
* @param string $ownerAttribute |
166
|
|
|
* |
167
|
|
|
* @return bool |
168
|
|
|
*/ |
169
|
|
|
public function addOwner(int $ownerId, string $owner, string $ownerAttribute): bool |
170
|
|
|
{ |
171
|
|
|
return OwnerMediafile::addOwner($this->id, $ownerId, $owner, $ownerAttribute); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Remove this mediafile owner. |
176
|
|
|
* |
177
|
|
|
* @param int $ownerId |
178
|
|
|
* @param string $owner |
179
|
|
|
* @param string $ownerAttribute |
180
|
|
|
* |
181
|
|
|
* @return bool |
182
|
|
|
*/ |
183
|
|
|
public static function removeOwner(int $ownerId, string $owner, string $ownerAttribute): bool |
184
|
|
|
{ |
185
|
|
|
return OwnerMediafile::removeOwner($ownerId, $owner, $ownerAttribute); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return \yii\db\ActiveQuery |
190
|
|
|
*/ |
191
|
|
|
public function getOwners() |
192
|
|
|
{ |
193
|
|
|
return $this->hasMany(OwnerMediafile::class, ['mediafileId' => 'id']); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Get preview html. |
198
|
|
|
* |
199
|
|
|
* @param string $baseUrl Url to asset files which is formed on the BaseAsset. |
200
|
|
|
* @param string $location Where is this function calling: |
201
|
|
|
* existing - if view template renders files, which are exist; |
202
|
|
|
* fileinfo - for "fileinfo/index" view template of "filemanager"; |
203
|
|
|
* fileitem - for "_fileItem" view template of "filemanager". |
204
|
|
|
* You can set new location attributes by custom in Module "previewOptions" |
205
|
|
|
* before using in this getPreview() function inside the view template. |
206
|
|
|
* It is necessary to set location attributes for certain types of mediafiles. |
207
|
|
|
* See how it's done in "preview-options" config file as an example. |
208
|
|
|
* @param array $options Options for the next tags, which must be the keys of the options array: |
209
|
|
|
* 1. Main tag: |
210
|
|
|
* mainTag - main preview html tag. Can contain different html tag options. |
211
|
|
|
* And also can contain "alias" from the number of module constant aliases: |
212
|
|
|
* default, original, small, medium, large. |
213
|
|
|
* 2. Addition tags: |
214
|
|
|
* leftTag - tag that is located to the left of the "mainTag"; |
215
|
|
|
* rightTag - tag that is located to the right of the "mainTag"; |
216
|
|
|
* externalTag - tag in which the mainTag with leftTag and rightTag are embedded. |
217
|
|
|
* Importantly! Addition tag keys must contain the next attributes: |
218
|
|
|
* name - the name of addition tag. |
219
|
|
|
* options - html options of addition tag. |
220
|
|
|
* By default, the mainTag is already in the "preview-options" configuration file. |
221
|
|
|
* You can insert configurations values of addition tags through the third parameter of this function. |
222
|
|
|
* |
223
|
|
|
* @return string |
224
|
|
|
*/ |
225
|
|
|
public function getPreview(string $baseUrl = '', string $location = null, array $options = []): string |
226
|
|
|
{ |
227
|
|
|
$module = $this->getModule(); |
228
|
|
|
|
229
|
|
|
if ($this->isImage()) { |
230
|
|
|
$options = $this->getOptions($options, $location, $module, UploadModelInterface::FILE_TYPE_IMAGE); |
231
|
|
|
$preview = $this->getImagePreview(isset($options['mainTag']) ? $options['mainTag'] : []); |
232
|
|
|
|
233
|
|
View Code Duplication |
} elseif ($this->isAudio()) { |
|
|
|
|
234
|
|
|
$options = $this->getOptions($options, $location, $module, UploadModelInterface::FILE_TYPE_AUDIO); |
235
|
|
|
$preview = $this->getAudioPreview(isset($options['mainTag']) ? $options['mainTag'] : []); |
236
|
|
|
|
237
|
|
|
} elseif ($this->isVideo()) { |
238
|
|
|
$options = $this->getOptions($options, $location, $module, UploadModelInterface::FILE_TYPE_VIDEO); |
239
|
|
|
$preview = $this->getVideoPreview(isset($options['mainTag']) ? $options['mainTag'] : []); |
240
|
|
|
|
241
|
|
View Code Duplication |
} elseif ($this->isApp()) { |
|
|
|
|
242
|
|
|
$options = $this->getOptions($options, $location, $module, UploadModelInterface::FILE_TYPE_APP); |
243
|
|
|
$preview = $this->getAppPreview($baseUrl, isset($options['mainTag']) ? $options['mainTag'] : []); |
244
|
|
|
|
245
|
|
|
} elseif ($this->isText()) { |
246
|
|
|
$options = $this->getOptions($options, $location, $module, UploadModelInterface::FILE_TYPE_TEXT); |
247
|
|
|
$preview = $this->getTextPreview($baseUrl, isset($options['mainTag']) ? $options['mainTag'] : []); |
248
|
|
|
|
249
|
|
View Code Duplication |
} else { |
|
|
|
|
250
|
|
|
$options = $this->getOptions($options, $location, $module, UploadModelInterface::FILE_TYPE_OTHER); |
251
|
|
|
$preview = $this->getOtherPreview($baseUrl, isset($options['mainTag']) ? $options['mainTag'] : []); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
View Code Duplication |
if (isset($options['leftTag']) && is_array($options['leftTag'])) { |
|
|
|
|
255
|
|
|
$preview = $this->compactPreviewAdditionTags($preview, 'leftTag', $options['leftTag']); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
View Code Duplication |
if (isset($options['rightTag']) && is_array($options['rightTag'])) { |
|
|
|
|
259
|
|
|
$preview = $this->compactPreviewAdditionTags($preview, 'rightTag', $options['rightTag']); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
View Code Duplication |
if (isset($options['externalTag']) && is_array($options['externalTag'])) { |
|
|
|
|
263
|
|
|
$preview = $this->compactPreviewAdditionTags($preview, 'externalTag', $options['externalTag']); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return $preview; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Get image preview. |
271
|
|
|
* |
272
|
|
|
* @param array $mainTagOptions |
273
|
|
|
* |
274
|
|
|
* @return string |
275
|
|
|
*/ |
276
|
|
|
public function getImagePreview(array $mainTagOptions): string |
277
|
|
|
{ |
278
|
|
|
if (!isset($mainTagOptions['alt'])) { |
279
|
|
|
$mainTagOptions['alt'] = $this->alt; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
if (isset($mainTagOptions['alias']) && is_string($mainTagOptions['alias'])) { |
283
|
|
|
return Html::img($this->getThumbUrl($mainTagOptions['alias']), $mainTagOptions); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
return Html::img($this->getThumbUrl(Module::THUMB_ALIAS_DEFAULT), $mainTagOptions); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Get audio preview. |
291
|
|
|
* |
292
|
|
|
* @param array $mainTagOptions |
293
|
|
|
* |
294
|
|
|
* @return string |
295
|
|
|
*/ |
296
|
|
View Code Duplication |
public function getAudioPreview(array $mainTagOptions): string |
|
|
|
|
297
|
|
|
{ |
298
|
|
|
return Html::audio($this->getViewUrl(), ArrayHelper::merge([ |
299
|
|
|
'source' => [ |
300
|
|
|
'type' => $this->type |
301
|
|
|
] |
302
|
|
|
], $mainTagOptions) |
303
|
|
|
); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Get video preview. |
308
|
|
|
* |
309
|
|
|
* @param array $mainTagOptions |
310
|
|
|
* |
311
|
|
|
* @return string |
312
|
|
|
*/ |
313
|
|
View Code Duplication |
public function getVideoPreview(array $mainTagOptions): string |
|
|
|
|
314
|
|
|
{ |
315
|
|
|
return Html::video($this->getViewUrl(), ArrayHelper::merge([ |
316
|
|
|
'source' => [ |
317
|
|
|
'type' => $this->type |
318
|
|
|
] |
319
|
|
|
], $mainTagOptions) |
320
|
|
|
); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Get application preview. |
325
|
|
|
* |
326
|
|
|
* @param string $baseUrl |
327
|
|
|
* @param array $mainTagOptions |
328
|
|
|
* |
329
|
|
|
* @return string |
330
|
|
|
*/ |
331
|
|
|
public function getAppPreview(string $baseUrl = '', array $mainTagOptions): string |
332
|
|
|
{ |
333
|
|
|
return Html::img($this->getAppPreviewUrl($baseUrl), $mainTagOptions); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Get text preview. |
338
|
|
|
* |
339
|
|
|
* @param string $baseUrl |
340
|
|
|
* @param array $mainTagOptions |
341
|
|
|
* |
342
|
|
|
* @return string |
343
|
|
|
*/ |
344
|
|
|
public function getTextPreview(string $baseUrl = '', array $mainTagOptions): string |
345
|
|
|
{ |
346
|
|
|
return Html::img($this->getTextPreviewUrl($baseUrl), $mainTagOptions); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Get other preview. |
351
|
|
|
* |
352
|
|
|
* @param string $baseUrl |
353
|
|
|
* @param array $mainTagOptions |
354
|
|
|
* |
355
|
|
|
* @return string |
356
|
|
|
*/ |
357
|
|
|
public function getOtherPreview(string $baseUrl = '', array $mainTagOptions): string |
358
|
|
|
{ |
359
|
|
|
return Html::img($this->getOtherPreviewUrl($baseUrl), $mainTagOptions); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Get application preview url. |
364
|
|
|
* |
365
|
|
|
* @param string $baseUrl |
366
|
|
|
* |
367
|
|
|
* @return string |
368
|
|
|
*/ |
369
|
|
|
public function getAppPreviewUrl($baseUrl = ''): string |
370
|
|
|
{ |
371
|
|
|
if (!empty($baseUrl) && is_string($baseUrl)) { |
372
|
|
|
$root = $baseUrl.DIRECTORY_SEPARATOR; |
373
|
|
|
} else { |
374
|
|
|
$root = DIRECTORY_SEPARATOR; |
375
|
|
|
} |
376
|
|
|
$module = $this->getModule(); |
377
|
|
|
|
378
|
|
|
if ($this->isExcel()) { |
379
|
|
|
$url = $root . $module->thumbStubUrls[UploadModelInterface::FILE_TYPE_APP_EXCEL]; |
380
|
|
|
|
381
|
|
|
} elseif ($this->isPdf()) { |
382
|
|
|
$url = $root . $module->thumbStubUrls[UploadModelInterface::FILE_TYPE_APP_PDF]; |
383
|
|
|
|
384
|
|
|
} elseif ($this->isWord()) { |
385
|
|
|
$url = $root . $module->thumbStubUrls[UploadModelInterface::FILE_TYPE_APP_WORD]; |
386
|
|
|
|
387
|
|
|
} else { |
388
|
|
|
$url = $root . $module->thumbStubUrls[UploadModelInterface::FILE_TYPE_APP]; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
return $url; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* Get text preview url. |
396
|
|
|
* |
397
|
|
|
* @param string $baseUrl |
398
|
|
|
* |
399
|
|
|
* @return string |
400
|
|
|
*/ |
401
|
|
View Code Duplication |
public function getTextPreviewUrl($baseUrl = ''): string |
|
|
|
|
402
|
|
|
{ |
403
|
|
|
if (!empty($baseUrl) && is_string($baseUrl)) { |
404
|
|
|
$root = $baseUrl.DIRECTORY_SEPARATOR; |
405
|
|
|
} else { |
406
|
|
|
$root = DIRECTORY_SEPARATOR; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
return $root . $this->getModule()->thumbStubUrls[UploadModelInterface::FILE_TYPE_TEXT]; |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Get other preview url. |
414
|
|
|
* |
415
|
|
|
* @param string $baseUrl |
416
|
|
|
* |
417
|
|
|
* @return string |
418
|
|
|
*/ |
419
|
|
View Code Duplication |
public function getOtherPreviewUrl($baseUrl = ''): string |
|
|
|
|
420
|
|
|
{ |
421
|
|
|
if (!empty($baseUrl) && is_string($baseUrl)) { |
422
|
|
|
$root = $baseUrl.DIRECTORY_SEPARATOR; |
423
|
|
|
} else { |
424
|
|
|
$root = DIRECTORY_SEPARATOR; |
425
|
|
|
} |
426
|
|
|
$module = $this->getModule(); |
427
|
|
|
|
428
|
|
|
return $root . $module->thumbStubUrls[UploadModelInterface::FILE_TYPE_OTHER]; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* Get thumbnails. |
433
|
|
|
* |
434
|
|
|
* @return array |
435
|
|
|
*/ |
436
|
|
|
public function getThumbs(): array |
437
|
|
|
{ |
438
|
|
|
return unserialize($this->thumbs) ?: []; |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* Get thumb url. |
443
|
|
|
* |
444
|
|
|
* @param string $alias |
445
|
|
|
* |
446
|
|
|
* @return string |
447
|
|
|
*/ |
448
|
|
|
public function getThumbUrl(string $alias): string |
449
|
|
|
{ |
450
|
|
|
if ($alias === Module::THUMB_ALIAS_ORIGINAL) { |
451
|
|
|
$url = $this->getViewUrl(); |
452
|
|
|
|
453
|
|
|
} else { |
454
|
|
|
$thumbs = $this->getThumbs(); |
455
|
|
|
|
456
|
|
|
$url = !empty($thumbs[$alias]) ? $thumbs[$alias] : ''; |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
if (empty($url)) { |
460
|
|
|
return ''; |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
return $this->getViewUrl($url); |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* Get thumb image. |
468
|
|
|
* |
469
|
|
|
* @param string $alias |
470
|
|
|
* @param array $options |
471
|
|
|
* |
472
|
|
|
* @return string |
473
|
|
|
*/ |
474
|
|
|
public function getThumbImage(string $alias, array $options = []): string |
475
|
|
|
{ |
476
|
|
|
$url = $this->getThumbUrl($alias); |
477
|
|
|
|
478
|
|
|
if (empty($url)) { |
479
|
|
|
return ''; |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
if (empty($options['alt'])) { |
483
|
|
|
$options['alt'] = $this->alt; |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
return Html::img($url, $options); |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
/** |
490
|
|
|
* Get default thumbnail url. |
491
|
|
|
* |
492
|
|
|
* @param string $baseUrl |
493
|
|
|
* |
494
|
|
|
* @return string |
495
|
|
|
*/ |
496
|
|
|
public function getDefaultThumbUrl($baseUrl = ''): string |
497
|
|
|
{ |
498
|
|
|
if ($this->isImage()) { |
499
|
|
|
return $this->getThumbUrl(Module::THUMB_ALIAS_DEFAULT); |
500
|
|
|
|
501
|
|
|
} elseif ($this->isApp()) { |
502
|
|
|
return $this->getAppPreviewUrl($baseUrl); |
503
|
|
|
|
504
|
|
|
} elseif ($this->isText()) { |
505
|
|
|
return $this->getTextPreviewUrl($baseUrl); |
506
|
|
|
|
507
|
|
|
} else { |
508
|
|
|
return $this->getOtherPreviewUrl($baseUrl); |
509
|
|
|
} |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
/** |
513
|
|
|
* @return string file size |
514
|
|
|
*/ |
515
|
|
|
public function getFileSize() |
516
|
|
|
{ |
517
|
|
|
\Yii::$app->formatter->sizeFormatBase = 1000; |
518
|
|
|
return \Yii::$app->formatter->asShortSize($this->size, 0); |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
/** |
522
|
|
|
* Check if the file is image. |
523
|
|
|
* |
524
|
|
|
* @return bool |
525
|
|
|
*/ |
526
|
|
|
public function isImage(): bool |
527
|
|
|
{ |
528
|
|
|
return strpos($this->type, UploadModelInterface::FILE_TYPE_IMAGE) !== false; |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
/** |
532
|
|
|
* Check if the file is audio. |
533
|
|
|
* |
534
|
|
|
* @return bool |
535
|
|
|
*/ |
536
|
|
|
public function isAudio(): bool |
537
|
|
|
{ |
538
|
|
|
return strpos($this->type, UploadModelInterface::FILE_TYPE_AUDIO) !== false; |
539
|
|
|
} |
540
|
|
|
|
541
|
|
|
/** |
542
|
|
|
* Check if the file is video. |
543
|
|
|
* |
544
|
|
|
* @return bool |
545
|
|
|
*/ |
546
|
|
|
public function isVideo(): bool |
547
|
|
|
{ |
548
|
|
|
return strpos($this->type, UploadModelInterface::FILE_TYPE_VIDEO) !== false; |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
/** |
552
|
|
|
* Check if the file is text. |
553
|
|
|
* |
554
|
|
|
* @return bool |
555
|
|
|
*/ |
556
|
|
|
public function isText(): bool |
557
|
|
|
{ |
558
|
|
|
return strpos($this->type, UploadModelInterface::FILE_TYPE_TEXT) !== false; |
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
/** |
562
|
|
|
* Check if the file is application. |
563
|
|
|
* |
564
|
|
|
* @return bool |
565
|
|
|
*/ |
566
|
|
|
public function isApp(): bool |
567
|
|
|
{ |
568
|
|
|
return strpos($this->type, UploadModelInterface::FILE_TYPE_APP) !== false; |
569
|
|
|
} |
570
|
|
|
|
571
|
|
|
/** |
572
|
|
|
* Check if the file is excel. |
573
|
|
|
* |
574
|
|
|
* @return bool |
575
|
|
|
*/ |
576
|
|
|
public function isExcel(): bool |
577
|
|
|
{ |
578
|
|
|
return strpos($this->type, UploadModelInterface::FILE_TYPE_APP_EXCEL) !== false; |
579
|
|
|
} |
580
|
|
|
|
581
|
|
|
/** |
582
|
|
|
* Check if the file is pdf. |
583
|
|
|
* |
584
|
|
|
* @return bool |
585
|
|
|
*/ |
586
|
|
|
public function isPdf(): bool |
587
|
|
|
{ |
588
|
|
|
return strpos($this->type, UploadModelInterface::FILE_TYPE_APP_PDF) !== false; |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
/** |
592
|
|
|
* Check if the file is word. |
593
|
|
|
* |
594
|
|
|
* @return bool |
595
|
|
|
*/ |
596
|
|
|
public function isWord(): bool |
597
|
|
|
{ |
598
|
|
|
return strpos($this->type, UploadModelInterface::FILE_TYPE_APP_WORD) !== false; |
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
/** |
602
|
|
|
* @param string $url |
603
|
|
|
* |
604
|
|
|
* @return string |
605
|
|
|
*/ |
606
|
|
|
public function getFullPublicUrl(string $url): string |
607
|
|
|
{ |
608
|
|
|
return rtrim(rtrim($this->getModule()->publicBaseUrl, '/'), '\\') . '/' . ltrim(str_replace('\\', '/', $url), '/'); |
609
|
|
|
} |
610
|
|
|
|
611
|
|
|
/** |
612
|
|
|
* If storage is local, public base url will be linked with a url from DB. |
613
|
|
|
* It is useful for html templates. |
614
|
|
|
* |
615
|
|
|
* @param string|null $url |
616
|
|
|
* |
617
|
|
|
* @return string |
618
|
|
|
*/ |
619
|
|
|
public function getViewUrl(string $url = null): string |
620
|
|
|
{ |
621
|
|
|
$url = empty($url) ? $this->url : $url; |
622
|
|
|
|
623
|
|
|
return $this->storage == Module::STORAGE_TYPE_LOCAL ? $this->getFullPublicUrl($url) : $url; |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
/** |
627
|
|
|
* Get preview option by location and file type. |
628
|
|
|
* |
629
|
|
|
* @param array $options |
630
|
|
|
* @param string|null $location |
631
|
|
|
* @param Module $module |
632
|
|
|
* @param string $fileType |
633
|
|
|
* |
634
|
|
|
* @return array |
635
|
|
|
*/ |
636
|
|
|
private function getOptions( |
637
|
|
|
array $options, |
638
|
|
|
string $location = null, |
639
|
|
|
Module $module, |
640
|
|
|
string $fileType) { |
641
|
|
|
|
642
|
|
|
return null === $location ? |
643
|
|
|
$options : ArrayHelper::merge($module->getPreviewOptions($fileType, $location, $this), $options); |
644
|
|
|
} |
645
|
|
|
|
646
|
|
|
/** |
647
|
|
|
* Compact addition tag with the main preview. |
648
|
|
|
* |
649
|
|
|
* @param string $preview |
650
|
|
|
* @param string $additionTagType |
651
|
|
|
* @param array $additionTagConfig |
652
|
|
|
* |
653
|
|
|
* @throws InvalidConfigException |
654
|
|
|
* |
655
|
|
|
* @return string |
656
|
|
|
*/ |
657
|
|
|
private function compactPreviewAdditionTags( |
658
|
|
|
string $preview, |
659
|
|
|
string $additionTagType, |
660
|
|
|
array $additionTagConfig |
661
|
|
|
): string { |
662
|
|
|
|
663
|
|
|
if (!isset($additionTagConfig['name']) || !is_string($additionTagConfig['name'])) { |
664
|
|
|
throw new InvalidConfigException('Bad name configuration for addition tag: '.$additionTagType); |
665
|
|
|
} |
666
|
|
|
|
667
|
|
|
if (!isset($additionTagConfig['options']) || !is_array($additionTagConfig['options'])) { |
668
|
|
|
throw new InvalidConfigException('Bad options configuration for addition tag: '.$additionTagType); |
669
|
|
|
} |
670
|
|
|
|
671
|
|
|
switch ($additionTagType) { |
672
|
|
View Code Duplication |
case 'leftTag': |
|
|
|
|
673
|
|
|
$content = isset($additionTagConfig['content']) ? $additionTagConfig['content'] : ''; |
674
|
|
|
$preview = Html::tag($additionTagConfig['name'], $content, $additionTagConfig['options']) . $preview; |
|
|
|
|
675
|
|
|
break; |
676
|
|
|
|
677
|
|
View Code Duplication |
case 'rightTag': |
|
|
|
|
678
|
|
|
$content = isset($additionTagConfig['content']) ? $additionTagConfig['content'] : ''; |
679
|
|
|
$preview = $preview . Html::tag($additionTagConfig['name'], $content, $additionTagConfig['options']); |
|
|
|
|
680
|
|
|
break; |
681
|
|
|
|
682
|
|
|
case 'externalTag': |
683
|
|
|
$preview = Html::tag($additionTagConfig['name'], $preview, $additionTagConfig['options']); |
684
|
|
|
break; |
685
|
|
|
|
686
|
|
|
default: |
687
|
|
|
throw new InvalidConfigException('Unknown type of addition tag'); |
688
|
|
|
} |
689
|
|
|
|
690
|
|
|
return $preview; |
691
|
|
|
} |
692
|
|
|
} |
693
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.