| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace CloudControl\Cms\components | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |     use CloudControl\Cms\cc\Application; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |     use CloudControl\Cms\cc\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |     use CloudControl\Cms\storage\Storage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     class BaseComponent implements Component | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 		 * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 		protected $template; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 		 * @var \CloudControl\Cms\cc\Request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 		protected $request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 		 * @var Storage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 		protected $storage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 		 * @var mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 		protected $renderedContent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 		 * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		protected $parameters = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 		/*** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		 * @var \stdClass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 		protected $matchedSitemapItem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		 * BaseComponent constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		 * @param string  $template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 		 * @param Request $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		 * @param array   $parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		 * @param         $matchedSitemapItem | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		public function __construct($template='', Request $request, $parameters=array(), $matchedSitemapItem) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 			$this->template = $template; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 			$this->request = $request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 			$this->parameters = (array) $parameters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 			$this->matchedSitemapItem = $matchedSitemapItem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 		 * Hook for implementation in derived classes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		 * @param Storage $storage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		public function run(Storage $storage) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 			$this->storage = $storage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		 * Renders the template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		 * @param null|Application $application | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		 * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		public function render($application=null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 			$this->renderedContent = $this->renderTemplate($this->template, true, $application); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		 * Returns the rendered content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		 * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		public function get() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 			return $this->renderedContent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		 * Decoupled render method, for usage in derived classes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		 * @param string $template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		 * @param bool   $obClean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		 * @param null | Application   $application | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		 * @throws \Exception | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 94 |  |  | 		 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  | 		public function renderTemplate($template='', $obClean = true, $application=null) | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  | 		{ | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  | 		    $templateDir = $this->getTemplateDir($template, $application); | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  | 		    if ($application !== null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |                 $rootDir = $application->getRootDir(); | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |                 if (strpos($templateDir, $rootDir) === false) { | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |                     $templatePath = $rootDir . DIRECTORY_SEPARATOR . $templateDir; | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |                 } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |                     $templatePath = $templateDir; | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  | 		        $templatePath = $templateDir; | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  | 			if (realpath($templatePath) !== false) { | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  | 				if ($obClean) { | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  | 					ob_clean(); | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  | 				} | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  | 				$this->parameters['request'] = $this->request; | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  | 				if ($application !== null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  | 					$acParameters = $application->getAllApplicationComponentParameters(); | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  | 					foreach ($acParameters as $parameters) { | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  | 						extract($parameters); | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  | 					} | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  | 				} | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  | 				extract($this->parameters); | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  | 				include($templatePath); | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  | 				return ob_get_contents(); | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  | 			} else { | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  | 				if ($template !== null) { // If template is null, its a application component, which doesnt have a template | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  | 					throw new \Exception('Couldnt find template ' . $templatePath); | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  | 				} | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 		 * Alias for renderTemplate for usage to include templates in other templates | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 		 * @param string $template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 		 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 		 * @param array  $parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 		 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 		 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		 * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 		public function includeTemplate($template='', $parameters = array()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 			if (is_array($parameters)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 				foreach ($parameters as $name => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 					$this->parameters[$name] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 			return $this->renderTemplate($template, false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 		public function getParameters() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 			return $this->parameters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |          * @param $template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |          * @param null | Application $application | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |          * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         protected function getTemplateDir($template, $application=null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             $templatePath = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             if ($application !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |                 $templatePath = $application->getTemplateDir(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |             $templatePath = $templatePath . $template . '.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             return $templatePath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 168 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 169 |  |  | } |