<?php
class classUserRegistration
{
	  
		private $userID=NULL;
		private $email=NULL;
		private $userName=NULL;
		private $password=NULL;
		private $firstName=NULL;
		private $lastName=NULL;
		private $phoneNumber=NULL;
		private $userRoleID=NULL;
		private $createdBy=NULL;
		
		########################################## set functions ##############################
	  
	  	function setUserID($userID)
		{
			if($userID!="")
			{
				if(classFilter::validateInteger($userID))
				{
					$this->userID = classFilter::addMySlashes($userID);
				}
				else
				{
					throw new MyException(100100);
				}
			}
			else
			{
				throw new MyException(100101);
			}
		}
		
		function setEmailAddress($email) 
		{
			if($email!="")
			{
				if(classFilter::validateEmail($email))
				{
					$this->email = classFilter::addMySlashes($email);
				}
				else
				{
					throw new MyException(100102);
				}
			}
			else
			{
				//throw new MyException(100103);
			}
		}
		
		function setUserName($userName) 
		{
			if($userName!="")
			{
				if(classFilter::validateUniqueName($userName))
				{
					$this->userName = classFilter::addMySlashes($userName);
				}
				else
				{
					throw new MyException(100104);
				}
			}
			else
			{
				throw new MyException(100105);
			}
		}
		
		function setPassword($password)
		{
			if($password!="")
			{
				if(strlen($password)>=8)
				{
					$this->password = $password;
				}
				else
				{
					throw new MyException(100106);
				}
			}
			else
			{
				throw new MyException(100107);
			}
		
		}
		
		function setFirstName($firstName) 
		{
			if($firstName!="")
			{
				if(classFilter::validateName($firstName))
				{
					$this->firstName = classFilter::addMySlashes($firstName);
				}
				else
				{
					throw new MyException(100108);
				}
			}
			else
			{
				throw new MyException(100109);
			}
		}
		
		function setLastName($lastName) 
		{
			if($lastName!="")
			{
				if(classFilter::validateName($lastName))
				{
					$this->lastName = classFilter::addMySlashes($lastName);
				}
				else
				{
					throw new MyException(100110);
				}
			}
			else
			{
				//throw new MyException(100111);
			}
		}
		
		function setPhoneNumber($userPhoneNumber) 
		{
			if($userPhoneNumber!="")
			{
				if(classFilter::validateNumber($userPhoneNumber))
				{
					$this->phoneNumber = classFilter::addMySlashes($userPhoneNumber);
				}
				else
				{
					throw new MyException(100112);
				}
			}
			else
			{
				throw new MyException(100113);
			}
		}
		
		function setUserRoleID($userRoleID) 
		{
			if($userRoleID!="")
			{
				$this->userRoleID = $userRoleID;
			}
			else
			{
				throw new MyException(100122);
			}
		}
		
		function setCreatedBy($createdBy)
		{
			$this->createdBy = $createdBy;
		}
		
		public function setOrganizationID($organizationID)
		{
			if($organizationID!="")
			{
				$this->organizationID=$organizationID;	
			}
			else
			{
				throw new MyException(100124);
				
			}
		}
		
		public function setLocationID($locationID)
		{
			if($locationID!="")
			{
				$this->locationID=$locationID;	
			}
			else
			{
				throw new MyException(100125);
				
			}
		}
		
	##################################################################### GET FUNCTIONS  ########################


		public function getUserID()
		{
			if($this->userID!="")
			{
				return $this->userID;
			}
			else
			{
				throw new MyException(100114);
			}
		}
	
		public function getEmail()
		{
			if($this->email!="")
			{
				return $this->email;
			}
			else
			{
				//throw new MyException(100115);
			}
		}
		
		public function getUserName()
		{
			if($this->userName!="")
			{
				return $this->userName;
			}
			else
			{
				throw new MyException(100116);
			}
		}
		
		public function getPassword()
		{
			if($this->password!="")
			{
				return $this->password;
			}
			else
			{
				throw new MyException(100117);
			}
		}
		
		public function getFirstName()
		{
			if($this->firstName!="")
			{
				return $this->firstName;
			}
			else
			{
				throw new MyException(100118);
			}
		}
		
		public function getLastName()
		{
			if($this->lastName!="")
			{
				return $this->lastName;
			}
			else
			{
				//throw new MyException(100119);
			}
		}
		
		public function getPhonenumber()
		{
			if($this->phoneNumber!="")
			{
				return $this->phoneNumber;
			}
			else
			{
				throw new MyException(100120);
			}
		}
		
