-
Notifications
You must be signed in to change notification settings - Fork 6
/
helper.php
340 lines (265 loc) · 9.22 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
<?php
/**
* @package VINAORA NIVO SLIDER
* @subpackage mod_vt_nivo_slider
* @copyright Copyright (C) 2011-2013 VINAORA. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*
* @website http://vinaora.com
* @twitter http://twitter.com/vinaora
* @facebook http://facebook.com/vinaora
* @google+ https://plus.google.com/111142324019789502653
*/
// no direct access
defined('_JEXEC') or die;
class mod_VT_Nivo_SliderHelper
{
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(1, 20);
$params->set('slices', $param);
$param = (int) $params->get('boxCols', 0);
$param = ($param) ? $param : mt_rand(1, 20);
$params->set('boxCols', $param);
$param = (int) $params->get('boxRows', 0);
$param = ($param) ? $param : mt_rand(1, 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', $captionHeight);
// Valid the Margin of Slideshow Caption
$param = trim( $params->get('captionMarginVertical') );
$param = self::validDimension($param, "");
$params->set('captionMarginVertical', $param);
$param = trim( $params->get('captionMarginHorizontal') );
$param = self::validDimension($param, "");
$params->set('captionMarginHorizontal', $param);
// Valid the Font styles
$param = $params->get('titleFontStyle');
$param = trim($param);
$params->set('titleFontStyle', $param);
$param = $params->get('descFontStyle');
$param = trim($param);
$params->set('descFontStyle', $param);
}
/**
* Add jQuery Library to <head> tag
*/
public static function addjQuery($source='local', $version='1.9.1'){
$source = strtolower(trim($source));
$version = trim($version);
switch($source){
case 'local':
JHtml::script("media/mod_vt_nivo_slider/js/jquery/$version/jquery.min.js");
break;
case 'google':
$version = ($version == 'latest') ? '1' : $version;
JHtml::script("http://ajax.googleapis.com/ajax/libs/jquery/$version/jquery.min.js");
break;
case 'jquery':
JHtml::script("http://code.jquery.com/jquery-$version.min.js");
break;
default:
return false;
}
return true;
}
/**
* Add the main stylesheet of Nivo Slider to <head> tag
*/
public static function addNivoCSS(){
JHtml::stylesheet('media/mod_vt_nivo_slider/css/nivo-slider.min.css');
}
/**
* Add Nivo Slider script to <head> tag
*/
public static function addNivoScript(){
JHtml::script('media/mod_vt_nivo_slider/js/jquery.nivo.slider.min.js');
}
/**
* 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)){
JHtml::stylesheet($css);
}
else{
JHtml::stylesheet("media/mod_vt_nivo_slider/themes/default/default.css");
}
return;
}
/**
* Get Vinaora 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);
// 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);
$title = htmlspecialchars($title, ENT_QUOTES);
$desc = self::getParam($descs, $i, $separator);
$desc = trim($desc);
$desc = htmlspecialchars($desc, ENT_QUOTES);
$link = self::getParam($links, $i, $separator);
$link = trim($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";
$slider["captions"] .= "<div id=\"nivocaption$i\" 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=\"Vinaora Nivo Slider\"{$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($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 = 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'));
$param = JPath::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 = JFolder::files($param, $filter, true, true, $exclude, $excludefilter);
foreach($param as $key=>$path){
$path = substr($path, strlen(JPATH_BASE) - strlen($path) + 1);
$path = JPath::clean( $path, "/" );
$param[$key] = rtrim(JURI::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 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 = JURI::base(false);
if (substr($path, 0, strlen($base)) == $base){
$path = substr($path, strlen($base) - strlen($path));
}
else return $path;
}
$path = JPath::clean($path);
$path = ltrim($path, DIRECTORY_SEPARATOR);
if (!is_file(JPATH_BASE . DIRECTORY_SEPARATOR . $path)) return '';
// Convert it to URL path
$path = JPath::clean(JURI::base(true)."/$path", "/");
return $path;
}
}