From 69e975549a574face0e67a6958a0de814855ef54 Mon Sep 17 00:00:00 2001 From: sriramv Date: Tue, 10 Jan 2023 10:06:07 +0530 Subject: [PATCH] AuditHistory:GWM --- application/core/MY_Controller.php | 3 +- application/core/MY_Model.php | 2 +- .../modules/Asset/controllers/Asset.php | 60 +++++++--- .../modules/Audit/controllers/Audit.php | 113 ++++++++++++++++++ .../modules/Business/controllers/Business.php | 5 +- .../modules/Dashboard/views/dashboard.php | 2 +- 6 files changed, 165 insertions(+), 20 deletions(-) create mode 100644 application/modules/Audit/controllers/Audit.php diff --git a/application/core/MY_Controller.php b/application/core/MY_Controller.php index 2ceea24..f10541b 100644 --- a/application/core/MY_Controller.php +++ b/application/core/MY_Controller.php @@ -47,7 +47,8 @@ public function __construct() // Copyright // $this->global_data['copyright_from_mycontroller'] = $date; // $this->load->vars($this->global_data); - $this->load->module('layout'); + $this->load->module('layout'); + $this->load->module('audit'); } } diff --git a/application/core/MY_Model.php b/application/core/MY_Model.php index b39c0d8..371d75d 100644 --- a/application/core/MY_Model.php +++ b/application/core/MY_Model.php @@ -20,7 +20,7 @@ public function insert($data,$table){ function edit_option($action, $id, $table){ $this->db->where('id',$id); $this->db->update($table,$action); - return; + return true; } // edit function diff --git a/application/modules/Asset/controllers/Asset.php b/application/modules/Asset/controllers/Asset.php index 86c8709..c3258b2 100644 --- a/application/modules/Asset/controllers/Asset.php +++ b/application/modules/Asset/controllers/Asset.php @@ -87,9 +87,22 @@ public function createAssets() $upload_asset_image = $this->upload->data(); $data['asset_img'] = $upload_asset_image['file_name']; } - //print_r($data);die(); + $table="asset"; + + //create asset $create_assets = $this->MY_Model->insert($data,$table); + if($create_assets) + { + //set id $ table name for audit history + $this->audit->set('id',$create_assets); + $this->audit->set('table',$table); + //fetch new_data after post data insert + $this->audit->fetch('new_data'); + //audit history generation for insert + $test = $this->audit->create_history('insert'); + } + redirect('asset/assetList'); } @@ -150,9 +163,20 @@ public function editAssets() } - - - $assetData = $this->MY_Model->edit_option($action, $id, $table); + //set id $ table name for audit history + $this->audit->set('id',$this->input->post('id')); + $this->audit->set('table','asset'); + //fetch old_data before new_data update + $this->audit->fetch('old_data'); + //update asset value + $assetData = $this->MY_Model->edit_option($action, $id, $table); + if($assetData) + { + //fetch new_data after post data update + $this->audit->fetch('new_data'); + //audit history generation for update + $test = $this->audit->create_history('update'); + } redirect('asset/assetList'); } @@ -473,22 +497,28 @@ public function deleteAssetInsurance($id) public function test() { + $this->audit->set('id',1); + $this->audit->set('table','asset'); + // $test = $this->audit->fetch('old_data'); + + // $this->audit->set('id',2); + $this->audit->fetch('new_data'); + + $test = $this->audit->create_history('insert'); + // $id = 1; + // $table = "asset"; + // $assetData = $this->MY_Model->getData($table,$id,user()->business_id); + // $object = (object) $action; + // $data = audit_history($object,$table,user()->business_id); + // print_r($data);die(); + // echo "
"; - $id = 1; - $table = "asset"; - $assetData = $this->MY_Model->getData($table,$id,user()->business_id); + // $data = audit_history($assetData,$table,user()->business_id); - $object = (object) $action; - $data = audit_history($object,$table,user()->business_id); - print_r($data);die(); - - echo "
"; - - $data = audit_history($assetData,$table,user()->business_id); - print_r($assetData); + print_r($test); } diff --git a/application/modules/Audit/controllers/Audit.php b/application/modules/Audit/controllers/Audit.php new file mode 100644 index 0000000..190db25 --- /dev/null +++ b/application/modules/Audit/controllers/Audit.php @@ -0,0 +1,113 @@ + (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(); + $data = $this->MY_Model->getData($this->table,$this->id,user()->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; + $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; + $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; + + } + + + } + + + +} diff --git a/application/modules/Business/controllers/Business.php b/application/modules/Business/controllers/Business.php index abb245d..612ce43 100644 --- a/application/modules/Business/controllers/Business.php +++ b/application/modules/Business/controllers/Business.php @@ -137,8 +137,9 @@ public function edit_business() $table="business"; $assetData = $this->MY_Model->edit_option($action, $id, $table); - redirect('business/business_list'); - + if(is_superadmin()){ redirect('business/business_list');} + else if(is_admin()){ redirect('user/businessProfile/'.md5(user()->business_id)); } + } public function deleteAsset($id) diff --git a/application/modules/Dashboard/views/dashboard.php b/application/modules/Dashboard/views/dashboard.php index 2c367c6..1a39938 100644 --- a/application/modules/Dashboard/views/dashboard.php +++ b/application/modules/Dashboard/views/dashboard.php @@ -12,7 +12,7 @@ ?> - + history

LOGGEDIN :