<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class devotee_model extends CI_Model {
    public function __construct() {
        parent::__construct();
        $this->devotee = TBL_BD;
        $this->star = TBL_STAR;    
        $this->devotee_mem = TBL_BDI;
        $this->sms = TBL_SMS_TYPES;        
    }
    
    public function list_all() {
        $this->db->select('*, CASE status WHEN 1 THEN "Active" ELSE "Inactive" END as status_str',FALSE);
        $this->db->where('status !=', 2);
        $this->db->order_by('id', 'desc');
        return $this->db->get($this->devotee)->result();
    }
    
    public function get_by_id($id) {
        $this->db->select('d.*, s.name as star_name, s.tamil_name, CASE d.status WHEN 1 THEN "Active" ELSE "Inactive" END as status_str',FALSE);
        $this->db->join($this->star . ' as s','s.id = d.bd_star_id','left');
        $this->db->where('d.id',$id);
        return $this->db->get($this->devotee . ' as d')->row();
    }
    
    public function get_members_by_devoteee_id($id) {
        $this->db->select('d.*, s.name as star_name, s.tamil_name, CASE d.status WHEN 1 THEN "Active" ELSE "Inactive" END as status_str',FALSE);
        $this->db->join($this->star . ' as s','s.id = d.bdi_star_id','left');
        $this->db->where('d.bd_id',$id);
        $this->db->where('d.status !=', 2);
        return $this->db->get($this->devotee_mem . ' as d')->result();
    }
    
    public function update($data, $id){
        $this->db->where('id', $id);
        $this->db->update($this->devotee, $data);
        return $this->db->affected_rows();
    }
    
    public function get_sms_types($ids) {
        $this->db->select('*, GROUP_CONCAT(DISTINCT s.sms_type_name SEPARATOR ",") as sms_name',FALSE);
        $this->db->where_in('s.id',$ids);
        return $this->db->get($this->sms . ' as s')->row();
    }
    
    /* public  function insert($data){  
        $this->db->insert($this->devotee, $data);  
        return $this->db->insert_id();
    }
   
    public function devotee_name_exists($id = '', $name) {
        $this->db->where('name', $name);
        if($id != '')
            $this->db->where('id !=', $id);
       
        $this->db->where('status !=', 2);
        if($this->db->get($this->devotee)->num_rows())
            return 1;
        else 
            return 0;
   }
   
   public function devotee_url_key_exists($url_key, $id) {
        if (!empty($id)) {
            $this->db->where('id !=', $id);
        }
        $this->db->where('url_key', $url_key);
        return $this->db->get($this->devotee)->num_rows();
   } */
}
?>