Location: PHPKode > scripts > PHP-PAPA > php-papa/papa/controller.php
<?php
	class Controller{
		protected $useLayout   = true;
		private $theme = 'default';
		private $locale  = 'bn';
		private $defaultLocale = 'bn';//depricated
		private $layoutParams = array();
		private $layout = 'default';
	
		public $session;
		public $post;
		public $get;
		public $cookie;
		public $showBenchmark = true;
	
		public function __construct(){		
			$this->useLayout   = true;
		
			if(empty($_SESSION['__PAPA__defaultlocale__'])){
				papa_session_set('defaultlocale',$this->defaultLocale);
			}
			if(empty($_SESSION['__PAPA__defaultlocale__'])){
				papa_session_set('locale',$this->locale);
			}
		
		
			$this->theme = papa_get_current_theme();
		
		
			$this->session = new ControllerSession();
			$this->post	= new ControllerPost();
			$this->get = new ControllerGet();
			$this->cookie = new ControllerCookie();

			global $papa_config;
			$this->showBenchmark = $papa_config['settings']['benchmark']=='yes';
		
		
		}
	
		public function getCurrentTheme(){
			return $this->theme;
		}
	
		// Should be in global function
		public function setCurrentTheme($theme){
			$this->theme = $theme;
		}
	
		public function getCurrentLayout(){
			return $this->layout;
		}
	
		// Should be in global function
		public function setCurrentLayout($layout){
			$this->layout = $layout;
		}
		
		/**
		 * TODO :  Set the layout according to the content path;
		 * 
		 * For a content like 'guide/base/test.html',
		 * first check if 'layout/guide' is a directory, if yes
		 * check 'layout/guide/base'  is directory, if yes
		 * check 'layout/guide/base/test.php' is present, if yes set it as layout
		 * else, check 'layout/guide/base.php' is present, if yes set it as layout
		 * else check 'layout/guide.php' is present, if yes set it as layout
		 * else set 'default' as layout.
		 *
		 */
		 //papa 1.1
		public function updateLayout(){
			global $controllerPath;			
			$args = explode('/',$controllerPath);
			if(file_exists( 'theme/' . $this->theme . '/layouts/' . $args[0] . '.php' ) === true){
				
				$this->setCurrentLayout($args[0]);
			}elseif(file_exists( 'theme/' . 'default/layouts' . $args[0] . '.php') === true){
				$this->setCurrentLayout($args[0]);
			}else{
				$this->setCurrentLayout('default');
			}
			
		}
		public function updateLayout_old(){
			global $papa_current_path;			
			$args = explode('/',$papa_current_path);
			if(file_exists( 'theme/' . $this->theme . '/layouts/' . $args[0] . '.php' ) === true){
				
				$this->setCurrentLayout($args[0]);
			}elseif(file_exists( 'theme/' . 'default/layouts' . $args[0] . '.php') === true){
				$this->setCurrentLayout($args[0]);
			}else{
				$this->setCurrentLayout('default');
			}
			
		}
	
		public function addLayoutParam($key,$value){
			$this->layoutParams[$key] = $value;
		}
		public function setDefaultLocale($loc){
			$this->defaultLocale = $loc;
			$_SESSION['__PAPA__defaultlocale__'] = $loc;
		}
		public function setLocale($loc){
			$_SESSION['__PAPA__locale__'] = $loc;
		}
		public function getLocale(){
			return papa_session_get('locale');
		}
		public function getDefaultLocale(){
			papa_session_get('defaultlocale');
		}
		public function linkCSS($url){
			///TBD
		}
		public function linkJS($url){
			///TBD
		}
		public function img($url){
			///TBD
		}
		
		public function showLayout($params = array()){
			$this->includeLayout($params);
		}
		private function includeLayout($view,$params){
			global $__PAPA_DEBUG__;
			papa_session_get('defaultlocale');
			extract($this->layoutParams);
		
			$file = '';

			// try to get the layout file from the locale
			if(file_exists('theme/'.$this->theme.'/layouts/_'.$this->locale.'/'.$this->layout.'.php')){
				$file = 'theme/'.$this->theme.'/layouts/_'.$this->locale.'/'.$this->layout.'.php';			
		
				//try to get the file from theme folder
			}else if(file_exists('theme/'.$this->theme.'/layouts/'.$this->layout.'.php')){
				$file = 'theme/'.$this->theme.'/layouts/'.$this->layout.'.php';
		
				// try to get the file from the default theme
			}else{
				$file = 'theme/default/layouts/'.$this->layout.'.php';
			}
		
		
			$str = '';
			//*
			if(papa_is_debug_on()===true){			
				$str  .=  "<script>\nvar __PAPA_DEBUG__STATUS__=1;\nvar __PAPA_DEBUG_MSG__='';";
				$str .= "function __PAPA__DEBUG__CLICKED__(){\nvar __PAPA_DEBUG_DIV__ = document.getElementById('__PAPA_DEBUG__');\n";
				$str .= "if(__PAPA_DEBUG__STATUS__===1){\n";
				$str .= "\t__PAPA_DEBUG_DIV__.style.width='auto';\n\t__PAPA_DEBUG_DIV__.style.height='auto';\n";
				$str .= "\t__PAPA_DEBUG__STATUS__=0;\n";
				$str .= "\t __PAPA_DEBUG_MSG__ = __PAPA_DEBUG_DIV__.innerHTML;";
				$str .= "\t __PAPA_DEBUG_DIV__.innerHTML= 'Debug >>'; ";
				$str .= "}else{\n";
				$str .= "\t__PAPA_DEBUG_DIV__.style.width='100%';__PAPA_DEBUG_DIV__.style.height='auto';\n";
				$str .= "\t__PAPA_DEBUG__STATUS__=1;\n";
				$str .= "\t __PAPA_DEBUG_DIV__.innerHTML = __PAPA_DEBUG_MSG__;";
				$str .= "}\n}\n";
				$str .= '</script><div onmouseover="this.style.opacity=0.9"  onmouseout="this.style.opacity=0.2" onclick="__PAPA__DEBUG__CLICKED__()" id="__PAPA_DEBUG__" style="opacity:0.2;color:gray;padding:10px;cursor:pointer;background-color:white;border:1px outset gray;position:fixed;top:200px;left:0px;width:100%">';
				$str .= '<b> << <i>Debug:</i></b><br/><br/>';
				foreach($__PAPA_DEBUG__ as $deb){
					$str .= $deb;
				}			
				$str .= '</div>';
			}//*/
			//$page = $str.$page;
			echo $str;
			
			@include($file);
			//}
		}
		
		//papa 1.1
		private function includeView($view,array $params=array()){
			$file = papa_get_view($view);

			if($file===false)return false;
			
			extract($params);
			include($file);
		}
		
		private function includeView_old($view,array $params=array()){
			extract($params);
		
			$file_name= '';
			if(file_exists('theme/'.$this->theme.'/views/_'.$this->locale.'/'.papa_get_current_controller_name().'/'.$view.'.php')){
				$file_name = 'theme/'.$this->theme.'/views/_'.$this->locale.'/'.papa_get_current_controller_name().'/'.$view.'.php';
			}else if(file_exists('theme/'.$this->theme.'/views/'.papa_get_current_controller_name().'/'.$view.'.php')){
				$file_name = 'theme/'.$this->theme.'/views/'.papa_get_current_controller_name().'/'.$view.'.php';
			}else if(file_exists('theme/'.$this->theme.'/views/_'.$this->locale.'/'.$view.'.php')){
				$file_name = 'theme/'.$this->theme.'/views/_'.$this->locale.'/'.$view.'.php';
			}
			else{
				$file_name = 'theme/'.$this->theme.'/views/'.$view.'.php';
			}
		
		
			if(!file_exists($file_name)){
				if(file_exists('theme/default/views/_'.$this->locale.'/'.papa_get_current_controller_name().'/'.$view.'.php')){
					$file_name = 'theme/default/views/_'.$this->locale.'/'.papa_get_current_controller_name().'/'.$view.'.php';
				}else if(file_exists('theme/default/views/'.papa_get_current_controller_name().'/'.$view.'.php')){
					$file_name = 'theme/default/views/'.papa_get_current_controller_name().'/'.$view.'.php';
				}else if(file_exists('theme/default/views/_'.$this->locale.'/'.$view.'.php')){
					$file_name = 'theme/default/views/_'.$this->locale.'/'.$view.'.php';
				}
				else{
					$file_name = 'theme/default/views/'.$view.'.php';
				}
			}
			 
		
			if(!file_exists($file_name))return false;
			include($file_name);
			//}
		}
		
		public function isViewExists($view){
			$file_name= '';
			if(file_exists('theme/'.$this->theme.'/views/_'.$this->locale.'/'.papa_get_current_controller_name().'/'.$view.'.php')){
				$file_name = 'theme/'.$this->theme.'/views/_'.$this->locale.'/'.papa_get_current_controller_name().'/'.$view.'.php';
			}else if(file_exists('theme/'.$this->theme.'/views/'.papa_get_current_controller_name().'/'.$view.'.php')){
				$file_name = 'theme/'.$this->theme.'/views/'.papa_get_current_controller_name().'/'.$view.'.php';
			}else if(file_exists('theme/'.$this->theme.'/views/_'.$this->locale.'/'.$view.'.php')){
				$file_name = 'theme/'.$this->theme.'/views/_'.$this->locale.'/'.$view.'.php';
			}
			else{
				$file_name = 'theme/'.$this->theme.'/views/'.$view.'.php';
			}
			//__log('Includeing file: ' . $file_name);
			
			if(!file_exists($file_name))return false;
			return true;
		}

	
		public function show($view='',array $params=array()){		
		/*
		 * if view is not passed load assume view is $controller/$method.php
		 */
			if(empty($view) || is_array($view)){
				$view = papa_get_current_controller_name().'/'.papa_get_current_method_name();
			}

			// if layout is used, load the theme and layout
			if($this->useLayout===true){
				$this->includeLayout($view,$params);	
			
			}else{ // just return the view output, might be used in ajax call
			
				$this->includeView($view,$params);
			
			}
		}
	
	/**
	 * This method returns if the REQUEST_METHOD is POST.
	 */
		public function isPosted(){
			return ( 'POST' == $_SERVER['REQUEST_METHOD'] );
		}
		
		public function skipCall(){
			return false;
		}
	}
	
	
?>
Return current item: PHP-PAPA