concrete5 /web/concrete/core/libraries/view.php

Language PHP Lines 996
MD5 Hash d18ab6d4db487b6cb7db49480de94212 Estimated Cost $14,394 (why?)
Repository https://github.com/glockops/concrete5.git View Raw File
  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
<?

defined('C5_EXECUTE') or die("Access Denied.");

/**
 * @package Core
 * @category Concrete
 * @author Andrew Embler <andrew@concrete5.org>
 * @copyright  Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
 * @license    http://www.concrete5.org/license/     MIT License
 *
 */

/**
 * A generic object that every front-end template (view) or page extends.
 * @package Core
 * @author Andrew Embler <andrew@concrete5.org>
 * @category Concrete
 * @copyright  Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
 * @license    http://www.concrete5.org/license/     MIT License
 *
 */
	class Concrete5_Library_View extends Object {
			
		/**
		 * @var string
		 */ 
		private $viewPath;
		
		/**
		 * @var string
		 */
		protected $pkgHandle;
		
		/**
		 * @var bool
		 */
		protected $disableContentInclude = false;
		
		/**
		 * controller used by this particular view
		 * @access public
	     * @var object
		*/
		public $controller;
		
		
		/** 
		 * An array of items that get loaded into a page's header
		 * @var array
		 */
		private $headerItems = array();

		/** 
		 * An array of items that get loaded into just before body close
		 * @var array
		 */
		private $footerItems = array();

		/**
		 * themePaths holds the various hard coded paths to themes
		 * @access private
	     * @var array
		*/
		private $themePaths = array();	
	
		/**
		 * @var bool
		 */
		private $areLinksDisabled = false;
		
		/**
		 * editing mode is enabled or not
		 * @access private
	     * @var boolean
		*/	
		private $isEditingEnabled = true;
		
		/**
		 * getInstance() grabs one instance of the view w/the singleton pattern
		 * @return View
		*/
		public function getInstance() {
			static $instance;
			if (!isset($instance)) {
				$instance = new View();
			}
			return $instance;
		}		
		
		
		/**
		 * This grabs the theme for a particular path, if one exists in the themePaths array 
		 * @access private
	     * @param string $path
		 * @return string $theme
		*/
		private function getThemeFromPath($path) {
			// there's probably a more efficient way to do this
			$theme = false;
			$txt = Loader::helper('text');
			foreach($this->themePaths as $lp => $layout) {
				if ($txt->fnmatch($lp, $path)) {
					$theme = $layout;
					break;
				}
			}
			return $theme;
		}
		
		/** 
		 * Returns a stylesheet found in a themes directory - but FIRST passes it through the tools CSS handler
		 * in order to make certain style attributes found inside editable
		 * @param string $stylesheet
		 */
		public function getStyleSheet($stylesheet) {
			if ($this->isPreview()) {
				return REL_DIR_FILES_TOOLS . '/css/' . DIRNAME_THEMES . '/' . $this->getThemeHandle() . '/' . $stylesheet . '?mode=preview&time=' . time();
			}
			$pt = PageTheme::getByHandle($this->getThemeHandle());
			$file = $this->getThemePath() . '/' . $stylesheet;
			$cacheFile = DIR_FILES_CACHE . '/' . DIRNAME_CSS . '/' . $this->getThemeHandle() . '/' . $stylesheet;
			$env = Environment::get();
			$themeRec = $env->getUncachedRecord(DIRNAME_THEMES . '/' . $this->getThemeHandle() . '/' . $stylesheet, $pt->getPackageHandle());
			if (file_exists($cacheFile) && $themeRec->exists()) {
				if (filemtime($cacheFile) > filemtime($themeRec->file)) {
					return REL_DIR_FILES_CACHE . '/' . DIRNAME_CSS . '/' . $this->getThemeHandle() . '/' . $stylesheet;
				}
			}
			if ($themeRec->exists()) {
				$themeFile = $themeRec->file;
				if (!file_exists(DIR_FILES_CACHE . '/' . DIRNAME_CSS)) {
					@mkdir(DIR_FILES_CACHE . '/' . DIRNAME_CSS);
				}
				if (!file_exists(DIR_FILES_CACHE . '/' . DIRNAME_CSS . '/' . $this->getThemeHandle())) {
					@mkdir(DIR_FILES_CACHE . '/' . DIRNAME_CSS . '/' . $this->getThemeHandle());
				}
				$fh = Loader::helper('file');
				$stat = filemtime($themeFile);
				if (!file_exists(dirname($cacheFile))) {
					@mkdir(dirname($cacheFile), DIRECTORY_PERMISSIONS_MODE, true);
				}
				$style = $pt->parseStyleSheet($stylesheet);
				$r = @file_put_contents($cacheFile, $style);
				if ($r) {
					return REL_DIR_FILES_CACHE . '/' . DIRNAME_CSS . '/' . $this->getThemeHandle() . '/' . $stylesheet;
				} else {
					return $this->getThemePath() . '/' . $stylesheet;
				}
			}
		}

		/** 
		 * Function responsible for adding header items within the context of a view.
		 * @access private
		 */

		public function addHeaderItem($item, $namespace = 'VIEW') {
			if ($this->resolveItemConflicts($item)) {
				$this->headerItems[$namespace][] = $item;
			}
		}
		
		/** 
		 * Function responsible for adding footer items within the context of a view.
		 * @access private
		 */
		public function addFooterItem($item, $namespace = 'VIEW') {
			if ($this->resolveItemConflicts($item)) {
				$this->footerItems[$namespace][] = $item;
			}
		}
		
		/**
		 * Internal helper function for addHeaderItem() and addFooterItem().
		 * Looks through header and footer items for anything of the same type
		 * and having the same "unique handle" as the given item.
		 *
		 * HOW TO USE THIS FUNCTION:
		 * When calling this function, just pass the first $item argument
		 *  (the second optional argument is only for our own recursive use).
		 * If we return FALSE, that means the given item should NOT be added to headerItems/footerItems.
		 * If we return TRUE, then go ahead and add the item to headerItems/footerItems.
		 *
		 * NOTE: THIS FUNCTION HAS POTENTIAL SIDE-EFFECTS (IN ADDITION TO RETURN VALUE)...
		 * ~If no duplicate is found, we return TRUE (with no side-effects).
		 * ~If a duplicate is found and the given item has a HIGHER version than the found item,
		 *  we return TRUE **AND** we remove the found duplicate from headerItems or footerItems!!
		 * ~If a duplicate is found and the given item does NOT have a higher version than
		 *  the found item, we return FALSE (with no side-effects).
		 */
		private function resolveItemConflicts($checkItem, &$againstItems = null) {
			
			//Only check items that have "unique handles"
			if (empty($checkItem->handle)) {
				return true;
			}
			
			//Recursively check header items AND footer items
			if (is_null($againstItems)) {
				return ($this->resolveItemConflicts($checkItem, $this->headerItems) && $this->resolveItemConflicts($checkItem, $this->footerItems));
			}
			
			//Loop through all items and check for duplicates
			foreach ($againstItems as $itemNamespace => $namespaceItems) {
				foreach ($namespaceItems as $itemKey => $againstItem) {
					//Check the "unique handles"
					if (!empty($againstItem->handle) && (strtolower($checkItem->handle['handle']) == strtolower($againstItem->handle['handle']))) {
						//Check the item types (so js and css items can have the same handle without conflicting)
						//Note that we consider both the JavaScript and InlineScript items to be the same "type".
						$checkClass = get_class($checkItem);
						$againstClass = get_class($againstItem);
						if (($checkClass == $againstClass) || (!array_diff(array($checkClass, $againstClass), array('JavaScriptOutputObject', 'InlineScriptOutputObject')))) {
							//Does the given item have a higher version than the existing found item?
							if (version_compare($checkItem->handle['version'], $againstItem->handle['version'], '>')) {
								//Yes (new item is higher) so remove old item
								// and return true to indicate that the new item should be added.
								unset($againstItems[$itemNamespace][$itemKey]); // bug note: if we didn't return in the next line, this would cause problems the next time the loop iterated!
								return true;
							} else {
								//No (new item is not higher) so leave old item where it is
								// and return false to indicate that the new item should *not* be added.
								return false;
							}
						}
					}
				}
			}
			
			//No duplicates found, so return true to indicate that it's okay to add the item.
			return true;
		}
		
		/**
		 * returns an array of string header items, typically inserted into the html <head> of a page through the header_required element
		 * @return array
		 */
		public function getHeaderItems() {
			//Combine items from all namespaces into one list
			$a1 = (is_array($this->headerItems['CORE'])) ? $this->headerItems['CORE'] : array();
			$a2 = (is_array($this->headerItems['VIEW'])) ? $this->headerItems['VIEW'] : array();
			$a3 = (is_array($this->headerItems['CONTROLLER'])) ? $this->headerItems['CONTROLLER'] : array();
			
			$items = array_merge($a1, $a2, $a3);
			
			//Remove exact string duplicates (items whose string representations are equal)
			if (version_compare(PHP_VERSION, '5.2.9', '<')) {
				$items = array_unique($items);
			} else {
				// stupid PHP (see http://php.net/array_unique#refsect1-function.array-unique-changelog )
				$items = array_unique($items, SORT_STRING);
			}
			return $items;
		}
		
		/**
		 * returns an array of string footer items, typically inserted into the html before the close of the </body> tag of a page through the footer_required element
		 * @return array
		 */
		public function getFooterItems() {
			//Combine items from all namespaces into one list
			$a1 = (is_array($this->footerItems['CORE'])) ? $this->footerItems['CORE'] : array();
			$a2 = (is_array($this->footerItems['VIEW'])) ? $this->footerItems['VIEW'] : array();
			$a3 = (is_array($this->footerItems['CONTROLLER'])) ? $this->footerItems['CONTROLLER'] : array();
			$a4 = (is_array($this->footerItems['SCRIPT'])) ? $this->footerItems['SCRIPT'] : array();
			
			$items = array_merge($a1, $a2, $a3, $a4);
			
			//Remove exact string duplicates (items whose string representations are equal)
			if (version_compare(PHP_VERSION, '5.2.9', '<')) {
				$items = array_unique($items);
			} else {
				// stupid PHP (see http://php.net/array_unique#refsect1-function.array-unique-changelog )
				$items = array_unique($items, SORT_STRING);
			}
			
			//Also remove items having exact string duplicates in the header
			$headerItems = $this->getHeaderItems();
			$retItems = array();
			foreach($items as $it) {
				if (!in_array($it, $headerItems)) {
					$retItems[] = $it;
				}
			}
			
			return $retItems;
		}
		
		/** 
		 * Function responsible for outputting header items
		 * @access private
		 */
		public function outputHeaderItems() {
			$items = $this->getHeaderItems();
			
			foreach($items as $hi) {
				print $hi; // caled on two seperate lines because of pre php 5.2 __toString issues
				print "\n";
			}			
		}
		
		/** 
		 * Function responsible for outputting footer items
		 * @access private
		 */
		public function outputFooterItems() {
			$items = $this->getFooterItems();
			
			foreach($items as $hi) {
				print $hi; // caled on two seperate lines because of pre php 5.2 __toString issues
				print "\n";
			}
		}

		/**
		 * @param string
		 * @return mixed
		 */
		public function field($fieldName) {
			return $this->controller->field($fieldName);
		}
		
		
		/** 
		 * @access private
		 * @return void
		 */
		public function enablePreview() {
			$this->isPreview = true;
		}
		
		/** 
		 * @access private
		 * @return bool
		 */
		public function isPreview() {
			return $this->isPreview;
		}
		
		/** 
		 * @access private
		 * @return void
		 */
		public function disableLinks() {
			$this->areLinksDisabled = true;
		}
		
		/** 
		 * @access private
		 * @return void
		 */
		public function enableLinks() {
			$this->areLinksDisabled = false;
		}
		
		/** 
		 * @access private
		 * @return bool
		 */
		public function areLinksDisabled() {
			return $this->areLinksDisabled;
		}
		
		/** 
		 * Returns the path used to access this view
		 * @return string
		 */
		private function getViewPath() {
			return $this->viewPath;
		}
		
		/** 
		 * Returns the handle of the currently active theme
		 * @return string
		 */
		public function getThemeHandle() { return $this->ptHandle;}
		
		/**
		 * gets the theme include file for this particular view		
		 * @access public
		 * @return string $theme
		*/
		public function getTheme() { return $this->theme;}
	
	
		/**
		 * gets the relative theme path for use in templates
		 * @access public
		 * @return string $themePath
		*/
		public function getThemePath() { return $this->themePath; }


		/**
		 * set directory of current theme for use when loading an element
		 * @access public
		 * @param string $path
		*/
		public function setThemeDirectory($path) { $this->themeDir=$path; }

		/**
		 * get directory of current theme for use when loading an element
		 * @access public
		 * @return string $themeDir
		*/
		public function getThemeDirectory() {return $this->themeDir;}
		
	
		/**
		 * used by the theme_paths and site_theme_paths files in config/ to hard coded certain paths to various themes
		 * @access public
		 * @param $path string
		 * @param $theme object, if null site theme is default
		 * @return void
		*/
		public function setThemeByPath($path, $theme = NULL) {
			if ($theme != VIEW_CORE_THEME && $theme != 'dashboard') { // this is a hack until we figure this code out.
				if (is_string($theme)) {
					$pageTheme = PageTheme::getByHandle($theme);
					if(is_object($pageTheme) && $pageTheme->getThemeHandle() == $theme) { // is it the theme that's been requested?
						$theme = $pageTheme;
					}
				}
			}
			$this->themePaths[$path] = $theme;
		}
		
		/**
		 * Returns the value of the item in the POST array.
		 * @access public
		 * @param $key
		 * @return void
		*/
		public function post($key) {
			return $this->controller->post($key);
		}

		
		/**
		 * gets the collection object for the current view
		 * @access public
		 * @return Collection Object $c
		*/
		public function getCollectionObject() {
			return $this->c;
		}
		
		/**
		 * sets the collection object for the current view
		 * @access public
		 * @return void
		*/
		public function setCollectionObject($c) {
			$this->c = $c;
		}


		/**
		 * Includes file from the current theme path. Similar to php's include().
		 * Files included with this function will have all variables set using $this->controller->set() in their local scope,
		 * As well as access to all that controller's helper objects.
		 * @access public
		 * @param string $file
		 * @param array $args
		 * @return void
		*/
		public function inc($file, $args = array()) {
			extract($args);
			if (isset($this->c)) {
				$c = $this->c;
			}
			extract($this->controller->getSets());
			extract($this->controller->getHelperObjects());
			$env = Environment::get();
			include($env->getPath(DIRNAME_THEMES . '/' . $this->getThemeHandle() . '/' . $file, $this->pkgHandle));
		}

	
		/**
		 * editing is enabled true | false
		 * @access private
		 * @return boolean
		*/		
		public function editingEnabled() {
			return $this->isEditingEnabled;
		}
		
	
		/**
		 * set's editing to disabled
		 * @access private
		 * @return void
		*/
		public function disableEditing() {
			$this->isEditingEnabled = false;
		}


		/**
		 * sets editing to enabled
		 * @access private
		 * @return void
		*/
		public function enableEditing() {
			$this->isEditingEnabled = true;
		}
		
	
	
	
		/**
		 * This is rarely used. We want to render another view
		 * but keep the current controller. Views should probably not
		 * auto-grab the controller anyway but whatever
		 * @access private
		 * @param object $cnt
		 * @return void
		*/
		public function setController($cnt) {
			$this->controller = $cnt;
		}


		/**
		 * checks the current view to see if you're in that page's "section" (top level)
		 * (with one exception: passing in the home page url ('' or '/') will always return false)
		 * @access public
		 * @param string $url
		 * @return boolean | void
		*/	
		public function section($url) {
			$cPath = Page::getCurrentPage()->getCollectionPath();
			if (!empty($cPath)) {
				$url = '/' . trim($url, '/');
				if (strpos($cPath, $url) !== false && strpos($cPath, $url) == 0) {
					return true;
				}
			}
		}
		
		
		/**
		 * url is a utility function that is used inside a view to setup urls w/tasks and parameters		
		 * @access public
		 * @param string $action
		 * @param string $task
		 * @return string $url
		*/	
		public static function url($action, $task = null) {
			$dispatcher = '';
			if ((!defined('URL_REWRITING_ALL')) || (!URL_REWRITING_ALL)) {
				$dispatcher = '/' . DISPATCHER_FILENAME;
			}
			
			$action = trim($action, '/');
			if ($action == '') {
				return DIR_REL . '/';
			}
			
			// if a query string appears in this variable, then we just pass it through as is
			if (strpos($action, '?') > -1) {
				return DIR_REL . $dispatcher. '/' . $action;
			} else {
				$_action = DIR_REL . $dispatcher. '/' . $action . '/';
			}
			
			if ($task != null) {
				if (ENABLE_LEGACY_CONTROLLER_URLS) {
					$_action .= '-/' . $task;
				} else {
					$_action .= $task;			
				}
				$args = func_get_args();
				if (count($args) > 2) {
					for ($i = 2; $i < count($args); $i++){
						$_action .= '/' . $args[$i];
					}
				}
				
				if (strpos($_action, '?') === false) {
					$_action .= '/';
				}
			}
			
			return $_action;
		}

		public function checkMobileView() {
			if(isset($_COOKIE['ccmDisableMobileView']) && $_COOKIE['ccmDisableMobileView'] == true) {
				define('MOBILE_THEME_IS_ACTIVE', false);
				return false; // break out if we've said we don't want the mobile theme
			}
			
			$page = Page::getCurrentPage();
			if($page instanceof Page && $page->isAdminArea()) {
				define('MOBILE_THEME_IS_ACTIVE', false);
				return false; // no mobile theme for the dashboard
			}
			
			Loader::library('3rdparty/mobile_detect');
			$md = new Mobile_Detect();
			if ($md->isMobile()) {
				$themeId = Config::get('MOBILE_THEME_ID');
				if ($themeId > 0) {
					$mobileTheme = PageTheme::getByID($themeId);
					if($mobileTheme instanceof PageTheme) {
						define('MOBILE_THEME_IS_ACTIVE',true);
						// we have to grab the instance of the view
						// since on_page_view doesn't give it to us
						$this->setTheme($mobileTheme);
					}
				}
			}
			
			if (!defined('MOBILE_THEME_IS_ACTIVE')) {
				define('MOBILE_THEME_IS_ACTIVE', false);
			}
		}
		
		/**
		 * A shortcut to posting back to the current page with a task and optional parameters. Only works in the context of 
		 * @param string $action
		 * @param string $task
		 * @return string $url
		 */
		public function action($action, $task = null) {
			$a = func_get_args();
			array_unshift($a, $this->viewPath);
			$ret = call_user_func_array(array($this, 'url'), $a);
			return $ret;
		}

		/**
		 * render's a fata error using the built-in view. This is currently only
		 * used when the database connection fails
		 * @access public
		 * @param string $title
		 * @param string $error
		 * @return void
		*/	
		public function renderError($title, $error, $errorObj = null) {
			$innerContent = $error;
			$titleContent = $title; 
			header('HTTP/1.1 500 Internal Server Error');
			if (!isset($this) || (!$this)) {
				$v = new View();
				$v->setThemeForView(DIRNAME_THEMES_CORE, FILENAME_THEMES_ERROR . '.php', true);
				include($v->getTheme());	
				exit;
			}
			if (!isset($this->theme) || (!$this->theme) || (!file_exists($this->theme))) {
				$this->setThemeForView(DIRNAME_THEMES_CORE, FILENAME_THEMES_ERROR . '.php', true);
				include($this->theme);	
				exit;			
			} else {
				Loader::element('error_fatal', array('innerContent' => $innerContent, 
					'titleContent' => $titleContent));
			}
		}
		
		/**
		 * sets the current theme
		 * @access public
		 * @param string $theme
		 * @return void
		*/	
		public function setTheme($theme) {
			$this->themeOverride = $theme;
		}
		
		/**
		 * set theme takes either a text-based theme ("concrete" or "dashboard" or something)
		 * or a PageTheme object and sets information in the view about that theme. This is called internally
		 * and is always passed the correct item based on context
		 * 
		 * @access protected
		 * @param PageTheme object $pl
		 * @param string $filename
		 * @param boolean $wrapTemplateInTheme
		 * @return void
		*/	
		protected function setThemeForView($pl, $filename, $wrapTemplateInTheme = false) {
			// wrapTemplateInTheme gets set to true if we're passing the filename of a single page or page type file through 
			$pkgID = 0;
			$env = Environment::get();
			if ($pl instanceof PageTheme) {
				$this->ptHandle = $pl->getThemeHandle();
				if ($pl->getPackageID() > 0) {
					$pkgID = $pl->getPackageID();
					$this->pkgHandle = $pl->getPackageHandle();
				}
			
				$rec = $env->getRecord(DIRNAME_THEMES . '/' . $pl->getThemeHandle() . '/' . $filename, $this->pkgHandle);
				if (!$rec->exists()) {
					if ($wrapTemplateInTheme) {
						$theme = $env->getPath(DIRNAME_THEMES . '/' . $pl->getThemeHandle() . '/' . FILENAME_THEMES_VIEW, $this->pkgHandle);
					} else {
						$theme = $env->getPath(DIRNAME_THEMES . '/' . $pl->getThemeHandle() . '/' . FILENAME_THEMES_DEFAULT, $this->pkgHandle);
					}
				} else {
					$theme = $rec->file;
					$this->disableContentInclude = true;
				}
				
				$themeDir = str_replace('/' . FILENAME_THEMES_DEFAULT, '', $env->getPath(DIRNAME_THEMES . '/' . $pl->getThemeHandle() . '/' . FILENAME_THEMES_DEFAULT, $this->pkgHandle));
				$themePath = str_replace('/' . FILENAME_THEMES_DEFAULT, '', $env->getURL(DIRNAME_THEMES . '/' . $pl->getThemeHandle() . '/' . FILENAME_THEMES_DEFAULT, $this->pkgHandle));
			} else {
				$this->ptHandle = $pl;
				if (file_exists(DIR_FILES_THEMES . '/' . $pl . '/' . $filename)) {
					$themePath = DIR_REL . '/' . DIRNAME_THEMES . '/' . $pl;
					$theme = DIR_FILES_THEMES . "/" . $pl . '/' . $filename;
					$themeDir = DIR_FILES_THEMES . "/" . $pl;
				} else if (file_exists(DIR_FILES_THEMES . '/' . $pl . '/' . FILENAME_THEMES_VIEW)) {
					$themePath = DIR_REL . '/' . DIRNAME_THEMES . '/' . $pl;
					$theme = DIR_FILES_THEMES . "/" . $pl . '/' . FILENAME_THEMES_VIEW;
					$themeDir = DIR_FILES_THEMES . "/" . $pl;
				} else if (file_exists(DIR_FILES_THEMES . '/' . DIRNAME_THEMES_CORE . '/' . $pl . '.php')) {
					$theme = DIR_FILES_THEMES . '/' . DIRNAME_THEMES_CORE . "/" . $pl . '.php';
					$themeDir = DIR_FILES_THEMES . '/' . DIRNAME_THEMES_CORE;
				} else if (file_exists(DIR_FILES_THEMES_CORE . "/" . $pl . '/' . $filename)) {
					$themePath = ASSETS_URL . '/' . DIRNAME_THEMES . '/' . DIRNAME_THEMES_CORE . '/' . $pl;
					$theme = DIR_FILES_THEMES_CORE . "/" . $pl . '/' . $filename;
					$themeDir = DIR_FILES_THEMES_CORE . "/" . $pl;
				} else if (file_exists(DIR_FILES_THEMES_CORE . "/" . $pl . '/' . FILENAME_THEMES_VIEW)) {
					$themePath = ASSETS_URL . '/' . DIRNAME_THEMES . '/' . DIRNAME_THEMES_CORE . '/' . $pl;
					$theme = DIR_FILES_THEMES_CORE . "/" . $pl . '/' . FILENAME_THEMES_VIEW;
					$themeDir = DIR_FILES_THEMES_CORE . "/" . $pl;
				} else if (file_exists(DIR_FILES_THEMES_CORE_ADMIN . "/" . $pl . '.php')) {
					$theme = DIR_FILES_THEMES_CORE_ADMIN . "/" . $pl . '.php';
					$themeDir = DIR_FILES_THEMES_CORE_ADMIN;
				}
			}
			
			$this->theme = $theme;
			$this->themePath = $themePath;
			$this->themeDir = $themeDir;
			$this->themePkgID = $pkgID;
		}
		public function escape($text){
			Loader::helper('text');
			return TextHelper::sanitize($text);
		}
		/**
		 * render takes one argument - the item being rendered - and it can either be a path or a page object
		 * @access public
		 * @param string $view
		 * @param array $args
		 * @return void
		*/	
		public function render($view, $args = null) { 
		
			if (is_array($args)) {
				extract($args);
			}

			// strip off a slash if there is one at the end
			if (is_string($view)) {
				if (substr($view, strlen($view) - 1) == '/') {
					$view = substr($view, 0, strlen($view) - 1);
				}
			}
			
			$dsh = Loader::helper('concrete/dashboard');

			$wrapTemplateInTheme = false;
			$this->checkMobileView();
			if (defined('DB_DATABASE') && ($view !== '/upgrade')) {
				Events::fire('on_start', $this);
			}
			
			// Extract controller information from the view, and put it in the current context
			if (!isset($this->controller)) {
				$this->controller = Loader::controller($view);
				$this->controller->setupAndRun();
			}

			if ($this->controller->getRenderOverride() != '') {
			   $view = $this->controller->getRenderOverride();
			}
			
			// Determine which inner item to load, load it, and stick it in $innerContent
			$content = false;

			ob_start();			
			if ($view instanceof Page) {

				$_pageBlocks = $view->getBlocks();

				if (!$dsh->inDashboard()) {
					$_pageBlocksGlobal = $view->getGlobalBlocks();
					$_pageBlocks = array_merge($_pageBlocks, $_pageBlocksGlobal);
				}

				// do we have any custom menu plugins?
				$cp = new Permissions($view);
				if ($cp->canViewToolbar()) { 
					$ih = Loader::helper('concrete/interface/menu');
					$_interfaceItems = $ih->getPageHeaderMenuItems();
					foreach($_interfaceItems as $_im) {
						$_controller = $_im->getController();
						$_controller->outputAutoHeaderItems();
					}
					unset($_interfaceItems);
					unset($_im);
					unset($_controller);
				}
				unset($_interfaceItems);
				unset($_im);
				unset($_controller);
				
				
				// now, we output all the custom style records for the design tab in blocks/areas on the page
				$c = $this->getCollectionObject();
				$view->outputCustomStyleHeaderItems(); 	
				
				$viewPath = $view->getCollectionPath();
				$this->viewPath = $viewPath;
				
				$cFilename = $view->getCollectionFilename();
				$ctHandle = $view->getCollectionTypeHandle();
				$editMode = $view->isEditMode();
				$c = $view;
				$this->c = $c;
				
				$env = Environment::get();
				// $view is a page. It can either be a SinglePage or just a Page, but we're not sure at this point, unfortunately
				if ($view->getCollectionTypeID() == 0 && $cFilename) {
					$wrapTemplateInTheme = true;
					$cFilename = trim($cFilename, '/');
					$content = $env->getPath(DIRNAME_PAGES . '/' . $cFilename, $view->getPackageHandle());
					$themeFilename = $c->getCollectionHandle() . '.php';						
				} else {
					$rec = $env->getRecord(DIRNAME_PAGE_TYPES . '/' . $ctHandle . '.php', $view->getPackageHandle());
					if ($rec->exists()) {
						$wrapTemplateInTheme = true;
						$content = $rec->file;
					}
					$themeFilename = $ctHandle . '.php';
				}
				
				
			} else if (is_string($view)) {
				
				// if we're passing a view but our render override is not null, that means that we're passing 
				// a new view from within a controller. If that's the case, then we DON'T override the viewPath, we want to keep it
				
				// In order to enable editable 404 pages, other editable pages that we render without actually visiting
				if (defined('DB_DATABASE') && $view == '/page_not_found') {
					$pp = Page::getByPath($view);
					if (!$pp->isError()) {
						$this->c = $pp;
					}
				}
				
				$viewPath = $view;
				if ($this->controller->getRenderOverride() != '' && $this->getCollectionObject() != null) {
					// we are INSIDE a collection renderring a view. Which means we want to keep the viewPath that of the collection
					$this->viewPath = $this->getCollectionObject()->getCollectionPath();
				}
				
				// we're just passing something like "/login" or whatever. This will typically just be 
				// internal Concrete stuff, but we also prepare for potentially having something in DIR_FILES_CONTENT (ie: the webroot)
				if (file_exists(DIR_FILES_CONTENT . "/{$view}/" . FILENAME_COLLECTION_VIEW)) {
					$content = DIR_FILES_CONTENT . "/{$view}/" . FILENAME_COLLECTION_VIEW;
				} else if (file_exists(DIR_FILES_CONTENT . "/{$view}.php")) {
					$content = DIR_FILES_CONTENT . "/{$view}.php";
				} else if (file_exists(DIR_FILES_CONTENT_REQUIRED . "/{$view}/" . FILENAME_COLLECTION_VIEW)) {
					$content = DIR_FILES_CONTENT_REQUIRED . "/{$view}/" . FILENAME_COLLECTION_VIEW;
				} else if (file_exists(DIR_FILES_CONTENT_REQUIRED . "/{$view}.php")) {
					$content = DIR_FILES_CONTENT_REQUIRED . "/{$view}.php";
				} else if ($this->getCollectionObject() != null && $this->getCollectionObject()->isGeneratedCollection() && $this->getCollectionObject()->getPackageID() > 0) {
					//This is a single_page associated with a package, so check the package views as well
					$pagePkgPath = Package::getByID($this->getCollectionObject()->getPackageID())->getPackagePath();
					if (file_exists($pagePkgPath . "/single_pages/{$view}/" . FILENAME_COLLECTION_VIEW)) {
						$content = $pagePkgPath . "/single_pages/{$view}/" . FILENAME_COLLECTION_VIEW;
					} else if (file_exists($pagePkgPath . "/single_pages/{$view}.php")) {
						$content = $pagePkgPath . "/single_pages/{$view}.php";
					}
				}
				$wrapTemplateInTheme = true;
				$themeFilename = $view . '.php';
			}
			
			
			if (is_object($this->c)) {
				$c = $this->c;
				if (defined('DB_DATABASE') && ($view == '/page_not_found' || $view == '/login')) {
					$view = $c;
					$req = Request::get();
					$req->setCurrentPage($c);
					$_pageBlocks = $view->getBlocks();
					$_pageBlocksGlobal = $view->getGlobalBlocks();
					$_pageBlocks = array_merge($_pageBlocks, $_pageBlocksGlobal);
				}
			}
			
			if (is_array($_pageBlocks)) {
				foreach($_pageBlocks as $b1) {
					$b1p = new Permissions($b1);
					if ($b1p->canRead()) { 
						$btc = $b1->getInstance();
						// now we inject any custom template CSS and JavaScript into the header
						if('Controller' != get_class($btc)){
							$btc->outputAutoHeaderItems();
						}
						$btc->runTask('on_page_view', array($view));
					}
				}
			}			
			
			// Determine which outer item/theme to load
			// obtain theme information for this collection
			if (isset($this->themeOverride)) {
				$theme = $this->themeOverride;
			} else if ($this->controller->theme != false) {
				$theme = $this->controller->theme;
			} else if (($tmpTheme = $this->getThemeFromPath($viewPath)) != false) {
				$theme = $tmpTheme;
			} else if (is_object($this->c) && ($tmpTheme = $this->c->getCollectionThemeObject()) != false) {
				$theme = $tmpTheme;
			} else {
				$theme = FILENAME_COLLECTION_DEFAULT_THEME;
			}		
			
			$this->setThemeForView($theme, $themeFilename, $wrapTemplateInTheme);


			// finally, we include the theme (which was set by setTheme and will automatically include innerContent)
			// disconnect from our db and exit

			$this->controller->on_before_render();
			extract($this->controller->getSets());
			extract($this->controller->getHelperObjects());

			if ($content != false && (!$this->disableContentInclude)) {
				include($content);
			}

			$innerContent = ob_get_contents();
			
			if (ob_get_level() > OB_INITIAL_LEVEL) {
				ob_end_clean();
			}

			if (defined('DB_DATABASE') && ($view !== '/upgrade')) {
				Events::fire('on_before_render', $this);
			}
						
			if (defined('APP_CHARSET')) {
				header("Content-Type: text/html; charset=" . APP_CHARSET);
			}
			
			if (file_exists($this->theme)) {
				
				$cache = PageCache::getLibrary();
				$shouldAddToCache = $cache->shouldAddToCache($this);
				if ($shouldAddToCache) {
					$cache->outputCacheHeaders($c);
				}

				ob_start();
				include($this->theme);
				$pageContent = ob_get_contents();
				ob_end_clean();
				
				$ret = Events::fire('on_page_output', $pageContent);
				if($ret != '') {
					print $ret;
					$pageContent = $ret;
				} else {
					print $pageContent;
				}

				$cache = PageCache::getLibrary();
				if ($shouldAddToCache) {
					$cache->set($c, $pageContent);
				}

			
			} else {
				throw new Exception(t('File %s not found. All themes need default.php and view.php files in them. Consult concrete5 documentation on how to create these files.', $this->theme));
			}
			
			if (defined('DB_DATABASE') && ($view !== '/upgrade')) {
				Events::fire('on_render_complete', $this);
			}
			
			if (ob_get_level() == OB_INITIAL_LEVEL) {
				require(DIR_BASE_CORE . '/startup/jobs.php');
				require(DIR_BASE_CORE . '/startup/shutdown.php');
				exit;
			}
			
		}
	}
Back to Top