bbone/application/modules/Audit/controllers/Audit.php
suseendhiran17 3ae47e1bd1 suseendhiran
2023-01-12 17:22:38 +05:30

120 lines
3.2 KiB
PHP

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* AMS
*
* @package HMVC
* @author Venba
* @copyright 2022 Venba
* @license https://venbainfotech.com/licenses/MIT MIT License
* @link <URI> (description)
* @version GIT: $Id$
* @since Version 0.0.1
* @filesource
*
*/
/**
* Class Layout
*/
class Audit extends MX_Controller
{
public $table;
public $id;
public $old_data = array();
public $new_data = array();
public function __construct()
{
// To inherit directly the attributes of the parent class.
parent::__construct();
$this->load->model('MY_Model');
}
// set table name and primary key for current data
public function set()
{
$args = func_get_args();
if($args[0] == 'id'){ $this->id = $args[1]; }
else if($args[0] == 'table'){ $this->table = $args[1]; }
}
// fetching old or new data
public function fetch()
{
$args = func_get_args();
if(is_superadmin()){
$business_id = NULL;
}
else{
$business_id = user()->business_id;
}
$data = $this->MY_Model->getData($this->table,$this->id, $business_id);
if($args[0] == 'old_data'){ $this->old_data = $data; return $this->old_data; }
else if($args[0] == 'new_data'){ $this->new_data = $data; return $this->new_data; }
}
//construct audit history and insert audit data into history table
public function create_history()
{
$args = func_get_args();
if($args[0] == 'update')
{
$Output = array();
foreach($this->new_data as $postDatakey=>$postDatavalue)
{
foreach($this->old_data as $TableDatakey=>$TableDatavalue)
{
if($postDatakey == $TableDatakey)
{
if($postDatavalue != $TableDatavalue)
{
$data['table_name'] = $this->table;
$data['column_name'] = $postDatakey;
$data['old_value'] = $TableDatavalue;
$data['new_value'] = $postDatavalue;
$data['created_by'] = user()->id;
if(is_superadmin()){ $data['business_id'] = NULL; } else { $data['business_id'] = user()->business_id; }
$data['primary_id'] = $this->id;
$data['created_at'] = date("Y-m-d h:i:s");
array_push($Output,$data);
$this->MY_Model->insert($data,'history');
}
}
}
}
return $Output;
}else if($args[0] == 'insert') {
$Output = array();
foreach($this->new_data as $postDatakey=>$postDatavalue)
{
if($postDatakey != 'id' && $postDatakey != 'created_by ' && $postDatakey != 'is_active' && $postDatakey != 'created_at')
{
$data['table_name'] = $this->table;
$data['column_name'] = $postDatakey;
$data['new_value'] = $postDatavalue;
$data['created_by'] = user()->id;
if(is_superadmin()){ $data['business_id'] = NULL; } else { $data['business_id'] = user()->business_id; }
$data['primary_id'] = $this->id;
$data['created_at'] = date("Y-m-d h:i:s");
array_push($Output,$data);
$this->MY_Model->insert($data,'history');
}
}
return $Output;
}
}
}