<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class category_model extends CI_Model {
    public function __construct() {
        parent::__construct();
        $this->category = TBL_CATEGORY;
        $this->item = TBL_ITEM;
    }
    
    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->category)->result();
    }
    
    public function active_category_count() {
        $this->db->where('status', 1);
        return $this->db->get($this->category)->num_rows();
    }
    
    public  function insert($data){  
        $this->db->insert($this->category, $data);  
        return $this->db->insert_id();
    }   
    
    public function update($data, $category_id){
        $this->db->where('id', $category_id);
        $this->db->update($this->category, $data);
        return $this->db->affected_rows();
    }
    
    public function get_category_by_id($category_id) {
        $this->db->select('cat.*, CASE cat.status WHEN 1 THEN "Active" ELSE "Inactive" END as status_str',FALSE);
        $this->db->where('cat.id', $category_id);
        return $this->db->get($this->category . ' as cat')->row();
    }
    
    public function category_exists_in_item($id ) {
        $this->db->where('category_id', trim($id));
        $this->db->where('status !=', 2);
        if($this->db->get($this->item)->num_rows())
            return 1;
        else 
            return 0;
    }
   
    public function category_name_exists($category_name = '', $id = '') {
        $this->db->where('category_name', trim($category_name));
        if($id != '')
            $this->db->where('id !=', $id);
       
        $this->db->where('status !=', 2);
        if($this->db->get($this->category)->num_rows())
            return 1;
        else 
            return 0;
    }
    
    public function category_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->category)->num_rows();
   }
   
  
}
?>