Commit r2374: https://sourceforge.net/p/mrbs/code/2374/

------------------------------------------------------------------------
r2374 | cimorrison | 2012-08-12 20:11:43 +0100 (Sun, 12 Aug 2012) | 1 line
Changed paths:
   M /mrbs/trunk/web/day.php
   M /mrbs/trunk/web/functions_table.inc
   A /mrbs/trunk/web/js/refresh.js.php
   M /mrbs/trunk/web/js/resizable.js.php
   M /mrbs/trunk/web/js.inc
   M /mrbs/trunk/web/style.inc
   M /mrbs/trunk/web/week.php

Implemented page refresh in the day and week views using Ajax requests if 
possible instead of through a <meta> tag.   More efficient as only the main 
table is sent instead of the whole page.
------------------------------------------------------------------------

Index: mrbs/trunk/web/functions_table.inc
===================================================================
--- mrbs/trunk/web/functions_table.inc  (revision 2373)
+++ mrbs/trunk/web/functions_table.inc  (revision 2374)
@@ -636,6 +636,26 @@
   global $times_along_top, $row_labels_both_sides, $column_labels_both_ends;
   global $resolution, $morningstarts, $morningstarts_minutes, $eveningends, 
$eveningends_minutes;
   
+  $sql = "SELECT COUNT(*)
+            FROM $tbl_room R, $tbl_area A
+           WHERE R.area_id=$area
+             AND R.area_id = A.id
+             AND R.disabled = 0
+             AND A.disabled = 0";
+           
+  $n_rooms = sql_query1($sql);
+
+  if ($n_rooms < 0)
+  {
+    trigger_error(sql_error(), E_USER_WARNING);
+    fatal_error(FALSE, get_vocab("fatal_db_error"));
+  }
+  elseif ($n_rooms == 0)
+  {
+    // Add an 'empty' data flag so that the JavaScript knows whether this is a 
real table or not
+    return "<tbody 
data-empty=1><tr><td><h1>".get_vocab("no_rooms_for_area")."</h1></td></tr></tbody>";
+  }
+
   $format = ($enable_periods) ? "i" : "Gi";
   
   // form the room parameter for use in query strings.    We want to preserve 
