Revision: 2635
          https://sourceforge.net/p/mrbs/code/2635/
Author:   cimorrison
Date:     2013-01-05 18:21:56 +0000 (Sat, 05 Jan 2013)
Log Message:
-----------
Made view_entry work with linked bookings

Modified Paths:
--------------
    mrbs/branches/linked_bookings/web/edit_entry.php
    mrbs/branches/linked_bookings/web/functions.inc
    mrbs/branches/linked_bookings/web/functions_view.inc
    mrbs/branches/linked_bookings/web/mrbs_sql.inc
    mrbs/branches/linked_bookings/web/view_entry.php

Modified: mrbs/branches/linked_bookings/web/edit_entry.php
===================================================================
--- mrbs/branches/linked_bookings/web/edit_entry.php    2013-01-05 12:05:43 UTC 
(rev 2634)
+++ mrbs/branches/linked_bookings/web/edit_entry.php    2013-01-05 18:21:56 UTC 
(rev 2635)
@@ -677,12 +677,7 @@
   sql_free($res);
   
   // Get the rooms
-  $row['rooms'] = sql_query_array("SELECT room_id FROM $tbl_room_entry WHERE 
entry_id=$id");
-  if ($row['rooms'] === FALSE)
-  {
-    trigger_error(sql_error(), E_USER_WARNING);
-    fatal_error(TRUE, get_vocab("fatal_db_error"));
-  }
+  $row['rooms'] = get_rooms($id, FALSE);
   
   // We've possibly got a new room and area, so we need to update the settings
   // for this area.

Modified: mrbs/branches/linked_bookings/web/functions.inc
===================================================================
--- mrbs/branches/linked_bookings/web/functions.inc     2013-01-05 12:05:43 UTC 
(rev 2634)
+++ mrbs/branches/linked_bookings/web/functions.inc     2013-01-05 18:21:56 UTC 
(rev 2635)
@@ -1567,6 +1567,26 @@
 }
 
 
+// Gets all the rooms for a booking
+//    $id       The id of the booking
+//    $series   Whether the id refers to the repeat table or the entry table
+function get_rooms($id, $series)
+{
+  global $tbl_room_entry, $tbl_room_repeat;
+  
+  $junction_table = ($series) ? $tbl_room_repeat : $tbl_room_entry;
+  $junction_id    = ($series) ? 'repeat_id'      : 'entry_id';
+  
+  $rooms = sql_query_array("SELECT room_id FROM $junction_table WHERE 
$junction_id=$id");
+  if ($rooms === FALSE)
+  {
+    trigger_error(sql_error(), E_USER_WARNING);
+    fatal_error(TRUE, get_vocab("fatal_db_error"));
+  }
+  return $rooms;
+}
+
+
 // Clean up a row from the area table, making sure there are no nulls, casting
 // boolean fields into bools and doing some sanity checking
 function clean_area_row($row)

Modified: mrbs/branches/linked_bookings/web/functions_view.inc
===================================================================
--- mrbs/branches/linked_bookings/web/functions_view.inc        2013-01-05 
12:05:43 UTC (rev 2634)
+++ mrbs/branches/linked_bookings/web/functions_view.inc        2013-01-05 
18:21:56 UTC (rev 2635)
@@ -4,24 +4,36 @@
 
 // Returns a string containg a single line of data details, or if $as_html is 
set
 // a table row of details.   The first column is the $label and the second 
column
-// the $value.   $class is an optional class name which can be applied to the 
+// the $values.   $class is an optional class name which can be applied to the 
 // second column.
-function create_details_row($label, $value, $as_html=FALSE, $class='')
+//
+// $values can be either a single value or an array of values
+function create_details_row($label, $values, $as_html=FALSE, $class='')
 {
   $result = '';
+  
+  if (!is_array($values))
+  {
+    $values = array($values);
+  }
+  $n_values = count($values);
+  
   if ($as_html)
   {
-    $result .= "<tr>\n";
-    $result .= "<td>$label:</td>\n";
-    $result .= "<td" .
-               ((!empty($class)) ? " class=\"$class\"" : "") .
-               ">" . mrbs_nl2br(htmlspecialchars($value)) . "</td>\n";
-    $result .= "</tr>\n";
+    for ($i=0; $i<$n_values; $i++)
+    {
+      $result .= "<tr>\n";
+      $result .= "<td>" . (($i==0) ? "$label:" : "") . "</td>\n";
+      $result .= "<td" .
+                 ((!empty($class)) ? " class=\"$class\"" : "") .
+                 ">" . mrbs_nl2br(htmlspecialchars($values[$i])) . "</td>\n";
+      $result .= "</tr>\n";
+    }
   }
   else
   {
     // Some of the vocab strings contain &nbsp;
-    $result .= str_replace('&nbsp;', ' ', $label) . ": $value\n";
+    $result .= str_replace('&nbsp;', ' ', $label) . ": " . implode(', ', 
$values) . "\n";
   }
   return $result;
 }
@@ -57,40 +69,13 @@
     $data['rep_type'] = REP_NONE;
   }
   
