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

class Job_listings_model extends CI_Model {
    
    

    public function get_myjob_totalcount() {
        $sta = 'A';
        $user_id = $this->ion_auth->get_user_id();
        
        $this->db->where('status', $sta);
        $this->db->where('id =', $user_id);
        $num = $this->db->count_all_results('jobs_list');
        return $num;
    }	

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

        $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
        $sort_columns = array('company_name', 'position_name', 'job_type', 'city', 'state', 'country', 'closing_date','job_phone', 'job_email');
        $sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'position_name';
        $st = 'D';
        $user_id = $this->ion_auth->get_user_id();

        $this->db->limit($limit, $start);
        $query = $this->db->order_by($sort_by, $sort_order)->where('status !=', $st)->where('id =', $user_id)->get('jobs_list');

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

    public function add_myjob_listing($data) {
        
        $insert = $this->db->insert('jobs_list', $data);
        return $insert;
    }

    public function edit_myjob_listing($listing_id) { 
   
        
            
            $data = array(
                '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'),
				);
        
        
        $this->db->where('listing_id', $listing_id);
        $this->db->update('jobs_list', $data);
        return TRUE;
    }

    public function get_myjob_listing_data($listing_id) {
        $this->db->where('listing_id', $listing_id);
        $query = $this->db->get('jobs_list');
        return $query->row();
    }
    
    public function delete_myjob_listing($listing_id) {
        
        $data = array(
            'status' => 'D'
                );
        
        $this->db->where('listing_id', $listing_id);
        $this->db->update('jobs_list', $data);
        return TRUE;      
    }

    public function get_alljobs_totalcount() 
    {
        $sta = 'A';
        $this->db->where('status', $sta);
        $query = $this->db->get('jobs_list');

        if ($query->num_rows() > 0) 
        {
            $jobs = $query->result();
            $count = 0;
            foreach($jobs as $row)
            {
                $date1 = $row->closing_date;
                $date2 = date("Y-m-d");

                $diff = (strtotime($date1) - strtotime($date2));
                $diff = floor($diff);

                if($diff < 0)
                {
                    $count--;
                }

                $count++;

            }

            return $count;
        }
        else
        {
            return 0;
        }
    }

    public function get_adminjobs_totalcount() 
    {
        $num = $this->db->count_all_results('jobs_list');
        return $num;
    }
    
    public function get_alljobs_listings($limit, $start, $sort_by, $sort_order) {

        $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
        $sort_columns = array('company_name', 'position_name', 'job_type', 'city', 'state', 'country', 'closing_date','job_phone', 'job_email');
        $sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'position_name';
        $st = 'A';

        $this->db->limit($limit, $start);
        $this->db->where('status', $st);
        $query = $this->db->order_by($sort_by, $sort_order)->get('jobs_list');

        //Checks the date. If the current date is after expiration date, remove result from query.
        if ($query->num_rows() > 0) 
        {
            $jobs = $query->result();
            $count = 0;
            foreach($jobs as $row)
            {
                $date1 = $row->closing_date;
                $date2 = date("Y-m-d");

                $diff = (strtotime($date1) - strtotime($date2));
                $diff = floor($diff);

                if($diff < 0)
                {
                    unset($jobs[$count]);
                }

                $count++;

            }

            return $jobs;
        }
        return false;
    }

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

        $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
        $sort_columns = array('company_name', 'position_name', 'job_type', 'city', 'state', 'country', 'closing_date','job_phone', 'job_email');
        $sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'position_name';
        $st = 'A';

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

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

    public function search_alljobs_listings($part_c, $crit, $limit, $start, $sort_by, $sort_order) {

        $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
        $sort_columns = array('company_name', 'position_name', 'job_type', 'city', 'state', 'country', 'closing_date','job_phone', 'job_email');
        $sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'position_name';
        $st = 'D';

        $query = $this->db->select('
                 listing_id,
                 id,
                 status,
                 company_name,
                 position_name,
                 full_job_description,
                 job_type,
                 travel,
                 city,
                 state,
                 country,
                 website,
                 closing_date,
                 job_phone,
                 job_email             
                ')
                ->from('jobs_list')
                ->limit($limit, $start)
                ->order_by($sort_by, $sort_order)
                ->where('status !=', $st)
                ->like($part_c, $crit);

        $temp = $query->get();
        if ($temp->num_rows() > 0)
        {
            $jobs = $temp->result();
            $count = 0;
            foreach($jobs as $row)
            {
                $date1 = $row->closing_date;
                $date2 = date("Y-m-d");

                $diff = (strtotime($date1) - strtotime($date2));
                $diff = floor($diff);

                if($diff < 0)
                {
                    unset($jobs[$count]);
                }

                $count++;

            }

            $ret['rows'] = $jobs;

            return $ret;
        }
        else
        {
            $ret['rows'] = $query->get()->result();
            return $ret;
        }
    }

    public function search_adminjobs_listings($part_c, $crit, $limit, $start, $sort_by, $sort_order) {

        $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
        $sort_columns = array('company_name', 'position_name', 'job_type', 'city', 'state', 'country', 'closing_date','job_phone', 'job_email');
        $sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'position_name';

        $query = $this->db->select('
                 listing_id,
                 id,
                 status,
                 company_name,
                 position_name,
                 full_job_description,
                 job_type,
                 travel,
                 city,
                 state,
                 country,
                 website,
                 closing_date,
                 job_phone,
                 job_email             
                ')
                ->from('jobs_list')
                ->limit($limit, $start)
                ->order_by($sort_by, $sort_order)
                ->like($part_c, $crit);

        $ret['rows'] = $query->get()->result();

        return $ret;
    }

    public function search_alljobs_count($part_c, $crit) {
        $st = 'D';
        $query1 = $this->db->select('
                 listing_id,
                 id,
                 status,
                 company_name,
                 position_name,
                 full_job_description,
                 job_type,
                 travel,
                 city,
                 state,
                 country,
                 website,
                 closing_date,
                 job_phone,
                 job_email             
                ')
                ->from('jobs_list')
                ->where('status !=', $st)
                ->like($part_c, $crit);

        $temp = $query1->get();
        if ($temp->num_rows() > 0)
        {
            $jobs = $temp->result();
            $count = 0;
            foreach($jobs as $row)
            {
                $date1 = $row->closing_date;
                $date2 = date("Y-m-d");

                $diff = (strtotime($date1) - strtotime($date2));
                $diff = floor($diff);

                if($diff < 0)
                {
                    $count--;
                }

                $count++;

            }

            $ret1['num_rows'] = $count;

            return $ret1;
        }
        else
        {
            $ret1['num_rows'] = 0;
            return $ret1;
        }
    }

    public function search_adminjobs_count($part_c, $crit) {
        $query1 = $this->db->select('COUNT(*) as count', FALSE)
                ->from('jobs_list')
                ->like($part_c, $crit);
        $temp1 = $query1->get()->result();
        $ret1['num_rows'] = $temp1[0]->count;
        return $ret1;
    }
    
    public function get_alljobs_listing_data($listing_id) {
        $this->db->where('listing_id', $listing_id);
        $query = $this->db->get('jobs_list');
        return $query->row();
    }   
}
