Revision: 1888
          http://mrbs.svn.sourceforge.net/mrbs/?rev=1888&view=rev
Author:   cimorrison
Date:     2011-08-25 14:30:09 +0000 (Thu, 25 Aug 2011)
Log Message:
-----------
Switched to using an Ajax data source in the Report table for improved 
performance

Modified Paths:
--------------
    mrbs/branches/datatables/web/Themes/default/header.inc
    mrbs/branches/datatables/web/jquery/datatables/css/mrbs-table.css
    mrbs/branches/datatables/web/report.php

Modified: mrbs/branches/datatables/web/Themes/default/header.inc
===================================================================
--- mrbs/branches/datatables/web/Themes/default/header.inc      2011-08-25 
11:00:22 UTC (rev 1887)
+++ mrbs/branches/datatables/web/Themes/default/header.inc      2011-08-25 
14:30:09 UTC (rev 1888)
@@ -70,7 +70,8 @@
   global $mrbs_company, $mrbs_company_logo, $mrbs_company_url, 
$mrbs_company_more_info,
          $search_str, $locale_warning;
   global $tbl_entry, $tbl_room, $tbl_area;
-  global $PHP_SELF, $view_week_number, $weekstarts;
+  global $PHP_SELF, $HTTP_HOST, $QUERY_STRING;
+  global $view_week_number, $weekstarts;
   global $auth;
   global $default_language_tokens, $disable_automatic_language_changing, 
$override_locale;
   global $lang_map_windows, $langs, $server_os;
