<?php

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

class RT_Profiler extends CI_Profiler {

    private $_addition = array(
        'js',
        'css'
    );

    public function __construct() {
//        $this->_available_sections = $this->_addition;
        $this->_available_sections = array_merge($this->_addition, $this->_available_sections);
        parent::__construct();
    }

    /**
     * Create table with data
     * @param string $title
     * @param mixed $data
     * @return string 
     */
    protected function _add_report($title, $data) {
        if (empty($data) || empty($title))
            return '';
        $output = "\n\n";
        $output .= '<fieldset id="ci_profiler_js_css" style="border:1px solid #900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
        $output .= "\n";
        $output .= '<legend style="color:#900;">&nbsp;&nbsp;' . $title . '  Количество:' . count($data) . '&nbsp;&nbsp;</legend>';
        $output .= "\n";
        $output .= "\n\n<table style='width:100%'>\n";
        foreach ($data as $key => $val) {
            $output .= "<tr><td style='padding:5px;width:50%;color:#000;font-weight:bold;background-color:#ddd;'>" . $key . "&nbsp;&nbsp;</td><td style='padding:5px;width:50%;color:#900;font-weight:normal;background-color:#ddd;'>" . $val . "</td></tr>\n";
        }
        $output .= "</table>\n";
        $output .= "</fieldset>";
        return $output;
    }

    /**
     * Show loaded js
     * @return string
     */
    protected function _compile_js() {
        return $this->_add_report('Загруженные js файлы', $this->CI->js_css->get_js_list());
    }

    /**
     * Show loaded css
     * @return string
     */
    protected function _compile_css() {
        return $this->_add_report('Загруженные css файлы', $this->CI->js_css->get_css_list());
    }

    /**
     * Compile Queries
     *
     * @return	string
     */
    protected function _compile_queries() {
        $dbs = array();

        // Let's determine which databases are currently connected to
        foreach (get_object_vars($this->CI) as $CI_object) {
            if (is_object($CI_object) && is_subclass_of(get_class($CI_object), 'CI_DB')) {
                $dbs[] = $CI_object;
            }
        }

        if (count($dbs) == 0) {
            $output = "\n\n";
            $output .= '<fieldset id="ci_profiler_queries" style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
            $output .= "\n";
            $output .= '<legend style="color:#0000FF;">&nbsp;&nbsp;' . $this->CI->lang->line('profiler_queries') . '&nbsp;&nbsp;</legend>';
            $output .= "\n";
            $output .= "\n\n<table style='border:none; width:100%'>\n";
            $output .="<tr><td style='width:100%;color:#0000FF;font-weight:normal;background-color:#eee;padding:5px'>" . $this->CI->lang->line('profiler_no_db') . "</td></tr>\n";
            $output .= "</table>\n";
            $output .= "</fieldset>";

            return $output;
        }

        // Load the text helper so we can highlight the SQL
        $this->CI->load->helper('text');

        // Key words we want bolded
        $highlight = array('SELECT', 'DISTINCT', 'FROM', 'WHERE', 'AND', 'LEFT&nbsp;JOIN', 'ORDER&nbsp;BY', 'GROUP&nbsp;BY', 'LIMIT', 'INSERT', 'INTO', 'VALUES', 'UPDATE', 'OR&nbsp;', 'HAVING', 'OFFSET', 'NOT&nbsp;IN', 'IN', 'LIKE', 'NOT&nbsp;LIKE', 'COUNT', 'MAX', 'MIN', 'ON', 'AS', 'AVG', 'SUM', '(', ')');

        $output = "\n\n";

        foreach ($dbs as $db) {
            // all time
            $all_time = 0;
            foreach ($db->query_times as $v)
                $all_time += number_format($v, 4);

            $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
            $output .= "\n";
            $output .= '<legend style="color:#0000FF;">&nbsp;&nbsp;' . $this->CI->lang->line('profiler_database') . ':&nbsp; ' . $db->database . '&nbsp;&nbsp;&nbsp;' . $this->CI->lang->line('profiler_queries') . ': ' . count($db->queries) . ' (' . $all_time . ')' . '&nbsp;&nbsp;&nbsp;</legend>';
            $output .= "\n";
            $output .= "\n\n<table style='width:100%;'>\n";

            if (count($db->queries) == 0) {
                $output .= "<tr><td style='width:100%;color:#0000FF;font-weight:normal;background-color:#eee;padding:5px;'>" . $this->CI->lang->line('profiler_no_queries') . "</td></tr>\n";
            } else {
                foreach ($db->queries as $key => $val) {
                    $time = number_format($db->query_times[$key], 4);

                    $val = highlight_code($val, ENT_QUOTES);

                    foreach ($highlight as $bold) {
                        $val = str_replace($bold, '<strong>' . $bold . '</strong>', $val);
                    }

                    $output .= "<tr><td style='padding:5px; vertical-align: top;width:1%;color:#900;font-weight:normal;background-color:#ddd;'>" . $time . "&nbsp;&nbsp;</td><td style='padding:5px; color:#000;font-weight:normal;background-color:#ddd;'>" . $val . "</td></tr>\n";
                }
            }

            $output .= "</table>\n";
            $output .= "</fieldset>";
        }

        return $output;
    }

