<?php
class UserRight {
	private  $C_NO_ADMIN 	= 32;

	const C_ENABLE 	= 0 ;
	const C_ADD 	= 1 ;
	const C_UPDATE 	= 2;
	const C_DELETE 	= 3;
	const C_VIEW 	= 4;
	const C_PRINT 	= 5 ;

	const C_ADMIN 							= "admin";
	const C_MARKETING_ENTITY 				= 0;
	const C_SALES_ACCOUNT 					= 1;
	const C_SALES_SUSPECT 					= 2;
	const C_SALES_PROSPECT 					= 3;
	const C_SALES_OPPORTUNITY 				= 4;
	const C_INTEGRATION_CUSTOMER 			= 5;
	const C_INTEGRATION_PROSPECT 			= 6;
	const C_INTEGRATION_EXPORT_DATA 		= 7;
	const C_INTEGRATION_DESTINATION_MASTER 	= 8;
	const C_ADMIN_USER 						= 9;
	const C_ADMIN_USER_GROUP 				= 10;
	const C_ADMIN_INDUSTRY 					= 11;
	const C_ADMIN_BUSINESS_SECTOR 			= 12;
	const C_ADMIN_SOFTWARE 					= 13;
	const C_MARKETING_VIEW_OTHERS 			= 14;
	const C_MARKETING_EDIT_OTHERS 			= 15;
	const C_SALES_VIEW_OTHERS 				= 16;
	const C_SALES_EDIT_OTHERS 				= 17;
	const C_ADMIN_DEPARTMENT 				= 18;
	const C_REPORT_CUSTOMER_CONTACT_LISTING	= 19;
	const C_CORPORATE_GIFT_LIST				= 20;
	const C_CORPORATE_GIFT_COMPANY_PROFILE	= 21;
	const C_CORPORATE_GIFT_MASTER			= 22;
	const C_REPORT_CORPORATE_GIFT_SUMMARY	= 23;
	const C_REPORT_CORPORATE_GIFT_INQUIRY	= 24;
	const C_REPORT_CORPORATE_GIFT_HISTORY	= 25;
	const C_REPORT_CONVERSATION				= 26;
	const C_CAMPAIGN_LIST					= 27;
	const C_CAMPAIGN_COMPANY_PROFILE		= 28;
	const C_CAMPAIGN_MASTER					= 29;
	const C_ADMIN_BUSINESS_NATURE 			= 30;
	const C_REPORT_ALL_ACCOUNT				= 31;

	
	private $admin = array() ;

	function __construct() {
		$this->initRights() ;
	}
	function __destruct() {
	}
	function getAdminRights() {
		return $this->admin ;
	}

	function setAdminRights($value) {
		$this->admin = $value ;
	}

	function toRights($data) {
		$this->initRights() ;
		if ($data == "")
			return ;

		$r = trim(Util::decryptString(pack("H*" , $data),"","")) ;
		//$r = $data ;
		if ($r == "")
			return ;
		$d = explode("|",$r) ;
		$cnt = count($d) ;
		if ($cnt > 0)
			$this->toAdminRight($d[0]) ;
		else
			return ;
	}

	function toString() {
		// Log::write("toString");
		$s = "" ;
		for ($i=0;$i<$this->C_NO_ADMIN;$i++) {
			$s .= $this->access2Hex($this->admin[$i]) ;
		}
		return bin2hex(Util::encryptString($s,"","")) ;
		//return $s ;
	}
	function initRights() {
		// Log::write("initRights");
		$r  = '00';
		for ($i=0 ;$i < $this->C_NO_ADMIN;$i++) {
			$this->admin[$i] = $this->hex2Access($r) ;
		}
	}
	private function toAdminRight($data) {
		if (trim($data) == "")
			return ;

		$len = strlen($data) ;
		if ($len < 2)
			return ;

		if ($len > $this->C_NO_ADMIN)
			$len = $this->C_NO_ADMIN ;
		$idx = 0 ;
		for ($i=0;$i<$len;$i++) {
			$this->admin[$i] = $this->hex2Access(substr($data,$idx,2)) ;
			$idx += 2;
		}
	}

	private function access2Hex($access) {
		$data = "00000000";
		if (!is_null($access) && count($access) > 0) {
			$cnt = count($access) ;
			if ($cnt > 8)
				$cnt = 8 ;
			for ($i=0;$i < $cnt ;$i++) {
				if ($access[$i])
					$data[$i] = "1";
			}
		}
		return str_pad(dechex(bindec($data)),2,"0",STR_PAD_LEFT) ;
	}

	private function hex2Access($hex) {
		$access = array();
		for ($i=0;$i<8;$i++) {
			$access[$i] = false ;
		}
		// Log::write("hex normal : " . $hex);
		$r = str_pad(decbin(hexdec($hex)),8,"0",STR_PAD_LEFT) ;
		for ($i=0;$i<8;$i++) {
			if ($r[$i] == "1")
				$access[$i] = true ;
		}
		return $access ;
	}
	function bin2Access($binary) {
		$access = array() ;
		for ($i=0;$i < 8;$i++) {
			$access[$i] = false ;
		}
		$len = strlen($binary) ;
		if ($len > 8)
			$len = 8 ;
		for ($i=0;$i < $len; $i++) {
			if ($binary[$i] == 1) {
				$access[$i] = true ;
			}
		}
		return $access ;
	}

	private function access2HexNonBinary($access)
	{
		$data = "00000000";

		if (!is_null($access) && count($access) > 0) {
			$cnt = count($access);
			if ($cnt > 8)
				$cnt = 8;
			for ($i = 0; $i < $cnt; $i++) {
				if (intval($access[$i]) > 0)
					$data[$i] = $access[$i];
			}
		}
		return str_pad(dechex(intval($data)), 2, "0", STR_PAD_LEFT);
	}

	private function hex2AccessNonBinary($hex) {
		$access = array();
		for ($i = 0; $i < 8; $i++) {
			$access[$i] = false;
		}
		// $r = str_pad(decbin(hexdec($hex)), 8, "0", STR_PAD_RIGHT);
		$r = str_pad($hex, 8, "0", STR_PAD_RIGHT);

		$access[0] = hexdec($hex);

		return $access;
	}
	function bin2AccessNonBinary($binary)
	{
		$binary = str_pad($binary, 8, "0", STR_PAD_LEFT);

		$access = array();
		// for ($i = 0; $i < 8; $i++) {
		// 	$access[$i] = false;
		// }
		$len = strlen($binary);
		if ($len > 8)
			$len = 8;

		for ($i = 0; $i < $len; $i++) {
			$access[$i] = $binary[$i];
		}

		return $access;
	}
}
?>