		function getUserRoleID() 
		{
			if($this->userRoleID!="")
			{
				return $this->userRoleID;
			}
			else
			{
				throw new MyException(100123);
			}
		}
		
		function getCreatedBy()
		{
			return $this->createdBy;
		}
		
		public function getOrganizationID()
		{
			if($this->organizationID!="")
			{
				return $this->organizationID;	
			}
			else
			{
				throw new MyException(100126);
				
			}
		}
		
		public function getLocationID()
		{
			if($this->locationID!="")
			{
				return $this->locationID;	
			}
			else
			{
				throw new MyException(100127);
				
			}
		}
		
		
		
		##################################################################### Check FUNCTIONS  #####################
	  
		function checkUserNameRegistered($userName = NULL,$throwException = TRUE) 
		{
			$query = "SELECT user_name FROM users WHERE user_name = '$userName'";
			$queryOb = new classQueryGlobal;
			$array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			if(isset($array[0]['user_name']))
			{
				return 0;
			}
			else
			{
				return 1;
			}
		}
		
		##################################################################### Main FUNCTIONS  ###################
		
		function execute($exception=true) 
		{
			if($this->checkUserNameRegistered($this->getUserName(),false))
			{
					$first_name=$this->getFirstname();
					$last_name=$this->getLastname();
					$email=$this->getEmail();
					$user_name = $this->getUserName();
					$password=md5($this->getPassword());
					$phone=$this->getPhonenumber();
					$time_stamp=time();
					$created_by = $this->getCreatedBy();
					$userrole_id = $this->getUserRoleID();
					$org_id = $this->getOrganizationID();
					$location_id = $this->getLocationID();
					
					$query="INSERT INTO users 
					(
						fname,
						lname,
						email,
						user_name,
						password,
						phone,
						time_stamp,
						created_by,
						userrole_id,
						org_id,
						location_id,
						active
					) 
					VALUES 
					(
						'$first_name',
						'$last_name',
						'$email',
						'$user_name',
						'$password',
						'$phone',
						'$time_stamp',
						'$created_by',
						'$userrole_id',
						'$org_id',
						'$location_id',
						'1'
					)";
						
					$queryOb = new classQueryGlobal;
					$this->userID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
			}
			else
			{
				throw new MyException(100121);
			}
		}
		
		##################################################################### Main FUNCTIONS UPDATE  ###################
		
		function updateUser() 
		{
			
			$first_name=$this->getFirstname();
			$last_name=$this->getLastname();
			$email=$this->getEmail();
			$user_name = $this->getUserName();
			/*if(strlen($this->getPassword())==32)
			{
				$password=$this->getPassword();
			}
			else
			{
				$password=md5($this->getPassword());
			}*/
			$phone=$this->getPhonenumber();
			$time_stamp=time();
			$created_by = $this->getCreatedBy();
			$userrole_id = $this->getUserRoleID();
			$org_id = $this->getOrganizationID();
			$location_id = $this->getLocationID();
			
			$user_id = $this->getUserID();
			
			$query="UPDATE users SET
			
				fname = '$first_name',
				lname = '$last_name',
				email = '$email',
				user_name = '$user_name',
				phone = '$phone',
				time_stamp = '$time_stamp',
				created_by = '$created_by',
				userrole_id = '$userrole_id',
				org_id = '$org_id',
				location_id = '$location_id'
				WHERE user_id = '$user_id'";
			
				
			$queryOb = new classQueryGlobal;
			$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
			
		}
		
		function updateUserProfile() 
		{
			$first_name=$this->getFirstname();
			$last_name=$this->getLastname();
			$email=$this->getEmail();
			$phone=$this->getPhonenumber();
			$user_id = $this->getUserID();
			
			$query="UPDATE users SET
				fname = '$first_name',
				lname = '$last_name',
				email = '$email',
				phone = '$phone'
				WHERE user_id = '$user_id'";
			
			$queryOb = new classQueryGlobal;
			$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
			
		}
		
		function updateUserPwd() 
		{
			$password=md5($this->getPassword());
			$user_id = $this->getUserID();
			
			$query="UPDATE users SET password = '$password' WHERE user_id = '$user_id'";
			$queryOb = new classQueryGlobal;
			$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
			
		}
		
		##################################################################### Main FUNCTIONS RETRIEVE  #################
		
		function retrieveDefaultUserRoles()
		{
			$query = "SELECT * FROM user_role";
			$queryOb = new classQueryGlobal;
			$userRolesArray = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			if(isset($userRolesArray[0]))
			{
				return $userRolesArray;
			}
			
		}
		
		
	  
}
?>