<?php

class Event extends CI_Controller {

    public function __construct() {
        parent::__construct();
        if (!$this->session->userdata('logged_in')) {
            $this->session->set_flashdata('noaccess', 'Sorry, you are not logged in');
            redirect('auth/login');
        }
    }

    //displays the home page to start events
    function display($listing_id) {
        $id = $this->ion_auth->get_user_id();
        $user = $this->ion_auth->user($id)->row();
         
        $this->load->model('event_model');
        $this->data['this_listing'] = $this->event_model->get_listing_data($listing_id);
        
        $this->data['subview'] = 'conference/event_home_view';
        if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin()) {
            //load the view passing $data values
            $this->load->view("layouts/main_admin", $this->data);
        } else if ($this->ion_auth->logged_in() && !$this->ion_auth->is_admin()) {
            //load the view passing $data values
            $this->load->view("layouts/main_logged_in", $this->data);
        }
        //$this->load->view('layouts/main', $this->data);

        //$this->load->view('calendarview',$data1);
    }

    //displays the add event page when add event button is clicked
    function displayAdd() {
        $this->data['subview'] = 'conference/event_add_view';
        $this->load->view('layouts/main', $this->data);
    }

    //displays the edit event page when edit event button is clicked
    function displayEdit($listing_id) {
        $this->load->model('event_model');
        $this->data['this_listing'] = $this->event_model->get_listing_data($listing_id);
        $this->data['subview'] = 'conference/event_edit_view';
        $this->load->view('layouts/main', $this->data);
    }

    //displays the display event page when display event button is clicked
    function displayEvents() {

//load the even_model to use it's methods
        $this->load->model('event_model');
        //put the query brought from the model into variable $data to pass it to the view
        $this->data = array(
            'query' => $this->event_model->event_getall(),
            'subview' => 'conference/conference_list'
        );

        //$this->data['subview'] = 'conference/event_display_view';

        if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin()) {
            //load the view passing $data values
            $this->load->view("layouts/main_admin", $this->data);
        } else if ($this->ion_auth->logged_in() && !$this->ion_auth->is_admin()) {
            //load the view passing $data values
            $this->load->view("layouts/main_logged_in", $this->data);
        }

    }

    function display_my_Events() {

//load the even_model to use it's methods
        $this->load->model('event_model');
        //get all the events that belong to the user with the id below
        $this->id = $this->ion_auth->get_user_id();
        //put the query brought from the model into variable $data to pass it to the view
        $this->data = array(
            'query' => $this->event_model->get_listingsID($this->id),
            'subview' => 'conference/my_conference_list'
        );

        //$this->data['subview'] = 'conference/event_display_view';

        if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin()) {
            //load the view passing $data values
            $this->load->view("layouts/main_admin", $this->data);
        } else if ($this->ion_auth->logged_in() && !$this->ion_auth->is_admin()) {
            //load the view passing $data values
            $this->load->view("layouts/main_logged_in", $this->data);
        }



        //$this->load->view('layouts/main', $this->data);
    }

    function add_listing() {
        //set up validation rules in the fields before making any changes in the database
        //makes sure that users enter valid information before submission
        $this->form_validation->set_rules('eventID', 'EventID', 'trim|integer|xss_clean');
        $this->form_validation->set_rules('startDate', 'startDate', 'trim|required|xss_clean');
        $this->form_validation->set_rules('endDate', 'endDate', 'trim|required|xss_clean');
        $this->form_validation->set_rules('title', 'Title', 'trim|required|xss_clean');
        $this->form_validation->set_rules('description', 'Description', 'trim|required|xss_clean');
        $this->form_validation->set_rules('country', 'Country', 'trim|required|xss_clean');
        $this->form_validation->set_rules('state', 'State', 'trim|required|xss_clean');
        $this->form_validation->set_rules('city', 'City', 'trim|required|xss_clean');
        $this->form_validation->set_rules('zipcode', 'Zipcode', 'trim|integer|required|xss_clean');
        $this->form_validation->set_rules('address', 'address', 'trim|required|xss_clean');
        



        if ($this->form_validation->run() == FALSE) {
            $this->data['subview'] = 'conference/event_add_view';
            //$this->load->view("layouts/main", $this->data);
            if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin()) {
                //load the view passing $data values
                $this->load->view("layouts/main_admin", $this->data);
            } else if ($this->ion_auth->logged_in() && !$this->ion_auth->is_admin()) {
                //load the view passing $data values
                $this->load->view("layouts/main_logged_in", $this->data);
            }
        } else {
            $this->load->model('event_model');
            
            $this->form_validation->set_rules('userfile', 'Brochure', 'callback__file_validation|xss_clean');
            
            if ($this->form_validation->run() == TRUE) {
            
            
                
            
            
            if ($this->event_model->add_event()) {
                $this->session->set_flashdata('event_added', 'Event has been added to your list.');
                redirect('event/displayEvents');
            }
            }
            
            else
            {
            $this->data['subview'] = 'conference/event_add_view';
            $this->load->view("layouts/main_logged_in", $this->data);

                
            }
            
        }

    }

    function update_listing($event_id) {
        //set up validation rules in the fields before making any changes in the database
        //makes sure that users enter valid information before submission
        $this->load->model('event_model');
        $this->data['this_listing'] = $this->event_model->get_listing_data($event_id);
        $this->form_validation->set_rules('eventID', 'EventID', 'trim|integer|xss_clean');
        $this->form_validation->set_rules('startDate', 'startDate', 'trim|required|xss_clean');
        $this->form_validation->set_rules('endDate', 'endDate', 'trim|required|xss_clean');
        $this->form_validation->set_rules('title', 'Title', 'trim|required|xss_clean');
        $this->form_validation->set_rules('description', 'Description', 'trim|required|xss_clean');
        $this->form_validation->set_rules('country', 'Country', 'trim|required|xss_clean');
        $this->form_validation->set_rules('state', 'State', 'trim|required|xss_clean');
        $this->form_validation->set_rules('city', 'City', 'trim|required|xss_clean');
        $this->form_validation->set_rules('zipcode', 'Zipcode', 'trim|integer|required|xss_clean');
        $this->form_validation->set_rules('address', 'address', 'trim|xss_clean');




        if ($this->form_validation->run() == false) {
            $this->data['listing_id'] = $this->event_model->get_listing_data($event_id);
            $this->data['subview'] = 'conference/event_edit_view';
            //$this->load->view("layouts/main", $this->data);
            if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin()) {
                //load the view passing $data values
                $this->load->view("layouts/main_admin", $this->data);
            } else if ($this->ion_auth->logged_in() && !$this->ion_auth->is_admin()) {
                //load the view passing $data values
                $this->load->view("layouts/main_logged_in", $this->data);
            }
        } else {
            $this->form_validation->set_rules('userfile', 'Brochure', 'callback__file_validation|xss_clean');
            
            if ($this->form_validation->run() == TRUE) {
            if ($this->event_model->edit_event($event_id)) {
                $this->session->set_flashdata('event_updated', " Event listing: $event_id has been updated");
                redirect('event/displayEvents');
            }
        }
        }
    }

    public function _file_validation() {
        $id = $this->ion_auth->get_user_id();
        $user = $this->ion_auth->user($id)->row();
        $em = $user->email;
        //set the path where the files uploaded will be copied
        $config['upload_path'] = './members/'.$em.'/conference_listings/';

        // set the filter image types
        $config['allowed_types'] = 'pdf|gif|jpg|png|docx|doc|txt|rtf';

        //overwrite image
        //        $config['overwrite'] = TRUE;
        //load the upload library
        $this->load->library('upload', $config);

        if (isset($_FILES['userfile']) && !empty($_FILES['userfile']['name'])) {
            
            if (!$this->upload->do_upload('userfile')) {
               
                $this->form_validation->set_message('_file_validation', 'Invalid Format. Type of format allowed: pdf,gif,jpg,png,docx,doc,txt,rtf.');
                return FALSE;
                
            } else {
                $this->data['upload_data'] = $this->upload->data();
                return TRUE;
            }
        } else {
            
            return TRUE;
        }
    }

    function search() {
        //load the even_model to use it's methods
        $this->load->model('event_model');
        //put the query brought from the model into variable $data to pass it to the view
        $this->data = array(
            'query' => $this->event_model->get_search(),
            'subview' => 'conference/conference_list'
        );
        $this->data['query'] = $this->event_model->get_search();

        //$this->data['subview'] = 'conference/event_display_view';
        $this->load->view('layouts/main', $this->data);
    }

    public function my_event_list($sort_by = 'id', $sort_order = 'asc') {

        $this->data['fields'] = array(
            'startDate' => 'StartDate:',
            'endDate' => 'EndDate:',
            'title' => 'Title:'
        );

        //$user_id = $this->session->userdata('user_id');
        $this->load->model('event_model');
        $per_pg = 10;
        $total = $this->event_model->get_totalcount();


        $config['base_url'] = base_url() . "event/my_conference_list/$sort_by/$sort_order";
        $config['total_rows'] = $total;
        $config['per_page'] = $per_pg;
        $config['uri_segment'] = 5;
        $choice = $config['total_rows'] / $config['per_page'];
        $config['num_links'] = round($choice);


        $this->pagination->initialize($config);
        $page = ($this->uri->segment(5)) ? $this->uri->segment(5) : 0;

        $this->data['listings'] = $this->event_model->get_listings($config['per_page'], $page, $sort_by, $sort_order);
        $this->data['links'] = $this->pagination->create_links();

        if ($this->data['links'] != '') {
            $this->data['start'] = (($this->pagination->cur_page - 1) * $this->pagination->per_page) + 1;
            $this->data['end'] = ($this->pagination->cur_page * $this->pagination->per_page);

            if ($this->data['end'] > $this->pagination->total_rows) {
                $this->data['end'] = $this->pagination->total_rows;
            }
        }

        $this->data['ptotal'] = $this->event_model->get_ptotalcount();
        $this->data['total'] = $this->event_model->get_totalcount();

        //load view and layout
        $this->data['subview'] = 'event/my_conference_list';
        $this->data['sort_by'] = $sort_by;
        $this->data['sort_order'] = $sort_order;
        $this->load->view('layouts/main', $this->data);
    }

    public function display_listing($listing_id) {

        $this->load->model('event_model');
        $this->data['this_listing'] = $this->event_model->get_listing_data($listing_id);
        $this->data['subview'] = 'conference/event_detail';
        if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin()) {
            //load the view passing $data values
            $this->load->view("layouts/main_admin", $this->data);
        } else if ($this->ion_auth->logged_in() && !$this->ion_auth->is_admin()) {
            //load the view passing $data values
            $this->load->view("layouts/main_logged_in", $this->data);
        }


        // $this->load->view("layouts/main", $this->data);   
    }
    public function display_listingM($listing_id) {

        $this->load->model('event_model');
        $this->data['this_listing'] = $this->event_model->get_listing_data($listing_id);
        $this->data['subview'] = 'conference/event_Mdetail';
        if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin()) {
            //load the view passing $data values
            $this->load->view("layouts/main_admin", $this->data);
        } else if ($this->ion_auth->logged_in() && !$this->ion_auth->is_admin()) {
            //load the view passing $data values
            $this->load->view("layouts/main_logged_in", $this->data);
        }


        // $this->load->view("layouts/main", $this->data);   
    }

    //delete event by id controller calls model to access the database and make deletion
    public function delete_listing($event_id) {
        if (!$this->ion_auth->logged_in() && !$this->ion_auth->is_admin())
		{
			//redirect them to the login page
			redirect('auth/login', 'refresh');
		}
                else
                {
        $this->load->model('event_model');
        $this->event_model->delete_event($event_id);
        $this->session->set_flashdata('event_deleted', "Event listing number: $event_id has been deleted");
        redirect('event/display_my_Events');
                }
    }
    
    public function searchEvents() {

        $this->form_validation->set_rules('column', 'Search By', 'callback__column_dropdown|xss_clean');
        $this->form_validation->set_rules('search_field', 'Criteria', 'trim|required|xss_clean');

        

        if ($this->form_validation->run() == FALSE) {
            $this->data['subview'] = 'event/c';
            
            if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin())
		{
	             //load the view passing $data values
                     $this->load->view("layouts/main_admin", $this->data);
		}
                else if ($this->ion_auth->logged_in() && !$this->ion_auth->is_admin())
		{
		     //load the view passing $data values
                     $this->load->view("layouts/main_logged_in", $this->data);
		}
        } 
        else 
            {

            $search_term = array(
                'column' => $this->input->post('column_dropdown'),
                'criteria' => $this->input->post('search_field')
            );

            $this->session->set_userdata($search_term);


            redirect('event/displayEvents');
            }
    }

}