forked from Typesetter/Bootstrap-Carousel
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTwitterCarousel.php
executable file
·369 lines (297 loc) · 10.5 KB
/
TwitterCarousel.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
defined('is_running') or die('Not an entry point...');
class TwitterCarousel{
public static $config;
const old_content_keys = array('Carousel232','BS4Carousel');
const content_key = 'BSCarousel';
/**
* Determine if the $type matches this content key
*/
static function ContentKeyMatch($type){
global $addonFolderName;
if ( $type === self::content_key || in_array($type, self::old_content_keys) ){
return true;
}
}
/**
* Just get the current layout's Bootstrap main version quickly
* this function can be used stand-alone and does not require the other class functions
* @return boolean|integer false(not Bootstrap) | true(Bootstrap but no version number) | 2 | 3 | 4 ...
*/
static function IsBootstrap(){
global $page, $config, $gpLayouts;
// admin pages don't have a theme/layout
if( $page->pagetype === 'admin_display' ){
return false;
}
$layout_id = isset($page->TitleInfo['gpLayout']) ?
$page->TitleInfo['gpLayout'] : // page uses a custom layout
$config['gpLayout']; // page uses the default layout
// get more info from global $gpLayouts
$layout_arr = $gpLayouts[$layout_id];
// FrontEndFramework section in Addon.ini is not defined
// or the name(key) value is not 'Bootstrap'
if( !isset($layout_arr['framework']['name']) ||
strtolower($layout_arr['framework']['name']) != 'bootstrap' ){
return false;
}
// FrontEndFramework section in Addon.ini is defined and
// the name(key) value is 'Bootstrap'
// but there is no version(key)
if( empty($layout_arr['framework']['version']) ){
return true;
}
// extract the main version from the version value
// e.g. 4.3.1-b2 => 4
$pieces = explode('.', $layout_arr['framework']['version']);
$main_ver = preg_replace('/[^0-9]/', '', $pieces[0]);
return (int)$main_ver;
}
/**
* Set up carousel class names according to Bootstrap version
*/
static function InintBSSettigs(){
self::LoadPluginConfig();
$BSVer = self::IsBootstrap();
if ( $BSVer < 4 ){
$carousel_item_class = 'item';
$carousel_control_prev_class = 'carousel-control left';
$carousel_control_next_class = 'carousel-control right';
$lctrl='‹';
$rctrl='›';
} else {
$carousel_item_class = 'carousel-item';
$carousel_control_prev_class = 'carousel-control-prev';
$carousel_control_next_class = 'carousel-control-next';
if (self::$config['theme'] !== 'Bootstrap default' ) {
$lctrl='‹';
$rctrl='›';
}else{
$lctrl='<span class="carousel-control-prev-icon" aria-hidden="true"></span>';
$rctrl='<span class="carousel-control-next-icon" aria-hidden="true"></span>';
}
}
return array($carousel_item_class,$carousel_control_prev_class,$carousel_control_next_class,$lctrl,$rctrl);
}
/**
* @static
*/
static function GetHead() {
global $page, $addonRelativeCode;
$page->head_js[] = $addonRelativeCode . '/carousel.js';
}
static function SectionTypes($section_types, $generated_key = false ){
$section_types[self::content_key] = array();
$section_types[self::content_key]['label'] = 'Bootstrap4 Carousel Gallery';
return $section_types;
}
static function NewSections($links){
global $addonRelativeCode;
foreach($links as $key => $section_type_arr){
if( $section_type_arr[0] == self::content_key ){
$links[$key] = array(self::content_key, $addonRelativeCode . '/icon/section.png');
}
}
return $links;
}
static function SectionToContent($section_data){
if( !self::ContentKeyMatch($section_data['type']) ){
return $section_data;
}
//update content
if( !isset($section_data['content_version']) ){
$section_data['content'] = self::GenerateContent($section_data);
}
self::AddComponents();
return $section_data;
}
static function GenerateContent($section_data){
global $dataDir;
list($carousel_item_class,$carousel_control_prev_class,$carousel_control_next_class,$lctrl,$rctrl) = self::InintBSSettigs();
$section_data += array('images'=>array(),'height'=>'400');
$id = 'carousel_'.time();
$images = '';
$indicators = '';
$thumbs = '';
$j = 0;
foreach($section_data['images'] as $i => $img){
if( empty($img) ){
continue;
}
$caption = trim($section_data['captions'][$i]);
$class = '';
$style = '';
if( $j == 0 ){
$class = 'active';
}
if( !isset($section_data['content_version']) ){
$class .= ' gp_to_remove';
}
//images
$caption_class = '';
if( empty($caption) ){
$caption_class = 'no_caption';
}
$images .= '<div class="'.$carousel_item_class.' '.$class.'">'
.'<img src="'.common::GetDir('/include/imgs/blank.gif').'" style="background-image:url('.$img.')" alt="'.$caption.'">'
.'<div class="caption carousel-caption '.$caption_class.'">'.$caption.'</div>'
.'</div>';
//indicators
$thumb_path = common::ThumbnailPath($img);
if( !$section_data['show_indicators'] ){
$style = 'display: none;';
}
$indicators .= '<li data-target="#'.$id.'" data-slide-to="'.$j.'" class="'.$class.'" style="'.$style.'">';
$indicators .= '<a href="'.$img.'" aria-hidden="true"><img src="'.$thumb_path.'" alt="" aria-hidden="true" style="display: none"></a>';
$indicators .= '</li>';
$j++;
}
ob_start();
$class = 'gp_twitter_carousel carousel slide';
if( !$section_data['auto_start'] ){
$class .= ' start_paused';
}
if( $section_data['show_vertical'] ){
$class .= ' vertical';
}
$attr = ' data-speed="5000"';
if( isset($section_data['interval_speed']) && is_numeric($section_data['interval_speed']) ){
$attr = ' data-speed="'.$section_data['interval_speed'].'"';
}
$attr .= ' data-pause="hover"';
if (strpos($section_data['height'], '%') == false){
if( !empty($section_data['images']) ){
$style = 'height: auto;';
}else{
$style= 'height: 100%;';
}
}else{
$style = 'height: '.$section_data['height'].';';
}
echo '<div id="'.$id.'" class="'.$class.'"'.$attr.'" style="'.$style.'">';
if ($section_data['height'] == '' || $section_data['height'] == 'auto'){
if( !empty($section_data['images']) ){
echo '<div>';
echo '<img src="'.$section_data['images'][0].'" style="visibility: hidden; height: auto; width: 100%" aria-hidden="true">';
}else{
echo '<div style="padding-bottom:100%">';
}
}else{
echo '<div style="padding-bottom:'.$section_data['height'].'">';
}
// Indicators
echo '<ol class="carousel-indicators">';
echo $indicators;
echo '</ol>';
// Carousel items
echo '<div class="carousel-inner">';
echo $images;
echo '</div>';
// Carousel control
if( $section_data['show_controls'] ){
echo '<a class="'.$carousel_control_prev_class.'" data-target="#'.$id.'" href="#'.$id.'" role="button" data-slide="prev" aria-label="prev">'.$lctrl.'</a>';
echo '<a class="'.$carousel_control_next_class.'" data-target="#'.$id.'" href="#'.$id.'" role="button" data-slide="next" aria-label="next">'.$rctrl.'</a>';
}
echo '</div></div>';
return ob_get_clean();
}
static function DefaultContent($default_content,$type){
if( !self::ContentKeyMatch($type) ){
return $default_content;
}
$section = array();
$section['gp_label'] = 'Bootstrap Carousel Gallery';
$section['gp_color'] = '#8d3ee8';
$section['images'] = array(0 => '/include/imgs/default_image.jpg');
$section['captions'] = array(0 => 'Default Image');
$section['height'] = '30%';
$section['interval_speed'] = 5000;
$section['auto_start'] = false;
$section['show_vertical'] = false;
$section['show_controls'] = true;
$section['show_indicators'] = true;
$section['content'] = self::GenerateContent($section);
self::AddComponents();
return $section;
}
static function SaveSection($return, $section, $type){
if( !self::ContentKeyMatch($type) ){
return $return;
}
global $page;
$_POST += array(
'auto_start'=>'',
'show_vertical'=>'',
'show_controls'=>'',
'show_indicators'=>'',
'images'=>array()
);
$page->file_sections[$section]['images'] = $_POST['images'];
$page->file_sections[$section]['captions'] = $_POST['captions'];
$page->file_sections[$section]['height'] = $_POST['height'];
$page->file_sections[$section]['auto_start'] = ($_POST['auto_start'] == 'true');
$page->file_sections[$section]['show_vertical'] = ($_POST['show_vertical'] == 'true');
$page->file_sections[$section]['show_controls'] = ($_POST['show_controls'] == 'true');
$page->file_sections[$section]['show_indicators'] = ($_POST['show_indicators'] == 'true');
$page->file_sections[$section]['content_version'] = 2;
if( isset($_POST['interval_speed']) && is_numeric($_POST['interval_speed']) ){
$page->file_sections[$section]['interval_speed'] = $_POST['interval_speed'];
}
$page->file_sections[$section]['content'] = self::GenerateContent($page->file_sections[$section]);
return true;
}
/**
* Make sure the .js and .css is available to admins
*/
static function GenerateContent_Admin(){
global $addonFolderName, $page, $addonRelativeCode;
self::AddComponents();
}
static function AddComponents(){
global $addonFolderName, $page, $addonRelativeCode;
static $done = false;
if ( $done ) return;
self::LoadPluginConfig();
$BSVer = self::IsBootstrap();
if ( $BSVer == 2 ) {
common::LoadComponents( 'bootstrap-carousel' );
$page->css_user[] = $addonRelativeCode . '/carousel-def.css';
} elseif ( !$BSVer || ($BSVer == 3) ) {
common::LoadComponents( 'bootstrap3-carousel' );
$page->css_user[] = $addonRelativeCode . '/carousel-def.css';
$page->css_user[] = $addonRelativeCode . '/carousel.css';
} else {
common::LoadComponents( 'bootstrap4-carousel' );
if ( self::$config['theme'] !== 'Bootstrap default' ) {
$page->css_user[] = $addonRelativeCode . '/carousel-def.css';
}else{
$page->css_user[] = $addonRelativeCode . '/carousel-bs4.css';
}
}
$page->css_user[] = $addonRelativeCode . '/carousel-vert.css';
$done = true;
}
static function InlineEdit_Scripts($scripts,$type){
if( !self::ContentKeyMatch($type) ){
return $scripts;
}
global $addonRelativeCode;
$scripts[] = '/include/js/inline_edit/image_common.js';
$scripts[] = '/include/js/inline_edit/gallery_edit_202.js';
$scripts[] = $addonRelativeCode.'/gallery_options.js';
$scripts[] = '/include/js/jquery.auto_upload.js';
return $scripts;
}
public static function LoadPluginConfig(){
global $addonPathCode, $addonPathData;
$config_file = $addonPathData . '/config.php';
if( file_exists($config_file) ){
include $config_file ;
}else{
$config = array (
'theme' => 'Bootstrap default',
);
}
self::$config = $config;
}
}