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

class Careers_listings extends CI_Controller 
{

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

    public $data = array('subview' => 'Opps, forgot to set a subview');

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

        $per_pg = 10;
        $total = $this->job_listings_model->get_myjob_totalcount();


        $config['base_url'] = base_url() . "careers_listings/my_jobs_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->job_listings_model->get_myjob_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->resume_listings_model->get_myresume_totalcount();
        $this->data['total'] = $this->job_listings_model->get_myjob_totalcount();

        //load view and layout
        $this->data['subview'] = 'careers_listings/my_jobs_list';
        $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())
        {
            $idz = $this->ion_auth->get_user_id();
            $user = $this->ion_auth->user($idz)->row();
            
            if($user->listing == 'Y')                  
            {
                $this->load->view("layouts/main_logged_in", $this->data);
            }
            else
            {
                $this->data['subview'] = 'listings/listing_disabled';
                $this->load->view("layouts/main_logged_in", $this->data);
            }
        }
    }
	
	public function my_resume_list($sort_by = 'id', $sort_order = 'asc') 
    {
        $per_pg = 10;
        $total = $this->resume_listings_model->get_myresume_totalcount();


        $config['base_url'] = base_url() . "careers_listings/my_resume_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->resume_listings_model->get_myresume_listing($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->resume_listings_model->get_myresume_totalcount();
        $this->data['total'] = $this->job_listings_model->get_myjob_totalcount();

        //load view and layout
        $this->data['subview'] = 'careers_listings/my_resume_list';
        $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())
        {
            $idz = $this->ion_auth->get_user_id();
            $user = $this->ion_auth->user($idz)->row();
            
            $this->load->view("layouts/main_logged_in", $this->data);
            
        }
    }
	
	 public function add_job_listing() 
     {
        
        $this->form_validation->set_rules('company_name', 'Company Name', 'trim|required|xss_clean');
        $this->form_validation->set_rules('position_name', 'Position', 'trim|required|xss_clean');
        $this->form_validation->set_rules('full_job_description', 'Full Job Description', 'trim|required|xss_clean');
        $this->form_validation->set_rules('job_type', 'Job Type', 'callback__jobtype_dropdown|xss_clean');
        $this->form_validation->set_rules('city', 'City', 'trim|required|xss_clean');
		$this->form_validation->set_rules('state', 'State', 'trim|required|xss_clean');
		$this->form_validation->set_rules('country', 'Country', 'trim|required|xss_clean');
        $this->form_validation->set_rules('travel', 'Travel Required', 'callback__travel_dropdown|xss_clean');
        $this->form_validation->set_rules('website', 'Company Website', 'trim|xss_clean');
        $this->form_validation->set_rules('closing_date', 'Offer Closing Date', 'callback__date_validation|trim|xss_clean');
        $this->form_validation->set_rules('job_phone', 'Phone Number', 'numeric|exact_length[10]|trim|xss_clean');
		$this->form_validation->set_rules('job_email', 'E-Mail', 'trim|required|xss_clean');
        
        $this->data['total'] = $this->job_listings_model->get_myjob_totalcount();
        $this->data['ptotal'] = $this->resume_listings_model->get_myresume_totalcount();
        
        if ($this->form_validation->run() == FALSE) 
        {
            $this->data['subview'] = 'careers_listings/add_job_listing';
            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())
            {
                $idz = $this->ion_auth->get_user_id();
                $user = $this->ion_auth->user($idz)->row();
            
                if($user->listing == 'Y')                  
                {
                    $this->load->view("layouts/main_logged_in", $this->data);
                }
                else
                {
                    $this->data['subview'] = 'listings/listing_disabled';
                    $this->load->view("layouts/main_logged_in", $this->data);
                }
            }
        } 
        else 
        {             
            $data = array(
            'status' => 'A',
            'id' => $this->ion_auth->get_user_id(),    
            'company_name' => $this->input->post('company_name'),
            'position_name' => $this->input->post('position_name'),
            'full_job_description' => $this->input->post('full_job_description'),
            'job_type' => $this->input->post('jobtype_dropdown'),
            'city' => $this->input->post('city'),
			'state' => $this->input->post('state'),
			'country' => $this->input->post('country'),
            'travel' => $this->input->post('travel_dropdown'),
            'website'  => $this->input->post('website'),
            'closing_date'  => $this->input->post('closing_date'),
            'job_phone'  => $this->input->post('job_phone'),
			'job_email'  => $this->input->post('job_email'),
			);

			if($this->job_listings_model->add_myjob_listing($data)) 
			{
				$this->session->set_flashdata('job_added', 'Job has been added to your list.');
				redirect('careers_listings/add_job_listing');
			}
            else
            {
                $this->data['subview'] = 'careers_listings/add_job_listing';
                $this->load->view("layouts/main_logged_in", $this->data);
            }	
        }        
    }
	
	public function add_resume_listing() 
    {
              
        $this->form_validation->set_rules('r_skills', 'Skills', 'trim|xss_clean');
        $this->form_validation->set_rules('r_job_history', 'Most Recent Job', 'trim|xss_clean');
        $this->form_validation->set_rules('education', 'Most Recent Degree', 'trim|xss_clean');
        $this->form_validation->set_rules('r_phone', 'Phone Number', 'numeric|exact_length[10]|trim|xss_clean');
		
        
        $this->data['total'] = $this->job_listings_model->get_myjob_totalcount();
        $this->data['ptotal'] = $this->resume_listings_model->get_myresume_totalcount();
        $hasResume = $this->resume_listings_model->get_myresume_totalcount();
        
        if ($this->form_validation->run() == FALSE) 
        {
            $this->data['subview'] = 'careers_listings/add_resume_listing';
            
            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())
            {
                $idz = $this->ion_auth->get_user_id();
                $user = $this->ion_auth->user($idz)->row();
            
                $this->load->view("layouts/main_logged_in", $this->data);
                
            }
        } 
        else 
            {
    			$this->form_validation->set_rules('r_filename', 'Resume File', 'callback__rfile_validation|xss_clean');
				
                if ($this->form_validation->run() == TRUE)
                {


                	$add_id = $this->ion_auth->get_user_id();
                	$add_r_user = $this->ion_auth->user($add_id)->row();

                    $data = array(
                    'status' => 'A',
                    'id' => $this->ion_auth->get_user_id(),    
                    'first_name' => $add_r_user->first_name,
    				'last_name' => $add_r_user->last_name,
                    'r_skills' => $this->input->post('r_skills'),
                    'r_job_history' => $this->input->post('r_job_history'),
                    'education' => $this->input->post('education'),
                    'r_phone' => $this->input->post('r_phone'),
    				'r_email' => $add_r_user->email,
                    'r_filename' => $this->data['upload_data']['file_name']
    				);
    				if($this->resume_listings_model->add_myresume_listing($data)) 
    				{
    					$this->session->set_flashdata('resume_added', 'Your resume has been added.');
    					redirect('careers_listings/my_resume_list');
    				}
                }

                else
                {
                    $this->data['subview'] = 'careers_listings/add_resume_listing';
                    $this->load->view("layouts/main_logged_in", $this->data);
                }
            }        
    }
	
	public function edit_job_listing($listing_id) 
	{
        
        $this->form_validation->set_rules('company_name', 'Company Name', 'trim|required|xss_clean');
        $this->form_validation->set_rules('position_name', 'Position', 'trim|required|xss_clean');
        $this->form_validation->set_rules('full_job_description', 'Full Job Description', 'trim|required|xss_clean');
        $this->form_validation->set_rules('job_type', 'Job Type', 'callback__jobtype_dropdown|xss_clean');
        $this->form_validation->set_rules('city', 'City', 'trim|required|xss_clean');
		$this->form_validation->set_rules('state', 'State', 'trim|required|xss_clean');
		$this->form_validation->set_rules('country', 'Country', 'trim|required|xss_clean');
        $this->form_validation->set_rules('travel', 'Travel Required', 'callback__travel_dropdown|xss_clean');
        $this->form_validation->set_rules('website', 'Company Website', 'trim|xss_clean');
        $this->form_validation->set_rules('closing_date', 'Offer Closing Date', 'trim|xss_clean');
        $this->form_validation->set_rules('job_phone', 'Phone Number', 'numeric|exact_length[10]|trim|xss_clean');
		$this->form_validation->set_rules('job_email', 'E-Mail', 'trim|required|xss_clean');
        
        $this->data['total'] = $this->job_listings_model->get_myjob_totalcount();
        $this->data['ptotal'] = $this->resume_listings_model->get_myresume_totalcount();
        
        
        if ($this->form_validation->run() == FALSE) 
		{
			$this->data['this_listing'] = $this->job_listings_model->get_myjob_listing_data($listing_id);   
			
			$this->data['subview'] = 'careers_listings/edit_job_listing';

            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())
            {
                $idz = $this->ion_auth->get_user_id();
                $user = $this->ion_auth->user($idz)->row();

                if($user->listing == 'Y')                  
                {
                    $this->load->view("layouts/main_logged_in", $this->data);
                }
                else
                {
                    $this->data['subview'] = 'listings/listing_disabled';
                    $this->load->view("layouts/main_logged_in", $this->data);
                }
                
            }
			     
		}
	
		else 
        {        
			if($this->job_listings_model->edit_myjob_listing($listing_id)) 
			{
				   $this->session->set_flashdata('job_updated', "Job listing: $listing_id has been updated");
				   redirect('careers_listings/my_jobs_list');
			}        
		}
    }
	
	public function edit_resume_listing($listing_id) 
    {
        $this->form_validation->set_rules('r_skills', 'Skills', 'trim|xss_clean');
        $this->form_validation->set_rules('r_job_history', 'Most Recent Job', 'trim|xss_clean');
        $this->form_validation->set_rules('education', 'Most Recent Degree', 'trim|xss_clean');
        $this->form_validation->set_rules('r_phone', 'Phone Number', 'numeric|exact_length[10]|trim|xss_clean');
        
        $this->data['total'] = $this->job_listings_model->get_myjob_totalcount();
        $this->data['ptotal'] = $this->resume_listings_model->get_myresume_totalcount();
        
        
        if ($this->form_validation->run() == FALSE) 
        {
            $this->data['this_listing'] = $this->resume_listings_model->get_myresume_listing_data($listing_id);   
            
            $this->data['subview'] = 'careers_listings/edit_resume_listing';
            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())
            {
                $idz = $this->ion_auth->get_user_id();
                $user = $this->ion_auth->user($idz)->row();
            
                $this->load->view("layouts/main_logged_in", $this->data);
    
            }        
        }
	
        else 
        {
        	$this->form_validation->set_rules('r_filename', 'Resume File', 'callback__rfile_edit_validation|xss_clean');

        	if ($this->form_validation->run() == TRUE)
            {
	            if($this->resume_listings_model->edit_myresume_listing($listing_id, $this->data['upload_data']['file_name'])) 
	            {
	                   $this->session->set_flashdata('resume_updated', "Your Resume listing has been updated.");
	                   redirect('careers_listings/my_resume_list');
	            }
	            else
	            {
	            	$this->data['subview'] = 'careers_listings/edit_resume_listing';
            		$this->load->view("layouts/main_logged_in", $this->data);
	            }  
	        }      
        }
    }
	
	public function delete_job_listing($listing_id) 
	{
        $this->job_listings_model->delete_myjob_listing($listing_id);
        $this->session->set_flashdata('Job_deleted', "Job listing number: $listing_id has been deleted");
        redirect('careers_listings/my_jobs_list');
    }
	
	public function delete_resume_listing($listing_id) 
	{
        $this->resume_listings_model->delete_myresume_listing($listing_id);
        $this->session->set_flashdata('Resume_deleted', "Resume listing number: $listing_id has been deleted");
        redirect('careers_listings/my_resume_list');
    }
	
	public function display_job_listing($listing_id) 
    {
        
        $this->data['this_listing'] = $this->job_listings_model->get_myjob_listing_data($listing_id);  
        
        $this->data['total'] = $this->job_listings_model->get_myjob_totalcount();
        $this->data['ptotal'] = $this->resume_listings_model->get_myresume_totalcount();
        
        $this->data['subview'] = 'careers_listings/display_job_listing';
        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())
            {
                $idz = $this->ion_auth->get_user_id();
                $user = $this->ion_auth->user($idz)->row();                 
        
                if($user->listing == 'Y')                  
                {
                    $this->load->view("layouts/main_logged_in", $this->data);
                }
                else
                {
                    $this->data['subview'] = 'listings/listing_disabled';
                    $this->load->view("layouts/main_logged_in", $this->data);
                }
            }   
    }
	
	public function display_resume_listing($listing_id) 
    {
        
        $this->data['this_listing'] = $this->resume_listings_model->get_myresume_listing_data($listing_id);  
        
        $this->data['total'] = $this->job_listings_model->get_myjob_totalcount();
        $this->data['ptotal'] = $this->resume_listings_model->get_myresume_totalcount();
        
        $this->data['subview'] = 'careers_listings/display_resume_listing';
        $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())
            {
                $idz = $this->ion_auth->get_user_id();
                $user = $this->ion_auth->user($idz)->row();                              
                
                $this->load->view("layouts/main_logged_in", $this->data);
                
            }
    }
	
	 public function _travel_dropdown() 
     {
        
        if($this->input->post('travel_dropdown') == '--Select Yes or No--') {
            $this->form_validation->set_message('_travel_dropdown', 'Select Yes or No on Travel Required');
            return FALSE;
        }
       
        
            $travel = array('--Select Yes or No--', 'Yes', 'No');
            
            $selected = $this->input->post('travel_dropdown');
            
           if (in_array($selected, $travel)) {
            return TRUE;
        } else {
            $this->form_validation->set_message('_travel_dropdown', 'Invalid Travel Input');
            return FALSE;
        }
        
    }
	
	public function _jobtype_dropdown() 
    {
        
        if($this->input->post('jobtype_dropdown') == '--Select a Job Type--') 
        {
            $this->form_validation->set_message('_jobtype_dropdown', 'Select a Job Type');
            return FALSE;
        }
       
        
        $jobtype = array('--Select a Job Type--', 'Full Time', 'Contract', 'Internship');
            
        $selected = $this->input->post('jobtype_dropdown');
            
        if (in_array($selected, $jobtype)) 
        {
            return TRUE;
        } 
        else 
        {
            $this->form_validation->set_message('_jobtype_dropdown', 'Invalid Job Type');
            return FALSE;
        }       
    }

    public function _rfile_validation() 
    {
        //set the path where the files uploaded will be copied
        $id = $this->ion_auth->get_user_id();
        $user = $this->ion_auth->user($id)->row();
        $em = $user->email;

        $config['upload_path'] = './members/'.$em.'/resume';

        // set the filter image types
        $config['allowed_types'] = 'pdf';
   
        //load the upload library
        $this->load->library('upload', $config);

        $myResume = $this->resume_listings_model->get_myresume_totalcount();

        if($myResume > 0)
        {
            $this->form_validation->set_message('_rfile_validation', 'You may only have one Resume Listing.');
            return false;
        }
        else
        {

            if (isset($_FILES['r_filename']) && !empty($_FILES['r_filename']['name'])) 
            {
                if (!is_dir('./members/'.$em.'/resume')) 
                {
                    mkdir('./members/'.$em.'/resume', 0777, true);
                }

                delete_files('./members/'.$em.'/resume');

                if (!$this->upload->do_upload('r_filename')) 
                {
                    $this->form_validation->set_message('_rfile_validation', 'Invalid format or file size. Only PDF files less than 128 megabytes may be uploaded.');
                    return FALSE;
                } 
                else 
                {
                    
                    $this->data['upload_data'] = $this->upload->data();
                    return TRUE;
                }
            }
            else
            {
            	 $this->form_validation->set_message('_rfile_validation', 'Resume File Field Required.'); 
                return FALSE;
            
            }
        }
    }

    public function _rfile_edit_validation()
    {
    	//set the path where the files uploaded will be copied
        $id = $this->ion_auth->get_user_id();
        $user = $this->ion_auth->user($id)->row();
        $em = $user->email;

        $config['upload_path'] = './members/'.$em.'/resume';

        // set the filter image types
        $config['allowed_types'] = 'pdf';
   
        //load the upload library
        $this->load->library('upload', $config);

        if (isset($_FILES['r_filename']) && !empty($_FILES['r_filename']['name'])) 
        {
            delete_files('./members/'.$em.'/resume');

            if (!$this->upload->do_upload('r_filename')) 
            {
                $this->form_validation->set_message('_rfile_edit_validation', 'Invalid format or file size. Only PDF files less than 128 megabytes may be uploaded.');
                return FALSE;
            } 
            else 
            {
                
                $this->data['upload_data'] = $this->upload->data();
                return TRUE;
            }
        }
        else
        { 
            return TRUE;        
        }
    }

    public function _date_validation() {
        $str = $_POST['closing_date'];

        if (empty($_POST['closing_date'])) {
            $this->form_validation->set_message('_date_validation', 'The Date Field is required.');
            return FALSE;
        } else {
            if (preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $str)) {

                return TRUE;
            } else {
                $this->form_validation->set_message('_date_validation', 'Invalid Date Format. Enter as Year-Month-Day');
                return FALSE;
            }
        }
    }
}

