/
Fileupload.php
90 lines (66 loc) · 1.89 KB
/
Fileupload.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
<?php
/*!
* yii - widget - file upload
* xiewulong <xiewulong@vip.qq.com>
* https://github.com/xiewulong/yii2-fileupload
* https://raw.githubusercontent.com/xiewulong/yii2-fileupload/master/LICENSE
* create: 2016/8/12
* update: 2016/8/14
* since: 0.0.2
*/
namespace yii\fileupload;
use Yii;
use yii\base\Widget;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
class Fileupload extends Widget {
public $model;
public $attribute;
public $value;
public $options = [];
public $hiddenOptions = [];
public $fileOptions = [];
public $action;
public $min;
public $max;
public $type;
public $sizes;
private $_active;
private $_name;
private $_value;
public function init() {
parent::init();
$this->setNameAndValue();
FileuploadAsset::register($this->getView());
}
public function run() {
return Html::tag('div', $this->hiddenInput . $this->fileInput, $this->options);
}
protected function getFileInput() {
return Html::input('file', null, null, ArrayHelper::merge($this->fileOptions, [
'data-fileupload' => $this->_name,
'data-action' => $this->action,
'data-min' => $this->min,
'data-max' => $this->max,
'data-type' => $this->type,
'data-sizes' => $this->sizes,
'data-csrf-param' => \Yii::$app->request->csrfParam,
'data-csrf-token' => \Yii::$app->request->csrfToken,
]));
}
protected function getHiddenInput() {
return $this->_value ? Html::input('hidden', $this->_name, $this->_value, $this->hiddenOptions) : null;
}
private function setNameAndValue() {
if($this->model) {
$this->_name = Html::getInputName($this->model, $this->attribute);
$this->_value = Html::getAttributeValue($this->model, $this->attribute);
if(!array_key_exists('id', $this->options)) {
$this->options['id'] = Html::getInputId($this->model, $this->attribute);
}
} else {
$this->_name = $this->attribute;
$this->_value = $this->value;
}
}
}