<?php
require_once (PATH_CONTROLLERS . "Common.php") ;
require_once (PATH_LIB . "LicenseFile.php") ;
require_once (PATH_MODELS . "admin/UserClass.php");
require_once (PATH_MODELS . "admin/UserGroupClass.php") ;
require_once (PATH_MODELS . "admin/UserRight.php") ;

class Home extends ActivityBase {
	private $clsGroup ;
	private $clsRights ;
	private $clsemp ;
	private $clspwd;
    private $user;
	private $cmd;
	function __construct() {
		$this->db 		= $_SESSION[SE_DB] ;
		$this->orgid 	= $_SESSION[SE_ORGID] ;
        $this->user = new UserClass($this->db);
		$this->clsGroup = new UserGroupClass($this->db) ;
		$this->clsRights = new UserRight() ;
	}
	function __destruct() {
        unset($this->user);
		unset($this->clsGroup) ;
		unset($this->clsRights) ;
		unset($this->db) ;
	}
	public function processRequest($params) {
		$this->type = REQ_VIEW ;
		try {
			$this->db->open() ;
			$this->loadSetting() ;
			if (isset($params) && count($params) > 0) {
				if (isset($params['type']))
					$this->type = $params['type'] ;
				if (isset($params['cmd']))
					$this->cmd = $params['cmd'] ;
			}
			switch ($this->type) {
				case Command::PortalHome:
 					$this->processHomeInfo() ;
					break ;
				case Command::PortalLogout:
					$this->processLogout() ;
					break ;
				case REQ_VIEW:
					$this->processView() ;
					break ;
				case Command::changePwd:
					$this->processChangePwd($params);
					break ;

				case "show_page_index":
					$this->showPageIndex($params);
					break ;
				case "show_page_detail":
					$this->showPageDetail($params);
					break ;
				case "show_page_submit":
					$this->showPageSubmit($params);
					break ;
				default:
					$this->sendJsonResponse(Status::Error,"invalid request.","",$this->type) ;
					break ;
			}
			$this->db->close() ;
			return true ;
		} catch (Exception $e) {
			$this->db->close() ;
			die ($e->getMessage()) ;
		}
	}
	
/*	private function updateLanguage($params) {
        $empLangClass = new EmployeeLanguageClass($this->db);
        
        try {
            $empLangClass->updateRecordByID($_SESSION[SE_EMPID], $params['lang_id']);
            $this->sendJsonResponse(Status::Ok,"","",$this->type) ;
            
        }  catch (Exception $e) {
			Log::write("[UPDATE_LANGUAGE] " . $e->getMessage());
			$this->sendJsonResponse(Status::Error,"Unsuccessful database operation","",$this->type) ;
        }
	}*/
	private function showPageIndex($params)
	{
		ob_start() ;
        
		include (PATH_VIEWS . "portal/testing/testingcustomerview.php") ;
		$var_screen = str_replace("[index]",$params['index'], Util::minifyHtml(ob_get_clean())) ;

		
		$this->sendJsonResponse(Status::Ok,"",$var_screen,$this->type) ;
	}

	private function showPageDetail($params)
	{
		ob_start() ;
        
		include (PATH_VIEWS . "portal/testing/testingcustomeraddview.php") ;
		$var_screen = str_replace("[index]",$params['index'], Util::minifyHtml(ob_get_clean())) ;

		
		$this->sendJsonResponse(Status::Ok,"",$var_screen,$this->type) ;
	}

	private function showPageSubmit($params)
	{
		ob_start() ;
        Log::write("asdasda");
		include (PATH_VIEWS . "portal/testing/testingcustomeraassetview.php") ;
		$var_screen = str_replace("[index]",$params['index'], Util::minifyHtml(ob_get_clean())) ;

		
		$this->sendJsonResponse(Status::Ok,"",$var_screen,$this->type) ;
	}
	private function processLogout() {
		session_unset();
		$this->sendJsonResponse(Status::Ok,"","",$this->type) ;
	}
	
