Skip to main content

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

How to Practice Coding Online: Tools and Demos

  If you are learning to code, you need two things: Good Logic and Right Tools . Many people read tutorials but forget to practice. Today, I will share some amazing tools that will help you learn faster. 1. Practice SQL without Installing Software Usually, to learn SQL, you have to install heavy software. But now you can do it online. On thetechinfo.net , there is a Live SQL Playground . You can write queries like: SELECT * FROM users WHERE status = 'active'; It is a great tool for beginners who want to see a Live Demo of how databases work. 2. PHP and String Logic Tools When preparing for interviews, you must practice string problems. For example, checking a Palindrome or Reversing a String . Example: To reverse 'Hello', you can use a simple loop in PHP. Demo: You can find step-by-step logic and code examples on thetechinfo.net to solve these problems easily. 3. Real-World Project Roadmap If you know the basics, you should build a project. A Multi-User Task Mana...

Google Ads

 Google Ads Google Ads is one of the most popular advertising platforms in the world. It's a pay-per-click advertising platform that allows businesses of all sizes to reach their target audience and generate leads and sales. In this blog, we will discuss what Google Ads is, how it works, and the benefits it offers to businesses. What is Google Ads? Google Ads is an online advertising platform that allows businesses to create and display ads on Google's search engine results pages (SERPs) and on other websites that are part of the Google Display Network. Advertisers can target specific audiences, locations, and keywords to ensure their ads are shown to the right people at the right time. How does Google Ads work? Google Ads works on a pay-per-click (PPC) model, which means that advertisers only pay when someone clicks on their ad. Advertisers bid on keywords and phrases that are relevant to their business, and Google uses a complex algorithm to determine which ads are shown and ...

iPhone SE 5G launched with A15 Bionic chip

iPhone SE 5G launched with A15 Bionic chip: Here’s all you need to know Apple today announced the Apple iPhone SE 5G at the Apple Peak Performance Spring 2022 event. The new iPhone SE 5G edition succeeds the older iPhone SE 2020 and comes with some much-needed upgrades. Here’s all you need to know about the iPhone SE 5G, including price, specifications, new features and availability detail iPhone SE 5G: What’s new? The iPhone SE 5G comes with largely the same design elements from the older iPhone SE 2020. This includes a 4.7-inch Retina HD display with reinforced glass on the front and back, the same protective glass that is also found on the iPhone 13 series. The iPhone SE 5G is also powered by the Apple A15 Bionic chip, which is 5G enabled. The A15 Bionic chip is the same chipset that was used in the Apple iPhone 13 series. This brings a few new powerful additions to the SE series. This includes a 6-core CPU, a 4-core GPU and a 16-core Neural Engine that enables features like Live Te...