-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUploadify.php
159 lines (141 loc) · 3.88 KB
/
Uploadify.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
<?php
/*
* 2014-10-15
* @author Prawee Wongsa <konkeanweb@gmail.com>
* original idea => xjflyttp/yii2-uploadify-widget
*/
namespace prawee\uploadify;
use Yii;
use yii\widgets\InputWidget;
use yii\helpers\Url;
use yii\helpers\Html;
use prawee\uploadify\UploadifyAsset;
use yii\web\JsExpression;
use yii\helpers\Json;
use yii\web\View;
class Uploadify extends InputWidget
{
/*
* @var string
* ['user/upload-photo']
* ['upload']
*/
public $url;
/*
* @var array
* [
* 'height'=>30,
* 'width'=>120,
* 'swf'=>'/uploadify/uploadify.swf',
* 'uploader'=>'/uploadify/uploadify.php',
* ]
*/
public $jsOptions=[];
/*
* @var array
*/
public $events=[
'onCancel', 'onClearQueue', 'onDestroy', 'onDialogClose', 'onDialogOpen',
'onDisable', 'onEnable', 'onFallback', 'onInit', 'onQueueComplete',
'onSelect', 'onSelectError', 'onSWFReady', 'onUploadComplete',
'onUploadError', 'onUploadProgress', 'onUploadStart', 'onUploadSuccess',
];
/*
* enable csrf verify
* @var bool
*/
public $csrf=true;
/*
* Tag
* @var bool
*/
public $renderTag=true;
/*
* Initializes the widget
*/
public function init() {
//init variable
if(empty($this->url)){
$this->url= Url::to('index');
}
if(empty($this->id)){
$this->id=$this->hasModel()? Html::getInputId($this->model, $this->attribute):$this->getId();
}
$this->options['id']=$this->id;
if(empty($this->name)){
$this->name=$this->hasModel() ? Html::getInputName($this->model, $this->attribute): $this->id;
}
//register css & js
$asset = UploadifyAsset::register($this->view);
//init options
$this->initUploadifdyOptions($asset);
parent::init();
}
/*
* Renders the widget
*/
public function run() {
$this->registerScripts();
if($this->renderTag===true){
echo $this->renderTag();
}
}
/*
* init Uploadify options
* @param [] $assets
* @return void
*/
private function initUploadifyOptions($asset){
$baseUrl=$asset->baseUrl;
$this->jsOptions['uploader']=$this->url;
$this->jsOptions['swf']=$baseUrl.'/uploadify.swf';
//csrf options
if($this->csrf){
$this->initUploadifyCsrfOption($this->jsOptions);
}
/*
* JsExpression convert
*/
foreach($this->jsOptions as $key => $val){
if(in_array($key,$this->events) && !($val instanceof JsExpression)){
$this->jsOptions[$key]=new JsExpression($val);
}
}
}
/*
* uploadify csrf options
* @param type $jsOptions
* @return void
*/
private function initUploadifyCsrfOption(&$jsOptions){
$session = Yii::$app->session;
$session->open();
$sessionIdName = $session->getName();
$sessionIdValue = $session->getId();
$request = Yii::$app->request;
$csrfName = $request->csrfParam;
$csrfValue = $request->getCsrfToken();
$session->set($csrfName, $csrfValue);
$jsOptions['formData'] = [
$sessionIdName => $sessionIdValue,
$csrfName => $csrfValue,
];
}
/**
* render file input tag
* @return string
*/
private function renderTag() {
return Html::fileInput($this->name, null, $this->options);
}
/**
* register script
*/
private function registerScripts() {
$jsonOptions = Json::encode($this->jsOptions);
$script = <<<EOF
$('#{$this->id}').uploadify({$jsonOptions});
EOF;
$this->view->registerJs($script, View::POS_READY);
}
}