<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Company_professions_model extends CI_Model {
        
    public function get_list_forform($company_id,$task_id=null){        
        $this->db->select('cp.*');
        $this->db->from('company_professions cp');
        if (is_numeric($company_id))
            $this->db->where('cp.company_id', $company_id);
        if (is_numeric($task_id)){
            $this->db->where('cp.id NOT IN  (SELECT DISTINCT t.profession_id FROM tasks_professions t WHERE t.task_id = '.$task_id.')');            
        }        
        $this->db->order_by('cp.profession_name asc');
        $list = $this->db->get()->result_array();        
        $options = array();
         foreach($list as $profession){
            $options[$profession['id']] = $profession['profession_name'];
        }
        return  $options;
    }

    public function get_list($company_id = null, $per_page = 10, $page = 1, $check_count = false) {
        $this->db->select('cp.*');
        $this->db->from('company_professions cp');
        if (is_numeric($company_id))
            $this->db->where('cp.company_id', $company_id);
        $this->db->order_by('cp.profession_name asc');

        if ($check_count)
            return $this->db->get()->num_rows();
        else {
            if ($per_page != '')
                $this->db->limit($per_page);
            if ($page != 0) {
                $offset = ($page == 1) ? 0 : ($page - 1) * $per_page;
                $this->db->offset($offset);
            }
            $result = $this->db->get()->result_array();
            return $result;
        }
    }

    public function get_profession($profession_id) {
        if (!is_numeric($profession_id))
            return null;
        $this->db->select('cp.*');
        $this->db->from('company_professions cp');
        $this->db->where('cp.id', $profession_id);
        $result = $this->db->get()->row_array();
        return $result;
    }

    public function get_companyid_byprofession($profession_id) {
        if (!is_numeric($profession_id))
            return null;
        $this->db->select('cp.company_id');
        $this->db->from('company_professions cp');
        $this->db->where('cp.id', $profession_id);
        $result = $this->db->get()->row_array();
        return $result['company_id'];
    }

    public function insert($company_id, $data) {
        if (!is_numeric($company_id))
            return false;
        $data['company_id'] = $company_id;
        $add = $this->db->insert('company_professions', $data);
        return $add;
    }

    public function update($profession_id, $data) {
        if (!is_numeric($profession_id))
            return false;
        $add = $this->db->update('company_professions', $data, array('id' => $profession_id));
        return $add;
    }

    public function delete($profession_id) {
        if (!is_numeric($profession_id))
            return false;
        $del = $this->db->delete('company_professions', array('id' => $profession_id));
        return $del;
    }

}