	private function processView() {
		ob_start() ;

		$this->clsRights->initRights() ;
		if ($_SESSION[SE_USERGROUP] != "")
		{
			$row = $this->clsGroup->getRecord($_SESSION[SE_USERGROUP]) ;
			if (!is_null($row)) {
				if (!is_null($row[UserGroupTable::C_RIGHTS]))
					$this->clsRights->toRights($row[UserGroupTable::C_RIGHTS]); ;
			}
		}
        
		$userCls        = new UserClass($this->db);
        $designation = $userCls->getRecord($_SESSION[SE_USERID])[UserTable::C_DESIGNATION];
		$user_name = $userCls->getRecord($_SESSION[SE_USERID])[UserTable::C_FULL_NAME];
		include (PATH_VIEWS . "portal/HomeView.php") ;
		echo Util::minifyHtml(ob_get_clean()) ;
	}
	
	private function processChangePwd($params){
		$empPwdRow = $this->user->getRecord($_SESSION[SE_USERID]);
		if($this->user->encryptPwd1($params['oldPwd']) != $empPwdRow[UserTable::C_PWD1] || $this->user->encryptPwd2($params['oldPwd']) != $empPwdRow[UserTable::C_PWD2]){
			$this->sendJsonResponse(Status::Error,"Wrong old password","",$this->type) ;
			return;
		}
		
        $strCheck = "";
		//$strCheck = Util::checkPasswordComplexity($params['newPwd']);
		if($strCheck != ""){
			$this->sendJsonResponse(Status::Error,$strCheck,"",$this->type) ;
			return;
		}
		
		try{
			$this->user->changePassword($_SESSION[SE_USERID], $params['newPwd']);
			$this->sendJsonResponse(Status::Ok,"Your password has been successfully changed!","",$this->type) ;
		} catch (Exception $e) {
			Log::write("[CHG_PWD] " . $e->getMessage());
			$this->sendJsonResponse(Status::Error,"Unsuccessful database operation.","",$this->type) ;
		}
		
	}
	
	private function getHomeInfo() {
		ob_start() ;
		$activityCls 		= new ActivityHeaderClass($this->db);
        $userCls        = new UserClass($this->db);
		$NUM_ACT_SHOW = 5;
		$deptId 		=0;
		
        
       /* $right = $this->getPermissionPortal();
        $deptId = 0;
        if ($right[UserRight::C_VIEW_DPT_INVOICE][UserRight::C_ENABLE]){
            $deptId = $userCls->getRecord($_SESSION[SE_USERID])[UserTable::C_DEPT];
        }*/
        
		$activityViewList = $activityCls->getActivityList($_SESSION[SE_USERID], "", "", $NUM_ACT_SHOW);

		//Log::Write(print_r($activityViewList,true));die();
		
		//$cls 		= new ContentClass($this->db) ;
		//$cate 		= new ContentCategoryClass($this->db) ;
		//$newsList 	= $cls->getListByType(ContentType::News,ContentTable::C_TITLE, true) ;
            
		include (PATH_VIEWS . "portal/HomeInfoView.php") ;
		return Util::minifyHtml(ob_get_clean()) ;
	}
	private function needChangePassword(){
		$userPwdRow = $this->user->getRecord($_SESSION[SE_USERID]);
		return $userPwdRow[UserTable::C_CHANGE_PWD];
	}
	private function processHomeInfo() {
		$dataReturn = array();
		$dataReturn['htmlContent']	= $this->getHomeInfo() ;
		$dataReturn['headTitle']	=  "My Dashboard" ;
		
		$this->sendJsonResponse(Status::Ok,"Load Home Info",$dataReturn,$this->type);
	}
	private function getOrganizationName() {
		return $_SESSION[SE_PORT_ORGNAME] ;
	}
	private function getFullName() {
		return $_SESSION[SE_PORT_FULLNAME] ;
	}
	/*private function createMenuFunc($href,$desc,$type) {
		return "javascript:showPage('" . Util::convertLink($href) . "','". $desc . "'". ",'" . "$type" . "')" ;
	}*/
	
	private function createMobileFunc($href,$type) {
		return "javascript:showMobilePage('" . $href . "','" . "$type" . "')" ;
	}
	
	private function createJsFunc($func) {
		return "javascript:" . $func ;
	}	
	private function getUserProfile() {
		$userId = $_SESSION[SE_USERID];
		return $this->user->getRecord($userId);
	}
    private function getLogo() {
		$userCls  = new UserClass($this->db);
		$coyid 	= $userCls->getRecord($_SESSION[SE_USERID])[UserTable::C_COY_ID];

		$fn = Util::getLogoFile(PATH_PICTURE, "company-".$coyid) ;
		if ($fn == "")
			return "" ;
		else 
			return "..\web\picture\company-" . $coyid . "/" . $fn ;
	}
}
?>