<?php

class Event_model extends CI_Model {

    var $conf;

    // table name
    //private $calendar= 'calendar';

    function Mycal_model() {
        parent::__construct();
    }

    function add_event() {



        $data = array(
            'userID' => $this->ion_auth->get_user_id(),
            'eventID' => $this->input->post('eventID'),
            'startDate' => $this->input->post('startDate'),
            'endDate' => $this->input->post('endDate'),
            'title' => $this->input->post('title'),
            'description' => $this->input->post('description'),
            'country' => $this->input->post('country'),
            'state' => $this->input->post('state'),
            'city' => $this->input->post('city'),
            'zipcode' => $this->input->post('zipcode'),
            'address' => $this->input->post('address'),
            //'brochure' => $this->upload->file_name,
            'brochure' => $this->data['upload_data']['file_name']
            //'brochure_folder' => $this->data['folder'],
            //'brochure_path' => $this->data['upload_data']['full_path']
        );

        $insert = $this->db->insert('calendarT', $data);
        echo "Event has been added";
        return $insert;

    }

    public function edit_event($event_id) {

        $str = $this->upload->file_name;
        $a = $event_id;
        if ($str != '') {
            $data = array(
                'userID' => $this->ion_auth->get_user_id(),
                //'eventID' => $this->input->post('eventID'),
                'startDate' => $this->input->post('startDate'),
                'endDate' => $this->input->post('endDate'),
                'title' => $this->input->post('title'),
                'description' => $this->input->post('description'),
                'country' => $this->input->post('country'),
                'state' => $this->input->post('state'),
                'city' => $this->input->post('city'),
                'zipcode' => $this->input->post('zipcode'),
                'address' => $this->input->post('address'),
                //problems in the edit brochure function
                'brochure' => $this->data['upload_data']['file_name']
            );
        } else {

            $data = array(
                'userID' => $this->ion_auth->get_user_id(),
                //'eventID' => $a,
                'startDate' => $this->input->post('startDate'),
                'endDate' => $this->input->post('endDate'),
                'title' => $this->input->post('title'),
                'description' => $this->input->post('description'),
                'country' => $this->input->post('country'),
                'state' => $this->input->post('state'),
                'city' => $this->input->post('city'),
                'zipcode' => $this->input->post('zipcode'),
                'address' => $this->input->post('address'),
                //problem in the edit brochure function
                'brochure' => $this->data['upload_data']['file_name']
            );
        }

        $this->db->where('eventID', $event_id);
        $this->db->update('calendarT', $data);
        return TRUE;
    }

    public function delete_event($event_id) {
        $this->db->where('eventID', $event_id);
        $this->db->delete('calendarT');
        return;
    }

    function event_getall() {

        $this->load->database();
        $query = $this->db->get('calendarT');

        return $query->result();
    }

    public function get_listing_data($event_id) {
        $this->db->where('eventID', $event_id);
        $query = $this->db->get('calendarT');
        return $query->row();
    }

    public function get_totalcount() {
        return $this->db->count_all('calendarT');
    }

    public function get_ptotalcount() {
        return $this->db->count_all('calendarT');
    }

    public function get_listings($limit, $start, $sort_by, $sort_order) {

        $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
        $sort_columns = array('eventID', 'startDate', 'startTime', 'title');
        $sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'id';

        $this->db->limit($limit, $start);
        $query = $this->db->order_by($sort_by, $sort_order)->get('calendarT');

        if ($query->num_rows() > 0) {
            return $query->result();
        }
        return false;
    }

    function get_search() {
 
        $criteria = $this->input->post('search_field');
        $dropdown = $this->input->post('column_dropdown');
        $this->db->like($dropdown, $criteria);
        $query = $this->db->get('calendarT');

        return $query->result();
    }

    function get_listingsID($id) {


        $this->db->like('userID', $id);
        $query = $this->db->get('calendarT');

        return $query->result();
    }

}

