Passed
Push — master ( 29d82b...e3431b )
by Sugeng
01:59
created

Dropify   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 34
c 5
b 0
f 0
dl 0
loc 84
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 10 2
A init() 0 12 2
1
<?php
2
3
namespace diecoding\dropify;
4
5
use diecoding\dropify\assets\DropifyAsset;
6
use yii\helpers\ArrayHelper;
7
use yii\helpers\Html;
8
use yii\helpers\Json;
9
use yii\widgets\InputWidget;
10
11
/**
12
 * Dropify is class for widgets that collect user file inputs.
13
 * 
14
 * @link [sugeng-sulistiyawan.github.io](sugeng-sulistiyawan.github.io)
15
 * @author Sugeng Sulistiyawan <[email protected]>
16
 * @copyright Copyright (c) 2023
17
 */
18
class Dropify extends InputWidget
19
{
20
    /**
21
     * @var array image file extensions for show preview
22
     */
23
    public $imgFileExtensions = [
24
        // Animated Portable Network Graphics
25
        'apng',
26
27
        // AV1 Image File Format
28
        'avif',
29
30
        // Graphics Interchange Format
31
        'gif',
32
33
        // Joint Photographic Expert Group image
34
        'jpeg',
35
        'jpg',
36
        'jpeg',
37
        'jfif',
38
        'pjpeg',
39
        'pjp',
40
41
        // Portable Network Graphics
42
        'png',
43
44
        // Scalable Vector Graphics
45
        'svg',
46
47
        // Web Picture format
48
        'webp',
49
50
        // Bitmap file
51
        'bmp',
52
53
        // Microsoft Icon
54
        'ico',
55
        'cur',
56
57
        // Tagged Image File Format
58
        'tif',
59
        'tiff',
60
    ];
61
62
    /**
63
     * @var bool default `false`, `true` if use custom or external dropify assets
64
     */
65
    public $skipCoreAssets = false;
66
67
    /**
68
     * @var array default `[]`, for option `$(#options['id']).dropify(pluginOptions);`
69
     */
70
    public $pluginOptions = [];
71
72
    /**
73
     * @inheritdoc
74
     */
75
    public function init()
76
    {
77
        parent::init();
78
79
        if ($this->skipCoreAssets === false) {
80
            $this->view->registerAssetBundle(DropifyAsset::class);
81
        }
82
83
        $this->pluginOptions = ArrayHelper::merge([
84
            "class"             => "dropify",
85
            "imgFileExtensions" => $this->imgFileExtensions,
86
        ], $this->pluginOptions);
87
    }
88
89
    /**
90
     * @inheritdoc
91
     */
92
    public function run()
93
    {
94
        $pluginOptions = Json::encode($this->pluginOptions);
95
96
        $this->view->registerJs("$('#{$this->options['id']}').dropify({$pluginOptions});");
97
98
        if ($this->hasModel()) {
99
            return Html::activeFileInput($this->model, $this->attribute, $this->options);
100
        } else {
101
            return Html::fileInput($this->name, $this->value, $this->options);
102
        }
103
    }
104
}
105