Codignator event log library. (PHP Framework)

Codeigniter library code (Logs), which creates the log in the database.



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

class Event_logger {

/**
     * ci
     *
     * @param instance object
     */
protected $CI;

    /**
     * log table name
     *
     * @param string
     */

    private $_log_table_name = 'event_logs';


// We'll use a constructor, as you can't directly call a function
// from a property definition.

public function __construct()
{
// Assign the CodeIgniter super-object
$this->CI =& get_instance();
    $this->CI->load->database();
$this->_log_table_name = 'event_logs';
}


/*
Logger Function
*/

public function log_event($event,$data,$table,$unique_identifier="",$unique_identifier_value=''){


if(!$this->CI->db->table_exists($table) )
{
echo "Event Logger : Table - $table  does not exists";
exit();
}

if($event == "update")
{
if(empty($unique_identifier) || empty($unique_identifier_value))
{
echo "Event Logger : Update operation unique key not defined";
exit();
}

$logData = "";

if(!empty($data) > 0 ){
foreach($data as $key=>$value){
$sql = "SELECT $key FROM $table WHERE $unique_identifier = $unique_identifier_value";
$query = $this->CI->db->query($sql);
$row = $query->row();
if (isset($row)){
if(strtolower($row->$key) != strtolower($value)){
$logData[$key] = array("old_value"=>$row->$key,"new_value" => $value);
}
}
}

if(!empty($logData)){
$finalData = array(
'event_name' =>$event,
'event_table' =>$table,
'table_unique_id' =>$unique_identifier,
'unique_id' =>$unique_identifier_value,
'event_values' =>json_encode($logData),
'updated_by' => $this->CI->session->userdata('admin_id'),
'ip_address' => $this->CI->input->ip_address(),
'user_agent' => $this->CI->input->user_agent(),
'added_on' => date('Y-m-d H:i:s'),

);
$this->CI->db->insert($this->_log_table_name, $finalData);
}
}else{
echo "Event Logger : update event data is blank";
exit();
}
}


if($event == "insert"){

$finalData = array(
'event_name' => $event,
'event_table' =>$table,
'event_values' =>json_encode($data),
'updated_by' => $this->CI->session->userdata('admin_id'),
'ip_address' => $this->CI->input->ip_address(),
'user_agent' => $this->CI->input->user_agent(),
'added_on' => date('Y-m-d H:i:s'),

);
$this->CI->db->insert($this->_log_table_name, $finalData);
}

if($event == "delete"){

$sql = "SELECT * FROM $table WHERE $unique_identifier = $unique_identifier_value";
$query = $this->CI->db->query($sql);
$row = $query->row();
$finalData = array(
'event_name' => $event,
'event_table' =>$table,
'event_values' =>json_encode($row),
'updated_by' => $this->CI->session->userdata('admin_id'),
'ip_address' => $this->CI->input->ip_address(),
'user_agent' => $this->CI->input->user_agent(),
'added_on' => date('Y-m-d H:i:s'),

);
$this->CI->db->insert($this->_log_table_name, $finalData);
}

}

}


Comments

Popular posts from this blog

Google Ads

Start a Blog in 2022

How to Add a Godaddy domain or any other custom domain in Vultr Cloud VPS server