<?php

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

class Company_model extends CI_Model {

    public function company_all() {
		$this->db->select('a.*');
		$this->db->select('if(sum(c.userID) > 0, false, true) as checkcompany', false);
		$this->db->from('company a');
		$this->db->where('a.companyID <>', 0);
		$this->db->where('a.projectID', $this->session->userdata('projectID'));
		$this->db->join('user b', 'a.companyID = b.companyID', 'LEFT');
		$this->db->join('vote c', 'b.userID = c.userID', 'LEFT');
		$this->db->group_by('companyID');
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
    }
	
	
	public function company_filterbydatatables($valuedatatable5){
		$this->db->select('a.*');
		$this->db->select('if(sum(c.userID) > 0, false, true) as checkcompany', false);
		$this->db->from('company a');
		$this->db->where($valuedatatable5);
		$this->db->where('a.companyID <>', 0);
		$this->db->where('a.projectID', $this->session->userdata('projectID'));
		$this->db->join('user b', 'a.companyID = b.companyID', 'LEFT');
		$this->db->join('vote c', 'b.userID = c.userID', 'LEFT');
		$this->db->group_by('companyID');
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }	
	}

	public function insert($tname, $data){
		$sql = $this->db->insert_string($tname, $data);
		$query = $this->db->query($sql);
		if($query === TRUE){
			return $this->db->insert_id();
		} else {
			return NULL;
		}
	}
	
	public function update($tname, $ID, $data) {
        $this->db->where('companyID', $ID);
        $query = $this->db->update($tname, $data);
        if ($query === TRUE) {
            return $ID;
        } else {
            return NULL;
        }
    }
	
	public function delete($tname, $ID) {
        $this->db->where('companyID', $ID);
        $query = $this->db->delete($tname);
        if ($query === TRUE) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
	
	

	
	
//***********************************
	

}
