1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/2amigos/yii2-file-input-widget |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2013-2015 2amigOS! Consulting Group LLC |
6
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace dosamigos\fileinput; |
10
|
|
|
|
11
|
|
|
use yii\helpers\Html; |
12
|
|
|
use yii\helpers\Json; |
13
|
|
|
use yii\widgets\InputWidget; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* BootstrapFileInput widget renders the improved and amazing plugin version from Krajee. It supports multiple file |
17
|
|
|
* preview with both images and/or text types. |
18
|
|
|
* |
19
|
|
|
* @author Antonio Ramirez <[email protected]> |
20
|
|
|
* |
21
|
|
|
* @link http://www.ramirezcobos.com/ |
22
|
|
|
* @link http://www.2amigos.us/ |
23
|
|
|
*/ |
24
|
|
|
class BootstrapFileInput extends InputWidget |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var array the options for the Bootstrap File Input plugin. Default options have exporting enabled. |
28
|
|
|
* Please refer to the Bootstrap File Input plugin Web page for possible options. |
29
|
|
|
* |
30
|
|
|
* @see http://plugins.krajee.com/file-input#options |
31
|
|
|
*/ |
32
|
|
|
public $clientOptions = []; |
33
|
|
|
/** |
34
|
|
|
* @var array the event handlers for the underlying Jasny file input JS plugin. |
35
|
|
|
* Please refer to the [Bootstrap File Input](http://plugins.krajee.com/file-input#events) plugin |
36
|
|
|
* Web page for possible events. |
37
|
|
|
*/ |
38
|
|
|
public $clientEvents = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
3 |
|
public function run() |
44
|
|
|
{ |
45
|
3 |
|
if ($this->hasModel()) { |
46
|
3 |
|
echo Html::activeFileInput($this->model, $this->attribute, $this->options); |
47
|
3 |
|
} else { |
48
|
3 |
|
echo Html::fileInput($this->name, $this->value, $this->options); |
49
|
|
|
} |
50
|
3 |
|
$this->registerClientScript(); |
51
|
3 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Registers Bootstrap File Input plugin. |
55
|
|
|
*/ |
56
|
3 |
|
public function registerClientScript() |
57
|
|
|
{ |
58
|
3 |
|
$view = $this->getView(); |
59
|
|
|
|
60
|
3 |
|
BootstrapFileInputAsset::register($view); |
61
|
|
|
|
62
|
3 |
|
$id = $this->options['id']; |
63
|
|
|
|
64
|
3 |
|
$options = !empty($this->clientOptions) ? Json::encode($this->clientOptions) : ''; |
65
|
|
|
|
66
|
3 |
|
$js[] = ";jQuery('#$id').fileinput({$options});"; |
|
|
|
|
67
|
|
|
|
68
|
3 |
|
if (!empty($this->clientEvents)) { |
69
|
3 |
|
foreach ($this->clientEvents as $event => $handler) { |
70
|
3 |
|
$js[] = ";jQuery('#$id').on('$event', $handler);"; |
71
|
3 |
|
} |
72
|
3 |
|
} |
73
|
3 |
|
$view->registerJs(implode("\n", $js)); |
74
|
3 |
|
} |
75
|
|
|
} |
76
|
|
|
|
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.