<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class contact_model extends CI_Model 
{

	function __construct()
    {
        parent::__construct();
        
    }
    function allcontact_details(){ 
        $this->db->select('c.categoryid,c.name,c.address,c.city,c.mobile,c.email,c.id,cat.name as catname');
        $this->db->from('contact as c');
        $this->db->join('category as cat','cat.id=c.categoryid','LEFT');  
        $this->db->where('c.status','1');
        $this->db->where('c.is_delete','0');
        $this->db->order_by("c.name", "asc");
        $query=$this->db->get();
        return $query->result_array();
    }
    function addcontact($data){
        if($data['name']!=''){
            $name = $this->contact_model->check_already_contact($data['name'],$data['address'],$data['categoryid']);
            if($name>0){
                $return = array();
                $return['msg']='alreadyexist';
                $return['error']='none';
            }else{
               $this->db->insert('contact', $data);
               $return = array();
               $return['msg']=$this->db->insert_id();
               $return['error']='none';
            }
        }   
        return $return;
    }
    function check_already_contact($name,$address,$categoryid){
        $this->db->select('name');
        $this->db->from('contact');
        $this->db->where('name', $name);
        $this->db->where('address', $address);
        $this->db->where('categoryid', $categoryid);
        $query = $this->db->get();
        $numr =$query->num_rows();
        return $numr;
    }
    function editcontact($data){
        $this->db->update("contact", $data, array('id' => $data['id']));
        return $this->db->affected_rows();
    }
    function single_contact_details($contactid){ 
        $this->db->select('*');
        $this->db->from('contact');
        $this->db->where('status','1');
        $this->db->where('is_delete','0');
        $this->db->where("id", $contactid);
        $query=$this->db->get();
        return $query->row_array();
    }
    function contact_details($categoryid){
        $this->db->select('cat.name as catname,c.name,c.address,c.city,c.mobile,c.email,c.id,c.categoryid');
        $this->db->from('contact as c');
        $this->db->join('category as cat','cat.id=c.categoryid','LEFT');  
        $this->db->where('c.status','1');
        $this->db->where('c.is_delete','0');
        //$this->db->where('c.categoryid',$categoryid);
        //$setcol= $categoryid.',';
        $this->db->where('c.categoryid LIKE', '%'.$categoryid.'%');
        $this->db->order_by("cat.name", "asc");
        $this->db->order_by("c.name", "asc");
        $query=$this->db->get();
        return $query->result_array();
    }
    function duplicat_details($name='',$categoryid='',$address=''){
        $this->db->select('cat.name as catname');
        $this->db->from('contact as c');
        $this->db->join('category as cat','cat.id=c.categoryid','LEFT');  
        $this->db->where('c.status','1');
        $this->db->where('c.is_delete','0');
        if($name!=''){
            $this->db->where('c.name',$name); 
        }
        if($categoryid!=''){
            $this->db->where('c.categoryid',$categoryid);
        }
        if($address!=''){
            $this->db->where('c.address',$address);
        }
        $this->db->order_by("cat.name", "asc");
        $query=$this->db->get();
        return $query->result_array();
    }
    function getcontact($keyword){ 
        $this->db->select('name');
        $this->db->from('contact');  
        $this->db->where('status','1');
        $this->db->where('is_delete','0');
        $this->db->like('name', $keyword);
        $this->db->group_by("name");
        $query = $this->db->get();
        if($query->num_rows() > 0){
          foreach ($query->result_array() as $row){
            $row_set[] = htmlentities(stripslashes($row['name'])); //build an array
          }
          echo json_encode($row_set); //format the array into json data
        }
    }
    function get_contact_details($data){ 
        $this->db->select('*');
        $this->db->from('contact');
        $this->db->where('status','1');
        $this->db->where('is_delete','0');
        if($data['name']!=''){
          $this->db->where("name", $data['name']);  
        }
        $query=$this->db->get();
        return $query->row_array();
    }
    function record_count($categoryid,$name) {
        $this->db->where('status','1');
        $this->db->where('is_delete','0');
        if($categoryid!=''){
            //$this->db->where('categoryid',$categoryid);
            $this->db->where('categoryid LIKE', '%'.$categoryid.'%');
        }
        if($name!=''){
            $this->db->where("`name` LIKE '%$name%'"); 
        }
        return $this->db->count_all_results("contact");
    }
    function fetch_contact($limit,$start,$categoryid,$name){ 
        $this->db->select('c.categoryid,c.name,c.address,c.city,c.mobile,c.email,c.id,cat.name as catname');
        $this->db->from('contact as c');
        $this->db->join('category as cat','cat.id=c.categoryid','LEFT');  
        $this->db->where('c.status','1');
        $this->db->where('c.is_delete','0');
        if($categoryid!=''){
        	//$this->db->where('c.categoryid',$categoryid);
            $this->db->where('c.categoryid LIKE', '%'.$categoryid.'%');
        }
        if($name!=''){
            $this->db->where("c.name LIKE '%$name%'"); 
        }
        // $this->db->group_by("c.name");
        // $this->db->group_by("c.address");
        $this->db->order_by("c.name", "asc");
        $this->db->limit($limit,$start);
        $query=$this->db->get();
        return $query->result_array();
      
    }
    function allcontact_details_withgroupby(){ 
        $this->db->select('c.categoryid,c.name,c.address,c.city,c.mobile,c.email,c.id');
        $this->db->from('contact as c');
        $this->db->where('c.status','1');
        $this->db->where('c.is_delete','0');
        // $this->db->group_by("c.name");
        // $this->db->group_by("c.address");
        $this->db->order_by("c.name", "asc");
        $query=$this->db->get();
        return $query->result_array();
    }
    function get_category($category){ 
        $this->db->select('name');
        $this->db->from('category');
        if($category!=''){
            $ids = explode(',', $category);
            $this->db->where_in('id', $ids );
        }
        $query=$this->db->get();
        return $query->result_array();
    }
    function all_is_multiple(){ 
        $this->db->select('c.categoryid,c.name,c.address,c.city,c.mobile,c.email,c.id');
        $this->db->from('contact as c');
        $this->db->where('c.status','1');
        $this->db->where('c.is_delete','0');
        $this->db->where('c.is_multiple','1');
        $this->db->order_by("c.name", "asc");
        $query=$this->db->get();
        return $query->result_array();
    }
    function contact_full_details($categoryid,$contactid){ 
        $this->db->select('c.categoryid,c.name,c.address,c.city,c.mobile,c.email,c.id');
        $this->db->from('contact as c');
        $this->db->where('c.status','1');
        $this->db->where('c.is_delete','0');
        // if($categoryid!=''){
        //     $this->db->where_in('c.categoryid',$categoryid);
        // }
        if($contactid!=''){
            $this->db->where_in('c.id',$contactid);
        }
        $this->db->order_by("c.categoryid", "asc");
        $this->db->order_by("c.name", "asc");
        $query=$this->db->get();
        return $query->result_array();
    }
    function categorycontact_details($categoryid){
        $this->db->select('name,address,city,mobile,email,id,categoryid');
        $this->db->from('contact');
        $this->db->where('status','1');
        $this->db->where('is_delete','0');
        $this->db->where('categoryid LIKE', '%'.$categoryid.'%');
        $this->db->order_by("name", "asc");
        $query=$this->db->get();
        //echo $this->db->last_query();exit;
        return $query->result_array();
    }
    
    
       
}
?>
