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

class Audit_trail extends CI_Model{

	function parseOrderby($col){
		if($col==1){
			return "datetime"; 
		}else if($col==2){
			return "ipaddress"; 
		}else if($col==3){
			return "username"; 
		}else if($col==4){
			return "tablename"; 
		}else if($col==5){
			return "action"; 
		}else if($col==6){
			return "title";
		}else{
			return "datetime";
		}
	}

	function get_audittrail($col=1,$type='DESC',$begin=0,$key=''){
		try {
			$orderby = $this->parseOrderby($col);
			$where_like ='';
			if(!empty($key)){
				$where_like = ' WHERE (datetime LIKE "%'.$key.'%" 
				OR ipaddress LIKE "%'.$key.'%"
				OR username LIKE "%'.$key.'%" 
				OR tablename LIKE "%'.$key.'%" 
				OR title LIKE "%'.$key.'%" 
				OR action LIKE "%'.$key.'%") ';
			}
			$this->db->select('');
			$query = $this->db->query("SELECT SQL_CALC_FOUND_ROWS id, datetime,ipaddress,username,tablename,action,title 
				FROM sys_audittrail ".$where_like." ORDER BY ".$orderby." ".$type." 
				LIMIT ".$begin.",50");
			return $query;
		} catch (Exception $e) {
			
		}
	}

	function fecth_data_num(){
		$query = $this->db->query("SELECT FOUND_ROWS() as cnt;");
		if($query->num_rows>0){
			foreach ($query->result() as $row) {
				return $row->cnt;
			}
    		return $query->num_rows;
    	}else{
    		return 0;
    	}
	}

	function get_detail_info($id){
		try {
			$this->db->where('id',$id);
			return $this->db->get('sys_audittrail');
		} catch (Exception $e) {
			
		}
	}

}