<?php

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

class Contactmain extends CI_Model {
	function __construct() {
		parent::__construct();
	}
	
	public function InsertContact($data, $user) {		
		$sql = "INSERT INTO contact_main ( ";		
			$keys = array_keys($data);		
			foreach($keys as $k=>$key) { $sql .= ($k) ? ", ".$key : $key; }		
		$sql .= ", createdDT, createdBy) VALUES (";
			$vals = array_values($data);		
			foreach($vals as $v=>$val) { $sql .= ($v) ? ", ".(($val) ? "'".$val."'" : (($val===0)?"0":"NULL")) : (($val) ? "'".$val."'" : (($value===0)?"0":"NULL")); }
		$sql .= ", NOW(), ".$user.")";
		
		if($b = ($this->db->query($sql)) ? $this->db->insert_ID() : 0) {
			$ticket = "CT".(sprintf("%05d", $b));			
			$sql = "UPDATE contact_main
								SET ticket = '".$ticket."',
										updatedDT = NOW(),
										updatedBy = ".$user."
								WHERE ID = ".$b;
			$this->db->query($sql);
		}
		return ($b) ? $b : 0;
	}
	
	public function getContactTicket($id) {
		$sql = "SELECT ticket FROM contact_main WHERE contact_main.ID = ".$id;						
		$q = $this->db->query($sql);
		return ($q && ($r = $q->result()) && $r[0]) ? strtoupper($r[0]->ticket) : NULL;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	public function ObjToArray($obj) {
		return ($obj) ? json_decode(json_encode($obj),true) : NULL;
	}
}
?>
