/
FileUpload.php
76 lines (66 loc) · 1.76 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
<?php
namespace vetoni\filekit;
use yii\base\Exception;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\jui\InputWidget;
/**
* Class FileUpload
* @package vetoni\filekit
*/
class FileUpload extends InputWidget
{
/**
* @var
*/
public $uploadUrl;
/**
* @var
*/
public $deleteUrl;
/**
* @var array
*/
public $clientOptions = [];
/**
* @throws Exception
*/
public function init()
{
parent::init();
if (!isset($this->uploadUrl)) {
throw new Exception('uploadUrl property should be set');
}
if (!isset($this->deleteUrl)) {
throw new Exception('deleteUrl property should be set');
}
}
/**
* @return string
*/
public function run()
{
$this->registerClientScript();
$attributeValue = $this->model->{$this->attribute};
$file = File::findOne(['fid' => $attributeValue]);
return $this->renderFile(dirname(__FILE__) . '/views/widget.php', [
'id' => $this->options['id'],
'name' => Html::getInputName($this->model, $this->attribute),
'fileInputId' => $this->id,
'fileInputName' => "_fileinput_" . $this->id,
'fid' => $attributeValue,
'url' => isset($file) ? $file->url : null,
'uploadUrl' => $this->uploadUrl,
'deleteUrl' => $this->deleteUrl,
]);
}
/**
* Registers all scripts required for the plugin to work
*/
protected function registerClientScript()
{
$this->view->registerAssetBundle(FileUploadAsset::className());
$options = Json::encode($this->clientOptions);
$this->view->registerJs("jQuery('#{$this->id}').yiiFileKit({$options});");
}
}