<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Ticketno_sub_model extends MY_Model{
	function __construct(){
		parent::__construct();
		
		$this->table = "WEB_TICKETNO_SUB";
		$this->pk = "PKKEY";
		$this->record_sql_action = FALSE;
		$this->created_by = "";
		$this->modified_by = "";
		
//		$this->join .= "LEFT JOIN WEB_GROUP ON WEB_GROUP.PKKEY = <T>.USER_GROUP ";
	}
	
	function get_data($params=array()){
		if(! isset($params['select']) || $params['select'] == "") $params['select'] = "<T>.*";
		$sql = $this->sql_select($params);
		$query = $this->db->query($sql);
		return $query->result();
	}
	
	function data_count($params=array()){
		$params['select'] = "COUNT(1) as number_of_rows ";
		$params['sort'] = " ";
		$sql = $this->sql_select($params);
		$query = $this->db->query($sql);
		$data_array = $query->result();
		
		return count($data_array) > 0? $data_array[0]->number_of_rows:0;
	}
	
	function edit($post){
		$return = array('error' => "");
		
		foreach($post as $field=>$v){
			$data[$field] = NULL;
			if(strlen($v) > 0){
				if(in_array($field, array('DATE'))){
					$data[$field] = date("Y-m-d", strtotime($v));
				}
				elseif(in_array($field, array('PASSWORD'))){
					$data[$field] = sha1($v);
				}
				elseif(in_array($field, array($this->pk))){
					unset($data[$field]);
				}
				else{
					$data[$field] = $v;
				}
			}
		}
		
		if(isset($data)){
			$return['pk'] = -1;
			if(! isset($post[$this->pk])){
				$insert_return = $this->sql_insert($data);
				$return['pk'] = $insert_return['pk'];
				if(! $insert_return['success']){
					$return['error'] = "Create Failed!";
				}
			}
			else{
				if($this->sql_update($data, $post[$this->pk])){
					$return['pk'] = $post[$this->pk];
				}
				else{
					$return['error'] = "Update Failed!";
				}
			}
			
			if($return['error'] == ""){
				
			}
		}
		
		return $return;
	}
	
	function remove($post){
		return $this->sql_delete($post[$this->pk]);
	}
}