<?php 
class Broker_model extends CI_Model {
	
	public function _consruct(){
		parent::_construct();
 	}

 	public function addBroker($broker_data = array()){
 		if(empty($broker_data)){
 			return 0;
 		}
 		$emailChk = $this->db->get_where('brokers',array('broker_email'=>$broker_data['broker_email'],'status !='=>'2'));
 		if(!empty($emailChk) && $emailChk->num_rows() > 0){
 			return 2;
 		}
 		$phoneChk = $this->db->get_where('brokers',array('broker_phone'=>$broker_data['broker_phone'],'status !='=>'2'));
 		if(!empty($phoneChk) && $phoneChk->num_rows() > 0){
 			return 3;
 		}
 		$status = $this->db->insert('brokers',$broker_data);
		return ($status)?1:0;
 	}

 	function getBroker($broker_id = ''){
 		$cond = (!empty($broker_id))?" AND broker_id = '$broker_id'":"";

		$result = $this->db->query("SELECT * FROM brokers WHERE status IN (0,1) $cond");
		if(empty($result)){
			return;
		}
		return (empty($broker_id))?$result->result():$result->row();
 	}

 	function changeStatus($broker_id = '', $status = '0'){
 		if(empty($broker_id)){
 			return 0;
 		}
 		$status = $this->db->update('brokers',array('status'=>$status), array('broker_id'=>$broker_id));
 		return $status;
 	}

 	function updateBroker($broker_id = '', $broker_data = array()){
 		if(empty($broker_id) || empty($broker_data)){
 			return 0;
 		}
		$emailChk = $this->db->get_where('brokers',array('broker_email'=>$broker_data['broker_email'],'status !='=>'2','broker_id !='=>$broker_id));
 		if(!empty($emailChk) && $emailChk->num_rows() > 0){
 			return 2;
 		}
 		$phoneChk = $this->db->get_where('brokers',array('broker_phone'=>$broker_data['broker_phone'],'status !='=>'2','broker_id !='=>$broker_id));
 		if(!empty($phoneChk) && $phoneChk->num_rows() > 0){
 			return 3;
 		}
 		$status = $this->db->update('brokers',$broker_data,array('broker_id'=>$broker_id));
		return ($status)?1:0;
 	}

}
?>