<?php

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

class Projects_tasks_model extends CI_Model {

    public function get_list($project_id = null, $per_page = 10, $page = 1, $check_count = false) {
        $this->db->select('pt.*');
        $this->db->from('projects_tasks pt');
        $this->db->where('pt.project_id', $project_id);
        $this->db->order_by('pt.date_start 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_companyid_bytask($task_id) {
        if (!is_numeric($task_id))
            return null;
        $this->db->select('t.company_id');
        $this->db->from('projects_tasks t');
        $this->db->where('t.id', $task_id);
        $result = $this->db->get()->row_array();
        return $result['company_id'];
    }

    public function get_task($task_id) {
        if (!is_numeric($task_id))
            return null;
        $this->db->select('t.*');
        $this->db->from('projects_tasks t');
        $this->db->where('t.id', $task_id);
        $result = $this->db->get()->row_array();
        return $result;
    }

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

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

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

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

    public function get_task_professions($task_id) {
        if (!is_numeric($task_id))
            return null;
        $this->db->select('t.*,p.profession_name');
        $this->db->from('tasks_professions t , company_professions p');
        $this->db->where('t.task_id', $task_id);
        $this->db->where('t.profession_id = p.id');
        $result = $this->db->get()->result_array();
        return $result;
    }

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

}
