<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class madappalli_model extends CI_Model {
    public function __construct() {
        parent::__construct();
        $this->madappalli = TBL_MADAPPALLI;
        $this->unit = TBL_UNIT;
    }
    
    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->madappalli)->result();
    }
    
    public function unit_list() {
        $this->db->where('status', 1);
        return $this->db->get($this->unit)->result();
    }
    
    public  function insert($data){  
        $this->db->insert($this->madappalli, $data);  
        return $this->db->insert_id();
    }   
    
    public function update($data, $id){
        $this->db->where('id', $id);
        $this->db->update($this->madappalli, $data);
        return $this->db->affected_rows();
    }
    
    public function get_by_id($id) {
        $this->db->select('m.*, u.unit_name, CASE m.status WHEN 1 THEN "Active" ELSE "Inactive" END as status_str',FALSE);
        $this->db->join($this->unit . ' as u','u.id = m.unit');
        $this->db->where('m.id',$id);
        return $this->db->get($this->madappalli . ' as m')->row();
    }
   
    public function prasadam_name_exists($id = '', $name) {
        $this->db->where('prasadam_name', $name);
        if($id != '')
            $this->db->where('id !=', $id);
       
        $this->db->where('status !=', 2);
        if($this->db->get($this->madappalli)->num_rows())
            return 1;
        else 
            return 0;
   }
   
   public function prasadam_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->madappalli)->num_rows();
   }
}
?>