    /**
     * Compile $_GET Data
     *
     * @return	string
     */
    protected function _compile_get() {
        $output = "\n\n";
        $output .= '<fieldset id="ci_profiler_get" style="border:1px solid #cd6e00;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
        $output .= "\n";
        $output .= '<legend style="color:#cd6e00;">&nbsp;&nbsp;' . $this->CI->lang->line('profiler_get_data') . '&nbsp;&nbsp;</legend>';
        $output .= "\n";

        if (empty($_GET)) {
            $output .= "<div style='color:#cd6e00;font-weight:normal;padding:4px 0 4px 0'>" . $this->CI->lang->line('profiler_no_get') . "</div>";
        } else {
            $output .= "\n\n<table style='width:100%; border:none'>\n";

            foreach ($_GET as $key => $val) {
                if (!is_numeric($key)) {
                    $key = "'" . $key . "'";
                }

                $output .= "<tr><td style='width:50%;color:#000;background-color:#ddd;padding:5px'>&#36;_GET[" . $key . "]&nbsp;&nbsp; </td><td style='width:50%;padding:5px;color:#cd6e00;font-weight:normal;background-color:#ddd;'>";
                if (is_array($val)) {
                    $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
                } else {
                    $output .= htmlspecialchars(stripslashes($val));
                }
                $output .= "</td></tr>\n";
            }

            $output .= "</table>\n";
        }
        $output .= "</fieldset>";

        return $output;
    }

    // --------------------------------------------------------------------

    /**
     * Compile $_POST Data
     *
     * @return	string
     */
    protected function _compile_post() {
        $output = "\n\n";
        $output .= '<fieldset id="ci_profiler_post" style="border:1px solid #009900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
        $output .= "\n";
        $output .= '<legend style="color:#009900;">&nbsp;&nbsp;' . $this->CI->lang->line('profiler_post_data') . '&nbsp;&nbsp;</legend>';
        $output .= "\n";

        if (empty($_POST)) {
            $output .= "<div style='color:#009900;font-weight:normal;padding:4px 0 4px 0'>" . $this->CI->lang->line('profiler_no_post') . "</div>";
        } else {
            $output .= "\n\n<table style='width:100%'>\n";

            foreach ($_POST as $key => $val) {
                if (!is_numeric($key)) {
                    $key = "'" . $key . "'";
                }

                $output .= "<tr><td style='width:50%;padding:5px;color:#000;background-color:#ddd;'>&#36;_POST[" . $key . "]&nbsp;&nbsp; </td><td style='width:50%;padding:5px;color:#009900;font-weight:normal;background-color:#ddd;'>";
                if (is_array($val)) {
                    $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, TRUE))) . "</pre>";
                } else {
                    $output .= htmlspecialchars(stripslashes($val));
                }
                $output .= "</td></tr>\n";
            }

            $output .= "</table>\n";
        }
        $output .= "</fieldset>";

        return $output;
    }

}
