-
Notifications
You must be signed in to change notification settings - Fork 4
/
equalheights.php
512 lines (437 loc) · 11.6 KB
/
equalheights.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
<?php
/**
* @package Joomla.Plugin
* @subpackage System.Equalheights
*
* @author Roberto Segura <[email protected]>
* @copyright (c) 2012 Digital Disseny, S.L. All Rights Reserved.
* @license GNU/GPL 2, http://www.gnu.org/licenses/gpl-2.0.htm
*/
defined('_JEXEC') or die;
JLoader::import('joomla.plugin.plugin');
/**
* Main plugin class
*
* @version 04/09/2012
* @package Joomla.Plugin
* @subpackage System.Equalheights
* @since 2.5
*
*/
class PlgSystemEqualheights extends JPlugin
{
private $_params = null;
// Plugin info constants
const TYPE = 'system';
const NAME = 'equalheights';
// Handy objects
private $_app = null;
private $_doc = null;
private $_jinput = null;
// Paths
private $_pathPlugin = null;
// URLs
private $_urlPlugin = null;
private $_urlJs = null;
private $_urlCss = null;
// URL parameters
private $_option = null;
private $_view = null;
private $_id = null;
// CSS & JS scripts calls
private $_cssCalls = array();
private $_jsCalls = array();
// HTML positions to inject CSS & JS
private $_htmlPositions = array(
'headtop' => array( 'pattern' => "/(<head>)/isU",
'replacement' => "$1\n\t##CONT##"),
'headbottom' => array( 'pattern' => "/(<\/head>)/isU",
'replacement' => "\n\t##CONT##\n$1"),
'bodytop' => array( 'pattern' => "/(<body)(.*)(>)/isU",
'replacement' => "$1$2$3\n\t##CONT##"),
'bodybottom' => array( 'pattern' => "/(<\/body>)/isU",
'replacement' => "\n\t##CONT##\n$1"),
'belowtitle' => array( 'pattern' => "/(<\/title>)/isU",
'replacement' => "$1\n\t##CONT##")
);
// Autogenerated with array_keys($this->_htmlPositions)
private $_htmlPositionsAvailable = array();
// Used to validate url
private $_componentsEnabled = array('*');
private $_viewsEnabled = array('*');
// Configure applications where enable plugin
private $_frontendEnabled = true;
private $_backendEnabled = true;
/**
* Constructor
*
* @param mixed &$subject Subject
*/
function __construct( &$subject )
{
parent::__construct($subject);
// Required objects
$this->_app = JFactory::getApplication();
$this->_doc = JFactory::getDocument();
$this->_jinput = $this->_app->input;
// Get url parameters
$this->_option = $this->_jinput->get('option', null);
$this->_view = $this->_jinput->get('view', null);
$this->_id = $this->_jinput->get('id', null);
// Set the HTML available positions
$this->_htmlPositionsAvailable = array_keys($this->_htmlPositions);
// Load plugin parameters
$this->_plugin = JPluginHelper::getPlugin(self::TYPE, self::NAME);
$this->_params = new JRegistry($this->_plugin->params);
// Init folder structure
$this->_initFolders();
// Load plugin language
$this->loadLanguage('plg_' . self::TYPE . '_' . self::NAME, JPATH_ADMINISTRATOR);
}
/**
* This event is triggered after pushing the document buffers into the template placeholders,
* retrieving data from the document and pushing it into the into the JResponse buffer.
* http://docs.joomla.org/Plugin/Events/System
*
* @return boolean
*/
function onAfterRender()
{
// Validate view
if (!$this->_validateUrl())
{
return true;
}
// Required objects
$app = JFactory::getApplication();
$doc = JFactory::getDocument();
// Configuration Parameters
$loadJquery = $this->_params->get('loadJquery', 0);
$injectPosition = $this->_params->get('injectPosition', 'headtop');
$selectorString = $this->_params->get('css_selectors', 0);
$onResize = $this->_params->get('on_resize', 1);
// Convert CSS selector string to array
$cssClasses = array();
if (!empty($selectorString) && $selectorString != '')
{
$classes = explode(',', $selectorString);
if (!empty($classes))
{
foreach ($classes as $class)
{
$cssClasses[] = trim($class);
}
}
}
// Only enabled in site app
if (!empty($cssClasses) && $app->isSite())
{
// Load jQuery ? jQuery is added to header to avoid non-ready errors
if ($loadJquery)
{
switch ($loadJquery)
{
// Load jQuery locally
case 1:
$jquery = $this->_urlJs . '/jquery.min.js';
break;
// Load jQuery from Google
default:
$jquery = 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js';
break;
}
// Add script to header
$this->_addJsCall($jquery, $injectPosition);
$this->_addJsCall('jQuery.noConflict();', $injectPosition, 'script');
}
// Add script to header
if (defined('JDEBUG') && JDEBUG)
{
$equalJs = JUri::root(true) . '/media/plg_system_equalheights/js/equalheights.js';
}
else
{
$equalJs = JUri::root(true) . '/media/plg_system_equalheights/js/equalheights.min.js';
}
$this->_addJsCall($equalJs, 'bodybottom');
// Equalize CSS classes
$jsArrayContent = '"' . implode('","', $cssClasses) . '"';
$script = "\n\tvar ddClasses = new Array(" . $jsArrayContent . " );";
$script .= "\njQuery(window).load(function() {";
$script .= "\n\tddEqualizeHeights();";
$script .= "\n});";
// Recheck the height on window resize?
if ($onResize)
{
$script .= "\nvar resizeTimer;";
$script .= "\njQuery(window).resize(function() {";
$script .= "\n\tclearTimeout(resizeTimer);";
$script .= "\n\tresizeTimer = setTimeout(ddEqualizeHeights, 50);";
$script .= "\n});";
}
$this->_addJsCall($script, 'bodybottom', 'script');
}
// JS load
if (!empty($this->_jsCalls))
{
$this->_loadJS();
}
// CSS load
if (!empty($this->_cssCalls))
{
$this->_loadCSS();
}
return true;
}
/**
* Add a css file declaration
*
* @param string $cssUrl url of the CSS file
* @param string $position position where we are going to load JS
*
* @author Roberto Segura - Digital Disseny, S.L.
* @version 23/04/2012
*
* @return void
*/
private function _addCssCall($cssUrl, $position = null)
{
// If position is not available we will try to load the url through $doc->addScript
if (is_null($position) || !in_array($position, $this->_htmlPositionsAvailable))
{
$position = 'addstylesheet';
$cssCall = $jsUrl;
}
else
{
$cssCall = '<link rel="stylesheet" type="text/css" href="' . $cssUrl . '" >';
}
// Initialize position
if (!isset($this->_cssCalls[$position]))
{
$this->_cssCalls[$position] = array();
}
// Insert CSS call
$this->_cssCalls[$position][] = $cssCall;
}
/**
* Add a JS script declaration
*
* @param string $jsUrl url of the JS file or script content for type != url
* @param string $position position where we are going to load JS
* @param string $type url || script
*
* @author Roberto Segura - Digital Disseny, S.L.
* @version 27/06/2012
*
* @return void
*/
private function _addJsCall($jsUrl, $position = null, $type = 'url')
{
// If position is not available we will try to load the url through $doc->addScript
if (is_null($position) || !in_array($position, $this->_htmlPositionsAvailable))
{
$position = 'addscript';
$jsCall = $jsUrl;
}
else
{
if ($type == 'url')
{
$jsCall = '<script src="' . $jsUrl . '" type="text/javascript"></script>';
}
else
{
$jsCall = '<script type="text/javascript">' . $jsUrl . '</script>';
}
}
// Initialize position
if (!isset($this->_jsCalls[$position]))
{
$this->_jsCalls[$position] = array();
}
// Insert JS call
$this->_jsCalls[$position][] = $jsCall;
}
/**
* Initialize folder structure
*
* @author Roberto Segura - Digital Disseny, S.L.
* @version 28/06/2012
*
* @return void
*/
private function _initFolders()
{
// Paths
$this->_pathPlugin = JPATH_PLUGINS . '/' . self::TYPE . '/' . self::NAME;
// URLs
$this->_urlPlugin = JURI::root(true) . "/plugins/" . self::TYPE . "/" . self::NAME;
$this->_urlJs = $this->_urlPlugin . "/js";
$this->_urlCss = $this->_urlPlugin . "/css";
}
/**
* Load / inject CSS
*
* @author Roberto Segura - Digital Disseny, S.L.
* @version 27/06/2012
*
* @return void
*/
private function _loadCSS()
{
if (!empty($this->_cssCalls))
{
$body = JResponse::getBody();
foreach ($this->_cssCalls as $position => $cssCalls)
{
if (!empty($cssCalls))
{
// If position is defined we append code (inject) to the desired position
if (in_array($position, $this->_htmlPositionsAvailable))
{
// Generate the injected code
$cssIncludes = implode("\n\t", $cssCalls);
$pattern = $this->_htmlPositions[$position]['pattern'];
$replacement = str_replace('##CONT##', $cssIncludes, $this->_htmlPositions[$position]['replacement']);
$body = preg_replace($pattern, $replacement, $body);
}
// Non-defined positions will be threated as css url to load with $doc->addStylesheet
else
{
$doc = JFactory::getDocument();
foreach ($cssCalls as $cssUrl)
{
$doc->addStyleSheet($cssUrl);
}
}
}
}
JResponse::setBody($body);
}
}
/**
* Load / inject Javascript
*
* @author Roberto Segura - Digital Disseny, S.L.
* @version 27/06/2012
*
* @return void
*/
private function _loadJS()
{
if (!empty($this->_jsCalls))
{
$body = JResponse::getBody();
foreach ($this->_jsCalls as $position => $jsCalls)
{
if (!empty($jsCalls))
{
// If position is defined we append code (inject) to the desired position
if (in_array($position, $this->_htmlPositionsAvailable))
{
// Generate the injected code
$jsIncludes = implode("\n\t", $jsCalls);
$pattern = $this->_htmlPositions[$position]['pattern'];
$replacement = str_replace('##CONT##', $jsIncludes, $this->_htmlPositions[$position]['replacement']);
$body = preg_replace($pattern, $replacement, $body);
}
// Non-defined positions will be threated as js url to load with $doc->addScript
else
{
$doc = JFactory::getDocument();
foreach ($jsCalls as $jsUrl)
{
$doc->addScript($jsUrl);
}
}
}
}
JResponse::setBody($body);
}
}
/**
* validate if the plugin is enabled for current application (frontend / backend)
*
* @author Roberto Segura - Digital Disseny, S.L.
* @version 28/06/2012
*
* @return boolean
*/
private function _validateApplication()
{
if ( ($this->_app->isSite() && $this->_frontendEnabled)
|| ($this->_app->isAdmin() && $this->_backendEnabled) )
{
return true;
}
return false;
}
/**
* Validate option in url
*
* @author Roberto Segura - Digital Disseny, S.L.
* @version 28/06/2012
*
* @return boolean
*/
private function _validateComponent()
{
if ( in_array('*', $this->_componentsEnabled) || in_array($this->_option, $this->_componentsEnabled) )
{
return true;
}
return false;
}
/**
* custom method for extra validations
*
* @author Roberto Segura - Digital Disseny, S.L.
* @version 28/06/2012
*
* @return boolean
*/
private function _validateExtra()
{
return true;
}
/**
* plugin enabled for this url?
*
* @author Roberto Segura - Digital Disseny, S.L.
* @version 28/06/2012
*
* @return boolean
*/
private function _validateUrl()
{
if ( $this->_validateComponent() && $this->_validateView())
{
if (method_exists($this, '_validateExtra'))
{
return $this->_validateExtra();
}
else
{
return true;
}
}
return false;
}
/**
* validate view parameter in url
*
* @author Roberto Segura - Digital Disseny, S.L.
* @version 28/06/2012
*
* @return boolean
*/
private function _validateView()
{
if ( in_array('*', $this->_viewsEnabled) || in_array($this->_view, $this->_viewsEnabled))
{
return true;
}
return false;
}
}