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

class Online_Category_Model extends CI_Model {

        var $tableName = 'tbl_online_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($onlineCatId) {  
        $this->db->where('online_cat_id',$onlineCatId);
        $query = $this->db->get($this->tableName);
        $result = $query->row_array();
        return $result;
    } 
    function getChildCategories($parentCatId) {  
        $this->db->where('parent_category != ',0);
          $this->db->join('tbl_province', $this->tableName . '.province_id=tbl_province.province_id');
        $query = $this->db->get($this->tableName);
        $result = $query->result_array();
        return $result;
    }
//    function getCategory($onlineCatId) {
//        $this->db->where('online_cat_id',$onlineCatId);       
//        $query = $this->db->get($this->tableName);
//        $result = $query->row_array();
//        return $result;
//    } 
    
    function add($online_cat_name,$online_cat_image) {        
            
            if($online_cat_image)
            $this->db->set('online_cat_image', $online_cat_image);
        
            $slug = $this->common_model->create_unique_slug_for_common($online_cat_name,$this->tableName);
            $this->db->set('slug',$slug);  
         if($this->input->post('parent_category'))
            $this->db->set('parent_category', $this->input->post('parent_category'));
            if($this->input->post('online_cat_name'))
            $this->db->set('online_cat_name', $this->input->post('online_cat_name'));
            if($this->input->post('online_cat_description'))
            $this->db->set('online_cat_description', $this->input->post('online_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($onlineCatId,$online_cat_image){         
            if($online_cat_image)
            $this->db->set('online_cat_image', $online_cat_image); 
            if($this->input->post('parent_category'))
            $this->db->set('parent_category', $this->input->post('parent_category'));
            if($this->input->post('online_cat_name'))
            $this->db->set('online_cat_name', $this->input->post('online_cat_name'));
            if($this->input->post('online_cat_description'))
            $this->db->set('online_cat_description', $this->input->post('online_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('online_cat_id',$onlineCatId);
            $query = $this->db->update($this->tableName);
            return $response;          
    }  
    
    public function updateStatus($onlineCatId,$status) {   
        $this->db->set('status',$status);
        $this->db->where('online_cat_id',$onlineCatId);
        $response = $this->db->update($this->tableName);
        return $response; 
    }
    
    public function delete($onlineCatId){        
        $this->db->where('online_cat_id',$onlineCatId);
        $response = $this->db->delete($this->tableName);
        return $response; 
    } 
}
