<?php 

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

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

	function checkEmailExist($email,$user_id=0){
		$sql = "SELECT * FROM tbl_user WHERE email_address = '".$email."' AND user_id != $user_id ";
		return connection::execute($sql,'select');
		return false;
	}
	
	function checkUsernameExist($email,$user_id=0){
		$sql = "SELECT * FROM tbl_user WHERE user_username = '".$email."' AND user_id != $user_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 user_id = ".$user->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 user_id = $id"; 
			return connection::execute($sql,'select');
		}
		return false;
	}
	
	function fromEmailAddress($email){
		
		if($email && $email!=''){
			$sql = "select * FROM tbl_user WHERE email_address = '".$email."' "; 
			return connection::execute($sql,'select');
		}
		return false;
	}
	

	function resetPassword($email_address,$password){

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

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

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

	
	function addRecords($data){
		
		if($data && count($data) > 0){
			$sql ="INSERT INTO tbl_user(
				user_name,
				mobile_no,
				email_address,
				user_status,
				user_created_date,
				user_ip_address,
				profile_image,
				user_password
				)  VALUES (
				'".$data->user_name."',
				'".$data->mobile_no."',
				'".$data->email_address."',
				'".$data->user_status."',
				'".$data->user_created_date."',
				'".$data->user_ip_address."',
				'".$data->profile_image."',
				'".$data->user_password."' )";
			return connection::execute($sql,'insert');
		}
		return false;
	}
	
	function updateRecords($data){
		
		if($data && !is_null($data)){
			$sql ="UPDATE tbl_user SET 
			user_name = '".$data->user_name."',
			profile_image = '".$data->profile_image."',
			mobile_no = '".$data->mobile_no."',
			email_address = '".$data->email_address."',
			user_status = '".$data->user_status."'
			WHERE user_id = ".$data->user_id;
			
			return connection::execute($sql,'update');
		}
		return false;
	}
	
	
	
	function changeStatus($user_id,$status){

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

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

		if($status=='Free'){
			$new_status = 'Paid';
		}else{
			$new_status = 'Free';
		}
		$sql ="UPDATE tbl_user SET payment_status = '".$new_status."' WHERE user_id = ".$user_id;
		return connection::execute($sql,'update');
	}
	
	function deleteRecord($user_id){
		$sql ="DELETE FROM tbl_user  WHERE user_id = ".$user_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 user_id IN(".implode(',',$checkArr).") " ;
				return connection::execute($sql,'delete');
				
		}else if($act_val=='activate_all' && $checkArr && count($checkArr) > 0){
				
				$sql ="UPDATE tbl_user SET user_status = '1'  WHERE user_id IN(".implode(',',$checkArr).") " ;
				return connection::execute($sql,'update');
		
		}else if($act_val=='inactivate_all' && $checkArr && count($checkArr) > 0){

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

	
}

?>
