BaseTheme::getViewAlias()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace yiicod\fileupload\themes\base;
4
5
use Yii;
6
use yii\helpers\ArrayHelper;
7
use yiicod\fileupload\themes\ThemeAbstract;
8
9
/**
10
 * Class BaseTheme
11
 * Base file upload widget theme
12
 *
13
 * @author Virchenko Maksim <[email protected]>
14
 *
15
 * @package yiicod\fileupload\themes\base
16
 */
17
class BaseTheme extends ThemeAbstract
18
{
19
    /**
20
     * Base view name.
21
     *
22
     * @var string
23
     */
24
    public $viewAlias = '@yiicod/yii2-fileupload/themes/base/views/base';
25
26
    /**
27
     * Button text.
28
     *
29
     * @var string
30
     */
31
    public $buttonText = 'Find & Upload';
32
33
//    /**
34
//     * Button text on drag&drop.
35
//     *
36
//     * @var string
37
//     */
38
//    public $dropFilesText = 'Drop Files Here';
39
40
    /**
41
     * Multiple file upload
42
     *
43
     * @var bool
44
     */
45
    public $multiple = false;
46
47
    /**
48
     * Accept type
49
     *
50
     * @var string
51
     */
52
    public $allowedAccept = 'video/*,image/*';
53
54
    /**
55
     * jQuery file upload options (client side).
56
     *
57
     * @var array
58
     */
59
    public $uploaderClientOptions = [];
60
61
    /**
62
     * Theme options
63
     *
64
     * @var array
65
     */
66
    public $themeClientOptions = [];
67
68
    /**
69
     * Get theme view name
70
     *
71
     * @return string
72
     */
73
    public function getViewAlias(): string
74
    {
75
        return $this->viewAlias;
76
    }
77
78
    /**
79
     * Get theme view data
80
     *
81
     * @return array
82
     */
83
    public function getViewData(): array
84
    {
85
        return [
86
            'buttonText' => $this->buttonText,
87
//            'dropFilesText' => $this->dropFilesText,
88
            'multiple' => $this->multiple,
89
            'allowedAccept' => $this->allowedAccept,
90
            'options' => [
91
                'uploader-params' => $this->getUploaderClientOptions(),
92
            ],
93
        ];
94
    }
95
96
    /**
97
     * Register theme client scripts
98
     */
99
    public function registerClientScripts()
100
    {
101
        BaseThemeAsset::register($this->widget->getView());
102
    }
103
104
    /**
105
     * @return array
106
     */
107
    protected function getUploaderClientOptions(): array
108
    {
109
        $clientOptions = ArrayHelper::merge([
110
            'dataType' => 'json',
111
            'minFileSize' => 0,
112
            'maxFileSize' => 10000000,
113
            'maxChunkSize' => 20000000,
114
            'maxNumberOfFiles' => '',
115
            'dropZone' => '#' . $this->widget->id . ' .dropzone',
116
            'messages' => [
117
                'maxNumberOfFiles' => Yii::t('fileupload', 'Maximum number of files exceeded'),
118
                'acceptFileTypes' => isset($this->uploaderClientOptions['acceptFileTypes']) ? Yii::t('fileupload', 'File type not allowed. Allowed extensions are: {extensions}', [
119
                    'extensions' => implode(',', $this->uploaderClientOptions['acceptFileTypes']),
120
                ]) : '',
121
                'maxFileSize' => Yii::t('fileupload', 'File is too large'),
122
                'minFileSize' => Yii::t('fileupload', 'File is too small'),
123
            ],
124
        ], $this->uploaderClientOptions);
125
126
        $clientOptions['url'] = $this->widget->generateUrl($this->widget->uploadUrl);
127
128
        return [
129
            'config' => $clientOptions,
130
//            'messages' => [
131
//                'done' => Yii::t('fileupload', 'Done.'),
132
//                'error' => Yii::t('fileupload', 'Error.'),
133
//                'drop' => $this->dropFilesText,
134
//                'select' => $this->buttonText
135
//            ],
136
        ];
137
    }
138
139
    /**
140
     * Get wrapper client options
141
     *
142
     * @return array
143
     */
144
    protected function getThemeClientOptions(): array
145
    {
146
        return ArrayHelper::merge([
147
            'error' => $this->widget->model->getFirstError($this->widget->attribute),
148
            'buttonText' => $this->buttonText,
149
//            'dropFilesText' => $this->dropFilesText,
150
            'multiple' => $this->multiple,
151
            'allowedAccept' => $this->allowedAccept,
152
        ], $this->themeClientOptions);
153
    }
154
}
155