<?php

CLASS RasioProject
{
	protected $em;
	protected $config;
	protected $app;
	protected $act;
	protected $param = array();

	public function getConfig($res)
	{
		$this->em			= $res->em;
		$this->config		= $res->config;
		$this->basePath		= $res->basePath;
		$this->baseTitle	= $res->baseTitle;
		$this->baseName		= $res->baseName;
		$this->project		= $res->project;
		$this->baseUrl		= $res->config['baseUrl'];
		$this->baseDomain	= $res->config['baseDomain'];
		$this->session		= $res->session;
		$this->login		= $this->setLogin($res->session);
		$this->getParameter	= $this->setGet();
	}
	
	public function setConfig()
	{
		$res->em 			= $this->em;
		$res->config 		= $this->config;
		$res->basePath 		= $this->basePath;
		$res->baseTitle 	= $this->baseTitle;
		$res->baseName 		= $this->baseName;
		$res->project 		= $this->project;
		$res->session 		= $this->session;
		return $res;
	}
	
	public function validation($user = "user_name_login", $pass = 'password_login')
	{
		$model = $this->session;
		if(!empty($model)){
			$login = $_SESSION[$model]['login'];
			if(!empty($login)){
				if(!empty($login['kd_user']) and !empty($login['nm_user'])){
					return true;
				}else{
					header("location:".$this->baseUrl.'login');
					return;
				}
			}else{
				header("location:".$this->baseUrl.'login');
				return;
			}
		}
	}
	
	protected function setLogin($mode)
	{
		if(!empty($mode)){
			return $_SESSION[$mode]['login'];
		}
	}
	
	public function getDb()
	{
		$this->config = $this->callConfig();
		$this->em = $this->callDb();
	}
	
	protected function callConfig()
	{
		require_once($this->basePath.'config/config.php');
		return $config;
	}
	
	protected function callDb()
	{
		$config = $this->config;
		require_once($this->basePath.'config/domain/db.php');
		require_once($this->basePath.$this->project.'/DBase/'.$config['dbConnection.dbServer'].'.php');
		$db = new $config['dbConnection.dbServer'];
		$db->getConnection($config);
		return $db;
	}
	
	protected function setRef($var)
	{
		$var = strtolower($var);
		$var = str_ireplace("-", ' ', $var);
		$var = ucwords($var);
		return str_ireplace(" ", '', $var);
	}
	
	protected function setGet()
	{
		$get = explode("/", $_GET['ref']);
		if(!empty($get[0])){
			$this->app = $this->setRef($get[0]);
		}else{
			$this->app = $this->setRef("index");
		}
		if(!empty($get[1])){
			$this->act = $this->setRef($get[1]);
		}else{
			$this->act = $this->setRef("index");
		}
		
		$ii = 0;
		$param = array();
		for($i = 2; $i < count($get); $i++){
			$this->param[$ii] = $get[$i];
			$param[$ii] = $get[$i];
			$ii++;
		}
		
		$res['controller'] = $this->app;
		$res['asliGetCotroller'] = $get[0];
		$res['action'] = $this->act;
		$res['param'] = $param;
		
		if(strtolower($res['action']) == strtolower('index')){
			$res['modul'] = $get[2];
		}
		
		return $res;
    }
	
	public function getPlug()
	{
		$folder = $this->basePath.'Plugin';
		$dir = opendir($folder);
		$app = array();
		while($file = readdir($dir)){
			if ($file == "." or $file == "..")continue;
			$app[] = $folder.'/'.$file;
		}
		foreach($app as $row){
			require_once($row);
		}
	}

	public function setApplication()
	{
		$folder = $this->basePath.$this->baseName;
		$dir = opendir($folder);
		$app = array();
		$ctr = array();
		while($file = readdir($dir)){
			if ($file == "." or $file == "..")continue;
				$jum = explode('.', $file);
				if(count($jum) == 1){
					$folder2 = $folder.'/'.$file;
					$dir2 = opendir($folder2);
					while($file2 = readdir($dir2)){
						if ($file2 == "." or $file2 == "..")continue;
						
						if(strtolower($file2) == 'menu.php'){
                            require_once($folder2.'/'.$file2);
                            continue;
                        }
						
						$modul = strtolower(str_ireplace(".php", "", $file2));
						$ctr[$modul]->modul = $file;
						
						if($file2 != 'func.php'){
							$app[$modul] = $folder2.'/'.$file2;
						}
					}
				}
		}
		return array(
            'ctr' => $ctr,
            'app' => $app
        );
	}

	public function setAutoload($app)
    {
        $base = $this->basePath.$this->baseName;
        spl_autoload_register(function ($class) use ($app, $base) {
            $key = strtolower($class);
            if(isset($app[$key]) && file_exists($app[$key])){
                require_once($app[$key]);
            }
        });
    }

	protected function loadApp($app, $act, $ctr)
	{
		$this->getConfig($this->setConfig());
		
		if(!empty($app->layout)){
			$this->baseLayout = $app->layout;
		}
		if(!empty($app->baseTitle)){
			$this->baseTitle = $app->baseTitle;
		}
		if(!empty($app->view)){
			$this->view = $app->view.'.phtml';
		}else{
			$this->view = 'default.phtml';
		}

		$this->disCK = false;
		if($app->enableCk == true){
			$this->disCK = false;
		}

		$nCtr = strtolower($this->getParameter['controller']);
		$nCtr = explode("___", $nCtr);
		$nCtr = $nCtr[0];
		$this->modul = $ctr[$nCtr]->modul;
		$this->data  = $app->data;
		
		require_once("rasio_form.php");
		$this->form = new rasio_form;
		$this->form->callForm();
		
		require_once($this->basePath.'assets/thema/'.$this->baseLayout.'.phtml');
	}
	
	public function getApp()
	{
		session_start();
		$res = $this->setApplication();
		$ctr = $res['ctr'];
		$app = $res['app'];
		$this->setAutoload($app);
		$this->setGet();
		$act = $this->act;
		
		$nCtr = strtolower($this->app);
		$nCtr = explode("___", $nCtr);
		$nCtr = $nCtr[0];
		
		// var_dump($ctr['mcl']);
		// var_dump($nCtr);
		// var_dump($ctr[$nCtr]->modul);
		
		if(empty($ctr[$nCtr]->modul)){
			$app = 'NotFound';
		}else{
			$app = explode("___", $this->app);
			$app = $app[0];
		}
		
		if(!empty($act)){
			$acts = strtoupper($act);
		}
		
		$idP1 = $app;
		$idP2 = $this->app;
		$app = new $app($this->setConfig());
		$act = $app->$act();
		
		
		if($acts == 'INDEX' or empty($acts)){
			$_SESSION['setMyCtrl'][$idP1] = $app->set();
			$_SESSION['setMyCtrl'][$idP2] = $app->set();
			
			
			$get = explode("/", $_GET['ref']);
			$menu = new Menu;
			$this->menu = $menu->listDetMenu($get[2]);
			$this->acl 	= $menu->cekAcl($this->em);
		}
		
		$this->loadApp($app, $act, $ctr);
	}
	
	public function getPlugin($res = false)
	{
		if($res != false){
			return new $res;
		}
	}
	
	public function getTrueMenus($sess)
	{
		require_once($this->basePath.$this->project.'/rasio_Permission.php');
		$acl = new rasio_Permission;
		return $acl->trueMenus($this->em, $sess);
	}
	
	public function sessionDet($class, $set, $data)
	{
		$res->set 	= $set->detail;
		$res->data 	= $data;
		
		$data = serialize($res);
		$myfile = fopen("det/".$class.".txt", "w") or die("Unable to open file!");
		fwrite($myfile, $data);
		fclose($myfile);		
	}
	
	public function vardump($tmp = array())
	{
		foreach($tmp as $r){
			echo $r.";<br>";
		}
	}
}