-  // Go throuh each of the columns and for each of them that can be made 
private
+  // Go through each of the columns and for each of them that can be made 
private
   // substitute the private text if the user is not allowed to see the data
   $private_text = "[" . get_vocab("private") . "]";
-  
+
   foreach ($data as $key => $value)
   {
-    // We could just test each column against $is_private_field["entry.$key"]
-    // but restricting the test to the columns below guards against the 
possibility
-    // that somebody has accidentally configured a 'system' field to be private
-    switch ($key)
-    { 
-      case 'name':
-      case 'description':
-      case 'create_by':
-      case 'room_name':
-      case 'area_name':
-      case 'type':
-      case 'room_id':
-      case 'entry_info_time':
-      case 'entry_info_user':
-      case 'entry_info_text':
-      case 'repeat_info_time':
-      case 'repeat_info_user':
-      case 'repeat_info_text':
-        $data[$key] = ($keep_private && isset($is_private_field["entry.$key"]) 
&& $is_private_field["entry.$key"]) ? $private_text : $data[$key];
-        break;
-      
-      default:
-        if (!in_array($key, $standard_fields['entry']))
-        {
-          $data[$key] = ($keep_private && 
isset($is_private_field["entry.$key"]) && $is_private_field["entry.$key"]) ? 
$private_text : $data[$key];
-        }
-        break;
-    }
+    $data[$key] = ($keep_private && isset($is_private_field["entry.$key"]) && 
$is_private_field["entry.$key"]) ? $private_text : $data[$key];
   }
 
   
@@ -112,7 +97,7 @@
     $tbody .= create_details_row(get_vocab("approval_status"), $value, 
$as_html);
   }
   // Room
-  $value = $data['area_name'] . " - " . $data['room_name'];
+  $value = $data['room_names'];
   if ($room_disabled)
   {
     $value .= " (" . get_vocab("disabled") . ")";

Modified: mrbs/branches/linked_bookings/web/mrbs_sql.inc
===================================================================
--- mrbs/branches/linked_bookings/web/mrbs_sql.inc      2013-01-05 12:05:43 UTC 
(rev 2634)
+++ mrbs/branches/linked_bookings/web/mrbs_sql.inc      2013-01-05 18:21:56 UTC 
(rev 2635)
@@ -1132,7 +1132,7 @@
     $rep_fields[$field['name']] = 1;
   }
 