room information
@@ -956,12 +976,26 @@
 
 function week_table_innerhtml($day, $month, $year, $room, $area, 
$timetohighlight=NULL)
 {
-  global $tbl_entry;
+  global $tbl_entry, $tbl_room, $tbl_area;
   global $enable_periods, $periods;
   global $times_along_top, $row_labels_both_sides, $column_labels_both_ends;
   global $resolution, $morningstarts, $morningstarts_minutes, $eveningends, 
$eveningends_minutes;
   global $weekstarts, $strftime_format;
 
+  // Check that we've gor a valid, enabled room
+  $sql = "SELECT COUNT(*)
+            FROM $tbl_room R, $tbl_area A
+           WHERE R.id=$room
+             AND R.area_id=A.id
+             AND R.disabled=0
+             AND A.disabled = 0";
+             
+  if (sql_query1($sql) != 1)
+  {
+    // Add an 'empty' data flag so that the JavaScript knows whether this is a 
real table or not
+    return "<tbody 
data-empty=1><tr><td><h1>".get_vocab("no_rooms_for_area")."</h1></td></tr></tbody>";
+  }
+  
   $num_of_days=7; // days in a week
   
   $format = ($enable_periods) ? "i" : "Gi";
Index: mrbs/trunk/web/day.php
===================================================================
--- mrbs/trunk/web/day.php      (revision 2373)
+++ mrbs/trunk/web/day.php      (revision 2374)
@@ -7,10 +7,19 @@
 
 // Get non-standard form variables
 $timetohighlight = get_form_var('timetohighlight', 'int');
+$ajax = get_form_var('ajax', 'int');
 
 // Check the user is authorised for this page
 checkAuthorised();
 
+$inner_html = day_table_innerhtml($day, $month, $year, $room, $area, 
$timetohighlight);
+
+if ($ajax)
+{
+  echo $inner_html;
+  exit;
+}
+
 // Form the room parameter for use in query strings.    We want to preserve 
room information
 // if possible when switching between views
 $room_param = (empty($room)) ? "" : "&amp;room=$room";
@@ -61,39 +70,21 @@
 $tm = date("m",$i);
 $td = date("d",$i);
 
-$sql = "SELECT COUNT(*)
-          FROM $tbl_room R, $tbl_area A
-         WHERE R.area_id=$area
-           AND R.area_id = A.id
-           AND R.disabled = 0
-           AND A.disabled = 0";
-           
-$n_rooms = sql_query1($sql);
 
-if ($n_rooms < 0)
+
+// Show current date and timezone
+echo "<div id=\"dwm\">\n";
+echo "<h2>" . utf8_strftime($strftime_format['date'], $timestamp) . "</h2>\n";
+if ($display_timezone)
 {
-  trigger_error(sql_error(), E_USER_WARNING);
-  fatal_error(FALSE, get_vocab("fatal_db_error"));
-}
-elseif ($n_rooms == 0)
-{
-  echo "<h1>".get_vocab("no_rooms_for_area")."</h1>";
-}
-else
-{
-  // Show current date and timezone
-  echo "<div id=\"dwm\">\n";
-  echo "<h2>" . utf8_strftime($strftime_format['date'], $timestamp) . 
"</h2>\n";
-  if ($display_timezone)
-  {
-    echo "<div class=\"timezone\">";
-    echo get_vocab("timezone") . ": " . date('T', $timestamp) . " (UTC" . 
date('O', $timestamp) . ")";
-    echo "</div>\n";
-  }
+  echo "<div class=\"timezone\">";
+  echo get_vocab("timezone") . ": " . date('T', $timestamp) . " (UTC" . 
date('O', $timestamp) . ")";
   echo "</div>\n";
+}
+echo "</div>\n";
   
-  // Generate Go to day before and after links
-  $before_after_links_html = "
+// Generate Go to day before and after links
+$before_after_links_html = "
 <div class=\"screenonly\">
   <div class=\"date_nav\">
     <div class=\"date_before\">
@@ -110,23 +101,23 @@
   </div>
 </div>\n";
 
-  // and output them
-  echo $before_after_links_html;
+// and output them
+echo $before_after_links_html;
 
-  echo "<table class=\"dwm_main\" id=\"day_main\" 
data-resolution=\"$resolution\">\n";
-  echo day_table_innerhtml($day, $month, $year, $room, $area, 
$timetohighlight);
-  echo "</table>\n";
+echo "<table class=\"dwm_main\" id=\"day_main\" 
data-resolution=\"$resolution\">\n";
+echo $inner_html;
+echo "</table>\n";
   
-  echo $before_after_links_html;
+echo $before_after_links_html;
 
-  show_colour_key();
-  // Draw the three month calendars
-  if ($display_calendar_bottom)
-  {
-    minicals($year, $month, $day, $area, $room, 'day');
-  }
+show_colour_key();
+// Draw the three month calendars
+if ($display_calendar_bottom)
+{
+  minicals($year, $month, $day, $area, $room, 'day');
 }
 
+
 output_trailer();
 
 ?>
Index: mrbs/trunk/web/style.inc
===================================================================
--- mrbs/trunk/web/style.inc    (revision 2373)
+++ mrbs/trunk/web/style.inc    (revision 2374)
@@ -50,9 +50,14 @@
     
     <meta name="robots" content="noindex">
 <?php
-if (($refresh_rate != 0) &&
-    preg_match("/(day|week|month)\.php/",$PHP_SELF))
+if (($refresh_rate != 0) && preg_match("/(day|week|month)\.php/", $PHP_SELF))
 {
-  echo "    <meta http-equiv=\"Refresh\" content=\"$refresh_rate\">\n";
+  // If we're using JavaScript and on the day or week pages we'll do the 
refresh by 
+  // getting a new table using Ajax requests, which means we only have to 
download
+  // the table not the whole page each time
+  $ajax_capable = preg_match("/(day|week)\.php/", $PHP_SELF);
+  echo ($ajax_capable) ? "<noscript>\n" : '';
+  echo "<meta http-equiv=\"Refresh\" content=\"$refresh_rate\">\n";
+  echo ($ajax_capable) ? "</noscript>\n" : '';
 }
 ?>
Index: mrbs/trunk/web/js/refresh.js.php
===================================================================
--- mrbs/trunk/web/js/refresh.js.php    (revision 0)
+++ mrbs/trunk/web/js/refresh.js.php    (revision 2374)
@@ -0,0 +1,50 @@
+<?php
+
+// $Id$
+
+require "../defaultincludes.inc";
+
+header("Content-type: application/x-javascript");
+expires_header(60*30); // 30 minute expiry
+
+
+
+// 
=================================================================================
+
+// Extend the init() function 
+?>
+
+var oldInitRefresh = init;
+init = function(args) {
+  oldInitRefresh.apply(this, [args]);
+
+  var data = {ajax: 1, 
+              day: args.day,
+              month: args.month,
+              year: args.year,
+              room: args.room,
+              area: args.area};
+  if (args.timetohighlight !== undefined)
+  {
+    data.timetohighlight = args.timetohighlight;
+  }
+  var table = $('table.dwm_main');
+  <?php // setTimeout not setInterval because the 'load' trigger restarts us ?>
+  window.setTimeout(function() {
+      $.post(args.page + '.php',
+             data,
+             function(result){
+                 <?php
+                 // (1) Empty the existing table in order to get rid of events
+                 // and data and prevent memory leaks (2) insert the updated 
+                 // table HTML and then (3) trigger a window load event so 
that 
+                 // the resizable bookings are re-created
+                 ?>
+                 table.empty()
+                 table.html(result);
+                 $(window).trigger('load');
+               },
+             'html');
+    }, <?php echo $refresh_rate * 1000 ?>);
+  
+}

Property changes on: mrbs/trunk/web/js/refresh.js.php
___________________________________________________________________
Added: svn:keywords
   + Id
Added: svn:eol-style
   + native

Index: mrbs/trunk/web/js/resizable.js.php
===================================================================
--- mrbs/trunk/web/js/resizable.js.php  (revision 2373)
+++ mrbs/trunk/web/js/resizable.js.php  (revision 2374)
@@ -42,6 +42,12 @@
     if (!lteIE8)
     {
       var table = $('table.dwm_main');
+      
+      <?php // Don't do anything if this is an empty table ?>
+      if (table.find('tbody').data('empty'))
+      {
+        return;
+      }
        
       <?php
       // function to reverse a collection of jQuery objects
Index: mrbs/trunk/web/js.inc
===================================================================
--- mrbs/trunk/web/js.inc       (revision 2373)
+++ mrbs/trunk/web/js.inc       (revision 2374)
@@ -146,12 +146,13 @@
 }
     
     
-// We need the resizable booking and the multiple bookings code for the day 
and week pages
+// We need the resizable booking, multiple bookings and refresh code for the 
day and week pages
 if (in_array($page, array('day', 'week')))
 {
   ?>
   <script type="text/javascript" src="js/resizable.js.php?<?php echo 
$standard_query_string ?>"></script>
   <script type="text/javascript" src="js/multiple.js.php?<?php echo 
$standard_query_string ?>"></script>
+  <script type="text/javascript" src="js/refresh.js.php?<?php echo 
$standard_query_string ?>"></script>
   <?php
 }
     
@@ -185,6 +186,16 @@
               area: '<?php echo $area?>',
               room: '<?php echo $room?>',
               page: '<?php echo $page?>'};
