forked from subdee/yii-dropzone
/
EDropzone.php
119 lines (102 loc) · 4.35 KB
/
EDropzone.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
<?php
/**
*
* Yii extension to the drag n drop HTML5 file upload Dropzone.js
* For more info, see @link http://www.dropzonejs.com/
*
* @link https://github.com/subdee/yii-dropzone
*
* @author Konstantinos Thermos
*
* @copyright
* Copyright (c) 2013 Konstantinos Thermos
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
class EDropzone extends CWidget {
/**
* @var string The name of the file field
*/
public $name = false;
/**
* @var CModel The model for the file field
*/
public $model = false;
/**
* @var string The attribute of the model
*/
public $attribute = false;
/**
* @var array An array of options that are supported by Dropzone
*/
public $options = array();
/**
* @var string The URL that handles the file upload
*/
public $url = false;
/**
* @var array An array of supported MIME types
*/
public $mimeTypes = array();
/**
* @var array The Javascript to be called on any event
*/
public $events = array();
/**
* @var array The HTML options using in the tag div
*/
public $htmlOptions = array();
/**
* @var string The path to custom css file
*/
public $customStyle = false;
/**
* Create a div and the appropriate Javascript to make the div into the file upload area
*/
public function run() {
if (!$this->url || $this->url == '')
$this->url = Yii::app()->createUrl('site/upload');
if (!$this->name && ($this->model && $this->attribute) && $this->model instanceof CModel)
$this->name = CHtml::activeName($this->model, $this->attribute);
echo CHtml::openTag('div', CMap::mergeArray(array('class' => 'dropzone', 'id' => $this->name), $this->htmlOptions));
echo CHtml::closeTag('div');
$this->mimeTypes = CJavaScript::encode($this->mimeTypes);
$onEvents = '';
foreach($this->events as $event => $func){
$onEvents .= "this.on('{$event}', function(param, param2, param3){{$func}} );";
}
$options = CMap::mergeArray(array(
'url' => $this->url,
'parallelUploads' => 5,
'paramName' => $this->name,
'accept' => "js:function(file, done){if(jQuery.inArray(file.type,{$this->mimeTypes}) == -1 ){done('File type not allowed.');}else{done();}}",
'init' => "js:function(){{$onEvents}}"
), $this->options);
$options = CJavaScript::encode($options);
$script = "Dropzone.options.{$this->name} = {$options}";
$this->registerAssets();
Yii::app()->getClientScript()->registerScript(__CLASS__ . '#' . $this->getId(), $script, CClientScript::POS_LOAD);
}
private function registerAssets() {
$basePath = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR;
$baseUrl = Yii::app()->getAssetManager()->publish($basePath, false, 1, YII_DEBUG);
Yii::app()->getClientScript()->registerCoreScript('jquery');
Yii::app()->getClientScript()->registerScriptFile("{$baseUrl}/js/dropzone.js", CClientScript::POS_BEGIN);
Yii::app()->getClientScript()->registerCssFile("{$baseUrl}/css/dropzone.css");
if(!$this->customStyle || $this->customStyle !== '')
Yii::app()->getClientScript()->registerCssFile($this->customStyle);
}
}
?>