forked from vinaora/vinaora-nivo-slider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.php
360 lines (290 loc) · 9.61 KB
/
helper.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
<?php
/**
* @package MASTERPRO NIVO SLIDER
* @subpackage mod_vt_nivo_slider
* @copyright Copyright (C) 2023 Masterpro Project. All rights reserved.
* @license GNU General Public License version 2 or later.
*/
// no direct access
defined('_JEXEC') or die;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Filesystem\Path;
use Joomla\CMS\Filesystem\Folder;
class ModVTNivoSliderHelper
{
function __construct(&$params)
{
// Valid the Dimension of Slideshow
$param = trim( $params->get('slide_width') );
$param = self::validDimension($param, 'auto');
$params->set('slide_width', $param);
$param = trim( $params->get('slide_height') );
$param = self::validDimension($param, 'auto');
$params->set('slide_height', $param);
$param = trim( $params->get('slide_bdwidth') );
$param = self::validDimension($param, '');
$params->set('slide_bdwidth', $param);
// Valid the number of Slices, Columns, Rows
$param = (int) $params->get('slices', 0);
$param = ($param) ? $param : mt_rand(5, 20);
$params->set('slices', $param);
$param = (int) $params->get('boxCols', 0);
$param = ($param) ? $param : mt_rand(5, 20);
$params->set('boxCols', $param);
$param = (int) $params->get('boxRows', 0);
$param = ($param) ? $param : mt_rand(5, 20);
$params->set('boxRows', $param);
// Valid the Dimension of Slideshow Caption
$param = trim( $params->get('captionWidth') );
$param = self::validDimension($param, 'auto');
$params->set('captionWidth', $param);
$param = trim( $params->get('captionHeight') );
$param = self::validDimension($param, 'auto');
$params->set('captionHeight', $param);
// Valid the Margin of Slideshow Caption
$param = trim( (string)$params->get('captionMarginVertical') );
$param = self::validDimension($param, '');
$params->set('captionMarginVertical', $param);
$param = trim( (string)$params->get('captionMarginHorizontal') );
$param = self::validDimension($param, '');
$params->set('captionMarginHorizontal', $param);
// Valid the Font styles
$param = $params->get('titleFontStyle');
$param = trim((string)$param);
$params->set('titleFontStyle', $param);
$param = $params->get('descFontStyle');
$param = trim((string)$param);
$params->set('descFontStyle', $param);
// Convert from 1/0 to true/false value
$param = $params->get('directionNav');
$param = ($param === '1') ? 'true' : 'false';
$params->set('directionNav', $param);
$param = $params->get('controlNav');
$param = ($param === '1') ? 'true' : 'false';
$params->set('controlNav', $param);
$param = $params->get('pauseOnHover');
$param = ($param === '1') ? 'true' : 'false';
$params->set('pauseOnHover', $param);
$param = $params->get('manualAdvance');
$param = ($param === '1') ? 'false' : 'true';
$params->set('manualAdvance', $param);
$param = $params->get('controlNavThumbs');
$param = ($param === '1') ? 'true' : 'false';
$params->set('controlNavThumbs', $param);
}
/**
* Add the main stylesheet of Nivo Slider to <head> tag
*/
public static function addNivoCSS()
{
HTMLHelper::stylesheet('media/mod_vt_nivo_slider/css/nivo-slider.min.css');
}
/**
* Add Nivo Slider script to <head> tag
*/
public static function addNivoScript()
{
HTMLHelper::script('media/mod_vt_nivo_slider/js/jquery.nivo.slider.min.js');
}
/**
* Add jQuery script to <head> tag
*/
public static function addJQuery()
{
HTMLHelper::_('jquery.framework');
}
/**
* Add file [theme].css to <head> tag
*/
public static function addThemCSS($theme='default', $layout='default')
{
// Remove prefix "demo-" if has
$demo = preg_replace("/^(_:)?demo\-/", '', $layout);
$theme = ($layout == $demo) ? $theme : $demo;
$css = "media/mod_vt_nivo_slider/themes/$theme/$theme.css";
if (file_exists(JPATH_BASE . "/$css"))
{
HTMLHelper::stylesheet($css);
}
else
{
HTMLHelper::stylesheet('media/mod_vt_nivo_slider/themes/default/default.css');
}
}
/**
* Get Masterpro Nivo Slider
*/
public static function getSlider($params, $separator = "\n")
{
$slider = array('images'=>'', 'captions'=>'');
// Get the directory which contain all images
$item_dir = $params->get('item_dir');
// Get the titles, descriptions and links of all images
$titles = $params->get('item_title');
$descs = $params->get('item_description');
$links = $params->get('item_url');
$target = $params->get('item_target');
// Get all images
$items = self::getItems($params);
// Get Module's ID
$module_id = $params->get('module_id');
// Not found images
if (empty($items) || !count($items))
{
return $slider;
}
foreach($items as $i=>$path)
{
++$i;
// Get the title, description and link of every image
$title = self::getParam($titles, $i, $separator);
$title = trim($title);
$desc = self::getParam($descs, $i, $separator);
$desc = trim($desc);
$link = self::getParam($links, $i, $separator);
$link = trim((string)$link);
$link = htmlspecialchars($link, ENT_QUOTES);
// Get the name in the full path of image
$image = strrchr($path, '/');
$data_thumb = '';
$captionid = '';
// The image has caption (title or description)
if (strlen($title) || strlen($desc))
{
$captionid = "#nivocaption{$i}_{$module_id}";
$slider['captions'] .= "<div id=\"nivocaption{$i}_{$module_id}\" class=\"nivo-html-caption\">";
// Found a title of image
if (strlen($title)) $slider['captions'] .= "<div class=\"nivo-heading\">$title</div>";
// Found a description of image
if (strlen($desc)) $slider['captions'] .= "<div class=\"nivo-description\">$desc</div>";
$slider['captions'] .= "</div>\n";
}
$captionid = ( empty($captionid) ) ? '' : " title=\"$captionid\"";
$data_thumb = ( empty($data_thumb) ) ? '' : " data-thumb=\"$data_thumb\"";
$img = "<img src=\"$path\" alt=\"Masterpro Nivo Slider 3.x\"{$captionid}{$data_thumb}/>";
// Found or not found the link of image
if (!empty($link))
{
$slider['images'] .= "<a href=\"$link\" target=\"$target\">$img</a>";
}
else
{
$slider['images'] .= $img;
}
}
$slider['images'] .= "\n";
$slider['captions'] .= "\n";
return $slider;
}
/**
* Get the first slide to start slideshow
*/
public static function getStartSlide($params)
{
$startSlide = (int) $params->get('startSlide');
$items = self::getItems($params);
// Not found any image
if (empty($items) || !count($items)) return 0;
// Found the images
$total = count($items);
$startSlide = min($startSlide, $total);
$startSlide = (!$startSlide) ? mt_rand(0, $total-1) : $startSlide-1;
return $startSlide;
}
/**
* Valid the Dimension
*/
public static function validDimension($param, $default='auto')
{
if( ($param != $default) && strlen($param) )
{
$param = preg_replace('/((\s+)?px)?$/', '', $param).'px';
}
return $param;
}
/**
* Get a Parameter in a Parameters String which are separated by a specify symbol (default: vertical bar '|').
* Example: Parameters = "value1 | value2 | value3". Return "value2" if positon = 2
*/
public static function getParam($param, $position=1, $separator='|')
{
$position = (int) $position;
// Not found the separator in string
if( strpos((string)$param, $separator) === false )
{
if ( $position == 1 ) return $param;
}
// Found the separator in string
else
{
$param = ($separator == "\n") ? str_replace(array("\r\n","\r"), "\n", $param) : $param;
$items = explode($separator, $param);
if ( ($position > 0) && ($position < count($items)+1) ) return $items[$position-1];
}
return '';
}
/**
* Get the Paths of Items
*/
public static function getItems($params)
{
$param = $params->get('item_path');
$param = isset($param) ? str_replace(array("\r\n","\r"), "\n", $param) : '';
$param = explode("\n", $param);
// Get Paths from invidual paths
foreach($param as $key=>$value)
{
$param[$key] = self::validPath($value);
}
// Remove empty elements
$param = array_filter($param);
// Get Paths from directory
if (empty($param))
{
$param = trim($params->get('item_dir'));
if(!$param) return null;
$param = Path::clean( JPATH_BASE . "/$param" );
// Not found the directory
if( !is_dir($param) ) return null;
$filter = '([^\s]+(\.(?i)(jpg|png|gif|bmp))$)';
$exclude = array('index.html', '.svn', 'CVS', '.DS_Store', '__MACOSX', '.htaccess');
$excludefilter = array();
// Get all images in the directory
$param = Folder::files($param, $filter, true, true, $exclude, $excludefilter);
foreach($param as $key=>$path)
{
$path = substr($path, strlen(JPATH_BASE) - strlen($path) + 1);
$path = Path::clean( $path, "/" );
$param[$key] = rtrim(Uri::base(true), "/"). "/$path";
}
}
// Reset keys
$param = array_values($param);
return $param;
}
/**
* Get the Valid Path of Item
*/
public static function validPath($path)
{
$path = trim($path);
// Check the file type is image or not
if( !preg_match( '/[^\s]+(\.(?i)(jpg|png|gif|bmp))$/', $path) ) return '';
// The path includes http(s) or not
if( preg_match('/^(?i)(https?):\/\//', $path) ){
$base = Uri::base(false);
if (substr($path, 0, strlen($base)) == $base){
$path = substr($path, strlen($base) - strlen($path));
}
else return $path;
}
// Check the File path is exits or not
$path = Path::clean($path);
$path = ltrim($path, DIRECTORY_SEPARATOR);
if (!is_file(JPATH_BASE . DIRECTORY_SEPARATOR . $path)) return '';
// Convert File path to URL path
$path = Path::clean(Uri::base(true)."/$path", "/");
return $path;
}
}