1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/2amigos/yii2-gallery-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\gallery; |
9
|
|
|
|
10
|
|
|
use yii\base\Widget; |
11
|
|
|
use yii\helpers\ArrayHelper; |
12
|
|
|
use yii\helpers\Html; |
13
|
|
|
use yii\helpers\Json; |
14
|
|
|
use yii\web\JsExpression; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Gallery renders a BlueImp Gallery items |
18
|
|
|
* |
19
|
|
|
* @author Alexander Kochetov <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Gallery extends Widget |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array the HTML attributes for the links container tag. |
25
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
26
|
|
|
*/ |
27
|
|
|
public $options = []; |
28
|
|
|
/** |
29
|
|
|
* @var array the HTML attributes for the lightbox container tag. |
30
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
31
|
|
|
*/ |
32
|
|
|
public $templateOptions = []; |
33
|
|
|
/** |
34
|
|
|
* @var array the options for the BlueImp Gallery plugin. |
35
|
|
|
* Please refer to the BlueImp Gallery plugin Web page for possible options. |
36
|
|
|
* @see https://github.com/blueimp/Gallery/blob/master/README.md#setup |
37
|
|
|
*/ |
38
|
|
|
public $clientOptions = []; |
39
|
|
|
/** |
40
|
|
|
* @var array the event handlers for the underlying Bootstrap Switch 3 input JS plugin. |
41
|
|
|
* Please refer to the [BlueImp Gallery plugin](https://github.com/blueimp/Gallery/blob/master/README.md#event-callbacks) |
42
|
|
|
* for information about their callbacks. |
43
|
|
|
*/ |
44
|
|
|
public $clientEvents = []; |
45
|
|
|
/** |
46
|
|
|
* @var array The array of items that compound the gallery. The syntax is as follows: |
47
|
|
|
* |
48
|
|
|
* - src: string, the image to display |
49
|
|
|
* - url: string, the image to display on the lightbox. If none found, will display `src` |
50
|
|
|
* - options: HTML attributes of the link |
51
|
|
|
* - imageOptions: HTML attributes of the image to be displayed |
52
|
|
|
*/ |
53
|
|
|
public $items = array(); |
54
|
|
|
/** |
55
|
|
|
* @var bool whether to display the controls on initialization |
56
|
|
|
*/ |
57
|
|
|
public $showControls = true; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritdoc |
61
|
|
|
*/ |
62
|
6 |
|
public function init() |
63
|
|
|
{ |
64
|
6 |
|
parent::init(); |
65
|
6 |
|
if (!isset($this->options['id'])) { |
66
|
6 |
|
$this->options['id'] = $this->getId(); |
67
|
6 |
|
} |
68
|
6 |
|
$this->templateOptions['id'] = ArrayHelper::getValue($this->templateOptions, 'id', 'blueimp-gallery'); |
69
|
6 |
|
Html::addCssClass($this->templateOptions, 'blueimp-gallery'); |
70
|
6 |
|
if ($this->showControls) { |
71
|
6 |
|
Html::addCssClass($this->templateOptions, 'blueimp-gallery-controls'); |
72
|
6 |
|
} |
73
|
|
|
|
74
|
6 |
|
foreach($this->clientEvents as $key => $event) { |
75
|
6 |
|
if(!($event instanceof JsExpression)) { |
76
|
6 |
|
$this->clientOptions[$key] = new JsExpression($event); |
77
|
6 |
|
} |
78
|
6 |
|
} |
79
|
6 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @inheritdoc |
83
|
|
|
*/ |
84
|
6 |
|
public function run() |
85
|
|
|
{ |
86
|
6 |
|
if (empty($this->items)) { |
87
|
6 |
|
return null; |
88
|
|
|
} |
89
|
6 |
|
echo $this->renderItems(); |
90
|
6 |
|
echo $this->renderTemplate(); |
91
|
6 |
|
$this->registerClientScript(); |
92
|
6 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return string the items that are need to be rendered. |
96
|
|
|
*/ |
97
|
6 |
|
public function renderItems() |
98
|
|
|
{ |
99
|
6 |
|
$items = []; |
100
|
6 |
|
foreach ($this->items as $item) { |
101
|
6 |
|
$items[] = $this->renderItem($item); |
102
|
6 |
|
} |
103
|
6 |
|
return Html::tag('div', implode("\n", array_filter($items)), $this->options); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param mixed $item |
108
|
|
|
* @return null|string the item to render |
109
|
|
|
*/ |
110
|
6 |
|
public function renderItem($item) |
111
|
|
|
{ |
112
|
6 |
|
if (is_string($item)) { |
113
|
3 |
|
return Html::a(Html::img($item), $item, ['class' => 'gallery-item']); |
114
|
|
|
} |
115
|
6 |
|
$src = ArrayHelper::getValue($item, 'src'); |
116
|
6 |
|
if ($src === null) { |
117
|
3 |
|
return null; |
118
|
|
|
} |
119
|
6 |
|
$url = ArrayHelper::getValue($item, 'url', $src); |
120
|
6 |
|
$options = ArrayHelper::getValue($item, 'options', []); |
121
|
6 |
|
$imageOptions = ArrayHelper::getValue($item, 'imageOptions', []); |
122
|
6 |
|
Html::addCssClass($options, 'gallery-item'); |
123
|
|
|
|
124
|
6 |
|
return Html::a(Html::img($src, $imageOptions), $url, $options); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Renders the template to display the images on a lightbox |
129
|
|
|
* @return string the template |
130
|
|
|
*/ |
131
|
3 |
|
public function renderTemplate() |
132
|
|
|
{ |
133
|
3 |
|
$template[] = '<div class="slides"></div>'; |
|
|
|
|
134
|
3 |
|
$template[] = '<h3 class="title"></h3>'; |
135
|
3 |
|
$template[] = '<a class="prev">‹</a>'; |
136
|
3 |
|
$template[] = '<a class="next">›</a>'; |
137
|
3 |
|
$template[] = '<a class="close">×</a>'; |
138
|
3 |
|
$template[] = '<a class="play-pause"></a>'; |
139
|
3 |
|
$template[] = '<ol class="indicator"></ol>'; |
140
|
|
|
|
141
|
3 |
|
return Html::tag('div', implode("\n", $template), $this->templateOptions); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Registers the client script required for the plugin |
146
|
|
|
*/ |
147
|
3 |
|
public function registerClientScript() |
148
|
|
|
{ |
149
|
3 |
|
$view = $this->getView(); |
150
|
3 |
|
GalleryAsset::register($view); |
151
|
3 |
|
DosamigosAsset::register($view); |
152
|
|
|
|
153
|
3 |
|
$id = $this->options['id']; |
154
|
3 |
|
$options = Json::encode($this->clientOptions); |
155
|
3 |
|
$js = "dosamigos.gallery.registerLightBoxHandlers('#$id a', $options);"; |
156
|
3 |
|
$view->registerJs($js); |
157
|
|
|
|
158
|
3 |
|
if (!empty($this->clientEvents)) { |
159
|
3 |
|
$js = []; |
160
|
3 |
|
foreach ($this->clientEvents as $event => $handler) { |
161
|
3 |
|
$js[] = "jQuery('$id').on('$event', $handler);"; |
162
|
3 |
|
} |
163
|
3 |
|
$view->registerJs(implode("\n", $js)); |
164
|
3 |
|
} |
165
|
3 |
|
} |
166
|
|
|
} |
167
|
|
|
|
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.