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

class Careers_market_listings extends CI_Controller {

    public function __construct() 
    {
        parent::__construct();
        
        if (isset($_SERVER['HTTP_REFERER'])) {
            $this->session->set_userdata('previous_page', $_SERVER['HTTP_REFERER']);
        } else {
            $this->session->set_userdata('previous_page', base_url());
        }
    }

    public $data = array('subview' => 'Opps, forgot to set a subview');
	
	public function job_listings($sort_by = 'position_name', $sort_order = 'asc') 
    {

        $per_pg = 10;
        if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin())
        {
            $total = $this->job_listings_model->get_adminjobs_totalcount();
        }
        else
        {
            $total = $this->job_listings_model->get_alljobs_totalcount();
        }
        $ptotal = $this->resume_listings_model->get_allresume_totalcount();


        $config['base_url'] = base_url() . "careers_market_listings/job_listings/$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;

        if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin())
        {
            $this->data['jlistings'] = $this->job_listings_model->get_adminjobs_listings($config['per_page'], $page, $sort_by, $sort_order);
        }
        else
        {
            $this->data['jlistings'] = $this->job_listings_model->get_alljobs_listings($config['per_page'], $page, $sort_by, $sort_order);
        }
        $this->data['jlinks'] = $this->pagination->create_links();

        if ($this->data['jlinks'] != '') 
        {
            $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'] = $ptotal;
        $this->data['total'] = $total;
        
        $this->data['subview'] = 'careers_market_listings/job_listings';
        $this->data['sort_by'] = $sort_by;
        $this->data['sort_order'] = $sort_order;
        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);
        }
    }
	
	public function display_joblisting($listing_id) 
    {
        
        $this->data['this_jlisting'] = $this->job_listings_model->get_alljobs_listing_data($listing_id);

        if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin())
        {
            $total = $this->job_listings_model->get_adminjobs_totalcount();
        }
        else
        {
            $total = $this->job_listings_model->get_alljobs_totalcount();
        }
        $ptotal = $this->resume_listings_model->get_allresume_totalcount();  
        
        $this->data['total'] = $total;
        $this->data['ptotal'] = $ptotal;
        
        $this->data['subview'] = 'careers_market_listings/display_joblisting';
        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);
        }
    }

    public function search_job_listings() 
    {

        $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->ion_auth->logged_in() && $this->ion_auth->is_admin())
        {
            $total = $this->job_listings_model->get_adminjobs_totalcount();
        }
        else
        {
            $total = $this->job_listings_model->get_alljobs_totalcount();
        }
        $ptotal = $this->resume_listings_model->get_allresume_totalcount();

        $this->data['ptotal'] = $ptotal;
        $this->data['total'] = $total;

        $this->data['subview'] = 'careers_market_listings/search_job_listings';

        if ($this->form_validation->run() == FALSE) 
        {
            $this->data['subview'] = 'careers_market_listings/search_job_listings';
            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('careers_market_listings/job_search_results');
        }
    }

    public function job_search_results($sort_by = 'position_name', $sort_order = 'asc') 
    {

        $part_c = $this->session->userdata('column');
        $crit = $this->session->userdata('criteria');

        if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin())
        {
            $total = $this->job_listings_model->get_adminjobs_totalcount();
            $num_results = $this->job_listings_model->search_adminjobs_count($part_c, $crit);
        }
        else
        {
            $total = $this->job_listings_model->get_alljobs_totalcount();
            $num_results = $this->job_listings_model->search_alljobs_count($part_c, $crit);
        }
        $ptotal = $this->resume_listings_model->get_allresume_totalcount();


        $this->data['ptotal'] = $ptotal;
        $this->data['total'] = $total;



        $this->data['job_results_total'] = $num_results['num_rows'];
        $limit = 8;


        $config = array();
        $config['base_url'] = base_url() . "careers_market_listings/job_listings/$sort_by/$sort_order";
        $config['total_rows'] = $num_results['num_rows'];
        $config['per_page'] = $limit;
        $config['uri_segment'] = 8;
        $choice = $config['total_rows'] / $config['per_page'];
        $config['num_links'] = round($choice);

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

        if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin())
        {
            $results = $this->job_listings_model->search_adminjobs_listings($part_c, $crit, $config['per_page'], $ppage, $sort_by, $sort_order);
        }
        else
        {
            $results = $this->job_listings_model->search_alljobs_listings($part_c, $crit, $config['per_page'], $ppage, $sort_by, $sort_order);
        }

        $this->data['jlistings'] = $results['rows'];

        $this->data['jlinks'] = $this->pagination->create_links();


        if ($this->data['jlinks'] != '') 
        {
            $this->data['jstart'] = (($this->pagination->cur_page - 1) * $this->pagination->per_page) + 1;
            $this->data['jend'] = ($this->pagination->cur_page * $this->pagination->per_page);

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

        $this->data['subview'] = 'careers_market_listings/job_listings';
        $this->data['sort_by'] = $sort_by;
        $this->data['sort_order'] = $sort_order;
        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);
        }
    }
	
	public function resume_listings($sort_by = 'last_name', $sort_order = 'asc') 
    {       

        $per_pg = 10;
        if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin())
        {
            $total = $this->job_listings_model->get_adminjobs_totalcount();
        }
        else
        {
            $total = $this->job_listings_model->get_alljobs_totalcount();
        }
        $ptotal = $this->resume_listings_model->get_allresume_totalcount();


        $config['base_url'] = base_url() . "careers_market_listings/resume_listings/$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['rlistings'] = $this->resume_listings_model->get_allresume_listings($config['per_page'], $page, $sort_by, $sort_order);
        $this->data['rlinks'] = $this->pagination->create_links();

        if ($this->data['rlinks'] != '') 
        {
            $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'] = $ptotal;
        $this->data['total'] = $total;
        
        $this->data['subview'] = 'careers_market_listings/resume_listings';
        $this->data['sort_by'] = $sort_by;
        $this->data['sort_order'] = $sort_order;
        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);
        }
    }
	
	public function display_resumelisting($listing_id) 
    {
        
        $this->data['this_rlisting'] = $this->resume_listings_model->get_allresume_listing_data($listing_id);  
        
        $this->data['total'] = $this->job_listings_model->get_alljobs_totalcount();
        $this->data['ptotal'] = $this->resume_listings_model->get_allresume_totalcount();
        
        $this->data['subview'] = 'careers_market_listings/display_resumelisting';
        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);
        } 
    }
	
	public function search_resume_listings() 
    {

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

        if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin())
        {
            $total = $this->job_listings_model->get_adminjobs_totalcount();
        }
        else
        {
            $total = $this->job_listings_model->get_alljobs_totalcount();
        }
        $ptotal = $this->resume_listings_model->get_allresume_totalcount();

        $this->data['ptotal'] = $ptotal;
        $this->data['total'] = $total;

        $this->data['subview'] = 'careers_market_listings/search_resume_listings';

        if ($this->form_validation->run() == FALSE) 
        {
            $this->data['subview'] = 'careers_market_listings/search_resume_listings';
            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('rcolumn_dropdown'),
                'criteria' => $this->input->post('search_field')
            );

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


            redirect('careers_market_listings/resume_search_results');
        }
    }
	
	public function resume_search_results($sort_by = 'last_name', $sort_order = 'asc') 
    {

        $part_c = $this->session->userdata('column');
        $crit = $this->session->userdata('criteria');


        $this->data['ptotal'] = $this->resume_listings_model->get_allresume_totalcount();
        $this->data['total'] = $this->job_listings_model->get_alljobs_totalcount();



        $num_results = $this->resume_listings_model->search_allresume_count($part_c, $crit);
        $this->data['job_results_total'] = $num_results['num_rows'];
        $limit = 8;


        $config = array();
        $config['base_url'] = base_url() . "careers_market_listings/resume_listings/$sort_by/$sort_order";
        $config['total_rows'] = $num_results['num_rows'];
        $config['per_page'] = $limit;
        $config['uri_segment'] = 8;
        $choice = $config['total_rows'] / $config['per_page'];
        $config['num_links'] = round($choice);

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


        $results = $this->resume_listings_model->search_allresume_listings($part_c, $crit, $config['per_page'], $ppage, $sort_by, $sort_order);
        $this->data['rlistings'] = $results['rows'];

        $this->data['rlinks'] = $this->pagination->create_links();


        if ($this->data['rlinks'] != '') 
        {
            $this->data['rstart'] = (($this->pagination->cur_page - 1) * $this->pagination->per_page) + 1;
            $this->data['rend'] = ($this->pagination->cur_page * $this->pagination->per_page);

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

        $this->data['subview'] = 'careers_market_listings/resume_listings';
        $this->data['sort_by'] = $sort_by;
        $this->data['sort_order'] = $sort_order;
        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);
        }
    }

    public function _column_dropdown() 
    {

        if ($this->input->post('column_dropdown') == '--Select Column--') 
        {
            $this->form_validation->set_message('_column_dropdown', 'The Search By field is required.');
            return FALSE;
        }


        $columns = array('--Select Column--', 'company_name', 'position_name', 'job_type', 'city', 'state', 'country');

        $select = $this->input->post('column_dropdown');

        if (in_array($select, $columns)) {
            return TRUE;
        } else {
            $this->form_validation->set_message('_column_dropdown', 'Invalid Column');
            return FALSE;
        }
    }
	
	public function _rcolumn_dropdown() 
    {

        if ($this->input->post('rcolumn_dropdown') == '--Select Column--') 
        {
            $this->form_validation->set_message('_rcolumn_dropdown', 'The Search By field is required.');
            return FALSE;
        }


        $columns = array('--Select Column--', 'first_name', 'last_name', 'r_skills', 'r_email');

        $select = $this->input->post('rcolumn_dropdown');

        if (in_array($select, $columns)) 
        {
            return TRUE;
        } 
        else 
        {
            $this->form_validation->set_message('_rcolumn_dropdown', 'Invalid Column');
            return FALSE;
        }
    }
}