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