+  <?php
+  // We need $timetohighlight for the day and week pages
+  $timetohighlight = get_form_var('timetohighlight', 'int');
+  if (isset($timetohighlight))
+  {
+    ?>
+    args.timetohighlight = '<?php echo $timetohighlight?>';
+    <?php
+  }
+  ?>
   init(args);
 
 });
Index: mrbs/trunk/web/week.php
===================================================================
--- mrbs/trunk/web/week.php     (revision 2373)
+++ mrbs/trunk/web/week.php     (revision 2374)
@@ -9,10 +9,19 @@
 
 // Get non-standard form variables
 $timetohighlight = get_form_var('timetohighlight', 'int');
+$ajax = get_form_var('ajax', 'int');
 
 // Check the user is authorised for this page
 checkAuthorised();
 
+$inner_html = week_table_innerhtml($day, $month, $year, $room, $area, 
$timetohighlight);
+
+if ($ajax)
+{
+  echo $inner_html;
+  exit;
+}
+
 // print the page header
 print_header($day, $month, $year, $area, isset($room) ? $room : "");
 
@@ -20,15 +29,6 @@
 
 echo "<div id=\"dwm_header\" class=\"screenonly\">\n";
 
-// Get the area and room names (we will need them later for the heading)
-$this_area_name = "";
-$this_room_name = "";
-$this_area_name = sql_query1("SELECT area_name FROM $tbl_area WHERE id=$area 
AND disabled=0 LIMIT 1");
-$this_room_name = sql_query1("SELECT room_name FROM $tbl_room WHERE id=$room 
AND disabled=0 LIMIT 1");
-// The room is invalid if it doesn't exist, or else it has been disabled, 
either explicitly
-// or implicitly because the area has been disabled
-$room_invalid = ($this_area_name === -1) || ($this_room_name === -1);
-
 // Show all available areas
 echo make_area_select_html('week.php', $area, $year, $month, $day);   
 // Show all available rooms in the current area:
@@ -42,16 +42,20 @@
 
 echo "</div>\n";
 
-// Don't continue if this room is invalid, which could be because the area
-// has no rooms, or else the room or area has been disabled
-if ($room_invalid)
+// Show area and room:
+// Get the area and room names
+$this_area_name = sql_query1("SELECT area_name FROM $tbl_area WHERE id=$area 
AND disabled=0 LIMIT 1");
+$this_room_name = sql_query1("SELECT room_name FROM $tbl_room WHERE id=$room 
AND disabled=0 LIMIT 1");
+// The room is invalid if it doesn't exist, or else it has been disabled, 
either explicitly
+// or implicitly because the area has been disabled
+if ($this_area_name === -1)
 {
-  echo "<h1>".get_vocab("no_rooms_for_area")."</h1>";
-  output_trailer();
-  exit;
+  $this_area_name = '';
 }
-
-// Show area and room:
+if ($this_room_name === -1)
+{
+  $this_room_name = '';
+}
 echo "<div id=\"dwm\">\n";
 echo "<h2>" . htmlspecialchars("$this_area_name - $this_room_name") . 
"</h2>\n";
 echo "</div>\n";
@@ -95,7 +99,7 @@
 print $before_after_links_html;
 
 echo "<table class=\"dwm_main\" id=\"week_main\" 
data-resolution=\"$resolution\">";
-echo week_table_innerhtml($day, $month, $year, $room, $area, $timetohighlight);
+echo $inner_html;
 echo "</table>\n";
 
 print $before_after_links_html;

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Mrbs-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mrbs-commits

Reply via email to