<?php

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

class Voter_model extends CI_Model {

    public function voter_all() {
		
		$this->lang->load("voter",$this->session->userdata('language'));
		
		$this->db->select('a.*, c.companyName');
		$this->db->select('if(sum(b.userID) > 0, false, true) as checkStatus', false);
		$this->db->select('if(a.status = 1, "'.$this->lang->line('voted').'", "'.$this->lang->line('noVote').'") as statusName', false);
		$this->db->from('user a');
		$this->db->where('a.projectID', $this->session->userdata('projectID'));
		$this->db->join('vote b', 'a.userID = b.userID', 'LEFT');
		$this->db->join('company c', 'a.companyID = c.companyID', 'LEFT');
		$this->db->group_by('a.userID');
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
    }
	
	public function voter_bystatus($status) {
		
		$this->lang->load("voter",$this->session->userdata('language'));
		
		$this->db->select('a.*, c.companyName');
		$this->db->select('if(sum(b.userID) > 0, false, true) as checkStatus', false);
		$this->db->select('if(a.status = 1, "'.$this->lang->line('voted').'", "'.$this->lang->line('noVote').'") as statusName', false);
		$this->db->from('user a');
		$this->db->where('a.status', $status);
		$this->db->where('a.projectID', $this->session->userdata('projectID'));
		$this->db->join('vote b', 'a.userID = b.userID', 'LEFT');
		$this->db->join('company c', 'a.companyID = c.companyID', 'LEFT');
		$this->db->group_by('a.userID');
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
    }

	public function voter_byprojectID($projectID) {
		
		$this->lang->load("voter",$this->session->userdata('language'));
		
		$this->db->select('a.*, c.companyName');
		$this->db->select('if(sum(b.userID) > 0, false, true) as checkStatus', false);
		$this->db->select('if(a.status = 1, "'.$this->lang->line('voted').'", "'.$this->lang->line('noVote').'") as statusName', false);
		$this->db->from('user a');
		$this->db->where('a.projectID', $projectID);
		$this->db->join('vote b', 'a.userID = b.userID', 'LEFT');
		$this->db->join('company c', 'a.companyID = c.companyID', 'LEFT');
		$this->db->group_by('a.userID');
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
    }
	
	public function voter_filterbydatatables($valuedatatable5) {
		
		$this->lang->load("voter",$this->session->userdata('language'));
		
		$this->db->select('a.*, c.companyName');
		$this->db->select('if(sum(b.userID) > 0, false, true) as checkStatus', false);
		$this->db->select('if(a.status = 1, "'.$this->lang->line('voted').'", "'.$this->lang->line('noVote').'") as statusName', false);
		$this->db->from('user a');
		$this->db->where($valuedatatable5);
		$this->db->where('a.projectID', $this->session->userdata('projectID'));
		$this->db->join('vote b', 'a.userID = b.userID', 'LEFT');
		$this->db->join('company c', 'a.companyID = c.companyID', 'LEFT');
		$this->db->group_by('a.userID');
		$query = $this->db->get();
		//echo $this->db->last_query();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
    }

	public function checkVoter($voterID){
		$q = $this->db->select('noAhli')
				 	  ->where('noAhli', $voterID)
				 	  ->get('user');

		return ($q->num_rows() >= 1) ? true : false;
	}

	public function getVoterbyVoterID($voterID){
		$q = $this->db->select('*')
				 	  ->where('projectID', $this->session->userdata('projectID'))
				 	  ->where('noAhli', $voterID)
				 	  ->get('user');

		return ($q->num_rows() > 0) ? $this->ObjToArray($q->result())[0] : null;
	}
	
	
	public function voter_filterbydatatablesandstatus($status, $valuedatatable5) {
		
		$this->lang->load("voter",$this->session->userdata('language'));
		
		$this->db->select('a.*, c.companyName');
		$this->db->select('if(sum(b.userID) > 0, false, true) as checkStatus', false);
		$this->db->select('if(a.status = 1, "'.$this->lang->line('voted').'", "'.$this->lang->line('noVote').'") as statusName', false);
		$this->db->from('user a');
		$this->db->where($valuedatatable5);
		$this->db->where('a.status', $status);
		$this->db->where('a.projectID', $this->session->userdata('projectID'));
		$this->db->join('vote b', 'a.userID = b.userID', 'LEFT');
		$this->db->join('company c', 'a.companyID = c.companyID', 'LEFT');
		$this->db->group_by('a.userID');
		$query = $this->db->get();
		//echo $this->db->last_query();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
    }
	
	
	public function company_all() {
		$this->db->where('projectID', $this->session->userdata('projectID'))
				->from('company');
		$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 TRUE;
		} else {
			return FALSE;
		}
	}
	
	public function update($tname, $ID, $data) {
        $this->db->where('userID', $ID);
        $query = $this->db->update($tname, $data);
        if ($query === TRUE) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
	
	public function delete($tname, $ID) {
        $this->db->where('userID', $ID);
        $query = $this->db->delete($tname);
        if ($query === TRUE) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
	
	
	public function ObjToArray($item) { return json_decode(json_encode($item),true); }
	
	
//***********************************
	

}
