Module::getUpdateUrl()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Itstructure\MFUploader;
4
5
use Yii;
6
use yii\web\View;
7
use yii\helpers\ArrayHelper;
8
use yii\base\{Module as BaseModule, InvalidConfigException};
9
use Imagine\Image\ImageInterface;
10
use Itstructure\MFUploader\interfaces\ThumbConfigInterface;
11
use Itstructure\MFUploader\components\{LocalUploadComponent, S3UploadComponent, ThumbConfig};
12
use Itstructure\MFUploader\models\Mediafile;
13
14
/**
15
 * Multi format uploader module class.
16
 *
17
 * @property null|string|array $loginUrl Login url.
18
 * @property array $accessRoles Array of roles to module access.
19
 * @property string $fileAttributeName Name of the file field to load using Ajax request.
20
 * @property array $previewOptions Preview options for som types of mediafiles according with their location.
21
 * @property array $thumbsConfig Thumbs config with their types and sizes.
22
 * @property string $thumbFilenameTemplate Thumbnails name template.
23
 * Values can be the next: {original}, {width}, {height}, {alias}, {extension}
24
 * @property array $thumbStubUrls Default thumbnail stub urls according with file type.
25
 * @property bool $enableCsrfValidation Csrf validation.
26
 * @property string $defaultStorageType Default storage type. Can be 'local' or 's3'.
27
 * @property string $publicBaseUrl Public base project url, to set before media files url.
28
 * @property View $view View component to render content.
29
 *
30
 * @package Itstructure\MFUploader
31
 *
32
 * @author Andrey Girnik <[email protected]>
33
 */
