<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class general_model extends CI_Model {

	function __construct() {
		parent::__construct();
	}

	public function get_all($tname='', $orderBy='') { 
	  if ($tname == '') return null;
	  if ($orderBy > '') $this->db->order_by($orderBy);
	  $query = $this->db->get($tname);
	  return ($query->num_rows() >0)?$query->result():null;
	}
	
	public function delete_row($tname='',$filter=array()) {  //by abu
		if ($filter) foreach ($filter as $col => $val) {
			$this->db->where($col, $val); 
		} 
		return $this->db->delete($tname);
	}

	public function get_byFilter($tname='',$select=array('*'),$filter=array(),$join=array(),$order=array()) {
		if ($select) foreach($select as $col){
			$this->db->select($col,false);
		}
		if ($filter) foreach($filter as $col => $val){
			$this->db->where($col,$val);
		}
		if ($join) foreach($join as $rowJoin) {
			$this->db->join($rowJoin[0],$rowJoin[1],$rowJoin[2],false);
		}
/*		if ($order) foreach ($order[0] as $orderBy) {
			$this->db->order_by($orderBy, $order[1]);
		} */
		if ($order) foreach($order as $col) $this->db->order_by($col) ;
		if ($tname=='') return null;
		$query = $this->db->get($tname);
		return ($query->num_rows() >0)?$query->result():null;
	}

	public function insert($tname='',$data){
		if ($tname=='') return null;
		$this->db->insert($tname, $data);
		return ($this->db->affected_rows() > 0)?$this->db->insert_ID():null;
	}

	public function update($tname='', $data, $filter) {
		if ($filter) foreach($filter as $col => $val){
			$this->db->where($col,$val);
		}
		if ($tname=='') return false;
		$query = $this->db->update($tname, $data);
		return ($query === TRUE)?true:false;
	}

    public function jsonToArray($json) {
    	$object = json_decode($this->input->post("$json"));
        if (is_object($object)) {
            foreach ($object as $key => $value) {
                $array [$key] = $value;
            }
        } else {
            $array = $object;
        }
        return $array;
    }

	public function json_decode_multi($json, $assoc = false, $depth = 512, $options = 0) {
		if (substr($json, -1) == ',')
			$json = substr($json, 0, -1);
		return json_decode("[$json]", $assoc, $depth, $options);
	}

}
//akmal 20160503 general model for general purpose
