|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the 2amigos/yii2-croppie-widget project. |
|
5
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
|
6
|
|
|
* For the full copyright and license information, please view |
|
7
|
|
|
* the LICENSE file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace dosamigos\croppie; |
|
11
|
|
|
|
|
12
|
|
|
use yii\base\Widget; |
|
13
|
|
|
use yii\helpers\Html; |
|
14
|
|
|
use yii\helpers\Json; |
|
15
|
|
|
|
|
16
|
|
|
class CroppieWidget extends Widget |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var array the displayed tag option |
|
20
|
|
|
*/ |
|
21
|
|
|
public $options = []; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var array the plugin options |
|
24
|
|
|
* @see http://foliotek.github.io/Croppie/ |
|
25
|
|
|
*/ |
|
26
|
|
|
public $clientOptions = []; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var array the plugin events. |
|
29
|
|
|
* @see http://foliotek.github.io/Croppie/ |
|
30
|
|
|
*/ |
|
31
|
|
|
public $clientEvents = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @inheritdoc |
|
35
|
|
|
*/ |
|
36
|
12 |
|
public function init() |
|
37
|
|
|
{ |
|
38
|
12 |
|
if (!isset($this->options['id'])) { |
|
39
|
9 |
|
$this->options['id'] = $this->getId(); |
|
40
|
3 |
|
} |
|
41
|
12 |
|
parent::init(); |
|
42
|
12 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @inheritdoc |
|
46
|
|
|
*/ |
|
47
|
9 |
|
public function run() |
|
48
|
|
|
{ |
|
49
|
9 |
|
echo Html::tag('div', '', $this->options); |
|
50
|
|
|
|
|
51
|
9 |
|
$this->registerClientScript(); |
|
52
|
9 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Registers required script for the plugin to work |
|
56
|
|
|
*/ |
|
57
|
9 |
|
public function registerClientScript() |
|
58
|
|
|
{ |
|
59
|
9 |
|
$view = $this->getView(); |
|
60
|
|
|
|
|
61
|
9 |
|
CroppieAsset::register($view); |
|
62
|
|
|
|
|
63
|
9 |
|
if (isset($this->clientOptions['enableExif']) && $this->clientOptions['enableExif'] === true) { |
|
64
|
|
|
ExifJsAsset::register($view); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
9 |
|
$options = !empty($this->clientOptions) |
|
68
|
5 |
|
? Json::encode($this->clientOptions) |
|
69
|
9 |
|
: ''; |
|
70
|
|
|
|
|
71
|
9 |
|
$id = $this->getId(); |
|
72
|
|
|
|
|
73
|
9 |
|
$js[] = ";jQuery('#$id').croppie($options);"; |
|
|
|
|
|
|
74
|
9 |
|
if (!empty($this->clientEvents)) { |
|
75
|
|
|
foreach ($this->clientEvents as $event => $handler) { |
|
76
|
|
|
$js[] = "jQuery('#$id').on('$event', $handler);"; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
9 |
|
$view->registerJs(implode("\n", $js)); |
|
81
|
9 |
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
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.