This repository was archived by the owner on Jul 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathFileUploadUI.php
133 lines (117 loc) · 3.9 KB
/
FileUploadUI.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* @link https://github.com/2amigos/yii2-file-upload-widget
* @copyright Copyright (c) 2013-2019 2amigOS! Consulting Group LLC
* @license http://opensource.org/licenses/BSD-3-Clause
*/
namespace dosamigos\fileupload;
use dosamigos\gallery\GalleryAsset;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
/**
* FileUploadUI
*
* Widget to render the jQuery File Upload UI plugin as shown in
* [its demo](http://blueimp.github.io/jQuery-File-Upload/index.html)
*
* @author Antonio Ramirez <[email protected]>
*/
class FileUploadUI extends BaseUpload
{
/**
* @var bool whether to use the Bootstrap Gallery on the images or not
*/
public $gallery = true;
/**
* @var bool load previously uploaded images or not
*/
public $load = false;
/**
* @var array the HTML attributes for the file input tag.
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $fieldOptions = [];
/**
* @var string the ID of the upload template, given as parameter to the tmpl() method to set the uploadTemplate option.
*/
public $uploadTemplateId;
/**
* @var string the ID of the download template, given as parameter to the tmpl() method to set the downloadTemplate option.
*/
public $downloadTemplateId;
/**
* @var string the form view path to render the JQuery File Upload UI
*/
public $formView = 'form';
/**
* @var string the upload view path to render the js upload template
*/
public $uploadTemplateView = 'upload';
/**
* @var string the download view path to render the js download template
*/
public $downloadTemplateView = 'download';
/**
* @var string the gallery
*/
public $galleryTemplateView = 'gallery';
/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->fieldOptions['multiple'] = true;
$this->fieldOptions['id'] = ArrayHelper::getValue($this->options, 'id');
$this->options['id'] .= '-fileupload';
$this->options['data-upload-template-id'] = $this->uploadTemplateId ? : 'template-upload';
$this->options['data-download-template-id'] = $this->downloadTemplateId ? : 'template-download';
}
/**
* @inheritdoc
*/
public function run()
{
echo $this->render($this->uploadTemplateView);
echo $this->render($this->downloadTemplateView);
echo $this->render($this->formView);
if ($this->gallery) {
echo $this->render($this->galleryTemplateView);
}
$this->registerClientScript();
}
/**
* Registers required script for the plugin to work as jQuery File Uploader UI
*/
public function registerClientScript()
{
$view = $this->getView();
if ($this->gallery) {
GalleryAsset::register($view);
}
FileUploadUIAsset::register($view);
$options = Json::encode($this->clientOptions);
$id = $this->options['id'];
$js[] = ";jQuery('#$id').fileupload($options);";
if (!empty($this->clientEvents)) {
foreach ($this->clientEvents as $event => $handler) {
$js[] = "jQuery('#$id').on('$event', $handler);";
}
}
$view->registerJs(implode("\n", $js));
if ($this->load) {
$view->registerJs("
$('#$id').addClass('fileupload-processing');
$.ajax({
url: $('#$id').fileupload('option', 'url'),
dataType: 'json',
context: $('#$id')[0]
}).always(function () {
$(this).removeClass('fileupload-processing');
}).done(function (result) {
$(this).fileupload('option', 'done').call(this, $.Event('done'), {result: result});
});
");
}
}
}