<?php
	class Hindi_Model extends CI_Model{

		protected $_table_name = '';
		protected $_primary_key = 'id';
		protected $_primary_filter = 'intval';
		protected $_order_by = '';
		public $rules = array();
		protected $_timestamps = FALSE;
		


		public function __construct(){
			parent::__construct();
			error_reporting(0);
		}

		public function array_from_post($fields){
			$data = array();
			foreach ($fields as $key => $value) {
				$data[$value] = $this->input->post($value);
			}

			return $data;
		}

		public function get($id = NULL, $single = FALSE){
			if($id != NULL){
				$filter = $this->_primary_key;
				//$id = $filter($id);
				$this->hindi_db->where($this->_primary_key, $id);
				$method = 'row';
			}else if($single == TRUE){
				$method = 'row';
			}else{
				$method = 'result';
			}
			if(!count($this->hindi_db->order_by($this->_order_by))) {
				$this->hindi_db->order_by($this->_order_by);
			}
			return $this->hindi_db->get($this->_table_name)->$method();
		}

		public function get_count($where = array()){
			if(!empty($where)){
				$this->hindi_db->where($where);
			}
			return $this->hindi_db->count_all_results($this->_table_name);
		}

		public function get_by($where, $single = FALSE){
			//print_r($where);die;
			$this->hindi_db->where($where);
			return $this->get(NULL, $single);
		}

		public function save($data, $id = NULL){

			//Set timestamp
			if($this->_timestamps === TRUE){
				$now = Date('Y-m-d H:i:s');
				$id || $data['created_at'] = $now;
				$data['updated_at'] = $now;
			}

			//Insert
			if($id === NULL){
				!isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
				$this->hindi_db->set($data);
				$this->hindi_db->insert($this->_table_name);
				$id = $this->hindi_db->insert_id();
			}

			//Update
			else{
				$filter = $this->_primary_filter;
				$id = $filter($id);
				$this->hindi_db->set($data);
				$this->hindi_db->where($this->_primary_key, $id);
				$this->hindi_db->update($this->_table_name);
			}

			return $id;
		}

		public function save_batch($data = array()){
			//Set timestamp
			if($this->_timestamps === TRUE){
				$now = Date('Y-m-d H:i:s');
				$id || $data['created_at'] = $now;
				$data['updated_at'] = $now;
			}

			$count = count($data);

			$result = $this->hindi_db->insert_batch($this->_table_name, $data);

			return $result ? $this->hindi_db->insert_id() : 0;
		}

		public function delete($id){
			$filter = $this->_primary_filter;
			$id = $filter($id);
			if(!$id){
				return FALSE;
			}

			$this->hindi_db->where($this->_primary_key, $id);
			$this->hindi_db->limit(1);
			$result = $this->hindi_db->delete($this->_table_name);
			return $result;
		}


		public function get_search_count($search_params, $like_params){
			if(!empty($search_params)){
				$this->hindi_db->where($search_params);
			}

			if(!empty($like_params)){
				foreach ($like_params as $key => $value) {
					$this->hindi_db->like($key, $value);
				}
			}
			return $this->hindi_db->count_all_results($this->_table_name);
		}
	}
?>