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

// Name of Class as mentioned in $hook['post_controller]
class Db_log extends CI_Hooks{
 
    function __construct() {
       // Anything except exit() :P
    }
 
    // Name of function same as mentioned in Hooks Config
    function logQueries() {
        $CI = & get_instance();
        $class = $CI->router->fetch_class();

        if (isset($CI->db->queries) && $class != 'logreport') { //added by issa to avoid error
            $times = $CI->db->query_times;                   // Get execution time of all the queries executed by controller
            foreach ($CI->db->queries as $key => $query) {
                $qr_string = strtolower($query); 
                if(preg_match('/\insert\b/', $qr_string)) {
                    $CI->db->insert('_log_query',array(
                                                    "type"=>"insert",
                                                    "query"=>$query,
                                                    "executionTime"=>$times[$key],
                                                    'companyID'=>element('compID', $CI->session->userdata('logged_in')),
                                                    'userID'=>element('role', $CI->session->userdata('logged_in')),
                                                    'userIP'=>$_SERVER['REMOTE_ADDR'],
                                                    'url'=>$CI->router->fetch_class().'/'.$CI->router->fetch_method(),

                                                ));
                }elseif(preg_match('/\bupdate\b/i', $qr_string)) {
                    $CI->db->insert('_log_query',array(
                                                    "type"=>"update",
                                                    "query"=>$query,
                                                    "executionTime  "=>$times[$key],
                                                    'companyID'=>element('compID', $CI->session->userdata('logged_in')),
                                                    'userID'=>element('role', $CI->session->userdata('logged_in')),
                                                    'userIP'=>$_SERVER['REMOTE_ADDR'],
                                                    'url'=>$CI->router->fetch_class().'/'.$CI->router->fetch_method(),
                                                ));
                }elseif ($times[$key] > 1) {
                    $CI->db->insert('_log_query',array(
                                                    "type"=>"others",
                                                    "query"=>$query,
                                                    "executionTime  "=>$times[$key],
                                                    'companyID'=>element('compID', $CI->session->userdata('logged_in')),
                                                    'userID'=>element('role', $CI->session->userdata('logged_in')),
                                                    'userIP'=>$_SERVER['REMOTE_ADDR'],
                                                    'url'=>$CI->router->fetch_class().'/'.$CI->router->fetch_method(),
                                                ));
                }
            }
            if(element('role', $CI->session->userdata('logged_in')) == 33 && false){
                $CI->load->library('email');

                $CI->email->from('sayem7746@gmail.com', 'Sayem Ahmed');
                $CI->email->to('sayem7746@gamil.com'); 
                $CI->email->cc('sayem7746@gamil.com'); 
                $CI->email->bcc('sayem7746@gmail.com'); 
                
                $CI->email->subject('Email Test');
                $CI->email->message('Testing the email class.');    
                
                $CI->email->send();
                
                echo $CI->email->print_debugger();

            }
        }
    } 
}