<?php 

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

		if($username!='' && $password!=''){
			$sql = "SELECT * FROM tbl_user WHERE user_username = '".mysql_real_escape_string($username)."' AND user_password = '".$password."' AND payment_status = '".$type."' 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;
		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 addRecords($data){
		
		if($data && count($data) > 0){
			$sql ="INSERT INTO tbl_user(
				user_name,
				about_me,
				email_address,
				user_native_place,
				user_profile_created_by,
				user_username,
				user_cast,
				user_sub_cast,
				user_gotra,
				outercast_interest,
				interested_cast,
				user_profile_image,
				father_name,
				father_occupation,
				user_password,
				mother_name,
				mother_occupation,
				screen_name,
				marriage_time,
				gender,
				date_of_birth,
				marital_status,				
				have_children,
				chiledren_live_with,
				num_of_son,
				num_of_doughter,
				religion,
				community,
				mother_toungue,
				lifestyle,
				last_degree,
				degree_detail,				
				proffession,
				annual_incom,
				family_status,
				country,
				state,
				city,
				post_code,
				address,
				nationality,
				mobile_no,				
				phone_no,
				suitable_time,
				person_contact,
				height,
				weight,
				spects,
				star,
				mangal,
				horpscope_belive,
				blood_group,				
				complexion,
				eye_color,
				hair_color,
				eating_habit,
				drinking_habit,
				smoking_habit,
				special_case,
				father_alive,
				mother_alive,
				total_brothers,				
				total_sisters,
				married_brother,
				married_sister,				
				user_ip_address,
				user_created_date,
				age_in_year,
				user_last_login_date,
				payment_status,
				user_status
				)  VALUES (
				'".$data->user_name."',
				'".$data->about_me."',
				'".$data->email_address."',
				'".$data->user_native_place."',
				'".$data->user_profile_created_by."',
				'".$data->user_username."',
				'".$data->user_cast."',
				'".$data->user_sub_cast."',
				'".$data->user_gotra."',
				'".$data->outercast_interest."',
				'".$data->interested_cast."',
				'".$data->user_profile_image."',
				'".$data->father_name."',
				'".$data->father_occupation."',
				'".$data->user_password."',
				'".$data->mother_name."',
				'".$data->mother_occupation."',
				'".$data->screen_name."',
				'".$data->marriage_time."',
				'".$data->gender."',
				'".$data->date_of_birth."',
				'".$data->marital_status."',				
				'".$data->have_children."',
				'".$data->chiledren_live_with."',
				'".$data->num_of_son."',
				'".$data->num_of_doughter."',
				'".$data->religion."',
				'".$data->community."',
				'".$data->mother_toungue."',
				'".$data->lifestyle."',
				'".$data->last_degree."',
				'".$data->degree_detail."',				
				'".$data->proffession."',
				'".$data->annual_incom."',
				'".$data->family_status."',
				'".$data->country."',
				'".$data->state."',
				'".$data->city."',
				'".$data->post_code."',
				'".$data->address."',
				'".$data->nationality."',
				'".$data->mobile_no."',				
				'".$data->phone_no."',
				'".$data->suitable_time."',
				'".$data->person_contact."',
				'".$data->height."',
				'".$data->weight."',
				'".$data->spects."',
				'".$data->star."',
				'".$data->mangal."',
				'".$data->horpscope_belive."',
				'".$data->blood_group."',				
				'".$data->complexion."',
				'".$data->eye_color."',
				'".$data->hair_color."',
				'".$data->eating_habit."',
				'".$data->drinking_habit."',
				'".$data->smoking_habit."',
				'".$data->special_case."',
				'".$data->father_alive."',
				'".$data->mother_alive."',
				'".$data->total_brothers."',				
				'".$data->total_sisters."',
				'".$data->married_brother."',
				'".$data->married_sister."',				
				'".$_SERVER['REMOTE_ADDR']."',
				'".$data->user_created_date."',
				'".$data->age_in_year."',
				'',
				'".$data->payment_status."',
				'".$data->user_status."' )";
				
			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."',
			about_me = '".$data->about_me."',
			age_in_year = '".$data->age_in_year."',
			user_native_place = '".$data->user_native_place."',
			email_address = '".$data->email_address."',
			user_profile_created_by = '".$data->user_profile_created_by."',
			user_username = '".$data->user_username."',
			user_cast = '".$data->user_cast."',
			user_sub_cast = '".$data->user_sub_cast."',
			user_gotra = '".$data->user_gotra."',
			outercast_interest = '".$data->outercast_interest."',
			interested_cast = '".$data->interested_cast."',
			user_profile_image = '".$data->user_profile_image."',
			father_name = '".$data->father_name."',			
			father_occupation = '".$data->father_occupation."',
			mother_name = '".$data->mother_name."',
			mother_occupation = '".$data->mother_occupation."',
			screen_name = '".$data->screen_name."',
			marriage_time = '".$data->marriage_time."',
			gender = '".$data->gender."',
			marital_status = '".$data->marital_status."',
			date_of_birth = '".$data->date_of_birth."',
			have_children = '".$data->have_children."',			
			chiledren_live_with = '".$data->chiledren_live_with."',
			num_of_son = '".$data->num_of_son."',
			num_of_doughter = '".$data->num_of_doughter."',
			religion = '".$data->religion."',
			community = '".$data->community."',
			mother_toungue = '".$data->mother_toungue."',
			lifestyle = '".$data->lifestyle."',
			last_degree = '".$data->last_degree."',
			degree_detail = '".$data->degree_detail."',
			proffession = '".$data->proffession."',			
			annual_incom = '".$data->annual_incom."',
			family_status = '".$data->family_status."',
			country = '".$data->country."',
			state = '".$data->state."',
			city = '".$data->city."',
			post_code = '".$data->post_code."',
			address = '".$data->address."',
			nationality = '".$data->nationality."',
			mobile_no = '".$data->mobile_no."',
			phone_no = '".$data->phone_no."',			
			suitable_time = '".$data->suitable_time."',
			person_contact = '".$data->person_contact."',
			height = '".$data->height."',
			weight = '".$data->weight."',
			spects = '".$data->spects."',
			star = '".$data->star."',
			mangal = '".$data->mangal."',
			horpscope_belive = '".$data->horpscope_belive."',
			blood_group = '".$data->blood_group."',
			complexion = '".$data->complexion."',
			eye_color = '".$data->eye_color."',
			hair_color = '".$data->hair_color."',
			eating_habit = '".$data->eating_habit."',
			drinking_habit = '".$data->drinking_habit."',
			smoking_habit = '".$data->smoking_habit."',			
			special_case = '".$data->special_case."',
			father_alive = '".$data->father_alive."',
			mother_alive = '".$data->mother_alive."',
			total_brothers = '".$data->total_brothers."',
			total_sisters = '".$data->total_sisters."',
			married_brother = '".$data->married_brother."',
			married_sister = '".$data->married_sister."',	
			user_status = '".$data->user_status."'
			WHERE user_id = ".$data->user_id;
			
			return connection::execute($sql,'update');
		}
		return false;
	}
	
	function updateProfile($data){
		
		if($data && !is_null($data)){
			$sql ="UPDATE tbl_user SET 
			user_name = '".$data->user_name."',
			about_me = '".$data->about_me."',			
			age_in_year = '".$data->age_in_year."',
			user_native_place = '".$data->user_native_place."',
			email_address = '".$data->email_address."',
			user_profile_created_by = '".$data->user_profile_created_by."',
			user_username = '".$data->user_username."',
			user_cast = '".$data->user_cast."',
			user_sub_cast = '".$data->user_sub_cast."',
			user_gotra = '".$data->user_gotra."',
			outercast_interest = '".$data->outercast_interest."',
			interested_cast = '".$data->interested_cast."',
			father_name = '".$data->father_name."',			
			father_occupation = '".$data->father_occupation."',
			mother_name = '".$data->mother_name."',
			mother_occupation = '".$data->mother_occupation."',
			screen_name = '".$data->screen_name."',
			marriage_time = '".$data->marriage_time."',
			gender = '".$data->gender."',
			marital_status = '".$data->marital_status."',
			date_of_birth = '".$data->date_of_birth."',
			have_children = '".$data->have_children."',			
			chiledren_live_with = '".$data->chiledren_live_with."',
			num_of_son = '".$data->num_of_son."',
			num_of_doughter = '".$data->num_of_doughter."',
			religion = '".$data->religion."',
			community = '".$data->community."',
			mother_toungue = '".$data->mother_toungue."',
			lifestyle = '".$data->lifestyle."',
			last_degree = '".$data->last_degree."',
			degree_detail = '".$data->degree_detail."',
			proffession = '".$data->proffession."',			
			annual_incom = '".$data->annual_incom."',
			family_status = '".$data->family_status."',
			country = '".$data->country."',
			state = '".$data->state."',
			city = '".$data->city."',
			post_code = '".$data->post_code."',
			address = '".$data->address."',
			nationality = '".$data->nationality."',
			mobile_no = '".$data->mobile_no."',
			phone_no = '".$data->phone_no."',			
			suitable_time = '".$data->suitable_time."',
			person_contact = '".$data->person_contact."',
			height = '".$data->height."',
			weight = '".$data->weight."',
			spects = '".$data->spects."',
			star = '".$data->star."',
			mangal = '".$data->mangal."',
			horpscope_belive = '".$data->horpscope_belive."',
			blood_group = '".$data->blood_group."',
			complexion = '".$data->complexion."',
			eye_color = '".$data->eye_color."',
			hair_color = '".$data->hair_color."',
			eating_habit = '".$data->eating_habit."',
			drinking_habit = '".$data->drinking_habit."',
			smoking_habit = '".$data->smoking_habit."',			
			special_case = '".$data->special_case."',
			father_alive = '".$data->father_alive."',
			mother_alive = '".$data->mother_alive."',
			total_brothers = '".$data->total_brothers."',
			total_sisters = '".$data->total_sisters."',
			married_brother = '".$data->married_brother."',
			married_sister = '".$data->married_sister."'
			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');
	}

	
}

?>
