<?php 

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

		if($username!='' && $password!=''){
			$sql = "SELECT * FROM tbl_admin WHERE admin_email_address = '".$username."' AND admin_password = '".$password."' AND admin_status = '1' ";
			return connection::execute($sql,'select');
			
		}
		return false;
	}
	
	function checkEmailExist($email,$admin_id=null){

		if($email!='' && $admin_id!=null){
			$sql = "SELECT * FROM tbl_admin WHERE admin_email_address = '".$email."' AND admin_id != $admin_id";
			return connection::execute($sql,'select');
		}else if($email!=''){
			$sql = "SELECT * FROM tbl_admin WHERE admin_email_address = '".$email."' ";
			return connection::execute($sql,'select');
		}
		return false;
	}
	
	function updateLastLoginDate(){

		if(isset($_SESSION['admin_id'])){
			$sql = "UPDATE tbl_admin SET admin_last_login_date = NOW(), admin_last_login_ip = '".$_SERVER['REMOTE_ADDR']."'   WHERE admin_id = ".$_SESSION['admin_id'];
			return connection::execute($sql,'update');
		
		}
	}
	function resetPassword($email_address,$password){

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

		$sql = "UPDATE tbl_admin SET admin_password = '$data->admin_password' WHERE admin_id = $data->admin_id ";
		return connection::execute($sql,'update');
	}
	
	function getAllRecords($all='*',$where='',$orderby='',$limit=''){
		
		$sql ="select $all FROM tbl_admin WHERE 1=1 "; 
		if($where!=''){
			$sql .= " AND $where ";
		}
		
		if($orderby!=''){
			$sql .= " $orderby ";
		}
		
		if($limit!=''){
			$sql .= " $limit ";
		}
		
		return connection::execute($sql,'select-all');
	}
	
	function fromId($id){
		
		if($id && !is_null($id)){
			$sql ="select * FROM tbl_admin WHERE admin_id = $id"; 
			return connection::execute($sql,'select');
		}
		return false;
	}
	
	function getDetailFromEmail($email){
		
		if($email && $email!=''){
			$sql ="select * FROM tbl_admin WHERE admin_email_address = '".$email."' "; 
			return connection::execute($sql,'select');
		}
		return false;
	}
	
	
	function addRecords($data){
		
		if($data && count($data) > 0){
			$sql ="INSERT INTO tbl_admin(admin_fname,admin_mname,admin_lname,admin_email_address,admin_password,admin_access,access_event,admin_created_date,admin_last_login_date,admin_status)  VALUES ('".$data->admin_fname."', '".$data->admin_mname."', '".$data->admin_lname."', '".$data->admin_email_address."', '".$data->admin_password."', '".$data->admin_access."','".$data->access_event."',NOW(),NOW(), '".$data->admin_status."' )";
			return connection::execute($sql,'insert');
		}
		return false;
	}
	
	function updateRecords($data){
		
		if($data && !is_null($data)){
			$sql ="UPDATE tbl_admin SET admin_fname = '".$data->admin_fname."',admin_mname = '".$data->admin_mname."', admin_lname = '".$data->admin_lname."',admin_email_address = '".$data->admin_email_address."',admin_access = '".$data->admin_access."', access_event = '".$data->access_event."', admin_status = '".$data->admin_status."' WHERE admin_id = ".$data->admin_id;
			return connection::execute($sql,'update');
		}
		return false;
	}
	
	function changeStatus($admin_id,$status){

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

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

				$sql ="UPDATE tbl_admin SET admin_status = '0'  WHERE admin_id IN(".implode(',',$checkArr).") " ;
				return connection::execute($sql,'update');
		}
	}
	
	function GetPageModule($pagename){
		$sql = "SELECT * FROM tbl_module_pages  p INNER JOIN tbl_modules m ON p.module_id = m.id WHERE p.page = '$pagename' ";
		return connection::execute($sql,'select');
	}
	
	function getAllModules(){
		$sql = "SELECT * FROM tbl_modules ";
		return connection::execute($sql,'select-all');
	}
	
	function refresh_session($admin_id){
		
		$result = $this->fromId($admin_id);
		if($result){
			$_SESSION['admin_id'] = $result->admin_id;
			$_SESSION['admin_fname'] = $result->admin_fname;
			$_SESSION['admin_mname'] = $result->admin_mname;
			$_SESSION['admin_lname'] = $result->admin_lname;
			$_SESSION['admin_user'] = $result->admin_email_address;
			$_SESSION['admin_access'] = $result->admin_access;
			$_SESSION['admin_cat_access'] = $result->admin_cat_access;
			
		}
	}
	
}

?>