<?php 

class user {
	
	function checkLogin($username,$password){

		if($username!='' && $password!=''){
			$sql = "SELECT * FROM tbl_user WHERE email = '".$username."' AND designation = '".$password."' AND status = '1' ";
			return connection::execute($sql,'select');
		}
		return false;
	}

	function checkEmailExist($email,$id=0){
		$sql = "SELECT * FROM tbl_user WHERE email = '".$email."' AND id != $id ";
		return connection::execute($sql,'select');
		return false;
	}
	
	function checkUsernameExist($email,$id=0){
		$sql = "SELECT * FROM tbl_user WHERE email = '".$email."' AND id != $id ";
		return connection::execute($sql,'select');
		return false;
	}

	function updateLastLoginDate(){

		if(isset($_SESSION['user'])){
			$user  = unserialize($_SESSION['user']);
			$current_date = date('Y-m-d H:i:s');
			$sql = "UPDATE tbl_user SET user_last_login_date = '".$current_date."' WHERE id = ".$user->id;
			return connection::execute($sql,'update');
		
		}
	}
	
	function getAllRecords($all='*',$where='',$orderby='',$limit=''){
		
		$sql ="select $all FROM tbl_user WHERE 1=1 "; 
		if($where!=''){
			$sql .= " AND $where ";
		}
		
		if($orderby!=''){
			$sql .= " $orderby ";
		}
		
		if($limit!=''){
			$sql .= " $limit ";
		}
		//echo $sql;
		//exit;
		return connection::execute($sql,'select-all');
	}
	
	function getAllCount($where='',$group_by=''){
		
		$sql ="select COUNT(*) as total FROM tbl_user WHERE 1=1 "; 
		if($where!=''){
			$sql .= " AND $where ";
		}
		
		if($group_by!=''){
			$sql .= " GROUP BY  $group_by ";
		}

		$result = connection::execute($sql,'select');
		if($result && count($result)> 0){
			return $result->total;
		}
		return 0;
	}
	
	function fromId($id){
		
		if($id && !is_null($id)){
			$sql ="select * FROM tbl_user WHERE id = $id"; 
			return connection::execute($sql,'select');
		}
		return false;
	}
	
	function fromEmailAddress($email){
		
		if($email && $email!=''){
			$sql = "select * FROM tbl_user WHERE email = '".$email."' "; 
			return connection::execute($sql,'select');
		}
		return false;
	}
	

	function resetPassword($email,$password){

		if($email!='' && $password!=''){
			$sql = "UPDATE tbl_user SET designation = '$password' WHERE email = '$email' ";
			return connection::execute($sql,'update');
		
		}
	}
	
	function changePassword($data){

		$sql = "UPDATE tbl_user SET designation = '$data->designation' WHERE id = $data->id ";
		return connection::execute($sql,'update');
	}
	function changePasswordB($id,$password){

		$sql = "UPDATE tbl_user SET designation = '$password' WHERE id = $id ";
		return connection::execute($sql,'update');
	}

	
	function addRecords($data){
		
		if($data && count($data) > 0){
			$sql ="INSERT INTO tbl_user(
				name,
				mobile_no,
				email,
				status,
				created,
				where_did_hear,
				address,
				designation,
				type,
				emirates,
				nationality
				)  VALUES (
				'".$data->name."',
				'".$data->mobile_no."',
				'".$data->email."',
				'".$data->status."',
				'".$data->created."',
				'".$data->where_did_hear."',
				'".$data->address."',
				'".$data->designation."',
				'".$data->type."',
				'".$data->emirates."',
				'".$data->nationality."' )";
				
			return connection::execute($sql,'insert');
		}
		return false;
	}
	
	function updateRecords($data){
		
		if($data && !is_null($data)){
			$sql ="UPDATE tbl_user SET 
			name = '".$data->name."',
			address = '".$data->address."',
			mobile_no = '".$data->mobile_no."',
			email = '".$data->email."',
			status = '".$data->status."'
			WHERE id = ".$data->id;
			
			return connection::execute($sql,'update');
		}
		return false;
	}
	
	
	
	function changeStatus($id,$status){

		if($status==0){
			$new_status = 1;
		}else{
			$new_status = 0;
		}
		$sql ="UPDATE tbl_user SET status = '".$new_status."' WHERE id = ".$id;
		return connection::execute($sql,'update');
	}
	
	function changeHomepageStatus($id,$status){

		if($status==0){
			$new_status = 1;
		}else{
			$new_status = 0;
		}
		$sql ="UPDATE tbl_user SET on_home_page = '".$new_status."' WHERE id = ".$id;
		return connection::execute($sql,'update');
	}
	
	function changePaymentStatus($id,$status){

		if($status=='Free'){
			$new_status = 'Paid';
		}else{
			$new_status = 'Free';
		}
		$sql ="UPDATE tbl_user SET payment_status = '".$new_status."' WHERE id = ".$id;
		return connection::execute($sql,'update');
	}
	
	function deleteRecord($id){
		$sql ="DELETE FROM tbl_user  WHERE id = ".$id;
		return connection::execute($sql,'delete');
	}
	
	function allAction($act_val,$checkArr){
		
		if($act_val=='delete_all'  && $checkArr && count($checkArr) > 0){

				$sql ="DELETE FROM tbl_user  WHERE id IN(".implode(',',$checkArr).") " ;
				return connection::execute($sql,'delete');
				
		}else if($act_val=='activate_all' && $checkArr && count($checkArr) > 0){
				
				$sql ="UPDATE tbl_user SET status = '1'  WHERE id IN(".implode(',',$checkArr).") " ;
				return connection::execute($sql,'update');
		
		}else if($act_val=='inactivate_all' && $checkArr && count($checkArr) > 0){

				$sql ="UPDATE tbl_user SET status = '0'  WHERE id IN(".implode(',',$checkArr).") " ;
				return connection::execute($sql,'update');
		}
	}
	
	function refresh_session($id){
		
		$result = $this->fromId($id);
		if($result){
			$_SESSION['id'] = $result->id;
			$_SESSION['name'] = $result->user_fname;
			$_SESSION['email'] = $result->email;
		}
	}
	
	function getDetailFromEmail($email){
		
		if($email && $email!=''){
			$sql ="select * FROM tbl_user WHERE email = '".$email."' "; 
			return connection::execute($sql,'select');
		}
		return false;
	}
	function addUniqId($user_uniq_id, $id)
	{
		$sql ="UPDATE tbl_user SET user_uniq_id = '".$user_uniq_id."' WHERE id = ".$id;
		return connection::execute($sql,'update');
	}

	
}

?>
