-
Notifications
You must be signed in to change notification settings - Fork 3
/
twbootstrap.php
executable file
·1009 lines (875 loc) · 24.2 KB
/
twbootstrap.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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @package Joomla.Plugin
* @subpackage System.Twbootstrap
*
* @author Roberto Segura <[email protected]>
* @copyright (c) 2012 - 2014 Digital Disseny, S.L. & Roberto Segura. All Rights Reserved.
* @license GNU/GPL 2, http://www.gnu.org/licenses/gpl-2.0.htm
* @link http://digitaldisseny.com/en/extensions/twitter-bootstrap-plugin-joomla
*/
defined('_JEXEC') or die( 'Restricted access' );
JLoader::import('joomla.plugin.plugin');
/**
* Main plugin class
*
* @package Joomla.Plugin
* @subpackage System.Twbootstrap
* @since 1.0
*
*/
class PlgSystemTwbootstrap extends JPlugin
{
private $_params = null;
// Plugin info constants
const TYPE = 'system';
const NAME = 'twbootstrap';
// Paths
private $_pathPlugin = null;
private $_pathTemplate = null;
private $_pathOverrides = null;
// URLs
private $_urlPlugin = null;
private $_urlJs = null;
private $_urlCss = null;
private $_urlOverrides = null;
private $_urlJsOverrides = null;
private $_urlCssOverrides = null;
// CSS & JS scripts calls
private $_cssCalls = array();
private $_jsCalls = array();
private $_bootstrapCssFiles = array(
// CSS Reset
'cssReset' => 'reset.less',
// Core variables and mixins
'cssVariables' => 'variables.less',
'cssMixins' => 'mixins.less',
// Grid system and page structure
'cssScafolding' => 'scaffolding.less',
'cssGrid' => 'grid.less',
'cssLayouts' => 'layouts.less',
// Grid system and page structure
'cssType' => 'type.less',
'cssCode' => 'code.less',
'cssForms' => 'forms.less',
'cssTables' => 'tables.less',
// Grid system and page structure
'cssSprites' => 'sprites.less',
'cssDropdowns' => 'dropdowns.less',
'cssWells' => 'wells.less',
'cssAnimations' => 'component-animations.less',
'cssClose' => 'close.less',
// Grid system and page structure
'cssButtons' => 'buttons.less',
'cssButtonGroups' => 'button-groups.less',
'cssAlerts' => 'alerts.less',
// Components: Nav
'cssNavs' => 'navs.less',
'cssNavbars' => 'navbar.less',
'cssBreadcrumbs' => 'breadcrumbs.less',
'cssPagination' => 'pagination.less',
'cssPager' => 'pager.less',
// Components: Popovers
'cssModals' => 'modals.less',
'cssTooltips' => 'tooltip.less',
'cssPopovers' => 'popovers.less',
// Components: Misc
'cssThumbnails' => 'thumbnails.less',
'cssMedia' => 'media.less',
'cssLabelsBadges' => 'labels-badges.less',
'cssProgressBars' => 'progress-bars.less',
'cssAccordion' => 'accordion.less',
'cssCarousel' => 'carousel.less',
'cssHeroUnit' => 'hero-unit.less',
// Utility classes
'jsTypeahead' => 'utilities.less',
// Responsive
'cssResponsive' => 'responsive-utilities.less',
'cssResp1200aMin' => 'responsive-1200px-min.less',
'cssResp768a979' => 'responsive-768px-979px.less',
'cssResp767aMax' => 'responsive-767px-max.less',
'cssRespNav' => 'responsive-navbar.less'
);
private $_bootstrapJsFiles = array(
'jsAffix' => 'bootstrap-affix.js',
'jsAlert' => 'bootstrap-alert.js',
'jsButton' => 'bootstrap-button.js',
'jsCarousel' => 'bootstrap-carousel.js',
'jsCollapse' => 'bootstrap-collapse.js',
'jsDropdown' => 'bootstrap-dropdown.js',
'jsModal' => 'bootstrap-modal.js',
'jsTooltip' => 'bootstrap-tooltip.js',
'jsPopover' => 'bootstrap-popover.js',
'jsScrollspy' => 'bootstrap-scrollspy.js',
'jsTab' => 'bootstrap-tab.js',
'jsTransition' => 'bootstrap-transition.js',
'jsTypeahead' => 'bootstrap-typeahead.js'
);
// HTML positions & associated regular expressions
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##")
);
private $_htmlPositionsAvailable = array();
// Used to validate url
private $_componentsEnabled = array('*');
private $_viewsEnabled = array('*');
// Configure applications where enable plugin
private $_frontendEnabled = true;
private $_backendEnabled = false;
/**
* Constructor
*
* @param string $subject current identifier
*/
function __construct( $subject )
{
parent::__construct($subject);
// Required objects
$this->_app = JFactory::getApplication();
$this->_doc = JFactory::getDocument();
// 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 the framework has loaded and the application initialise method has been called.
* http://docs.joomla.org/Plugin/Events/System
*
* @return boolean
*/
function onAfterInitialise()
{
// Validate view
if (!$this->_validateUrl())
{
return true;
}
// Plugin parameters
$comColumns = $this->_params->get('comColumns', 12);
$bootstrapMode = $this->_params->get('bootstrapMode', 'fluid');
// Generate row and column classes
switch ($bootstrapMode)
{
case 'fluid':
$bootstrapContainerClass = 'container-fluid';
$bootstrapRowClass = 'row-fluid';
break;
default:
$bootstrapContainerClass = 'container';
$bootstrapRowClass = 'row';
break;
}
// Define constants | check if defined to allow override
if (!defined('BOOTSTRAP_VERSION'))
{
define('BOOTSTRAP_VERSION', '2.0.4');
}
if (!defined('BOOTSTRAP_COM_COLUMNS'))
{
define('BOOTSTRAP_COM_COLUMNS', $comColumns);
}
if (!defined('BOOTSTRAP_CONTAINER_CLASS'))
{
define('BOOTSTRAP_CONTAINER_CLASS', $bootstrapContainerClass);
}
if (!defined('BOOTSTRAP_ROW_CLASS'))
{
define('BOOTSTRAP_ROW_CLASS', $bootstrapRowClass);
}
}
/**
* This event is triggered immediately before 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 onBeforeRender()
{
// Validate view
if (!$this->_validateUrl())
{
return true;
}
// Required objects
$app = JFactory::getApplication();
$doc = JFactory::getDocument();
$pageParams = $app->getParams();
// Disable any Bootstrap JS
unset($doc->_scripts[JURI::root(true) . '/media/jui/js/bootstrap.min.js']);
foreach ($doc->_scripts as $script => $value)
{
if (substr_count($script, 'bootstrap.min.js'))
{
unset($doc->_scripts[$script]);
}
}
// If we are going to load jQuery disable any default jQuery loaded
if ($this->_params->get('loadJquery', 0))
{
unset($doc->_scripts[JURI::root(true) . '/media/jui/js/jquery.min.js']);
unset($doc->_scripts[JURI::root(true) . '/media/jui/js/jquery-migrate.min.js']);
unset($doc->_scripts[JURI::root(true) . '/media/jui/js/jquery-noconflict.js']);
foreach ($doc->_scripts as $script => $value)
{
if ($script == 'jquery-1.7.2.min.js' || $script == 'jquery.min.js')
{
unset($doc->_scripts[$script]);
}
}
}
// Disable any bootstrap CSS
foreach ($doc->_styleSheets as $style => $value)
{
if (substr_count($style, 'bootstrap.min.css') || substr_count($style, 'bootstrap-noconflict.css'))
{
unset($doc->_styleSheets[$style]);
}
}
// Check if we have to disable Bootstrap for this item
$bsEnabled = $pageParams->get('twbs_enabled', $this->_params->get('twbs_defmode', 0));
if ($bsEnabled)
{
// Disable 3rd party extensions added by the user
if ($manualDisable = $this->_params->get('manualDisable', null))
{
$scripts = explode(',', $manualDisable);
foreach ($scripts as $script)
{
// Try to disable relative and full URLs
unset($doc->_scripts[$script]);
unset($doc->_scripts[JURI::root(true) . $script]);
}
}
}
return true;
}
/**
* 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();
$pageParams = $app->getParams();
// URL params
$jinput = $app->input;
$tmpl = $jinput->get('tmpl', null, 'cmd');
// Plugin parameters
$loadFrontBack = $this->_params->get('loadFrontBack', 'frontend');
$onlyHTML = $this->_params->get('onlyHTML', 1);
$disableModal = $this->_params->get('disableModal', 1);
$loadJquery = $this->_params->get('loadJquery', 0);
$injectPosition = $this->_params->get('injectPosition', 'headtop');
$updated = $this->_params->get('updated', '0000-00-00 00:00:00');
$loadBootstrap = $pageParams->get('twbs_enabled', $this->_params->get('twbs_defmode', 0));
// Check modals
$disabledTmpls = array('component', 'raw');
if ($disableModal && in_array($tmpl, $disabledTmpls))
{
return true;
}
// Check HTML only
if ($onlyHTML && $doc->getType() != 'html')
{
return true;
}
// Site modifications
if ( ($app->isSite() && ($loadFrontBack == 'frontend' || $loadFrontBack == 'both'))
|| ($app->isAdmin() && ($loadFrontBack == 'backend' || $loadFrontBack == 'both')) )
{
// Load jQuery ? jQuery is added to header to avoid non-ready errors
if ($loadJquery)
{
switch ($loadJquery)
{
// Load jQuery locally
case 1:
$this->_addJsCall($this->_urlJs . '/jquery/jquery.min.js', $injectPosition);
$this->_addJsCall($this->_urlJs . '/jquery/jquery-migrate.min.js', $injectPosition);
break;
// Load jQuery from Google
default:
$this->_addJsCall('//code.jquery.com/jquery-1.11.0.min.js', $injectPosition);
$this->_addJsCall('//code.jquery.com/jquery-migrate-1.2.1.min.js', $injectPosition);
break;
}
// Ensure jQuery is loaded in noConflict mode
$this->_addJsCall('jQuery.noConflict();', $injectPosition, 'script');
}
// Load Bootstrap ?
if ($loadBootstrap)
{
// Bootstrap CSS - loaded in header
$bootstrapCss = $this->_urlCss . '/bootstrap.min.css';
// Bootstrap responsive CSS
$bootstrapResponsiveCss = $this->_urlCss . '/bootstrap-responsive.min.css';
$activeJsFiles = $this->getBootstrapActiveJsFiles();
$activeCssFiles = $this->getBootstrapActiveCssFiles();
if (count($activeCssFiles) != count($this->_bootstrapCssFiles))
{
$sourceDir = dirname(__FILE__) . '/less';
$outputDir = dirname(__FILE__) . '/css';
$outputFilename = 'boostrap-custom.min.css';
require_once __DIR__ . '/lib/lessphp/twbs-lessc.php';
$lessc = new JoomlaLessCompiler;
$lessc->setUpdated($updated);
$lessc->setFormatter("compressed");
$sourceCss = '';
$outputCss = '';
foreach ($activeCssFiles as $file)
{
$lessc->addLessFile($sourceDir . '/' . $file);
}
try
{
$lessc->createCssFile($outputDir . '/' . $outputFilename);
$bootstrapCss = $this->_urlCss . '/' . $outputFilename;
$bootstrapResponsiveCss = null;
}
catch (exception $e)
{
echo 'LESS compile error: ' . implode('<br />', $lessc->errors);
}
}
$this->_addCssCall($bootstrapCss, $injectPosition);
if (!is_null($bootstrapResponsiveCss))
{
$this->_addCssCall($bootstrapResponsiveCss, $injectPosition);
}
// User has chosen some files (and not all to be loaded)
if ($activeJsFiles && count($activeJsFiles) != count($this->_bootstrapJsFiles))
{
require_once __DIR__ . '/lib/php-closure/my-php-closure.php';
$jsCompiler = new MyPhpClosure;
foreach ($activeJsFiles as $file)
{
$jsCompiler->add(__DIR__ . '/js/bootstrap/' . $file);
}
$jsCompiler->simpleMode();
// Advanced mode fails to compile bootstrap | $c->advancedMode()
$jsCompiler->useClosureLibrary();
$jsCompiler->setUpdated($updated);
// TODO : Change or make sure path exists and is writable.
$jsCompiler->cacheDir(__DIR__ . '/js');
ob_start();
$jsCompiler->write();
$content = ob_end_clean();
// Load the Bootstrap customized version
$bootstrapJs = $this->_urlJs . '/bootstrap-custom.min.js';
}
else
{
// Load the Bootstrap standard version
$bootstrapJs = $this->_urlJs . '/bootstrap.min.js';
}
// Bootstrap JS - loaded before body ending
$this->_addJsCall($bootstrapJs, 'bodybottom');
}
}
// CSS load
if (!empty($this->_cssCalls))
{
$this->_loadCSS();
}
// JS load
if (!empty($this->_jsCalls))
{
$this->_loadJS();
}
return true;
}
/**
* Change forms before they are shown to the user
*
* @param JForm $form JForm object
* @param array $data Data array
*
* @return boolean
*/
public function onContentPrepareForm($form, $data)
{
// Check we have a form
if (!($form instanceof JForm))
{
$this->_subject->setError('JERROR_NOT_A_FORM');
return false;
}
// Extra parameters for menu edit
if ($form->getName() == 'com_menus.item')
{
$form->loadFile($this->_pathPlugin . '/form/menuitem.xml');
$form->setFieldAttribute('twbs_enabled', 'default', $this->_params->get('twbs_defmode', 0), 'params');
}
return true;
}
/**
* Check if a folder/file path exists
*
* @param string $path Path to check
* @param boolean $file Is a file check?
*
* @return boolean
*
* @author Roberto Segura - Digital Disseny, S.L.
* @version 27/06/2012
*/
public function checkPath($path, $file = false)
{
if ($file && file_exists($path))
{
return true;
}
elseif (is_dir($path))
{
return true;
}
return false;
}
/**
* Check if a url is valid
*
* @param string $url url to check
* @param boolean $file is a file check
*
* @return boolean
*
* @author Roberto Segura - Digital Disseny, S.L.
* @version 27/06/2012
*/
public function checkUrl($url, $file = false)
{
$subpath = str_replace(JURI::root(true), '', $url);
if (empty($subpath))
{
return true;
}
else
{
$subpath = str_replace('/', DIRECTORY_SEPARATOR, $subpath);
$calculatedPath = JPATH_ROOT . DIRECTORY_SEPARATOR . $subpath;
if ($file && file_exists($calculatedPath))
{
return true;
}
elseif (is_dir($calculatedPath))
{
return true;
}
}
return false;
}
/**
* Get the list of active bootstrap JS files
*
* @return array Active Bootstrap JS files to load
*/
function getBootstrapActiveJsFiles()
{
$files = array();
if (!empty($this->_bootstrapJsFiles))
{
foreach ($this->_bootstrapJsFiles as $paramName => $jsFile)
{
if ($this->_params->get($paramName, 0))
{
$files[] = $jsFile;
}
}
}
return $files;
}
/**
* Get the list of active bootstrap CSS files
*
* @return array Active Bootstrap CSS files to load
*/
function getBootstrapActiveCssFiles()
{
$files = array();
if (!empty($this->_bootstrapCssFiles))
{
foreach ($this->_bootstrapCssFiles as $paramName => $cssFile)
{
if ($this->_params->get($paramName, 0) || $paramName == 'cssVariables' || $paramName == 'cssMixins' )
{
$files[] = $cssFile;
}
}
}
return $files;
}
/**
* Get the name of the active Template
*
* @return string template name
*
* @author Roberto Segura - Digital Disseny, S.L.
* @version 27/06/2012
*/
public function getCurrentTplName()
{
// Required objects
$app = JFactory::getApplication();
$jinput = $app->input;
$db = JFactory::getDBO();
// Default values
$menuParams = new JRegistry;
$client_id = $app->isSite() ? 0 : 1;
$itemId = $jinput->get('Itemid', 0);
$tplName = null;
// Try to load custom template if assigned
if ($itemId)
{
$sql = " SELECT ts.template " .
" FROM #__menu as m " .
" INNER JOIN #__template_styles as ts" .
" ON ts.id = m.template_style_id " .
" WHERE m.id=" . (int) $itemId . " " .
"";
$db->setQuery($sql);
$tplName = $db->loadResult();
}
// If no itemId or no custom template assigned load default template
if (!$itemId || empty($tplName))
{
$tplName = $this->getDefaultTplName($client_id);
}
return $tplName;
}
/**
* Get the default template name
*
* @param integer $client_id 0->site | 1->admin
*
* @return string
*
* @author Roberto Segura - Digital Disseny, S.L.
* @version 27/06/2012
*/
public function getDefaultTplName($client_id = 0)
{
$result = null;
$db = JFactory::getDBO();
$query = " SELECT template FROM #__template_styles " .
" WHERE client_id=" . (int) $client_id . " " .
" AND home = 1 ";
$db->setQuery($query);
try
{
$result = $db->loadResult();
}
catch (JDatabaseException $e)
{
return $e;
}
return $result;
}
/**
* Add a css file declaration
*
* @param string $cssUrl url of the CSS file
* @param string $position position where we are going to load JS
*
* @return none
*
* @author Roberto Segura - Digital Disseny, S.L.
* @version 23/04/2012
*/
private function _addCssCall($cssUrl, $position = null)
{
// Check for CSS overrides
$overrideUrl = str_replace($this->_urlCss, $this->_urlCssOverrides, $cssUrl);
if ($this->checkUrl($overrideUrl, true))
{
$cssUrl = $overrideUrl;
}
// 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 = $cssUrl;
}
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
*
* @return none
*
* @author Roberto Segura - Digital Disseny, S.L.
* @version 27/06/2012
*
*/
private function _addJsCall($jsUrl, $position = null, $type = 'url')
{
// Check for overrides
if ($type == 'url')
{
$overrideUrl = str_replace($this->_urlJs, $this->_urlJsOverrides, $jsUrl);
if ($this->checkUrl($overrideUrl, true))
{
$jsUrl = $overrideUrl;
}
}
// 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 required folder structure
*
* @return none
*
* @author Roberto Segura - Digital Disseny, S.L.
* @version 28/06/2012
*
*/
private function _initFolders()
{
// Active template
$currentTemplate = $this->getCurrentTplName();
// Paths
$this->_pathPlugin = JPATH_PLUGINS . '/' . self::TYPE . '/' . self::NAME;
$this->_pathTemplate = JPATH_THEMES . '/' . $currentTemplate;
$this->_pathOverrides = $this->_pathTemplate . '/html/plg_' . 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";
$this->_urlOverrides = JURI::root(true) . '/templates/' . $currentTemplate
. '/html/plg_' . self::TYPE . '_' . self::NAME;
$this->_urlCssOverrides = $this->_urlOverrides . '/css';
$this->_urlJsOverrides = $this->_urlOverrides . '/js';
}
/**
* Load / inject CSS
*
* @return none
*
* @author Roberto Segura - Digital Disseny, S.L.
* @version 27/06/2012
*
*/
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);
}
else
{
$doc = JFactory::getDocument();
foreach ($cssCalls as $cssUrl)
{
$doc->addStyleSheet($cssUrl);
}
}
}
}
JResponse::setBody($body);
return $body;
}
}
/**
* Load / inject Javascript
*
* @return none
*
* @author Roberto Segura - Digital Disseny, S.L.
* @version 27/06/2012
*
*/
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);
}
else
{
$doc = JFactory::getDocument();
foreach ($jsCalls as $jsUrl)
{
$doc->addScript($jsUrl);
}
}
}
}
JResponse::setBody($body);
return $body;
}
}
/**
* validate if the plugin is enabled for current application (frontend / backend)
*
* @return boolean
*
* @author Roberto Segura
* @version 14/08/2012
*
*/
private function _validateApplication()
{
if ( ($this->_app->isSite() && $this->_frontendEnabled) || ($this->_app->isAdmin() && $this->_backendEnabled) )
{
return true;
}
return false;
}
/**
* Validate option in url
*
* @return boolean
*
* @author Roberto Segura
* @version 14/08/2012
*
*/
private function _validateComponent()
{
if ( in_array('*', $this->_componentsEnabled) || in_array($this->_option, $this->_componentsEnabled) )
{
return true;
}
return false;
}
/**
* custom method for extra validations
*
* @return true
*
* @author Roberto Segura
* @version 14/08/2012
*
*/
private function _validateExtra()
{
return $this->_validateApplication();
}
/**
* plugin enabled for this url?
*
* @return boolean
*
* @author Roberto Segura
* @version 14/08/2012
*
*/
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
*
* @return boolean
*
* @author Roberto Segura
* @version 14/08/2012
*
*/
private function _validateView()