-
Notifications
You must be signed in to change notification settings - Fork 0
/
UploadifyWidget.php
88 lines (82 loc) · 2.36 KB
/
UploadifyWidget.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
<?php
namespace cliff363825\uploadify;
use Yii;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\InputWidget;
/**
* Class UploadifyWidget
* @package cliff363825\uploadify
*/
class UploadifyWidget extends InputWidget
{
/**
* The name of this widget.
*/
const PLUGIN_NAME = 'Uploadify';
/**
* @var string
*/
public $uniqueSalt = '';
/**
* @var array the Uploadify plugin options.
* @see http://www.uploadify.com/documentation/
*/
public $clientOptions = [];
/**
* @inheritdoc
*/
public function run()
{
$this->registerClientScript();
if ($this->hasModel()) {
echo Html::activeHiddenInput($this->model, $this->attribute, ['id' => null])
. Html::activeInput('file', $this->model, $this->attribute, $this->options);
} else {
echo Html::hiddenInput($this->name, $this->value, ['id' => null])
. Html::input('file', $this->name, $this->value, $this->options);
}
}
/**
* Registers the needed client script and options.
*/
public function registerClientScript()
{
$view = $this->getView();
$this->initClientOptions();
$asset = UploadifyAsset::register($view);
if (empty($this->clientOptions['swf'])) {
$this->clientOptions['swf'] = $asset->baseUrl . '/uploadify.swf';
}
$id = $this->options['id'];
$js = "jQuery('#{$id}').uploadify(" . Json::encode($this->clientOptions) . ");";
$view->registerJs($js);
}
/**
* Initializes client options
*/
protected function initClientOptions()
{
$options = array_merge($this->defaultOptions(), $this->clientOptions);
// $_POST['_csrf'] = ...
$options['formData'][Yii::$app->request->csrfParam] = Yii::$app->request->getCsrfToken();
// $_POST['PHPSESSID'] = ...
$options['formData'][Yii::$app->session->name] = Yii::$app->session->id;
$this->clientOptions = $options;
}
/**
* Default client options
* @return array
*/
protected function defaultOptions()
{
$timestamp = time();
return [
'uploader' => '',
'formData' => [
'timestamp' => $timestamp,
'token' => md5($this->uniqueSalt . $timestamp),
],
];
}
}