-
Notifications
You must be signed in to change notification settings - Fork 0
/
SliderRevolution.php
221 lines (173 loc) · 7.15 KB
/
SliderRevolution.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
namespace mrlco\revolutionslider;
use mrlco\revolutionslider\SliderAsset;
use yii\helpers\Html;
use yii\base\InvalidConfigException;
/**
* Slider Revolution Widget
*
* @package yii2-revolutionslider5
* @version 1.0.0
*/
class SliderRevolution extends \yii\base\Widget
{
public $defaultConfig = ['delay' => 9000, 'startwidth' => 1170, 'startheight' => 500, 'hideThumbs' => 10];
// default from docs
public $container = ['class' => 'tp-banner-container'];
// default from docs
public $wrapper = ['class' => 'tp-banner'];
// none by default
public $ulOptions = [];
//<li data-transition="fade" data-slotamount="2" data-masterspeed="1500" >
public $defaultSlideOptions = ['data' => ['transition' => 'fade', 'slotamount' => '2', 'masterspeed' => '1500']];
//alt="slidebg1" data-bgfit="cover" data-bgposition="left center" data-bgrepeat="no-repeat"
public $defaultImageOptions = ['alt' => '', 'data' => ['bgfit' => 'cover', 'bgposition' => 'left center', 'bgrepeat' => 'no-repeat']];
// class="tp-caption lft"
public $defaultLayerOptions = ['class' => 'tp-caption lft', 'data' => ['x' => 'center', 'y' => 'top', 'hoffset' => '0', 'voffset' => '50', 'speed' => '2500', 'start' => '1200', 'easing' => 'Power4.easeOut', 'endspeed' => '300', 'endeasing' => 'Power1.easeIn', 'captionhidden' => 'off']];
public $defaultContent = 'My Caption';
public $config;
public $slides;
private $basePath;
private $baseUrl;
private $rawSliderHtml;
private $_module;
private $_pluginLocation;
public function init()
{
parent::init();
$this->_module = \Yii::$app->getModule('revolutionslider');
$this->_pluginLocation = $this->_module->getPluginLocation();
if ($this->_module === null) {
throw new InvalidConfigException('Slider Revolution Error: Module not found!');
}
if ($this->_pluginLocation === null) {
throw new InvalidConfigException('Slider Revolution Error: Plugin location not found!');
}
if ($this->config === null || !is_array($this->config)) {
$this->config = $this->defaultConfig;
}
if (!isset($this->slides)) {
throw new InvalidConfigException('Slider Revolution Error: You must have at least 1 slide!');
}
$asset = SliderAsset::register($this->view);
$this->basePath = $asset->basePath;
$this->baseUrl = $asset->baseUrl;
}
public function run()
{
$this->buildJsScript();
$this->buildSlider();
return $this->getSliderHtml();
}
public function buildJsScript()
{
$jqueryIdentifier;
if (isset($this->wrapper['id'])) {
$jqueryIdentifier = '#' . $this->wrapper['id'];
} elseif (isset($this->wrapper['class'])) {
$jqueryIdentifier = '.' . $this->wrapper['class'];
} else {
throw new InvalidConfigException('Slider Revolution Error: Wrapper requires an id or a class!');
}
$config = '';
foreach ($this->config as $key => $val) {
if (is_array($val)) {
$item = '{';
foreach ($val as $key2 => $val2) {
if (is_array($val2)) {
$subItem = '{';
foreach ($val2 as $key3 => $val3) {
if (is_array($val3)) {
$subItem2 = '{';
foreach ($val3 as $key4 => $val4) {
$subItem2 .= $key4 . ':' . $val4 . ',';
}
$subItem .= $key3 . ':' . rtrim($subItem2, ',') . '},';
} else {
$subItem .= $key3 . ':' . $val3 . ',';
}
}
$item .= $key2 . ':' . rtrim($subItem, ',') . '},';
} else {
$item .= $key2 . ':' . $val2 . ',';
}
}
$config .= $key . ':' . rtrim($item, ',') . '},';
} else {
$config .= $key . ':' . $val . ',' . PHP_EOL;
}
}
$code = "
var tpj=jQuery;
var revapi24;
tpj(document).ready(function() {
if(tpj('" . $jqueryIdentifier . "').revolution == undefined){
revslider_showDoubleJqueryError('" . $jqueryIdentifier . "');
} else {
revapi24 = tpj('" . $jqueryIdentifier . "').show().revolution({
" . rtrim($config, ','.PHP_EOL) . "
});
}
if (revapi24) revapi24.revSliderSlicey();
});";
$this->view->registerJs($code, \yii\web\View::POS_END, 'yii2-revolutionslider5');
}
public function buildSlider()
{
$this->rawSliderHtml = '';
$this->rawSliderHtml .= Html::beginTag('div', $this->container);
$this->rawSliderHtml .= Html::beginTag('div', $this->wrapper);
$this->rawSliderHtml .= Html::beginTag('ul', $this->ulOptions);
$this->rawSliderHtml .= $this->renderSlides();
$this->rawSliderHtml .= Html::endTag('ul');
$this->rawSliderHtml .= Html::tag('div', '',['class' => 'tp-bannertimer', 'style' => 'height: 7px; background: rgba(255, 255, 255, 0.25);']);
$this->rawSliderHtml .= Html::endTag('div');
$this->rawSliderHtml .= Html::endTag('div');
}
public function renderSlides()
{
$slidesHtml = '';
foreach ($this->slides as $slide) {
$slidesHtml .= $this->renderSlide($slide);
}
return $slidesHtml;
}
public function renderSlide($slide)
{
if (!isset($slide['options'])) {
$slide['options'] = $this->defaultSlideOptions;
}
if (!isset($slide['image']) || !isset($slide['image']['src']) || empty($slide['image']['src'])) {
throw new InvalidConfigException('Slider Revolution Error: Missing slide image!');
}
if (!isset($slide['image']['options'])) {
$slide['image']['options'] = $this->defaultImageOptions;
}
$slideHtml = '';
$slideHtml .= Html::beginTag('li', $slide['options']);
// image
$slideHtml .= Html::img($slide['image']['src'], $slide['image']['options']);
// layers: div content /div
$layersHtml = '';
foreach ($slide['layers'] as $layer) {
if (!isset($layer['options'])) {
$layer['options'] = $this->defaultLayerOptions;
}
if (!isset($layer['content'])) {
$layer['content'] = $this->defaultContent;
}
$layerHtml = '';
$layerHtml .= Html::beginTag('div', $layer['options']);
$layerHtml .= $layer['content'];
$layerHtml .= Html::endTag('div');
$layersHtml .= $layerHtml;
}
$slideHtml .= $layersHtml;
$slideHtml .= Html::endTag('li');
return $slideHtml;
}
public function getSliderHtml()
{
return $this->rawSliderHtml;
}
}