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

class Menu_Category_Model extends CI_Model {

        var $tableName = 'tbl_menu_category';  
  
    
    function getCategoryList() {  
        $location_id = $this->session->userdata('admin_location_id');
        if($location_id!='0'){
        $this->db->where($this->tableName .'.province_id',$location_id);
        }
        $query = $this->db->get($this->tableName);
        $result = $query->result_array();
        return $result;
    }  
    
    function getCategoryById($categoryId) {  
        $this->db->where('item_cat_id',$categoryId);
        $query = $this->db->get($this->tableName);
        $result = $query->row_array();
        return $result;
    } 
    
    function getCategory($categoryId,$catId) {
        $this->db->where('item_cat_id',$categoryId);
        $this->db->where('parent_id',$catId);
        $query = $this->db->get($this->tableName);
        $result = $query->row_array();
        return $result;
    } 
    
    function add($item_cat_name,$item_cat_image) {        
            
            if($item_cat_image)
            $this->db->set('item_cat_image', $item_cat_image);
        
            $slug = $this->common_model->create_unique_slug_for_common($item_cat_name,$this->tableName);
            $this->db->set('slug',$slug);  
        
            if($this->input->post('item_cat_name'))
            $this->db->set('item_cat_name', $this->input->post('item_cat_name'));
            if($this->input->post('item_cat_description'))
            $this->db->set('item_cat_description', $this->input->post('item_cat_description'));
            if($this->input->post('meta_title'))
            $this->db->set('meta_title', $this->input->post('meta_title'));          
            if($this->input->post('meta_keywords'))
            $this->db->set('meta_keywords', $this->input->post('meta_keywords'));
            if($this->input->post('meta_description'))
            $this->db->set('meta_description', $this->input->post('meta_description'));
            if ($this->input->post('country_id'))
            $this->db->set('country_id', $this->input->post('country_id'));        
            if ($this->input->post('province_id'))
            $this->db->set('province_id', $this->input->post('province_id'));            

            $query = $this->db->insert($this->tableName);
            $response=$this->db->insert_id();
        return $response;
    }   
     
     function update($menuCatId,$item_cat_image){         
            if($item_cat_image)
            $this->db->set('item_cat_image', $item_cat_image);  
            
                 if($this->input->post('item_cat_name'))
            $this->db->set('item_cat_name', $this->input->post('item_cat_name'));
            if($this->input->post('item_cat_description'))
            $this->db->set('item_cat_description', $this->input->post('item_cat_description'));
            if($this->input->post('meta_title'))
            $this->db->set('meta_title', $this->input->post('meta_title'));          
            if($this->input->post('meta_keywords'))
            $this->db->set('meta_keywords', $this->input->post('meta_keywords'));
            if($this->input->post('meta_description'))
            $this->db->set('meta_description', $this->input->post('meta_description'));
            if ($this->input->post('country_id'))
            $this->db->set('country_id', $this->input->post('country_id'));        
            if ($this->input->post('province_id'))
            $this->db->set('province_id', $this->input->post('province_id'));

           $this->db->where('item_cat_id',$menuCatId);
            $query = $this->db->update($this->tableName);
            return $response;          
    }  
    
    public function updateStatus($menuCatId,$status) {   
        $this->db->set('status',$status);
        $this->db->where('item_cat_id',$menuCatId);
        $response = $this->db->update($this->tableName);
        return $response; 
    }
    
    public function delete($menuCatId){        
        $this->db->where('item_cat_id',$menuCatId);
        $response = $this->db->delete($this->tableName);
        return $response; 
    } 
}
