FileUploadUI   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 68%

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 6
dl 0
loc 112
ccs 34
cts 50
cp 0.68
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 11 3
A run() 0 12 2
B registerClientScript() 0 36 5
1
<?php
2
/**
3
 * @link https://github.com/2amigos/yii2-file-upload-widget
4
 * @copyright Copyright (c) 2013-2017 2amigOS! Consulting Group LLC
5
 * @license http://opensource.org/licenses/BSD-3-Clause
6
 */
7
8
namespace dosamigos\fileupload;
9
10
use dosamigos\gallery\GalleryAsset;
11
use yii\helpers\ArrayHelper;
12
use yii\helpers\Json;
13
14
/**
15
 * FileUploadUI
16
 *
17
 * Widget to render the jQuery File Upload UI plugin as shown in
18
 * [its demo](http://blueimp.github.io/jQuery-File-Upload/index.html)
19
 *
20
 * @author Antonio Ramirez <[email protected]>
21
 */
22
class FileUploadUI extends BaseUpload
23
{
24
    /**
25
     * @var bool whether to use the Bootstrap Gallery on the images or not
26
     */
27
    public $gallery = true;
28
    /**
29
     * @var bool load previously uploaded images or not
30
     */
31
    public $load = false;
32
    /**
33
     * @var array the HTML attributes for the file input tag.
34
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
35
     */
36
    public $fieldOptions = [];
37
    /**
38
     * @var string the ID of the upload template, given as parameter to the tmpl() method to set the uploadTemplate option.
39
     */
40
    public $uploadTemplateId;
41
    /**
42
     * @var string the ID of the download template, given as parameter to the tmpl() method to set the downloadTemplate option.
43
     */
44
    public $downloadTemplateId;
45
    /**
46
     * @var string the form view path to render the JQuery File Upload UI
47
     */
48
    public $formView = 'form';
49
    /**
50
     * @var string the upload view path to render the js upload template
51
     */
52
    public $uploadTemplateView = 'upload';
53
    /**
54
     * @var string the download view path to render the js download template
55
     */
56
    public $downloadTemplateView = 'download';
57
    /**
58
     * @var string the gallery
59
     */
60
    public $galleryTemplateView = 'gallery';
61
62
63
    /**
64
     * @inheritdoc
65
     */
66 2
    public function init()
67
    {
68 2
        parent::init();
69
70 1
        $this->fieldOptions['multiple'] = true;
71 1
        $this->fieldOptions['id'] = ArrayHelper::getValue($this->options, 'id');
72
73 1
        $this->options['id'] .= '-fileupload';
74 1
        $this->options['data-upload-template-id'] = $this->uploadTemplateId ? : 'template-upload';
75 1
        $this->options['data-download-template-id'] = $this->downloadTemplateId ? : 'template-download';
76 1
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81 1
    public function run()
82
    {
83 1
        echo $this->render($this->uploadTemplateView);
84 1
        echo $this->render($this->downloadTemplateView);
85 1
        echo $this->render($this->formView);
86
87 1
        if ($this->gallery) {
88 1
            echo $this->render($this->galleryTemplateView);
89 1
        }
90
91 1
        $this->registerClientScript();
92 1
    }
93
94
    /**
95
     * Registers required script for the plugin to work as jQuery File Uploader UI
96
     */
97 1
    public function registerClientScript()
98
    {
99 1
        $view = $this->getView();
100
101 1
        if ($this->gallery) {
102 1
            GalleryAsset::register($view);
103 1
        }
104
105 1
        FileUploadUIAsset::register($view);
106
107 1
        $options = Json::encode($this->clientOptions);
108 1
        $id = $this->options['id'];
109
110 1
        $js[] = ";jQuery('#$id').fileupload($options);";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$js was never initialized. Although not strictly required by PHP, it is generally a good practice to add $js = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
111 1
        if (!empty($this->clientEvents)) {
112 1
            foreach ($this->clientEvents as $event => $handler) {
113 1
                $js[] = "jQuery('#$id').on('$event', $handler);";
114 1
            }
115 1
        }
116 1
        $view->registerJs(implode("\n", $js));
117
118 1
        if ($this->load) {
119
            $view->registerJs("
120
                $('#$id').addClass('fileupload-processing');
121
                $.ajax({
122
                    url: $('#$id').fileupload('option', 'url'),
123
                    dataType: 'json',
124
                    context: $('#$id')[0]
125
                }).always(function () {
126
                    $(this).removeClass('fileupload-processing');
127
                }).done(function (result) {
128
                    $(this).fileupload('option', 'done').call(this, $.Event('done'), {result: result});
129
                });
130
            ");
131
        }
132 1
    }
133
}
134