<?php
require_once (PATH_TABLES . "admin/AuditLogTable.php") ;
require_once (PATH_TABLES . "admin/TableKeyList.php") ;
require_once (PATH_MODELS . "base/MasterBase.php") ;

class AuditRecordType {
    const Insert = 0;
    const Update = 1;
    const Delete = 2;
}

class AuditLogClass extends MasterBase {
	
	function __construct($db) {
		$this->db = $db ;
		$this->tbl = AuditLogTable::C_TABLE ;
		$this->fldid = AuditLogTable::C_ID ;
		$this->fldorg = AuditLogTable::C_ORG_ID ;
		$this->flddesc = AuditLogTable::C_DESC ;
	}
	function __destruct() {
	}

	function getAuditLogTableList($log_bu="") {
		//to remove other bu table
		$filter = "";
		$sql = " select distinct(" . AuditLogTable::C_LOG_TABLE . ") from " . AuditLogTable::C_TABLE .
				" where " . $filter .
				" order by " . AuditLogTable::C_LOG_TABLE;
		// log::write($sql);
		$rows = $this->db->getTable($sql);
	
		$datas = array() ;	
		if(!is_null($rows) && count($rows)>0){
			foreach ($rows as $row) {
				$data = array();
				$data['code'] = $row[AuditLogTable::C_LOG_TABLE];

				$log_table = $row[AuditLogTable::C_LOG_TABLE];
				$bu = substr($log_table, 0, strpos($log_table, '_'));
				if($log_bu == $bu){
					$log_table = strtolower(substr($log_table, strpos($log_table, '_') + 1));
				}
				$data['desc'] = str_replace("_", " ", ucwords(strtolower($log_table), "_"));
				
				array_push($datas, $data);
			}
		}
		// log::write($datas);
		return $datas;		
	}

	function getAuditLogList($log_bu="", $log_table="", $log_table_desc="", $log_user='0', $log_activity="", $log_date_from="", $log_date_to="") {
		$clsUser = new UserClass($this->db);
		
		$sql = " select * from " . AuditLogTable::C_TABLE;
				// " where " . AuditLogTable::C_LOG_TABLE . " ";

		if ($log_table != '')
			$sql .= " WHERE " . AuditLogTable::C_LOG_TABLE . " = '" . $log_table . "'";

		if ($log_user != '0')
			$sql .= " AND " . AuditLogTable::C_USER . " = '" . $log_user . "'";

		if ($log_activity != '')
			$sql .= " AND " . AuditLogTable::C_ACT_TYPE . " = '" . $log_activity . "'";

		if($log_date_from != "" && $log_date_to != "") {
			$sql .= " AND " . AuditLogTable::C_DATE . " BETWEEN '" . $log_date_from . "' AND '" . $log_date_to . "' ";
		}
		$rows = $this->db->getTable($sql);
	
		$datas = array() ;	
		if(!is_null($rows) && count($rows)>0){
			foreach ($rows as $row) {
				$user_desc = $clsUser->getDescription($row[AuditLogTable::C_USER]);
				$data = array() ;				
				$data['log_id']	    	= $row[AuditLogTable::C_ID];
				$data['log_table'] 		= $log_table_desc; //$row[AuditLogTable::C_LOG_TABLE];
				$data['log_user']	    = $user_desc == '' ? $row[AuditLogTable::C_USER]: $user_desc;
				$data['log_date']	    = date_create($row[AuditLogTable::C_DATE])->format('d/m/Y h:i A');
				$data['log_type']	    = $row[AuditLogTable::C_ACT_TYPE];
				$data['log_data']	    = $row[AuditLogTable::C_DATA];
				$data['log_desc']	    = $row[AuditLogTable::C_DESC];

				// $tableName = "";
				// $foundTableKey = false;
				// foreach (TableKeyList::C_LIST as $tk) {
				// 	$tk = explode("|", $tk);
				// 	if (strcmp($tk[0],  $row[AuditLogTable::C_LOG_TABLE]) == 0) {
				// 		$foundTableKey = true;
				// 		$tableName = str_replace(" ", "", $tk[1]);
				// 		break;
				// 	}
				// }

				// if(!$foundTableKey){
				// 	$tableName = str_replace(" ", "", ucwords(strtolower($log_table_desc), " "));
				// }

				// $className = $tableName . "Class";
				// $cls = new $className($this->db); //based on table filtered
				
				// $logStr = $cls->beautifyAuditLog($tableName, $row[AuditLogTable::C_DATA]);
				// $data['log_data']	    = $logStr;

				array_push($datas, $data);
			}
		}
		
		return $datas;		
	}
}
?>