@@ -144,7 +145,6 @@
     // dataTables initialisation
     if (in_array($page, array('admin', 'edit_users', 'report')))
     {
-
       // Include the JavaScript for those pages that use dataTables
       ?>
       <script type="text/javascript" 
src="jquery/datatables/js/jquery.dataTables.min.js"></script>
@@ -1124,7 +1124,27 @@
     // Turn the list of users into a dataTable
     ?>
     var tableOptions = new Object();
-    <?php 
+    <?php
+    // Use an Ajax source if we're able to - gives much better
+    // performance for large tables
+    if (function_exists('json_encode'))
+    {
+      $ajax_url = "http://"; . $HTTP_HOST . $PHP_SELF . "?" . $QUERY_STRING . 
"&ajax=1";
+      ?>
+      tableOptions.sAjaxSource = "<?php echo $ajax_url ?>";
+      <?php
+    }
+    // Add in a hidden input to the form so that we can tell if we have
+    // JavaScript enabled.   We need to know this because when we're using
+    // an Ajax data source we don't want to send the HTML version of the table
+    // data.
+    ?>
+    $('<input>').attr({
+        type: 'hidden',
+        name: 'js',
+        value: '1',
+      }).appendTo('#report_form');
+    <?php
     // Fix the left hand two columns.  This has to be done when 
     // initialisation is complete as the language files are loaded
     // asynchronously

Modified: mrbs/branches/datatables/web/jquery/datatables/css/mrbs-table.css
===================================================================
--- mrbs/branches/datatables/web/jquery/datatables/css/mrbs-table.css   
2011-08-25 11:00:22 UTC (rev 1887)
+++ mrbs/branches/datatables/web/jquery/datatables/css/mrbs-table.css   
2011-08-25 14:30:09 UTC (rev 1888)
@@ -42,7 +42,7 @@
 
 .dataTables_processing {
   position: absolute;
-  top: 200px;
+  top: 12em;
   left: 50%;
   z-index: 100;
   width: 250px;

Modified: mrbs/branches/datatables/web/report.php
===================================================================
--- mrbs/branches/datatables/web/report.php     2011-08-25 11:00:22 UTC (rev 
1887)
+++ mrbs/branches/datatables/web/report.php     2011-08-25 14:30:09 UTC (rev 
1888)
@@ -74,11 +74,17 @@
 // Output the first row (header row) for CSV reports
 function report_header()
 {
-  global $output_as_csv, $output_as_html;
+  global $output_as_csv, $output_as_html, $ajax;
   global $csv_row_sep;
   global $custom_fields, $tbl_entry;
   global $approval_somewhere, $confirmation_somewhere;
   
+  // Don't do anything if this is an Ajax request: we only want to send the 
data
+  if ($ajax)
+  {
+    return;
+  }
+  
   // Build an array of values to go into the header row
   $values = array();
   $values[] = get_vocab("area") . ' - ' . get_vocab("room");
@@ -148,10 +154,15 @@
 
 function open_report()
 {
-  global $output_as_html;
+  global $output_as_html, $ajax, $json_data;
   
-  if ($output_as_html)
+  // If this is an Ajax request, initialise the $json_data variable
+  if ($ajax)
   {
+    $json_data['aaData'] = array();
+  }
+  elseif ($output_as_html)
+  {
     echo "<div id=\"report_output\">\n";
     echo "<table class=\"admin_table display\" id=\"report_table\">\n";
   }
@@ -161,10 +172,15 @@
 
 function close_report()
 {
-  global $output_as_html;
+  global $output_as_html, $ajax, $json_data;
   
-  if ($output_as_html)
+  // If this is an Ajax request, we can now send the JSON data
+  if ($ajax)
   {
+    echo json_encode($json_data);
+  }
+  elseif ($output_as_html)
+  {
     echo "</tbody>\n";
     echo "</table>\n";
     echo "</div>\n";
@@ -175,13 +191,21 @@
 function report_row(&$row, $sortby)
 {
   global $typel;
-  global $output_as_csv;
+  global $output_as_csv, $ajax, $ajax_capable, $json_data;
   global $csv_row_sep, $csv_col_sep;
   global $custom_fields, $field_natures, $field_lengths, $tbl_entry;
   global $approval_somewhere, $confirmation_somewhere;
   global $strftime_format;
   global $select_options, $enable_periods;
   
+  // If we're capable of delivering an Ajax request and this is not Ajax 
request,
+  // then don't do anything.  We're going to save sending the data until we 
actually
+  // get the Ajax request;  we just send the rest of the page at this stage.
+  if ($ajax_capable && !$ajax)
+  {
+    return;
+  }
+  
   $values = array();
   
   // Area-room
@@ -310,8 +334,12 @@
   // Last updated:
   $values[] = time_date_string($row['last_updated']);
 
-  if ($output_as_csv)
+  if ($ajax)
   {
+    $json_data['aaData'][] = $values;
+  }
+  elseif ($output_as_csv)
+  {
     $line = '"';
     $line .= implode("\"$csv_col_sep\"", $values);
     $line .= $csv_row_sep;
@@ -694,8 +722,9 @@
 $match_approved = get_form_var('match_approved', 'string');
 $match_confirmed = get_form_var('match_confirmed', 'string');
 $match_private = get_form_var('match_private', 'string');
+$ajax = get_form_var('ajax', 'int');  // Set if this is an Ajax request
+$js = get_form_var('js', 'int');  // Will only be set if JavaScript is enabled 
in the browser
 
-
 // Check the user is authorised for this page
 checkAuthorised();
 
@@ -703,6 +732,16 @@
 $user = getUserName();
 $is_admin =  (isset($user) && authGetUserLevel($user)>=2) ;
 
+// Set up for Ajax.   We need to know whether we're capable of dealing with 
Ajax
+// requests, which will only be if (a) JavaScript is enabled in the browser 
and (b)
+// we can do JSON encoding.    We also need to initialise the JSON data array.
+$ajax_capable = $js && function_exists('json_encode');
+
+if ($ajax)
+{
+  $json_data = array();
+}
+
 // Set some defaults
 if (!isset($match_approved))
 {
@@ -967,8 +1006,12 @@
 }
 
 // print the page header
-if ($output_as_html || empty($nmatch))
+if ($ajax)
 {
+  // don't do anything if this is an Ajax request:  we only want the data
+}
+elseif ($output_as_html || empty($nmatch))
+{
   print_header($day, $month, $year, $area, isset($room) ? $room : "");
 }
 elseif ($output_as_csv)
@@ -1039,12 +1082,12 @@
 
 
 // Upper part: The form.
-if ($output_as_html || empty($nmatch))
+if (($output_as_html || empty($nmatch)) && !$ajax)  // We don't want the form 
if this is an Ajax request
 {
   ?>
   <div class="screenonly">
  
-    <form class="form_general" method="get" action="report.php">
+    <form class="form_general" id="report_form" method="get" 
action="report.php">
       <fieldset>
       <legend><?php echo get_vocab("report_on");?></legend>
       
@@ -1316,7 +1359,7 @@
       exit;
     }
     
-    if ($output_as_html)
+    if ($output_as_html & !$ajax)
     {
       echo "<p class=\"report_entries\">" . $nmatch . " "
       . ($nmatch == 1 ? get_vocab("entry_found") : get_vocab("entries_found"))
@@ -1358,7 +1401,7 @@
   }
 }
 
-if ($output_as_html || empty($nmatch))
+if (($output_as_html || empty($nmatch)) & !$ajax)
 {
   require_once "trailer.inc";
 }

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
EMC VNX: the world's simplest storage, starting under $10K
The only unified storage solution that offers unified management 
Up to 160% more powerful than alternatives and 25% more efficient. 
Guaranteed. http://p.sf.net/sfu/emc-vnx-dev2dev
_______________________________________________
Mrbs-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mrbs-commits

Reply via email to