<?php

class Cities_model extends CI_Model {

    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }

    function get_all_cities($id_cities=null){
        
        $this->db->select('*');
        
        if($this->session->userdata('ho')=='No'){
            $this->db->where('business_id', $this->session->userdata('businessid'));
        }
        
        if(null!=$id_cities){
            $this->db->where('id_cities', $id_cities);
        }
        
        $query = $this->db->get('cities');
        
        return $query->result_array();
        
    }
    
    function get_all_city_areas($city_id, $id_city_area){
        $this->db->select('*');
        $this->db->join('cities', 'city_areas.city_id=cities.id_cities');
        if(null!=$city_id){
            $this->db->where('city_id', $city_id);
        }        
        if(null!=$id_city_area){
            $this->db->where('id_city_areas', $id_city_area);
        }
        
        $query = $this->db->get('city_areas');
        
        return $query->result_array();
    }
    
    function update_city(){
        
        $data = array(
            'city' => $this->input->post('city')
            ,'province'=>$this->input->post('province')
            ,'country'=>$this->input->post('country')
            
        );
     
        $this->db->where('id_cities', $this->input->post('id_cities'));
        $this->db->update('cities', $data);
        
        return $this->db->affected_rows();
        
    }
    
    function add_city(){
        
        $data = array(
            'city' => $this->input->post('city')
            ,'province'=>$this->input->post('province')
            ,'country'=>$this->input->post('country')
            ,'business_id'=>$this->session->userdata('businessid')
        );
     
        $this->db->insert('cities', $data);
        
        return $this->db->insert_id();
        
    }
    
    
    function update_city_area(){
        
        $data = array(
            'city_id' => $this->input->post('city_id')
            ,'city' => $this->input->post('city')
            ,'city_area'=>$this->input->post('city_area')                  
        );
     
        $this->db->where('id_city_areas', $this->input->post('id_city_areas'));
        $this->db->update('city_areas', $data);
        
        return $this->db->affected_rows();
        
    }
    
    function add_city_area(){
        
        $data = array(
            'city' => $this->input->post('city')
            ,'city_area'=>$this->input->post('city_area')
            ,'city_id'=>$this->input->post('city_id')
        );
     
        $this->db->insert('city_areas', $data);
        
        return $this->db->insert_id();
        
    }
    
}