34
class Module extends BaseModule
35
{
36
    const MODULE_NAME = 'mfuploader';
37
38
    const THUMB_ALIAS_DEFAULT  = 'default';
39
    const THUMB_ALIAS_ORIGINAL = 'original';
40
    const THUMB_ALIAS_SMALL    = 'small';
41
    const THUMB_ALIAS_MEDIUM   = 'medium';
42
    const THUMB_ALIAS_LARGE    = 'large';
43
44
    const URL_FILE_MANAGER   = '/'.self::MODULE_NAME.'/managers/filemanager';
45
    const URL_UPLOAD_MANAGER = '/'.self::MODULE_NAME.'/managers/uploadmanager';
46
    const URL_FILE_INFO      = '/'.self::MODULE_NAME.'/fileinfo/index';
47
    const URL_LOCAL_SEND     = '/'.self::MODULE_NAME.'/upload/local-upload/send';
48
    const URL_LOCAL_UPDATE   = '/'.self::MODULE_NAME.'/upload/local-upload/update';
49
    const URL_LOCAL_DELETE   = '/'.self::MODULE_NAME.'/upload/local-upload/delete';
50
    const URL_S3_SEND        = '/'.self::MODULE_NAME.'/upload/s3-upload/send';
51
    const URL_S3_UPDATE      = '/'.self::MODULE_NAME.'/upload/s3-upload/update';
52
    const URL_S3_DELETE      = '/'.self::MODULE_NAME.'/upload/s3-upload/delete';
53
54
    const BACK_URL_PARAM = '__backUrl';
55
56
    const ORIGINAL_PREVIEW_WIDTH = 300;
57
    const ORIGINAL_PREVIEW_HEIGHT = 240;
58
    const SCANTY_PREVIEW_SIZE = 50;
59
60
    const STORAGE_TYPE_LOCAL = 'local';
61
    const STORAGE_TYPE_S3 = 's3';
62
63
    /**
64
     * Login url.
65
     *
66
     * @var null|string|array
67
     */
68
    public $loginUrl = null;
69
70
    /**
71
     * Array of roles to module access.
72
     *
73
     * @var array
74
     */
75
    public $accessRoles = ['@'];
76
77
    /**
78
     * Name of the file field to load using Ajax request.
79
     *
80
     * @var string
81
     */
82
    public $fileAttributeName = 'file';
83
84
    /**
85
     * Preview options for som types of mediafiles according with their location.
86
     * See how it's done in "preview-options" config file as an example.
87
     *
88
     * @var array
89
     */
90
    public $previewOptions = [];
91
92
    /**
93
     * Thumbs config with their types and sizes.
94
     * See how it's done in "thumbs-config" config file as an example.
95
     *
96
     * @var array of thumbnails.
97
     */
98
    public $thumbsConfig = [];
99
100
    /**
101
     * Use thumb config from /config/thumbs-config.php
102
     *
103
     * @var bool
104
     */
105
    public $useInitialThumbsConfig = true;
106
107
    /**
108
     * Thumbnails name template.
109
     * Values can be the next: {original}, {width}, {height}, {alias}, {extension}
110
     *
111
     * @var string
112
     */
113
    public $thumbFilenameTemplate = '{original}-{width}-{height}-{alias}.{extension}';
114
115
    /**
116
     * Default thumbnail stub urls according with file type.
117
     * See how it's done in "thumb-stub-urls" config file as an example.
118
     *
119
     * @var array
120
     */
121
    public $thumbStubUrls = [];
122
123
    /**
124
     * Csrf validation.
125
     *
126
     * @var bool
127
     */
128
    public $enableCsrfValidation = true;
129
130
    /**
131
     * Default storage type. Can be 'local' or 's3'.
132
     *
133
     * @var string
134
     */
135
    public $defaultStorageType = self::STORAGE_TYPE_LOCAL;
136
137
    /**
138
     * Public base project url, to set before media files url.
139
     *
140
     * @var string
141
     */
142
    public $publicBaseUrl = '';
143
144
    /**
145
     * View component to render content.
146
     *
147
     * @var View
148
     */
149
    private $_view = null;
150
151
    /**
152
     * Urls to send new files depending on the storage type.
153
     *
154
     * @var array
155
     */
156
    private static $sendUrls = [
157
        self::STORAGE_TYPE_LOCAL => self::URL_LOCAL_SEND,
158
        self::STORAGE_TYPE_S3 => self::URL_S3_SEND,
159
    ];
160
161
    /**
162
     * Urls to update existing files depending on the storage type.
163
     *
164
     * @var array
165
     */
166
    private static $updateUrls = [
167
        self::STORAGE_TYPE_LOCAL => self::URL_LOCAL_UPDATE,
168
        self::STORAGE_TYPE_S3 => self::URL_S3_UPDATE,
169
    ];
170
171
    /**
172
     * Urls to delete existing files depending on the storage type.
173
     *
174
     * @var array
175
     */
176
    private static $deleteUrls = [
177
        self::STORAGE_TYPE_LOCAL => self::URL_LOCAL_DELETE,
178
        self::STORAGE_TYPE_S3 => self::URL_S3_DELETE,
179
    ];
180
181
    /**
182
     * Module translations.
183
     *
184
     * @var array|null
185
     */
186
    private static $_translations = null;
187
188
    /**
189
     * @inheritdoc
190
     */
191
    public function init()
192
    {
193
        parent::init();
194
195
        Yii::setAlias('@'.self::MODULE_NAME.'', static::getBaseDir());
196
197
        if (null !== $this->loginUrl && method_exists(Yii::$app, 'getUser')) {
198
            Yii::$app->getUser()->loginUrl = $this->loginUrl;
199
        }
200
201
        self::registerTranslations();
202
203
        $this->previewOptions = array_merge(
204
            require __DIR__ . '/config/preview-options.php',
205
            $this->previewOptions
206
        );
207
208
        $this->thumbStubUrls = array_merge(
209
            require __DIR__ . '/config/thumb-stub-urls.php',
210
            $this->thumbStubUrls
211
        );
212
213
        $this->thumbsConfig = array_merge($this->useInitialThumbsConfig ? require __DIR__ . '/config/thumbs-config.php' : [],
214
            $this->thumbsConfig
215
        );
216
217
        /**
218
         * Set aws s3 upload component.
219
         */
220
        $this->setComponents(
221
            ArrayHelper::merge($this->getS3UploadComponentConfig(), $this->components)
222
        );
223
224
        /**
225
         * Set local upload component.
226
         */
227
        $this->setComponents(
228
            ArrayHelper::merge($this->getLocalUploadComponentConfig(), $this->components)
229
        );
230
    }
231
232
    /**
233
     * Get the view.
234
     *
235
     * @return View
236
     */
237
    public function getView()
238
    {
239
        if (null === $this->_view) {
240
            $this->_view = $this->get('view');
241
        }
242
243
        return $this->_view;
244
    }
245
246
    /**
247
     * Returns module root directory.
248
     *
249
     * @return string
250
     */
251
    public static function getBaseDir(): string
252
    {
253
        return __DIR__;
254
    }
255
256
    /**
257
     * Get src to send new files.
258
     *
259
     * @param string $storageType
260
     *
261
     * @return string
262
     *
263
     * @throws InvalidConfigException
264
     */
265
    public static function getSendUrl(string $storageType): string
266
    {
267
        if (!isset(self::$sendUrls[$storageType])) {
268
            throw new InvalidConfigException('There is no such storage type in the send src urls.');
269
        }
270
271
        return self::$sendUrls[$storageType];
272
    }
273
274
    /**
275
     * Get src to update existing files.
276
     *
277
     * @param string $storageType
278
     *
279
     * @return string
280
     *
281
     * @throws InvalidConfigException
282
     */
283
    public static function getUpdateUrl(string $storageType): string
284
    {
285
        if (!isset(self::$updateUrls[$storageType])) {
286
            throw new InvalidConfigException('There is no such storage type in the update src urls.');
287
        }
288
289
        return self::$updateUrls[$storageType];
290
    }
291
292
    /**
293
     * Get src to delete existing files.
294
     *
295
     * @param string $storageType
296
     *
297
     * @return string
298
     *
299
     * @throws InvalidConfigException
300
     */
301
    public static function getDeleteUrl(string $storageType): string
302
    {
303
        if (!isset(self::$deleteUrls[$storageType])) {
304
            throw new InvalidConfigException('There is no such storage type in the delete src urls.');
305
        }
306
307
        return self::$deleteUrls[$storageType];
308
    }
309
310
    /**
311
     * Set thumb configuration.
312
     *
313
     * @param string $alias
314
     * @param array  $config
315
     *
316
     * @throws InvalidConfigException
317
     *
318
     * @return ThumbConfigInterface
319
     *
320
     */
321
    public static function configureThumb(string $alias, array $config): ThumbConfigInterface
322
    {
323
        if (!isset($config['name']) ||
324
            !isset($config['size']) ||
325
            !is_array($config['size']) ||
326
            (!isset($config['size'][0]) && !is_null($config['size'][0])) ||
327
            (!isset($config['size'][1]) && !is_null($config['size'][1]))
328
        ) {
329
            throw new InvalidConfigException('Error in thumb configuration.');
330
        }
331
332
        $thumbConfig = [
333
            'class'  => ThumbConfig::class,
334
            'alias'  => $alias,
335
            'name'   => $config['name'],
336
            'width'  => $config['size'][0],
337
            'height' => $config['size'][1],
338
            'mode'   => (!empty($config['mode']) ? $config['mode'] : ImageInterface::THUMBNAIL_OUTBOUND),
339
        ];
340
341
        /* @var ThumbConfigInterface $object */
342
        $object = Yii::createObject($thumbConfig);
343
344
        return $object;
345
    }
346
347
    /**
348
     * Default thumb config
349
     *
350
     * @return array
351
     */
352
    public static function getDefaultThumbConfig(): array
353
    {
354
        return [
355
            'name' => 'Default size',
356
            'size' => [150, 150],
357
        ];
358
    }
359
360
    /**
361
     * Get preview options for som types of mediafiles according with their location.
362
     *
363
     * @param string $fileType
364
     * @param string $location
365
     * @param Mediafile|null $mediafile
366
     *
367
     * @return array
368
     */
369
    public function getPreviewOptions(string $fileType, string $location, Mediafile $mediafile = null): array
370
    {
371
        if (null === $fileType || null === $location) {
0 ignored issues
show
introduced by
The condition null === $location is always false.
Loading history...
372
            return [];
373
        }
374
375
        if (!isset($this->previewOptions[$fileType]) || !is_array($this->previewOptions[$fileType])) {
376
            return [];
377
        }
378
379
        $previewOptions = $this->previewOptions[$fileType];
380
381
        if (!isset($previewOptions[$location])) {
382
            return [];
383
        }
384
385
        $result = [];
386
387
        if (is_callable($previewOptions[$location])) {
388
            $result = call_user_func($previewOptions[$location], $mediafile);
389
390
            if (!is_array($result)) {
391
                return [];
392
            }
393
394
        } else if (is_array($previewOptions[$location])) {
395
            $result = $previewOptions[$location];
396
        }
397
398
        return $result;
399
    }
400
401
    /**
402
     * Module translator.
403
     *
404
     * @param       $category
405
     * @param       $message
406
     * @param array $params
407
     * @param null  $language
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $language is correct as it would always require null to be passed?
Loading history...
408
     *
409
     * @return string
410
     */
411
    public static function t($category, $message, $params = [], $language = null)
412
    {
413
        if (null === self::$_translations) {
414
            self::registerTranslations();
415
        }
416
417
        return Yii::t('modules/'.self::MODULE_NAME.'/' . $category, $message, $params, $language);
418
    }
419
420
    /**
421
     * Set i18N component.
422
     *
423
     * @return void
424
     */
425
    private static function registerTranslations(): void
426
    {
427
        self::$_translations = [
428
            'modules/'.self::MODULE_NAME.'/*' => [
429
                'class'          => 'yii\i18n\PhpMessageSource',
430
                'forceTranslation' => true,
431
                'sourceLanguage' => Yii::$app->language,
432
                'basePath'       => '@'.self::MODULE_NAME.'/messages',
433
                'fileMap'        => [
434
                    'modules/'.self::MODULE_NAME.'/main' => 'main.php',
435
                    'modules/'.self::MODULE_NAME.'/album' => 'album.php',
436
                    'modules/'.self::MODULE_NAME.'/filemanager' => 'filemanager.php',
437
                    'modules/'.self::MODULE_NAME.'/uploadmanager' => 'uploadmanager.php',
438
                    'modules/'.self::MODULE_NAME.'/actions' => 'actions.php',
439
                ],
440
            ]
441
        ];
442
443
        Yii::$app->i18n->translations = ArrayHelper::merge(
444
            self::$_translations,
445
            Yii::$app->i18n->translations
446
        );
447
    }
448
449
    /**
450
     * File local upload component config.
451
     *
452
     * @return array
453
     */
454
    private function getLocalUploadComponentConfig(): array
455
    {
456
        return [
457
            'local-upload-component' => [
458
                'class' => LocalUploadComponent::class,
459
                'thumbsConfig' => $this->thumbsConfig,
460
                'thumbFilenameTemplate' => $this->thumbFilenameTemplate,
461
            ]
462
        ];
463
    }
464
465
    /**
466
     * File aws s3 upload component config.
467
     *
468
     * @return array
469
     */
470
    private function getS3UploadComponentConfig(): array
471
    {
472
        return [
473
            's3-upload-component' => [
474
                'class' => S3UploadComponent::class,
475
                'thumbsConfig' => $this->thumbsConfig,
476
                'thumbFilenameTemplate' => $this->thumbFilenameTemplate,
477
            ]
478
        ];
479
    }
480
}
481