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
|
|
|
|