1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/2amigos/yii2-file-upload-widget |
4
|
|
|
* @copyright Copyright (c) 2013-2016 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 Alexander Kochetov <[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
|
|
|
* @inheritdoc |
35
|
|
|
*/ |
36
|
2 |
|
public function init() |
37
|
|
|
{ |
38
|
2 |
|
parent::init(); |
39
|
|
|
|
40
|
1 |
|
$url = Url::to($this->url); |
41
|
1 |
|
$this->options['data-url'] = $url; |
42
|
1 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritdoc |
46
|
|
|
*/ |
47
|
1 |
|
public function run() |
48
|
|
|
{ |
49
|
1 |
|
$input = $this->hasModel() |
50
|
1 |
|
? Html::activeFileInput($this->model, $this->attribute, $this->options) |
51
|
1 |
|
: Html::fileInput($this->name, $this->value, $this->options); |
52
|
|
|
|
53
|
1 |
|
echo $this->useDefaultButton |
54
|
1 |
|
? $this->render('uploadButton', ['input' => $input]) |
55
|
1 |
|
: $input; |
56
|
|
|
|
57
|
1 |
|
$this->registerClientScript(); |
58
|
1 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Registers required script for the plugin to work as jQuery File Uploader |
62
|
|
|
*/ |
63
|
1 |
|
public function registerClientScript() |
64
|
|
|
{ |
65
|
1 |
|
$view = $this->getView(); |
66
|
|
|
|
67
|
1 |
|
if($this->plus) { |
68
|
1 |
|
FileUploadPlusAsset::register($view); |
69
|
1 |
|
} else { |
70
|
1 |
|
FileUploadAsset::register($view); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
$options = Json::encode($this->clientOptions); |
74
|
1 |
|
$id = $this->options['id']; |
75
|
|
|
|
76
|
1 |
|
$js[] = ";jQuery('#$id').fileupload($options);"; |
|
|
|
|
77
|
1 |
|
if (!empty($this->clientEvents)) { |
78
|
1 |
|
foreach ($this->clientEvents as $event => $handler) { |
79
|
1 |
|
$js[] = "jQuery('#$id').on('$event', $handler);"; |
80
|
1 |
|
} |
81
|
1 |
|
} |
82
|
1 |
|
$view->registerJs(implode("\n", $js)); |
83
|
1 |
|
} |
84
|
|
|
} |
85
|
|
|
|
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
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key 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.