|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://github.com/2amigos/yii2-file-upload-widget |
|
4
|
|
|
* @copyright Copyright (c) 2013-2017 2amigOS! Consulting Group LLC |
|
5
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace dosamigos\fileupload; |
|
9
|
|
|
|
|
10
|
|
|
use yii\helpers\Html; |
|
11
|
|
|
use yii\helpers\Json; |
|
12
|
|
|
use yii\helpers\Url; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* FileUpload |
|
16
|
|
|
* |
|
17
|
|
|
* Widget to render the jQuery File Upload Basic Uploader |
|
18
|
|
|
* |
|
19
|
|
|
* @author Antonio Ramirez <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class FileUpload extends BaseUpload |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var bool whether to register the js files for the basic + |
|
25
|
|
|
*/ |
|
26
|
|
|
public $plus = false; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var bool whether to render the default button |
|
30
|
|
|
*/ |
|
31
|
|
|
public $useDefaultButton = true; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string the upload view path to render the js upload template |
|
35
|
|
|
*/ |
|
36
|
|
|
public $uploadButtonTemplateView = 'uploadButton'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @inheritdoc |
|
40
|
|
|
*/ |
|
41
|
2 |
|
public function init() |
|
42
|
|
|
{ |
|
43
|
2 |
|
parent::init(); |
|
44
|
|
|
|
|
45
|
1 |
|
$url = Url::to($this->url); |
|
46
|
1 |
|
$this->options['data-url'] = $url; |
|
47
|
1 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @inheritdoc |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public function run() |
|
53
|
|
|
{ |
|
54
|
1 |
|
$input = $this->hasModel() |
|
55
|
1 |
|
? Html::activeFileInput($this->model, $this->attribute, $this->options) |
|
56
|
1 |
|
: Html::fileInput($this->name, $this->value, $this->options); |
|
57
|
|
|
|
|
58
|
1 |
|
echo $this->useDefaultButton |
|
59
|
1 |
|
? $this->render($this->uploadButtonTemplateView, ['input' => $input]) |
|
60
|
1 |
|
: $input; |
|
61
|
|
|
|
|
62
|
1 |
|
$this->registerClientScript(); |
|
63
|
1 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Registers required script for the plugin to work as jQuery File Uploader |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function registerClientScript() |
|
69
|
|
|
{ |
|
70
|
1 |
|
$view = $this->getView(); |
|
71
|
|
|
|
|
72
|
1 |
|
if($this->plus) { |
|
73
|
1 |
|
FileUploadPlusAsset::register($view); |
|
74
|
1 |
|
} else { |
|
75
|
1 |
|
FileUploadAsset::register($view); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
$options = Json::encode($this->clientOptions); |
|
79
|
1 |
|
$id = $this->options['id']; |
|
80
|
|
|
|
|
81
|
1 |
|
$js[] = ";jQuery('#$id').fileupload($options);"; |
|
|
|
|
|
|
82
|
1 |
|
if (!empty($this->clientEvents)) { |
|
83
|
1 |
|
foreach ($this->clientEvents as $event => $handler) { |
|
84
|
1 |
|
$js[] = "jQuery('#$id').on('$event', $handler);"; |
|
85
|
1 |
|
} |
|
86
|
1 |
|
} |
|
87
|
1 |
|
$view->registerJs(implode("\n", $js)); |
|
88
|
1 |
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.