-
Notifications
You must be signed in to change notification settings - Fork 34
/
XUpload.php
205 lines (174 loc) · 6.98 KB
/
XUpload.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
Yii::import('zii.widgets.jui.CJuiInputWidget');
/**
* XUpload extension for Yii.
*
* jQuery file upload extension for Yii, allows your users to easily upload files to your server using jquery
* Its a wrapper of http://blueimp.github.com/jQuery-File-Upload/
*
* @author AsgarothBelem <asgaroth.belem@gmail.com>
* @link http://blueimp.github.com/jQuery-File-Upload/
* @link https://github.com/Asgaroth/xupload
* @version 0.2
*
*/
class XUpload extends CJuiInputWidget {
/**
* the url to the upload handler
* @var string
*/
public $url;
/**
* set to true to use multiple file upload
* @var boolean
*/
public $multiple = false;
/**
* The upload template id to display files available for upload
* defaults to null, meaning using the built-in template
*/
public $uploadTemplate;
/**
* The template id to display files available for download
* defaults to null, meaning using the built-in template
*/
public $downloadTemplate;
/**
* Whether or not to preview image files before upload
*/
public $previewImages = true;
/**
* Whether or not to add the image processing plugin
*/
public $imageProcessing = true;
/**
* Whether or not to include our CSS style
* @var bool defaults to true
*/
public $registerCSS = true;
/**
* set to true to auto Uploading Files
* @var boolean
*/
public $autoUpload = false;
/**
* @var string name of the form view to be rendered
*/
public $formView = 'form';
/**
* @var string name of the upload view to be rendered
*/
public $uploadView = 'upload';
/**
* @var string name of the download view to be rendered
*/
public $downloadView = 'download';
/**
* @var bool whether form tag should be used at widget
*/
public $showForm = true;
/**
* Publishes the required assets
*/
public function init() {
parent::init();
$this -> publishAssets();
}
/**
* Generates the required HTML and Javascript
*/
public function run() {
list($name, $id) = $this -> resolveNameID();
$model = $this -> model;
if ($this -> uploadTemplate === null) {
$this -> uploadTemplate = "#template-upload";
}
if ($this -> downloadTemplate === null) {
$this -> downloadTemplate = "#template-download";
}
$this -> render($this->uploadView);
$this -> render($this->downloadView);
if (!isset($this -> htmlOptions['enctype'])) {
$this -> htmlOptions['enctype'] = 'multipart/form-data';
}
if (!isset($this -> htmlOptions['id'])) {
$this -> htmlOptions['id'] = get_class($model) . "-form";
}
$this->options['url'] = $this->url;
$this->options['autoUpload'] = $this -> autoUpload;
if (!$this->multiple) {
$this->options['maxNumberOfFiles'] = 1;
}
$options = CJavaScript::encode($this -> options);
Yii::app() -> clientScript -> registerScript(__CLASS__ . '#' . $this -> htmlOptions['id'], "jQuery('#{$this->htmlOptions['id']}').fileupload({$options});", CClientScript::POS_READY);
$htmlOptions = array();
if ($this -> multiple) {
$htmlOptions["multiple"] = true;
/* if($this->hasModel()){
$this -> attribute = "[]" . $this -> attribute;
}else{
$this -> attribute = "[]" . $this -> name;
}*/
}
$this -> render($this->formView, compact('htmlOptions'));
}
/**
* Publishes and registers the required CSS and Javascript
* @throws CHttpException if the assets folder was not found
*/
public function publishAssets() {
$assets = dirname(__FILE__) . '/assets';
$baseUrl = Yii::app() -> assetManager -> publish($assets);
if (is_dir($assets)) {
if($this->registerCSS){
Yii::app() -> clientScript -> registerCssFile($baseUrl . '/css/jquery.fileupload-ui.css');
}
//The Templates plugin is included to render the upload/download listings
Yii::app() -> clientScript -> registerScriptFile($baseUrl . '/js/tmpl.min.js', CClientScript::POS_END);
// The basic File Upload plugin
Yii::app() -> clientScript -> registerScriptFile($baseUrl . '/js/jquery.fileupload.js', CClientScript::POS_END);
if($this->previewImages || $this->imageProcessing){
Yii::app() -> clientScript -> registerScriptFile($baseUrl . '/js/load-image.min.js', CClientScript::POS_END);
Yii::app() -> clientScript -> registerScriptFile($baseUrl . '/js/canvas-to-blob.min.js', CClientScript::POS_END);
}
//The Iframe Transport is required for browsers without support for XHR file uploads
Yii::app() -> clientScript -> registerScriptFile($baseUrl . '/js/jquery.iframe-transport.js', CClientScript::POS_END);
// The File Upload image processing plugin
if($this->imageProcessing){
Yii::app() -> clientScript -> registerScriptFile($baseUrl . '/js/jquery.fileupload-ip.js', CClientScript::POS_END);
}
//The File Upload user interface plugin
Yii::app() -> clientScript -> registerScriptFile($baseUrl . '/js/jquery.fileupload-ui.js', CClientScript::POS_END);
//The localization script
$messages = CJavaScript::encode(array(
'fileupload' => array(
'errors' => array(
"maxFileSize" => $this->t('File is too big'),
"minFileSize" => $this->t('File is too small'),
"acceptFileTypes" => $this->t('Filetype not allowed'),
"maxNumberOfFiles" => $this->t('Max number of files exceeded'),
"uploadedBytes" => $this->t('Uploaded bytes exceed file size'),
"emptyResult" => $this->t('Empty file upload result'),
),
'error' => $this->t('Error'),
'start' => $this->t('Start'),
'cancel' => $this->t('Cancel'),
'destroy' => $this->t('Delete'),
),
));
$js = "window.locale = {$messages}";
Yii::app()->clientScript->registerScript('XuploadI18N', $js, CClientScript::POS_END);
/**
<!-- The XDomainRequest Transport is included for cross-domain file deletion for IE8+ -->
<!--[if gte IE 8]><script src="<?php echo Yii::app()->baseUrl; ?>/js/cors/jquery.xdr-transport.js"></script><![endif]-->
*
*/
} else {
throw new CHttpException(500, __CLASS__ . ' - Error: Couldn\'t find assets to publish.');
}
}
protected function t($message, $params=array ( ))
{
return Yii::t('xupload.widget', $message, $params);
}
}