<?php
include(PATH_CONTROLLERS . "Common.php");
include(PATH_MODELS . "admin/UserGroupClass.php");

class UserGroupEntry extends GLBase
{
	private $type = "";
	private $user;
	private $clsright;

	function __construct()
	{
		$this->db = $_SESSION[SE_DB];
		$this->clsright = new UserRight();
	}
	function __destruct()
	{
		unset($this->user);
		unset($this->clsright);
	}
	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 (!$this->checkSessionTimeout($this->type)) {
				$this->db->close();
				return false;
			}

			switch ($this->type) {
				case Command::UserGroupEntryView:
					$this->getView($params);
					break;
				case REQ_GET:
					$this->getUserGroupEntryRecord($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 getView($params)
	{
		ob_start();
		include(PATH_VIEWS . "admin/usergroup/UserGroupEntryView.php");

		$action_menu = "";

		$var_screen = str_replace("[index]", $params['index'], Util::minifyHtml(ob_get_clean()));
		$dataReturn = [];
		$dataReturn['htmlContent'] = $var_screen;
		$dataReturn['action_menu'] = $action_menu;
		$dataReturn['headTitle']  = "User Group Entry";
		$this->sendJsonResponse(Status::Ok, "User Group Entry Info", $dataReturn, $this->type);
	}

	private function getUserGroupEntryRecord($params)
	{
		ob_start();

		$id             = $params['id'];
		$clsUserGroup    = new UserGroupClass($this->db);
		$row            = $clsUserGroup->getRecord($id);

		$datas = array();
		if (!is_null($row) && count($row) > 0) {
			//User Group info
			$datas['id']	= $row[UserGroupTable::C_ID];
			$datas['code']	= $row[UserGroupTable::C_CODE];
			$datas['name']	= $row[UserGroupTable::C_NAME];
			if (is_null($row[UserGroupTable::C_RIGHTS]))
				$this->clsright->initRights();
			else
				$this->clsright->toRights($row[UserGroupTable::C_RIGHTS]);;
			$datas['admin'] = $this->getAdminRights($this->clsright->getAdminRights());
		}
		include(PATH_VIEWS . "admin/usergroup/UserGroupEntryView.php");

		$var_screen = str_replace("[index]", $params['index'], Util::minifyHtml(ob_get_clean()));

		$dataReturn = array();
		$dataReturn['htmlContent']  = $var_screen;
		$dataReturn['headTitle']    = "User Group Entry";
		$dataReturn['headerData']   = $datas;

		$this->sendJsonResponse(Status::Ok, "User Group Entry", $dataReturn, $this->type);
		unset($clsUserGroup);
	}

	private function getAdminRights($rights)
	{
		$arr = array();
		$arr['marketing_entity'] 				= $rights[UserRight::C_MARKETING_ENTITY][UserRight::C_ENABLE];
		$arr['marketing_view_others'] 			= $rights[UserRight::C_MARKETING_VIEW_OTHERS][UserRight::C_ENABLE];
		$arr['marketing_edit_others'] 			= $rights[UserRight::C_MARKETING_EDIT_OTHERS][UserRight::C_ENABLE];
		$arr['sales_account'] 					= $rights[UserRight::C_SALES_ACCOUNT][UserRight::C_ENABLE];
		$arr['sales_suspect'] 					= $rights[UserRight::C_SALES_SUSPECT][UserRight::C_ENABLE];
		$arr['sales_prospect'] 					= $rights[UserRight::C_SALES_PROSPECT][UserRight::C_ENABLE];
		$arr['sales_opportunity'] 				= $rights[UserRight::C_SALES_OPPORTUNITY][UserRight::C_ENABLE];
		$arr['sales_view_others'] 				= $rights[UserRight::C_SALES_VIEW_OTHERS][UserRight::C_ENABLE];
		$arr['sales_edit_others'] 				= $rights[UserRight::C_SALES_EDIT_OTHERS][UserRight::C_ENABLE];
		$arr['integration_customer'] 			= $rights[UserRight::C_INTEGRATION_CUSTOMER][UserRight::C_ENABLE];
		$arr['integration_prospect'] 			= $rights[UserRight::C_INTEGRATION_PROSPECT][UserRight::C_ENABLE];
		$arr['integration_export_data'] 		= $rights[UserRight::C_INTEGRATION_EXPORT_DATA][UserRight::C_ENABLE];
		$arr['integration_destination_master'] 	= $rights[UserRight::C_INTEGRATION_DESTINATION_MASTER][UserRight::C_ENABLE];
		$arr['admin_user']            			= $rights[UserRight::C_ADMIN_USER][UserRight::C_ENABLE];
		$arr['admin_user_group']      			= $rights[UserRight::C_ADMIN_USER_GROUP][UserRight::C_ENABLE];
		$arr['admin_industry'] 					= $rights[UserRight::C_ADMIN_INDUSTRY][UserRight::C_ENABLE];
		$arr['admin_business_sector'] 			= $rights[UserRight::C_ADMIN_BUSINESS_SECTOR][UserRight::C_ENABLE];
		$arr['admin_business_nature'] 			= $rights[UserRight::C_ADMIN_BUSINESS_NATURE][UserRight::C_ENABLE];
		$arr['admin_software'] 					= $rights[UserRight::C_ADMIN_SOFTWARE][UserRight::C_ENABLE];
		$arr['admin_department'] 				= $rights[UserRight::C_ADMIN_DEPARTMENT][UserRight::C_ENABLE];
		$arr['report_contact']        			= $rights[UserRight::C_REPORT_CUSTOMER_CONTACT_LISTING][UserRight::C_ENABLE];
		$arr['report_summary']        			= $rights[UserRight::C_REPORT_CORPORATE_GIFT_SUMMARY][UserRight::C_ENABLE];
		$arr['report_inquiry']        			= $rights[UserRight::C_REPORT_CORPORATE_GIFT_INQUIRY][UserRight::C_ENABLE];
		$arr['report_history']        			= $rights[UserRight::C_REPORT_CORPORATE_GIFT_HISTORY][UserRight::C_ENABLE];
		$arr['report_conversation']        		= $rights[UserRight::C_REPORT_CONVERSATION][UserRight::C_ENABLE];
		$arr['report_all_account']         		= $rights[UserRight::C_REPORT_ALL_ACCOUNT][UserRight::C_ENABLE];
		$arr['gift_company']          			= $rights[UserRight::C_CORPORATE_GIFT_COMPANY_PROFILE][UserRight::C_ENABLE];
		$arr['gift_listing'] 					= $rights[UserRight::C_CORPORATE_GIFT_LIST][UserRight::C_ENABLE];
		$arr['gift_master'] 					= $rights[UserRight::C_CORPORATE_GIFT_MASTER][UserRight::C_ENABLE];
		$arr['campaign_company'] 	       		= $rights[UserRight::C_CAMPAIGN_COMPANY_PROFILE][UserRight::C_ENABLE];
		$arr['campaign_listing'] 				= $rights[UserRight::C_CAMPAIGN_LIST][UserRight::C_ENABLE];
		$arr['campaign_master'] 				= $rights[UserRight::C_CAMPAIGN_MASTER][UserRight::C_ENABLE];
		return $arr;
	}
}
