-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhelper.php
More file actions
executable file
·373 lines (316 loc) · 11.9 KB
/
Copy pathhelper.php
File metadata and controls
executable file
·373 lines (316 loc) · 11.9 KB
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
370
371
372
373
<?php
/**
* @author JoomShaper http://www.joomshaper.com
* @copyright Copyright (C) 2010 - 2014 JoomShaper
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2
*/
// no direct access
defined('_JEXEC') or die('Restricted access');
class mod_SPSmartSlider
{
protected $params;
protected $module_id;
protected $module_name;
protected $module_dir;
//Initiate configurations
public function __construct($params, $id)
{
jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
$this->params = $params;
$this->module_id = $id;
$this->module_name = basename(dirname(__FILE__));
$this->module_dir = dirname(__FILE__);
return $this;
}
/**
* Format and beutify slider array
*
* @param object $params
* @return array
*/
public function getParams($params)
{
$source = (isset($params->source))?$params->source:array();
$data=array();
$i=0;
foreach($source as $type)
{
$data[$i]['source'] = $type;
if( !isset($$type) ) $$type = 0;
foreach($params->$type as $item=>$value)
{
$data[$i][$item] = $value[$$type];
}
$$type++;
$i++;
}
return $data;
}
/**
* Grab slider data, format it and return display
* @return array
*/
public function generate()
{
$data = array();
$params = $this->getParams($this->params->get('sliders'));
foreach((array) $params as $index=>$class)
{
$className = 'sp'.ucfirst($class['source']).'SliderHelper';
include_once 'helpers/'.$className.'.class.php';
$$class['source'] = new $className();
$$class['source']->params = $params[$index];
if( isset($params[$index]['state']) and $params[$index]['state']!='published' ) continue;
$data[]= $$class['source']->display($this);
}
return $data;
}
public function display()
{
if( $this->params->get('module_cache')==='1' )
{
$data = $this->Cache(
'sp_smart_slider.json',
array($this,'generate'),
array(),
(int) $this->params->get('cache_time'),
array($this,'onDataError'),
true
);
} else {
$data = $this->generate();
}
return $data;
}
/**
* Get article by id
*
* @param int $id
* @return array
*/
public function getArticle($id)
{
require_once (JPATH_SITE.'/components/com_content/helpers/route.php');
$database = JFactory::getDBO();
// SQL query for slider
$query = "
SELECT
`c`.`id` AS `id`,
`c`.`catid` AS `cid`,
`c`.`title` AS `title`,
`c`.`introtext` AS `introtext`,
`c`.`fulltext` AS `content`,
`c`.`created_by_alias` AS `author`,
`c`.`created` AS `date`,
`c`.`images` AS `images`
FROM
#__content AS `c`
WHERE
`c`.`id`='{$id}'
;";
// running query
$database->setQuery($query);
$data = (array) $database->loadAssoc();
$data['title'] = stripslashes($data['title']);
// set link
$data['link'] = JRoute::_(ContentHelperRoute::getArticleRoute($data['id'], $data['cid']));
return $data;
}
/**
* String show limitation
*
* @param string $text
* @param int $limit
* @param string $type, default: no others: no, char, word
* @return string
*/
public function textLimit($text, $limit, $type='no') { //function to cut text
$text = preg_replace('/<img[^>]+\>/i', "", $text);
if ($limit=='no') {//no limit
$allowed_tags = '<b><i><a><small><h1><h2><h3><h4><h5><h6><sup><sub><em><strong><u><br>';
//$text = strip_tags( $text, $allowed_tags );
$text = $text;
} else {
if ($type=='char')
{ // character lmit
$text = JFilterOutput::cleanText($text);
$sep = (utf8_strlen($text)>$limit) ? '' : ''; // core function of joomla. link: http://api.joomla.org/elementindex_utf8.html
$text = utf8_substr($text,0,$limit) . $sep;
} else { // word limit
$text = JFilterOutput::cleanText($text);
$text = explode(' ',$text);
$sep = (count($text)>$limit) ? '' : '';
$text = implode(' ', array_slice($text,0,$limit)) . $sep;
}
}
return $text;
}
/**
* Add scripts and stylesheet at frontend from style config file
*
* @param JFactory::getDocument() $document
* @param string $style
* @return mod_SPSmartSlider
*/
public function setAssets($document, $style)
{
$xml = simplexml_load_file($this->module_dir.'/tmpl/'.$style.'/config.xml');
if( isset($xml->files->public->filename) )
{
foreach($xml->files->public->filename as $file)
{
if( $file['type']=='javascript' )
{
if( $file['source']=='external')
{
$document->addScript($file);
} else {
$document->addScript( JURI::root(true).'/modules/'.$this->module_name.'/tmpl/'.$file );
}
}
if( $file['type']=='stylesheet' ){
if( $file['source']=='external')
{
$document->addStyleSheet($file->data());
} else {
$document->addStyleSheet(JURI::root(true).'/modules/'.$this->module_name.'/tmpl/'.$file);
}
}
}
}
return $this;
}
/***
* Adding jQuery in frontend
*
* @param object $document
* @param bool $usecdn. default is false
*/
public function addJQuery($document, $usecdn=false)
{
if (JVERSION < 3) {
$scripts = (array) array_keys( $document->_scripts );
$hasjquery=false;
foreach($scripts as $script)
{
if (preg_match("/\b(jquery|jquery-latest).([0-9\.min|max]+).(.js)\b/i", $script)) {
$hasjquery = true;
}
}
if( !$hasjquery )
{
if( $usecdn ) $document->addScript( 'http://code.jquery.com/jquery-latest.min.js' );
else $document->addScript( JURI::root(true).'/modules/'.$this->module_name.'/assets/jquery.min.js' );
}
} else {
JHtml::_('jquery.framework');
}
}
/**
* Error Container array
*
* @var array
*/
private $errors = array();
/**
* Get Errors, If index is null errors stored as numeric array.
*
* @param int | string $index default is NULL
* @return mixed
*/
public function error($index=null)
{
if( !empty($this->errors) )
{
if( is_null($index) ) return $this->errors;
else
{
if( is_null($this->errors[$index]) ) return false;
else return $this->errors[$index];
}
}
else return false;
}
/**
* Set errors in error variable. If index is null errors stored as numeric array.
*
* @param mixed $msg
* @param mixed $index default is null.
*/
public function setError($msg, $index=null)
{
if( is_null($index) ) $this->errors[] = $msg;
else $this->errors[$index] = $msg;
}
private function onDataError($params)
{
if( empty($params['data']) )
{
JFile::Delete($params['file']);
$this->setError('Cann\'t get any slider data to generate slide in module "'. $this->module_name.'".');
}
}
/**
* Simple caching function
* @version 1.3
* @param string $file
* @param string | array $datafn e.g: functionname | array( object, function) ,
* @param array $datafnarg default is array e.g: array( arg1, arg2, ...) ,
* @param mixed $time default is 900 = 15 min
* @param mixed $onerror string function or array(object, method )
* @param bool $usejson default is false. use json encode for caching
* @return string
*/
private function Cache( $file, $datafn, $datafnarg=array(), $time=900, $onerror='', $usejson=false)
{
if (is_writable(JPATH_CACHE))
{
// check cache dir or create cache dir
if (!JFolder::exists(JPATH_CACHE.'/'.$this->module_name))
{
JFolder::create(JPATH_CACHE.'/'.$this->module_name.'/');
}
$cache_file = JPATH_CACHE.'/'.$this->module_name.'/'.$this->module_id.'-'.$file;
// check cache file, if not then write cache file
if ( !JFile::exists($cache_file) )
{
$data = ($usejson==true) ? json_encode(call_user_func_array($datafn, $datafnarg)) :call_user_func_array($datafn, $datafnarg);
JFile::write($cache_file, $data);
}
// if cache file expires, then write cache
elseif ( filesize($cache_file) == 0 || ((filemtime($cache_file) + (int) $time ) < time()) )
{
$data = ($usejson==true) ? json_encode(call_user_func_array($datafn, $datafnarg)) :call_user_func_array($datafn, $datafnarg);
JFile::write($cache_file, $data);
}
// read cache file
$data = ($usejson==true) ? json_decode(JFile::read($cache_file), true):JFile::read($cache_file);
$params['file'] = $cache_file;
$params['data'] = $data;
if( !empty($onerror) ) call_user_func($onerror, $params);
return $data;
} else {
return ($usejson==true) ? json_encode(call_user_func_array($datafn, $datafnarg)) :call_user_func_array($datafn, $datafnarg);
}
}
/**
* Convert numeric number to language
*
* @param int | string $number
* @return language formatted text
*/
public function Num2Lang($number, $prefix = 'SP_')
{
$number = (array) str_split($number);
$formated = '';
foreach($number as $no)
{
if (ctype_digit($no))
{
$formated.=JText::_($prefix . $no);
} else $formated.=$no;
}
return $formated;
}
}