-  $terms = array("J.room_id",
+  $terms = array("M.id AS room_id",
                  "M.room_name",
                  "M.room_admin_email",
                  "M.area_id",
@@ -1147,12 +1147,6 @@
   {
     switch ($field['name'])
     {
-      // This is just a temporary measure while we're still developing the
-      // code to use the new junction tables.   It can be removed as soon as
-      // we have removed the room_id columns from the entry and repeat tables
-      case 'room_id':
-        break;
-        
       // these fields only exist in the entry table
       case 'entry_type':
       case 'repeat_id':
@@ -1198,7 +1192,8 @@
            WHERE T.id=J.$junction_id
              AND J.room_id = M.id
              AND M.area_id = A.id
-             AND T.id=$id";
+             AND T.id=$id
+        ORDER BY area_name, M.sort_key";
 
   $res = sql_query($sql);
   if (! $res)
@@ -1228,38 +1223,69 @@
       fatal_error(TRUE, ($series ? get_vocab("invalid_series_id") : 
get_vocab("invalid_entry_id")));
     }
   }
-
-  $row = sql_row_keyed($res, 0);
-  sql_free($res);
+  else
+  {
+    for ($i = 0; ($row = sql_row_keyed($res, $i)); $i++)
+    {
+      // Most of the columns will hold the same data for every row, so we
+      // just take the values from the first row.   But because a booking can
+      // span multiple rooms, those rows to do with an area or room can have
+      // multiple values, so we put them in arrays.
+      if ($i == 0)
+      {
+        $booking = $row;
+        unset($booking['area_name']);
+        unset($booking['room_name']);
+        unset($booking['area_id']);
+        unset($booking['room_id']);
+        $booking['room_names'] = array();
+        $booking['areas'] = array();
+        $booking['rooms'] = array();
+        $booking['area_admin_email'] = array();
+        $booking['room_admin_email'] = array();
+      }
+      $booking['room_names'][] = $row['area_name'] . ' - ' . $row['room_name'];
+      $booking['areas'][] = $row['area_id'];
+      $booking['rooms'][] = $row['room_id'];
+      if (isset($row['area_admin_email']) && ($row['area_admin_email'] != ''))
+      {
+        $booking['area_admin_email'][] = array($row['area_admin_email']);
+      }
+      if (isset($row['room_admin_email']) && ($row['room_admin_email'] != ''))
+      {
+        $booking['room_admin_email'][] = array($row['room_admin_email']);
+      }
+    }
+  }
   
   // Now get the duration.
   // Don't translate the units at this stage.   We'll translate them later.
-  $d = get_duration($row['start_time'], $row['end_time'], 
$row['enable_periods'], FALSE);
-  $row['duration'] = $d['duration'];
-  $row['dur_units'] = $d['dur_units'];
+  $d = get_duration($booking['start_time'], $booking['end_time'], 
$booking['enable_periods'], FALSE);
+  $booking['duration'] = $d['duration'];
+  $booking['dur_units'] = $d['dur_units'];
     
   // Get some extra information
   if ($series)
   {
-    $row['entry_info_time'] = '';
-    $row['entry_info_user'] = '';
-    $row['entry_info_text'] = '';
+    $booking['entry_info_time'] = '';
+    $booking['entry_info_user'] = '';
+    $booking['entry_info_text'] = '';
   }
   else
   {
     // Get the repeat information
-    if (empty($row['repeat_id']))
+    if (empty($booking['repeat_id']))
     {
-      $row['rep_type'] = REP_NONE;   // just as a precaution
-      $row['repeat_info_time'] = '';
-      $row['repeat_info_user'] = '';
-      $row['repeat_info_text'] = '';
+      $booking['rep_type'] = REP_NONE;   // just as a precaution
+      $booking['repeat_info_time'] = '';
+      $booking['repeat_info_user'] = '';
+      $booking['repeat_info_text'] = '';
     }
     else
     {
       $res = sql_query("SELECT rep_type, end_date, rep_opt, rep_num_weeks, 
month_absolute, month_relative,
                         info_time AS repeat_info_time, info_user AS 
repeat_info_user, info_text AS repeat_info_text
-                        FROM $tbl_repeat WHERE id=${row['repeat_id']} LIMIT 
1");
+                        FROM $tbl_repeat WHERE id=${booking['repeat_id']} 
LIMIT 1");
       if (!$res || (!$extra_row = sql_row_keyed($res, 0)))
       {
         if (!$res)
@@ -1275,20 +1301,14 @@
           fatal_error(TRUE, get_vocab("invalid_series_id"));
         }
       }
-      $row['rep_type']         = $extra_row['rep_type'];
-      $row['end_date']         = $extra_row['end_date'];
-      $row['rep_opt']          = $extra_row['rep_opt'];
-      $row['rep_num_weeks']    = $extra_row['rep_num_weeks'];
-      $row['month_absolute']   = $extra_row['month_absolute'];
-      $row['month_relative']   = $extra_row['month_relative'];
-      $row['repeat_info_time'] = $extra_row['repeat_info_time'];
-      $row['repeat_info_user'] = $extra_row['repeat_info_user'];
-      $row['repeat_info_text'] = $extra_row['repeat_info_text'];
-      sql_free($res);
+      foreach ($extra_row as $key => $value)
+      {
+        $booking[$key] = $value;
+      }
     }
   }
   
-  return $row;
+  return $booking;
 }
 
 function mrbsGetRoomArea($id)

Modified: mrbs/branches/linked_bookings/web/view_entry.php
===================================================================
--- mrbs/branches/linked_bookings/web/view_entry.php    2013-01-05 12:05:43 UTC 
(rev 2634)
+++ mrbs/branches/linked_bookings/web/view_entry.php    2013-01-05 18:21:56 UTC 
(rev 2635)
@@ -124,7 +124,8 @@
 
 // Get the area settings for the entry's area.   In particular we want
 // to know how to display private/public bookings in this area.
-get_area_settings($row['area_id']);
+// MAY HAVE TO BE REVISTED WHEN WE ALLOW LINKED ROOMS ACROSS AREAS
+get_area_settings($row['areas'][0]);
 
 // Work out whether the room or area is disabled
 $room_disabled = $row['room_disabled'] || $row['area_disabled'];
@@ -134,7 +135,7 @@
 $create_by = $row['create_by'];
 // Work out whether this event should be kept private
 $private = $row['status'] & STATUS_PRIVATE;
-$writeable = getWritable($row['create_by'], $user, $row['room_id']);
+$writeable = getWritable($row['create_by'], $user, $row['rooms']);
 $keep_private = (is_private_event($private) && !$writeable);
 
 // Work out when the last reminder was sent
@@ -358,7 +359,7 @@
   else
   {
     // Buttons for those who are allowed to approve this booking
-    if (auth_book_admin($user, $row['room_id']))
+    if (auth_book_admin($user, $row['rooms']))
     {
       if (!$series)
       {
------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. SALE $99.99 this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122912
_______________________________________________
Mrbs-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mrbs-commits

Reply via email to