<?php
defined('BASEPATH') OR exit('No direct script access allowed');
defined('DIR_SEPARATOR') || define('DIR_SEPARATOR', '/');

class Importmain extends CI_Model {
	
	public function __construct() { parent::__construct(); }
	
	public function getVoterProject($v, $idonly = TRUE) {
		$sql = "SELECT project, (SELECT title FROM project WHERE project.userID = user.project) projectTitle FROM user WHERE userID = ".$v;
		$q = $this->db->query($sql);
		return ($q && ($r = $q->result()) && $r[0]) ? (($idonly) ? $r[0]->project : $this->ObjToArray($r[0])) : NULL;
	}
	
	public function updateProjectVoterFile($project, $path) {
		$sql = "UPDATE project SET importfile = '".($path)."' WHERE ID = ".$project;
		return ($q = $this->db->query($sql)) ? TRUE : FALSE;
	}
	
	public function getUsedIDVoters($project, $request) {
		$sql = "SELECT noAhli
						FROM user
						WHERE (validity IS NULL OR validity = 0)
						AND projectID =  ".$project."
						AND LOWER(noAhli) IN ('".(implode("', '",$request))."')";
						
		$q = $this->db->query($sql);
		$return = NULL;
		if($result = ($q && ($r = $q->result())) ? $this->ObjToArray($r) : NULL) {
			foreach($result as $key=>$value) {
				$return[$key] = strtolower($value['noAhli']);
			}
		}
		return $return;
	}
	
	public function checkAuthIDExist($project, $noAhli) {
		$sql = "SELECT COUNT(1) CT
						FROM user
						WHERE (validity IS NULL OR validity = 0)
						AND projectID =  ".$project."
						AND LOWER(noAhli) = '".(strtolower($noAhli))."'";
		$q = $this->db->query($sql);
		return ($q && ($r = $q->result()) && $r[0] && $r[0]->CT) ? TRUE : FALSE;
	}
	
	public function clearVoter($project) {
		$sql = "DELETE FROM vote
						WHERE vote.projectID = ".$project."
						AND vote.userID IN ( SELECT user.userID FROM user WHERE user.projectID = ".$project." )";
		$this->db->query($sql);
		
		$sql = "DELETE FROM user WHERE projectID = ".$project;
		return ($this->db->query($sql)) ? TRUE : FALSE;
	}
	
	public function saveVoter($data) {
		$project = $data['project'];
		$name = (trim($data['name'])) ? addslashes(trim(str_replace("'","",str_replace('"','',$data['name'])))) : 'ANONYM'.(rand(0,1000));
		$noAhli = addslashes(trim(str_replace("'","",str_replace('"','',$data['authid']))));
		$password = md5($data['authkey']);
		
		$sql = "SELECT COUNT(1) CT
						FROM user
						WHERE (validity IS NULL OR validity = 0)
						AND projectID =  ".$project."
						AND LOWER(noAhli) = '".(strtolower($noAhli))."'";
		$q = $this->db->query($sql);
		
		if($q && ($r = $q->result()) && $r[0] && $r[0]->CT) {
			$sql = "UPDATE user
								SET
									name = '".$name."',
									noAhli = '".$noAhli."',
									userPassword = '".$password."',
									status = 0,
									validity = 0
								WHERE projectID = ".$project."
								AND LOWER(noAhli) = '".(strtolower($noAhli))."'";
			return ($this->db->query($sql)) ? TRUE : FALSE;
		} else {
			$sql = "INSERT INTO user (
								projectID, name,
								noAhli, userPassword,
								status, validity
							) VALUES (
								".$project.",
								'".$name."',
								'".$noAhli."',
								'".$password."',
								0,
								0
							)";
			return ($this->db->query($sql)) ? $this->db->insert_id() : 0;
		}
	}
	
	public function ObjToArray($item) { return json_decode(json_encode($item),true); }
	
}































