changeset: 2204:37301a84c8be
tag: tip
user: Greg Aker <greg.aker@ellislab.com>
date: Mon Apr 18 15:51:28 2011 -0500
summary: Adding toggle show/hide on database queries in the output profiler.  Added a profiler config item to set a threshold of when to hide the queries by default.  Additionally, fixed a bug I created earlier today by marking the $CI class var in CI_Profiler as private.
diff --git a/system/libraries/Profiler.php b/system/libraries/Profiler.php
index d1828b9..b73ddaf 100644
--- a/system/libraries/Profiler.php
+++ b/system/libraries/Profiler.php
@@ -32,8 +32,6 @@
  */
 class CI_Profiler {
 
-	private $CI;
-
 	protected $_available_sections = array(
 										'benchmarks',
 										'get',
@@ -46,12 +44,24 @@
 										'session_data',
 										'config'
 										);
+	
+	protected $_query_toggle_count = 25;
+	
+	protected $CI;	
 
+	// --------------------------------------------------------------------
+	
 	public function __construct($config = array())
 	{
 		$this->CI =& get_instance();
 		$this->CI->load->language('profiler');
 
+		if (isset($config['query_toggle_count']))
+		{
+			$this->_query_toggle_count = (int) $config['query_toggle_count'];
+			unset($config['query_toggle_count']);
+		}
+
 		// default all sections to display
 		foreach ($this->_available_sections as $section)
 		{
@@ -163,7 +173,7 @@
 			$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 .= "\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>";
@@ -178,14 +188,27 @@
 		$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";
-
+		
+		$count = 0;
+		
 		foreach ($dbs as $db)
 		{
+			$count++;
+			
+			$hide_queries = (count($db->queries) > $this->_query_toggle_count) ? ' display:none' : '';
+			
+			$show_hide_js = '(<span style="cursor: pointer;" onclick="var s=document.getElementById(\'ci_profiler_queries_db_'.$count.'\').style;s.display=s.display==\'none\'?\'\':\'none\';this.innerHTML=this.innerHTML==\''.$this->CI->lang->line('profiler_section_hide').'\'?\''.$this->CI->lang->line('profiler_section_show').'\':\''.$this->CI->lang->line('profiler_section_hide').'\';">'.$this->CI->lang->line('profiler_section_hide').'</span>)';
+			
+			if ($hide_queries != '')
+			{
+				$show_hide_js = '(<span style="cursor: pointer;" onclick="var s=document.getElementById(\'ci_profiler_queries_db_'.$count.'\').style;s.display=s.display==\'none\'?\'\':\'none\';this.innerHTML=this.innerHTML==\''.$this->CI->lang->line('profiler_section_show').'\'?\''.$this->CI->lang->line('profiler_section_hide').'\':\''.$this->CI->lang->line('profiler_section_show').'\';">'.$this->CI->lang->line('profiler_section_show').'</span>)';
+			}
+			
 			$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).'&nbsp;&nbsp;&nbsp;</legend>';
+			$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).'&nbsp;&nbsp;'.$show_hide_js.'</legend>';
 			$output .= "\n";
-			$output .= "\n\n<table style='width:100%;'>\n";
+			$output .= "\n\n<table style='width:100%;{$hide_queries}' id='ci_profiler_queries_db_{$count}'>\n";
 
 			if (count($db->queries) == 0)
 			{
diff --git a/user_guide/general/profiling.html b/user_guide/general/profiling.html
index f3ea0c6..78ece7d 100644
--- a/user_guide/general/profiling.html
+++ b/user_guide/general/profiling.html
@@ -105,55 +105,60 @@
 
 	<table cellpadding="0" cellspacing="1" border="0" style="width:100%" class="tableborder">
 		<tr>
-		<th>Key</th>
-		<th>Description</th>
-		<th>Default</th>
-		</tr><tr>
-
-		<td class="td"><strong>benchmarks</strong></td>
-		<td class="td">Elapsed time of Benchmark points and total execution time</td>
-		<td class="td">TRUE</td>
-		</tr><tr>
-
-		<td class="td"><strong>config</strong></td>
-		<td class="td">CodeIgniter Config variables</td>
-		<td class="td">TRUE</td>
-		</tr><tr>
-
-		<td class="td"><strong>controller_info</strong></td>
-		<td class="td">The Controller class and method requested</td>
-		<td class="td">TRUE</td>
-		</tr><tr>
-
-		<td class="td"><strong>get</strong></td>
-		<td class="td">Any GET data passed in the request</td>
-		<td class="td">TRUE</td>
-		</tr><tr>
-
-		<td class="td"><strong>http_headers</strong></td>
-		<td class="td">The HTTP headers for the current request</td>
-		<td class="td">TRUE</td>
-		</tr><tr>
-
-		<td class="td"><strong>memory_usage</strong></td>
-		<td class="td">Amount of memory consumed by the current request, in bytes</td>
-		<td class="td">TRUE</td>
-		</tr><tr>
-
-		<td class="td"><strong>post</strong></td>
-		<td class="td">Any POST data passed in the request</td>
-		<td class="td">TRUE</td>
-		</tr><tr>
-
-		<td class="td"><strong>queries</strong></td>
-		<td class="td">Listing of all database queries executed, including execution time</td>
-		<td class="td">TRUE</td>
-		</tr><tr>
-
-		<td class="td"><strong>uri_string</strong></td>
-		<td class="td">The URI of the current request</td>
-		<td class="td">TRUE</td>
-		</tr><tr>
+			<th>Key</th>
+			<th>Description</th>
+			<th>Default</th>
+		</tr>
+		<tr>
+			<td class="td"><strong>benchmarks</strong></td>
+			<td class="td">Elapsed time of Benchmark points and total execution time</td>
+			<td class="td">TRUE</td>
+		</tr>
+		<tr>
+			<td class="td"><strong>config</strong></td>
+			<td class="td">CodeIgniter Config variables</td>
+			<td class="td">TRUE</td>
+		</tr>
+		<tr>
+			<td class="td"><strong>controller_info</strong></td>
+			<td class="td">The Controller class and method requested</td>
+			<td class="td">TRUE</td>
+		</tr>
+		<tr>
+			<td class="td"><strong>get</strong></td>
+			<td class="td">Any GET data passed in the request</td>
+			<td class="td">TRUE</td>
+		</tr>
+		<tr>
+			<td class="td"><strong>http_headers</strong></td>
+			<td class="td">The HTTP headers for the current request</td>
+			<td class="td">TRUE</td>
+		</tr>
+		<tr>
+			<td class="td"><strong>memory_usage</strong></td>
+			<td class="td">Amount of memory consumed by the current request, in bytes</td>
+			<td class="td">TRUE</td>
+		</tr>
+		<tr>
+			<td class="td"><strong>post</strong></td>
+			<td class="td">Any POST data passed in the request</td>
+			<td class="td">TRUE</td>
+		</tr>
+		<tr>
+			<td class="td"><strong>queries</strong></td>
+			<td class="td">Listing of all database queries executed, including execution time</td>
+			<td class="td">TRUE</td>
+		</tr>
+		<tr>
+			<td class="td"><strong>uri_string</strong></td>
+			<td class="td">The URI of the current request</td>
+			<td class="td">TRUE</td>
+		</tr>
+		<tr>
+			<td class="td"><strong>query_toggle_count</strong></td>
+			<td class="td">The number of queries after which the query block will default to hidden.</td>
+			<td class="td">25</td>
+		</tr>
 	</table>