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

class Candidatemain extends CI_Model {
	
	public function __construct() { parent::__construct(); }
	
	public function saveCandidate($data) {
		$sql = "SELECT COUNT(1) CT FROM candidate WHERE candidateID = ".$data['candidateID']." AND projectID = ".$data['projectID'];
		$q = $this->db->query($sql);
		
		if($ct = ($q && ($r = $q->result()) && $r[0] && $r[0]->CT) ? TRUE : FALSE) {
			$sql = "UPDATE candidate
								SET
									candidateName = '".(addslashes(trim($data['candidateName'])))."',
									positionID = ".$data['positionID'].",
									companyID = ".$data['companyID']."
								WHERE candidateID = ".$data['candidateID']."
								AND projectID = ".$data['projectID'];
			return ($this->db->query($sql)) ? $data['candidateID'] : 0;
		} else {
			$sql = "INSERT INTO candidate (
								projectID, candidateName, positionID, companyID
							) VALUES (
								".($data['projectID']).",
								'".(addslashes(trim($data['candidateName'])))."',
								".($data['positionID']).",
								".($data['companyID'])."
							)";
							
			return ($this->db->query($sql)) ? $this->db->insert_id() : 0;
		}
	}
	
	public function saveImage($candidate, $project, $file) {
		$sql = "UPDATE candidate SET candidate_picture = '".($file)."' WHERE candidateID = ".$candidate." AND projectID = ".$project;
		return ($this->db->query($sql)) ? TRUE : FALSE;
	}
	
	public function resetImage($candidate, $project) {
		$sql = "UPDATE candidate SET candidate_picture = NULL WHERE candidateID = ".$candidate." AND projectID = ".$project;
		return ($this->db->query($sql)) ? TRUE : FALSE;
	}
	
	public function ObjToArray($item) { return json_decode(json_encode($item),true); }
	
}































