<?php
	class My_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->db->where($this->_primary_key, $id);
				$method = 'row';
			}else if($single == TRUE){
				$method = 'row';
			}else{
				$method = 'result';
			}
			if($this->db->order_by($this->_order_by)) {
				$this->db->order_by($this->_order_by);
			}
			return $this->db->get($this->_table_name)->$method();
		}

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

		public function get_by($where, $single = FALSE){
			//print_r($where);die;
			$this->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->db->set($data);
				$this->db->insert($this->_table_name);
				$id = $this->db->insert_id();
			}

			//Update
			else{
				$filter = $this->_primary_filter;
				$id = $filter($id);
				$this->db->set($data);
				$this->db->where($this->_primary_key, $id);
				$this->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->db->insert_batch($this->_table_name, $data);

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

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

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


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

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


		public function get_media_data($data = array()){
			$data['ids'] = rtrim($data['ids'], ",");
	    	$data['ids'] = ltrim($data['ids'], ",");
			$result = $this->db->select("* FROM hc_media WHERE is_active = 1 AND id IN (".$data['ids'].") ORDER BY FIELD(id, ".$data['ids'].")")->get()->result();

			$image_array = array();
			foreach ($result as $key1 => $value1) {
				if(isset($value1->file_name)){
	                $image_array["id"][$key1] = $value1->id;
	                $image_array["caption"][$key1] = $value1->caption;
	                $image_array["title"][$key1] = $value1->title;
	                $image_array["alt"][$key1] = $value1->alt;
	                $image_array["mime_type"][$key1] = $value1->mime_type;
	                $image_array["file_name"][$key1] = $value1->file_name;
	                $image_array["images"][$key1] = $this->config->item("media_path").'media/source/'.$value1->file_name;
	            }
			}

			$image_array["field_name"] = $data["field_name"];
			$image_array["select_type"] = $data["select_type"];

			if(isset($data["in_array"]) && $data["in_array"] == 1){
				return $image_array;
			}

			return $this->load->view($this->config->item('admin_folder')."/media/select_media_show", $image_array, TRUE);
		}
	}
?>