forked from bitweaver/themes
/
BitThemes.php
2350 lines (2136 loc) · 74.8 KB
/
BitThemes.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 themes
*/
/**
* Set-up
*/
require_once( KERNEL_PKG_PATH.'BitCache.php' );
/**
* BitThemes
*
* @package themes
* @uses BitBase
*/
class BitThemes extends BitSingleton {
// Array that contains a full description of the current layout
var $mLayout = array();
// contains the currently active style
var $mStyle;
// an array with style information
var $mStyles = array();
// Ajax libraries needed by current Ajax framework (MochiKit libs, etc.)
var $mAjaxLibs = array();
// Auxiliary Javascript and Css Files
var $mAuxFiles = array(
'js' => array(),
'css' => array()
);
// Raw Javascript and Css Files
var $mRawFiles = array(
'js' => array(),
'css' => array()
);
// Display Mode
var $mDisplayMode;
// When all modules are loaded they are loaded here
var $mModules = array();
/**
* Initiate class
*
* @return void
*/
function __construct() {
parent::__construct();
// start up caching engine
$this->mThemeCache = new BitCache( 'themes', TRUE );
}
public static function isCacheableClass() {
return false;
}
// {{{ =================== Styles ====================
/**
* load up style related information that must be
* loaded before template rendering begins
*
* @note this is a interim method as we continue sorting
* out the optimal order of operations for rendering
* pages. there was some conflict between rendering
* module templates and loading styles, where some
* style information needs to be loaded before the templates
* are rendered, and some such as packing javascript and css
* should happen after
*
* @see BitSystem::preDisplay
*/
function preLoadStyle(){
// define style url and path
if( !defined( 'THEMES_STYLE_URL' ) ) {
define( 'THEMES_STYLE_URL', $this->getStyleUrl() );
}
if( !defined( 'THEMES_STYLE_PATH' ) ) {
define( 'THEMES_STYLE_PATH', $this->getStylePath() );
}
}
/*
* load up all style related information
* populates mStyle and mStyles
*
* @access public
* @return void
*/
function loadStyle() {
global $gBitSystem;
// load default css files
if( empty( $this->mStyles['styleSheet'] )) {
$this->mStyles['styleSheet'] = $this->getStyleCssFile( NULL, TRUE );
}
// load tpl files that need to be included
$this->loadTplFiles( "html_head_inc" );
$this->loadTplFiles( "footer_inc" );
// join javascript files that have been loaded
$this->mStyles['joined_javascript'] = $this->joinAuxFiles( 'js' );
// layout is called as the viry first, package css is around pos 300 and theme / browser are called last
// css inserted in <pkg>/html_head_inc.tpl is called before these files since these are inserted last
// $this->loadCss( $this->getLayoutCssFile(), TRUE, 1, TRUE, TRUE );
$this->loadCss( $this->getStyleCssFile(), TRUE, 998, TRUE, TRUE );
$this->loadCss( $this->getBrowserStyleCssFile(), TRUE, 999, TRUE, TRUE );
// check for customized CSS file
if( file_exists( CONFIG_PKG_PATH.'css/config.css' ) ) {
$this->loadCss( CONFIG_PKG_PATH.'css/config.css' );
}
$this->mStyles['joined_css'] = $this->joinAuxFiles( 'css' );
}
/**
* figure out the current style
*
* @param string $ pScanFile file to be looked for
* @return none
* @access public
*/
function getStyle() {
global $gBitSystem;
if( empty( $this->mStyle )) {
$this->mStyle = $gBitSystem->getConfig( 'style' );
}
return $this->mStyle;
}
/**
* figure out the current style
*
* @param string $ pScanFile file to be looked for
* @return none
* @access public
*/
function setStyle( $pStyle ) {
global $gBitSmarty;
$this->mStyle = $pStyle;
$gBitSmarty->verifyCompileDir();
}
/**
* figure out the current style
*
* @param string $ pScanFile file to be looked for
* @return none
* @access public
*/
function getStyleCssFile( $pStyle = NULL, $pUrl = FALSE ) {
global $gBitSystem;
if( empty( $pStyle )) {
$pStyle = $this->getStyle();
}
$ret = '';
if( $pUrl ) {
$base = $this->getStyleUrl();
} else {
$base = $this->getStylePath();
}
if( $gBitSystem->getConfig( 'style_variation' ) && is_readable( $this->getStylePath().'alternate/'.$gBitSystem->getConfig( 'style_variation' ).'.css' )) {
$ret = $base.'alternate/'.$gBitSystem->getConfig( 'style_variation' ).'.css';
} elseif( is_readable( $this->getStylePath().$pStyle.'.css' )) {
$ret = $base.$pStyle.'.css';
}
return $ret;
}
/**
* get browser specific css file
*
* @param none
* @return path to browser specific css file
* @access public
*/
function getBrowserStyleCssFile( $pUrl = FALSE ) {
global $gSniffer;
if( $pUrl ) {
$base = $this->getStyleUrl();
} else {
$base = $this->getStylePath();
}
$subpath = $this->getStyle().'_'.$gSniffer->property( 'browser' );
// Allow us to split by major version with a fallback for others
if( file_exists( $this->getStylePath().$subpath.$gSniffer->property( 'maj_ver' ).'.css' )) {
$ret = $base.$subpath.$gSniffer->property( 'maj_ver' ).'.css';
} elseif( file_exists( $this->getStylePath().$subpath.'.css' )) {
$ret = $base.$subpath.'.css';
}
return !empty( $ret ) ? $ret : NULL;
}
/**
* get browser specific css file
*
* @param none
* @return path to browser specific css file
* @access public
*/
function getLayoutCssFile() {
global $gBitSystem;
if( $gBitSystem->isFeatureActive( 'site_style_layout' )) {
$ret = realpath( THEMES_PKG_PATH."layouts/".$gBitSystem->getConfig( 'site_style_layout' ).".css" );
}
return !empty( $ret ) ? $ret : NULL;
}
/**
* figure out the current style URL
*
* @param string $ pScanFile file to be looked for
* @return none
* @access public
*/
function getStyleUrl( $pStyle = NULL ) {
if( empty( $pStyle )) {
$pStyle = $this->getStyle();
}
return CONFIG_PKG_URL.'themes/'.$pStyle.'/';
}
/**
* figure out the current style URL
*
* @param string $ pScanFile file to be looked for
* @return none
* @access public
*/
function getStylePath( $pStyle = NULL ) {
if( empty( $pStyle )) {
$pStyle = $this->getStyle();
}
return CONFIG_PKG_PATH.'themes/'.$pStyle.'/';
}
/**
* getStyles
*
* @param array $pDir
* @param array $pNullOption
* @param array $bIncludeCustom
* @access public
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
*/
function getStyles( $pDir = NULL, $pNullOption = NULL, $bIncludeCustom = FALSE ) {
global $gBitSystem, $gBitUser;
if( empty( $pDir )) {
$pDir = CONFIG_PKG_PATH.'themes/';
}
$ret = array();
if( !empty( $pNullOption )) {
$ret[] = '';
}
if( is_dir( $pDir )) {
$h = opendir( $pDir );
while( $file = readdir( $h )) {
if ( is_dir( $pDir."$file" ) && ( $file != '.' && $file != '..' && $file != 'CVS' && $file != 'slideshows' && $file != 'blank' )) {
$ret[] = $file;
}
}
closedir( $h );
}
if( $bIncludeCustom && $gBitSystem->getConfig( 'themes_edit_css' )) {
// Include the users custom css if they have created one
$customCSSPath = $gBitUser->getStoragePath( NULL,$gBitUser->mUserId );
$customCSSFile = $customCSSPath.'custom.css';
if (file_exists($customCSSFile)) {
$ret[] = 'custom';
}
}
if( count( $ret )) {
sort( $ret );
}
return $ret;
}
/**
* getStyleLayouts
*
* @access public
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
*/
function getStyleLayouts() {
$ret = array();
if( is_dir( THEMES_PKG_PATH.'layouts/' )) {
$h = opendir( THEMES_PKG_PATH.'layouts/' );
// collect all layouts
while( FALSE !== ( $file = readdir( $h ))) {
if ( !preg_match( "/^\./", $file )) {
$ret[substr( $file, 0, ( strrpos( $file, '.' )))][substr( $file, ( strrpos( $file, '.' ) + 1 ))] = $file;
}
}
closedir( $h );
// weed out any files that don't have a css file associated with them
foreach( $ret as $key => $layout ) {
if( empty( $layout['css'] )) {
unset( $ret[$key] );
}
}
ksort( $ret );
}
return $ret;
}
/**
* @param $pSubDirs a subdirectory to scan as well - you can pass in multiple dirs using an array
*
* @param array $pDir
* @param array $pNullOption
* @param array $pSubDirs
* @access public
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
*/
function getStylesList( $pDir = NULL, $pNullOption = NULL, $pSubDirs = NULL ) {
global $gBitSystem;
$ret = array();
if( empty( $pSubDirs )) {
$subDirs[] = array( '' );
} elseif( !is_array( $pSubDirs )) {
$subDirs[] = $pSubDirs;
} else {
$subDirs = $pSubDirs;
}
if( empty( $pDir )) {
$pDir = CONFIG_PKG_PATH.'themes/';
}
if( !empty( $pNullOption )) {
$ret[] = '';
}
// open directories
if( is_dir( $pDir )) {
$h = opendir( $pDir );
// cycle through files / dirs
while( FALSE !== ( $file = readdir( $h ))) {
if ( is_dir( $pDir.$file ) && ( $file != '.' && $file != '..' && $file != 'CVS' && $file != 'slideshows' && $file != 'blank' )) {
$ret[$file]['style'] = $file;
// check if we want to have a look in any subdirs
foreach( $subDirs as $dir ) {
if( is_dir( $infoDir = $pDir.$file.'/'.$dir.'/' )) {
$dh = opendir( $infoDir );
// cycle through files / dirs
while( FALSE !== ( $f = readdir( $dh ))) {
if( is_readable( $infoDir.$f ) && ( $f != '.' && $f != '..' && $f != 'CVS' )) {
$ret[$file][$dir][preg_replace( "/\..*/", "", $f )] = CONFIG_PKG_URL.basename( dirname( dirname( $infoDir ))).'/'.$file.'/'.$dir.'/'.$f;
if( preg_match( "/\.htm$/", $f )) {
$fh = fopen( $infoDir.$f, "r" );
$ret[$file][$dir][preg_replace( "/\.htm$/", "", $f )] = fread( $fh, filesize( $infoDir.$f ));
fclose( $fh );
}
}
}
// sort the returned items
@ksort( $ret[$file][$dir] );
closedir( $dh );
}
}
}
}
closedir( $h );
}
if( count( $ret )) {
ksort( $ret );
}
return $ret;
}
/**
* get the icon cache path
*
* @access public
* @return absolute path on where the system should store it's icons
*/
function getIconCachePath() {
global $gSniffer, $gBitSystem, $gBitLanguage;
// use bitweaver version as dir in case there has been changes since the last version
$version = $gBitSystem->getBitVersion( FALSE );
// some browsers need special treatment due to different biticon feed.
if( $gSniffer->_browser_info['browser'] == 'ie' ) {
$browser = $gSniffer->_browser_info['browser'].$gSniffer->_browser_info['maj_ver'];
} else {
$browser = 'default';
}
$cachedir = TEMP_PKG_PATH.'themes/biticon/'.$version.'/'.$gBitSystem->getConfig( 'site_icon_style', DEFAULT_ICON_STYLE ).'/'.$gBitLanguage->getLanguage().'/'.$browser.'/';
if( !is_dir( $cachedir )) {
mkdir_p( $cachedir );
}
return $cachedir;
}
// }}}
// {{{ =================== Layout ====================
/**
* load current layout into mLayout
*
* @param $pParamHash
* @return none
* @access public
*/
function loadLayout( $pParamHash = NULL ) {
global $gBitSystem;
if( empty( $this->mLayout ) || !count( $this->mLayout )){
$this->mLayout = $this->getLayout( $pParamHash );
/**
* this needs to occur after loading the layout to ensure that we don't distrub the fallback process during layout loading
* we can disable clumns using various criteria:
* <package>_hide_<area>_col
* <display_mode>_hide_<area>_col
* <package>_<display_mode>_hide_<area>_col
*/
$areas = array( 't' => 'top', 'l' => 'left', 'r' => 'right', 'b' => 'bottom' );
foreach( $areas as $layout => $area ) {
if(
$gBitSystem->isFeatureActive( "{$this->mDisplayMode}_hide_{$area}_col" ) ||
$gBitSystem->isFeatureActive( "{$gBitSystem->mActivePackage}_hide_{$area}_col" ) ||
$gBitSystem->isFeatureActive( "{$gBitSystem->mActivePackage}_{$this->mDisplayMode}_hide_{$area}_col" )
) {
unset( $this->mLayout[$layout] );
}
}
}
}
function hasColumnModules( $pColumn ) {
return count( $this->mLayout[$pColumn] );
}
function displayLayoutColumn( $pColumn ) {
if( $colHtml = $this->fetchLayoutColumn( $pColumn ) ) {
print $colHtml;
}
}
function fetchLayoutColumn( $pColumn ) {
global $gBitSmarty, $gBitSystem;
$ret = '';
if( !empty( $this->mLayout[$pColumn] ) ) {
for ($i = 0; $i < count( $this->mLayout[$pColumn] ); $i++) {
$r = &$this->mLayout[$pColumn][$i];
if( !empty( $r['visible'] )) {
try {
// @TODO MODULE UPGRADE under new module organization this is not reliable as tpls are in sub dir in modules/ change this when upgrade is complete
list( $package, $template ) = explode( '/', $r['module_rsrc'] );
// deal with custom modules
if( $package == '_custom:custom' ) {
global $gBitLanguage;
// We're gonna run our own cache mechanism for user_modules
// the cache is here to avoid calls to consumming queries,
// each module is different for each language because of the strings
$cacheDir = TEMP_PKG_PATH.'modules/cache/';
if( !is_dir( $cacheDir )) {
mkdir_p( $cacheDir );
}
$cachefile = $cacheDir.'_custom.'.$gBitLanguage->mLanguage.'.'.$template.'.tpl.cache';
if( !empty( $r["cache_time"] ) && file_exists( $cachefile ) && !(( $gBitSystem->getUTCTime() - filemtime( $cachefile )) > $r["cache_time"] )) {
$fp = fopen( $cachefile, "r" );
$data = fread( $fp, filesize( $cachefile ));
fclose( $fp );
$r["data"] = $data;
} else {
if( $moduleParams = $this->getCustomModule( $template )) {
$moduleParams = array_merge( $r, $moduleParams );
$gBitSmarty->assign_by_ref( 'moduleParams', $moduleParams );
$ret .= $gBitSmarty->fetch( 'bitpackage:themes/custom_module.tpl' );
if( !empty( $r["cache_time"] ) ) {
// write to chache file
$fp = fopen( $cachefile, "w+" );
fwrite( $fp, $data, strlen( $data ));
fclose( $fp );
}
$r["data"] = $data;
}
}
unset( $data );
} else {
$explosion = explode( '/', $r['module_rsrc'] );
$template = array_pop( $explosion );
// using $module_rows, $module_params and $module_title is deprecated. please use $moduleParams hash instead
global $module_rows, $module_params, $module_title, $gBitLanguage;
$cacheDir = TEMP_PKG_PATH.'modules/cache/';
if( !is_dir( $cacheDir )) {
mkdir_p( $cacheDir );
}
// include tpl name and module id to uniquely identify
$cachefile = $cacheDir.'_module_'.$r['module_id'].'.'.$gBitLanguage->mLanguage.'.'.$template.'.cache';
// if the time is right get the cache else get it fresh
if( !empty( $r["cache_time"] ) && file_exists( $cachefile ) && filesize( $cachefile ) && !(( $gBitSystem->getUTCTime() - filemtime( $cachefile )) > $r["cache_time"] ) ) {
$fp = fopen( $cachefile, "r" );
$data = fread( $fp, filesize( $cachefile ));
fclose( $fp );
$r["data"] = $data;
} else {
$module_params = $r['module_params']; // backwards compatability
if( !$r['module_rows'] ) {
$r['module_rows'] = 10;
}
// if there's no custom title, get one from file name
if( !$r['title'] = ( isset( $r['title'] ) ? tra( $r['title'] ) : NULL )) {
$pattern[0] = "/.*\/mod_(.*)\.tpl/";
$replace[0] = "$1";
$pattern[1] = "/_/";
$replace[1] = " ";
$r['title'] = ( !empty( $r['title'] ) ? tra( $r['title'] ) : tra( ucwords( preg_replace( $pattern, $replace, $r['module_rsrc'] ))));
}
// moduleParams are extracted in BitSmarty::getSiblingAttachments() and passed on the the module php file
$moduleParams = $r;
$gBitSmarty->assign_by_ref( 'moduleParams', $moduleParams );
// assign the custom module title
$ret .= $gBitSmarty->fetch( $r['module_rsrc'] );
if( !empty( $r["cache_time"] ) ) {
// write to chache file
$fp = fopen( $cachefile, "w+" );
fwrite( $fp, $data, strlen( $data ));
fclose( $fp );
}
$r["data"] = $data;
}
unset( $moduleParams );
}
} catch( Exception $e ) {
print( '<div class="alert alert-warning">'.$e->getMessage() ).'</div>';
}
}
}
if( !empty( $ret ) && ($pColumn == 'l' || $pColumn == 'r') ) {
$ret = '<div class="panel-group col-xs-12">'.$ret.'</div>';
}
}
return $ret;
}
/**
* get the current layout from the database, layouts are fetched in this order in this order until one is successfully loaded: 'layout', 'fallback_layout', ACTIVE_PACKGE, DEFAULT_PACKAGE"
*
* @param array $pParamHash
* @access public
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
*/
function getLayout( $pParamHash = NULL ) {
global $gCenterPieces, $gBitUser, $gBitSystem, $gBitSmarty;
$ret = array( 'l' => NULL, 'c' => NULL, 'r' => NULL );
$layouts = array();
if( !empty( $pParamHash['layout'] )) {
$layouts[] = $pParamHash['layout'];
}
if( !empty( $pParamHash['fallback_layout'] )) {
$layouts[] = $pParamHash['fallback_layout'];
}
if( defined( 'ACTIVE_PACKAGE') ) {
$layouts[] = ACTIVE_PACKAGE;
}
$layouts[] = DEFAULT_PACKAGE;
foreach( $layouts AS $l ) {
$query = "SELECT tl.*
FROM `".BIT_DB_PREFIX."themes_layouts` tl
WHERE tl.`layout`=? ORDER BY ".$this->mDb->convertSortmode( "pos_asc" );
$result = $this->mDb->query( $query, array( $l ) );
if( $result && $result->RecordCount() ) {
break;
}
}
if( !empty( $result ) && $result->RecordCount() ) {
$row = $result->fetchRow();
// Check to see if we have ACTIVE_PACKAGE modules at the top of the results
if( isset( $row['layout'] ) && ( $row['layout'] != DEFAULT_PACKAGE ) && ( ACTIVE_PACKAGE != DEFAULT_PACKAGE )) {
$skipDefaults = TRUE;
} else {
$skipDefaults = FALSE;
}
if ( !is_array( $gCenterPieces ) ){
$gCenterPieces = array();
}
while( $row ) {
if( $skipDefaults && $row['layout'] == DEFAULT_PACKAGE ) {
// we're done! we've got all the non-DEFAULT_PACKAGE modules
break;
}
if ( defined ('ROLE_MODEL') ) {
// transform roles to managable array
if( empty( $row["roles"] )) {
// default is that module is visible at all times
$row["visible"] = TRUE;
$row["module_roles"] = array();
} else {
$row['module_roles'] = $this->parseRoles( $row['roles'] );
if( $gBitUser->isAdmin() ) {
if ( $gBitSystem->isFeatureActive('site_mods_req_admn_grp') ) {
if( in_array(1, $row['module_roles']) ) {
$row['visible'] = TRUE;
}
}
else {
$row["visible"] = TRUE;
}
} else {
// Check for the right roles
foreach( $row["module_roles"] as $modRoleId ) {
if( $gBitUser->isInRole( $modRoleId )) {
$row["visible"] = TRUE;
break; // no need to continue looping
}
}
}
}
} else {
// transform groups to managable array
if( empty( $row["groups"] )) {
// default is that module is visible at all times
$row["visible"] = TRUE;
$row["module_groups"] = array();
} else {
$row['module_groups'] = $this->parseGroups( $row['groups'] );
if( $gBitUser->isAdmin() ) {
if ( $gBitSystem->isFeatureActive('site_mods_req_admn_grp') ) {
if( in_array(1, $row['module_groups']) ) {
$row['visible'] = TRUE;
}
}
else {
$row["visible"] = TRUE;
}
} else {
// Check for the right groups
foreach( $row["module_groups"] as $modGroupId ) {
if( $gBitUser->isInGroup( $modGroupId )) {
$row["visible"] = TRUE;
break; // no need to continue looping
}
}
}
}
}
if( empty( $ret[$row['layout_area']] )) {
$ret[$row['layout_area']] = array();
}
$row['module_params'] = $this->parseString( $row['params'] );
if( !empty( $pParamHash['load_config'] ) ) {
global $moduleParams;
$row['config'] = $gBitSmarty->getModuleConfig( $row['module_rsrc'] );
}
if( $row['layout_area'] == CENTER_COLUMN ) {
array_push( $gCenterPieces, $row );
}
if( !empty( $row["visible"] )) {
array_push( $ret[$row['layout_area']], $row );
}
$row = $result->fetchRow();
}
}
return $ret;
}
/**
* isModuleLoaded will check if a given modules is being used in the currently active layout
*
* @param string $pModuleResource the module resource
* @param string $pArea optionally specify the area the module should be found in
* @access public
* @return TRUE on success, FALSE on failure
*/
function isModuleLoaded( $pModuleResource, $pArea = NULL ) {
// load the layout if it hasn't been done yet
$this->loadLayout();
if( !$this->verifyArea( $pArea ) && !empty( $this->mLayout[$pArea] )) {
foreach( $this->mLayout[$pArea] as $module ) {
if( $pModuleResource == $module['module_rsrc'] ) {
return TRUE;
}
}
} else {
foreach( array_keys( $this->mLayout ) as $area ) {
if( !empty( $this->mLayout[$area] )) {
foreach( $this->mLayout[$area] as $module ) {
if( $pModuleResource == $module['module_rsrc'] ) {
return TRUE;
}
}
}
}
}
return FALSE;
}
/**
* fix postional data in database using increments of 10 to make it easy for inserting new modules
*
* @access public
* @return void
*/
function fixPositions( $pLayout = NULL ) {
$layouts = $this->getAllLayouts();
// if we only want to fix the positions of a given layout, strip down the hash
if( !empty( $pLayout ) && !empty( $layouts[$pLayout] )) {
$layouts = array( $layouts[$pLayout] );
}
foreach( $layouts as $layout ) {
foreach( $layout as $column ) {
$i = 5;
foreach( $column as $module ) {
$this->mDb->query( "UPDATE `".BIT_DB_PREFIX."themes_layouts` SET pos=? WHERE `module_id`=?", array( $i, $module['module_id'] ));
$i += 5;
}
}
}
}
/**
* get a brief summary of set layouts
*
* @access public
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
*/
function getAllLayouts() {
$layouts = array();
$modules = $this->mDb->getAll( "SELECT tl.* FROM `".BIT_DB_PREFIX."themes_layouts` tl ORDER BY ".$this->mDb->convertSortmode( "pos_asc" ));
foreach( $modules as $module ) {
if( defined ( 'ROLE_MODEL') ) {
$module['module_roles'] = $this->parseRoles( $module['roles'] );
} else {
$module['module_groups'] = $this->parseGroups( $module['groups'] );
}
$layouts[$module['layout']][$module['layout_area']][] = $module;
}
ksort( $layouts );
// Take the default/kernel layout and make sure it is the first item in hash
if( ( count( $layouts ) > 1 ) && isset( $layouts['kernel'] ) ) {
$kernel_layout = $layouts['kernel'];
unset( $layouts['kernel'] );
$layouts = array('kernel' => $kernel_layout) + $layouts;
}
return $layouts;
}
/**
* cloneLayout
*
* @param array $pFromLayout
* @param array $pToLayout
* @access public
* @return boolean TRUE
*/
function cloneLayout( $pFromLayout, $pToLayout ) {
global $gBitSystem;
if( !empty( $pFromLayout ) && !empty( $pToLayout ) ) {
// nuke existing layout
$this->mDb->query( "DELETE FROM `".BIT_DB_PREFIX."themes_layouts` WHERE `layout`=?", array( $pToLayout ));
// get requested layout
$team = defined('ROLE_MODEL') ? 'roles' : 'groups';
$layout = $this->mDb->getAll( "
SELECT `title`, `layout_area`, `module_rows`, `module_rsrc`, `params`, `cache_time`, `$team`, `pos`
FROM `".BIT_DB_PREFIX."themes_layouts` WHERE `layout`=?", array( $pFromLayout ));
foreach( $layout as $module ) {
$module['layout'] = $pToLayout;
$this->storeModule( $module );
}
}
return TRUE;
}
/**
* expungeLayout
*
* @param array $pLayout
* @access public
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
*/
function expungeLayout( $pLayout = NULL ) {
$bindVars = array();
if( !empty( $pLayout )) {
$whereSql = "WHERE `layout`=?";
$bindVars[] = $pLayout;
}
$this->mDb->query( "DELETE FROM `".BIT_DB_PREFIX."themes_layouts` $whereSql", $bindVars );
}
/**
* transform groups string to handy array
*
* @param array $pParseString either space separated list of groups or serialised array
* @access public
* @return array of groups
*/
function parseGroups( $pParseString ) {
$ret = array();
// convert groups string to hash
if( preg_match( '/[A-Za-z]/', $pParseString )) {
// old style serialized group names
if( $grps = @unserialize( $pParseString )) {
foreach( $grps as $grp ) {
global $gBitUser;
if( !( $groupId = array_search( $grp, $gBitUser->mGroups ))) {
if( $gBitUser->isAdmin() ) {
$ret[] = $gBitUser->groupExists( $grp, '*' );
}
}
if( @$this->verifyId( $groupId )) {
$ret[] = $groupId;
}
}
}
} else {
// new imploded style
$ret = explode( ' ', $pParseString );
}
return $ret;
}
/**
* transform roles string to handy array
*
* @param array $pParseString either space separated list of roles or serialised array
* @access public
* @return array of roles
*/
function parseRoles( $pParseString ) {
$ret = array();
// convert role string to hash
if( preg_match( '/[A-Za-z]/', $pParseString )) {
// old style serialized role names
if( $grps = @unserialize( $pParseString )) {
foreach( $grps as $grp ) {
global $gBitUser;
if( !( $roleId = array_search( $grp, $gBitUser->mRoles ))) {
if( $gBitUser->isAdmin() ) {
$ret[] = $gBitUser->rollExists( $grp, '*' );
}
}
if( @$this->verifyId( $roleId )) {
$ret[] = $roleId;
}
}
}
} else {
// new imploded style
$ret = explode( ' ', $pParseString );
}
return $ret;
}
// }}}
// {{{ =================== Modules ====================
/**
* Verfiy module parameters when storing a new module
*
* @param array $pHash
* @access public
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
*/
function verifyModuleParams( &$pHash ) {
// we need at least a module_id or a module_rsrc
if( empty( $pHash['module_id'] ) && empty( $pHash['module_rsrc'] )) {
$this->mErrors['module_rsrc'] = tra( 'No module id or module file given.' );
} elseif( !empty( $pHash['module_id'] )) {
$pHash['store']['module_id'] = $pHash['module_id'];
} elseif( !empty( $pHash['module_rsrc'] )) {
$pHash['store']['module_rsrc'] = $pHash['module_rsrc'];
}
// if we don't have a valid area, we'll just shove it in the left column
if( $this->verifyArea( $pHash['layout_area'] )) {
$pHash['store']['layout_area'] = $pHash['layout_area'];
} else {
$pHash['store']['layout_area'] = 'l';
}
$pHash['store']['title'] = ( !empty( $pHash['title'] ) ? $pHash['title'] : NULL );
$pHash['store']['params'] = ( !empty( $pHash['params'] ) ? $pHash['params'] : NULL );
$pHash['store']['layout'] = ( !empty( $pHash['layout'] ) ? $pHash['layout'] : DEFAULT_PACKAGE );
$pHash['store']['module_rows'] = ( @is_numeric( $pHash['module_rows'] ) ? $pHash['module_rows'] : NULL );
$pHash['store']['cache_time'] = ( @is_numeric( $pHash['cache_time'] ) ? $pHash['cache_time'] : NULL );
$pHash['store']['pos'] = ( @is_numeric( $pHash['pos'] ) ? $pHash['pos'] : 1 );
if( !empty( $pHash['roles'] ) && is_array( $pHash['roles'] )) {
$pHash['store']['roles'] = implode( ' ', $pHash['roles'] );
} elseif( !empty( $pHash['groups'] ) && is_array( $pHash['groups'] )) {
$pHash['store']['groups'] = implode( ' ', $pHash['groups'] );
} elseif (defined('ROLE_MODEL') ) {
$pHash['store']['roles'] = NULL;
} else {
$pHash['store']['groups'] = NULL;
}
if( !empty( $pHash['config'] ) ) {
$pHash['store']['params'] = '';
foreach( $pHash['config'] as $paramName=>$paramValue ) {
$pHash['store']['params'] .= $paramName.'='.urlencode( $paramValue ).'&';
}
} else {
$pHash['store']['params'] = ( !empty( $pHash['params'] ) ? $pHash['params'] : NULL );
}
return( count( $this->mErrors ) == 0 );
}
/**
* storeModule
*
* @param array $pHash
* @access public
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
*/
function storeModule( &$pHash ) {
if( $this->verifyModuleParams( $pHash )) {
$table = BIT_DB_PREFIX."themes_layouts";
if( @BitBase::verifyId( $pHash['store']['module_id'] )) {
// if we've been passed a module_id, we are updating an entry in the DB
$result = $this->mDb->associateUpdate( $table, $pHash['store'], array( 'module_id' => $pHash['store']['module_id'] ));
} else {
// no module_id yet - let's get one
$pHash['store']['module_id'] = $this->mDb->GenID( 'themes_layouts_module_id_seq' );
$result = $this->mDb->associateInsert( $table, $pHash['store'] );
}
}
return( count( $this->mErrors ) == 0 );
}
/**
* getModuleData
*
* @param array $pModuleId
* @access public
* @return module details of the requested module id
*/
function getModuleData( $pModuleId ) {
if( @BitBase::verifyId( $pModuleId )) {
$ret = $this->mDb->getRow( "SELECT tl.* FROM `".BIT_DB_PREFIX."themes_layouts` tl WHERE `module_id`=? ", array( $pModuleId ));
$ret['module_params'] = $this->parseString( $ret['params'] );
return $ret;
}
}
/**
* moduleUp
*