<?
 
class userClass extends utilityClass{

	private $olduserid;
	private $firstname;
	private $lastname;
	private $email;
	private $mobile;
	private $password;
	private $isbroker;
	private $licensedate;
	private $licenseid;
	private $registereddate;
	private $usertype;
	private $username;
	
	public function getUserDetails($username){
		$fieldArr = array('Id','FirstName','LastName','EmailAddress','MobileNo','IsBroker','BrokerDate','BrokerLicense','RegisteredDate','Status','UserType','Username');
		$condArr = array('Username'=>$username);
		$order = "Id DESC";
		$output = self::retrieveSingleQuery($fieldArr,"user",$condArr,$order);
		return $output;
	}
	
	public function checkUserAccess($username,$password){
		self::query("SELECT Username FROM user WHERE Username=:email AND Password =:pass AND Status=:status");
		self::bind(":email", $username); 
		self::bind(":status", 'active'); 
		self::bind(":pass", md5("community_".$password."_community")); 
		$row = self::resultset();
		$ctrrow = count($row);
		if($ctrrow==0){
			$output = false;
		}else{
			$output = true;
		}
		return $output;
	}
	
	public function saveUser(){
			$output							=	"";
			$mainArr 						=	array();
			$condArr 						=	array();
			$task 							=	self::getCurrentAction();
			$firstname						=	self::getFirstName();
			$lastname						=	self::getLastName();
			$email							=	self::getEmail();
			$mobile							=	self::getMobile();
			$password						=	self::getPassword();
			$password						=	"community_".$password."_community";
			$isbroker						=	self::getIsBroker();
			$licensedate					=	self::getLicenseDate();
			$licenseid						=	self::getLicenseId(); 
			$registereddate					=	self::getRegisteredDate(); 
			$status							=	self::getStatus();
			$usertype						= 	self::getUserType();
		if($task=="create"):
			$userId							= 	self::getNextId("Id","user");
		 	$mainArr['Id']					= 	$userId;
		endif;
			$mainArr['FirstName']			=	$firstname; 
			$mainArr['LastName']			=	$lastname; 
			$mainArr['EmailAddress']		=	$email; 
			$mainArr['MobileNo']			=	$mobile;
			$mainArr['Password']			=	md5($password); 
			$mainArr['IsBroker']			=	$isbroker; 
			$mainArr['BrokerDate']			=	$licensedate; 
			$mainArr['BrokerLicense']		=	$licenseid; 
			$mainArr['RegisteredDate']		=	$registereddate; 
			$mainArr['Status']				=	$status;
			$mainArr['UserType']			=	$usertype;
		
		//check if exist
		$firstnameExist = self::checkExistField("FirstName","user","first_name",$firstname);
		$lastnameExist 	= self::checkExistField("LastName","user","last_name",$lastname);
		$emailExist 	= self::checkExistField("EmailAddress","user","email",$email);
		$mobileExist 	= self::checkExistField("MobileNo","user","mobile",$mobile);
		
		if($emailExist==true){
			$output = "exist";
		}elseif($firstnameExist==true && $lastnameExist==true && $emailExist==true && $mobileExist==true){ 
			$output = "exist"; 
		}else{
			if($task=="create"):
				$mainArr['CreatedDate'] 		= self::getCreatedDate();
				$mainArr['CreatedBy'] 			= self::getCreatedBy();
				$output = self::insertSingleQuery($mainArr,"user");
			elseif($task=="edit"):
				$mainArr['UpdatedDate'] 		= self::getUpdatedDate();
				$mainArr['UpdatedBy'] 			= self::getUpdatedBy(); 
				$condArr['Id']					= self::getOldUserId();
				$output = self::updateQuery($mainArr,"user",$condArr);
			endif; 
		}
	 	return $output;
	}
 
	public function deleteUser($condArr){
		self::deleteQuery("user",$condArr);
	}
	
	//GETTER AND SETTER PROPERTIES
	public function setOldUserId($olduserid){	$this->olduserid = $olduserid;	}
	public function getOldUserId(){	return $this->olduserid;	}
	public function setFirstName($firstname){	$this->firstname = $firstname;	}
	public function getFirstName(){	return $this->firstname;	}
	public function setLastName($lastname){	$this->lastname = $lastname;	}
	public function getLastName(){	return $this->lastname;	}
	public function setEmail($email){	$this->email = $email;	}
	public function getEmail(){	return $this->email;	}
	public function setMobile($mobile){	$this->mobile = $mobile;	}
	public function getMobile(){	return $this->mobile;	}
	public function setPassword($password){	$this->password = $password;	}
	public function getPassword(){	return $this->password;	}
	public function setIsBroker($isbroker){	$this->isbroker = $isbroker;	}
	public function getIsBroker(){	return $this->isbroker;	}
	public function setLicenseDate($licensedate){	$this->licensedate = $licensedate;	}
	public function getLicenseDate(){	return $this->licensedate;	}
	public function setLicenseId($licenseid){	$this->licenseid = $licenseid;	}
	public function getLicenseId(){	return $this->licenseid;	}
	public function setRegisteredDate($registereddate){	$this->registereddate = $registereddate;	}
	public function getRegisteredDate(){	return $this->registereddate;	}
	public function setUserType($usertype){	$this->usertype = $usertype;	}
	public function getUserType(){	return $this->usertype;	} 
}