<?php

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

class Block_model extends CI_Model {
	
	public function getListVoters($projectID, $validity = NULL) {
		$sql = "SELECT userID, name, noAhli, validity, (SELECT companyName FROM company WHERE company.companyID = user.companyID) companyName
						FROM user
						WHERE projectID = ".$projectID;
		$sql .= ($validity !== NULL) ? " AND validity = ".$validity : NULL;
		$sql .= " ORDER BY name ASC";
		$q = $this->db->query($sql);
		return ($q && ($r = $q->result())) ? $r : NULL;	//json_decode(json_encode($r),true) : NULL;
	}

    public function block_all() {
		$this->db->select('a.*, b.companyName');
		$this->db->where('a.validity', 1);
		$this->db->where('a.projectID', $this->session->userdata('projectID'));
		$this->db->from('user a');
		$this->db->join('company b', 'a.companyID = b.companyID', 'LEFT');
		$this->db->order_by("a.name", "asc");
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
    }
	
	public function block_filterbydatatablesandstatus($status, $valuedatatable5){
		$this->db->select('a.*, b.companyName');
		$this->db->select('if(a.validity > 0, "invalid", "valid") as blockstatus', false);
		if($status !== "all") $this->db->where('a.validity', $status);
		$this->db->where('a.projectID', $this->session->userdata('projectID'));
		$this->db->from('user a');
		$this->db->where($valuedatatable5);
		$this->db->join('company b', 'a.companyID = b.companyID', 'LEFT');
		$this->db->order_by("a.name", "asc");
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
	}
	
	public function block_byvalidity($status = NULL) {
		$this->db->select('a.*, b.companyName');
		$this->db->select('if(a.validity > 0, "invalid", "valid") as blockstatus', false);
		if($status !== NULL && $status !== "all") $this->db->where('a.validity', $status);
		$this->db->where('a.projectID', $this->session->userdata('projectID'));
		$this->db->from('user a');
		$this->db->join('company b', 'a.companyID = b.companyID', 'LEFT');
		$this->db->order_by("a.name", "asc");
		$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;
        }
    }
	
	

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

}
