(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; if ($this->table == 'users' || $this->table == 'business' || $this->table == 'asset') { $data['table_name']= $this->table; if ( $this->table == 'business') { $data['new_value'] = $this->new_data->business_name; } else { $data['new_value'] = $this->new_data->name; } $data['column_name'] = 'name'; $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"); $this->MY_Model->insert($data,'history'); return $data; } } } }