Revision: 2714
          https://sourceforge.net/p/mrbs/code/2714/
Author:   cimorrison
Date:     2013-03-10 10:12:40 +0000 (Sun, 10 Mar 2013)
Log Message:
-----------
Made email notification, including iCal attachements, work when editing 
this_and_future series

Modified Paths:
--------------
    mrbs/branches/this_and_future/web/edit_entry_handler.php
    mrbs/branches/this_and_future/web/mrbs_sql.inc

Modified: mrbs/branches/this_and_future/web/edit_entry_handler.php
===================================================================
--- mrbs/branches/this_and_future/web/edit_entry_handler.php    2013-03-07 
16:35:52 UTC (rev 2713)
+++ mrbs/branches/this_and_future/web/edit_entry_handler.php    2013-03-10 
10:12:40 UTC (rev 2714)
@@ -33,7 +33,7 @@
 
 // NOTE:  the code on this page assumes that array form variables are passed
 // as an array of values, rather than an array indexed by value.   This is
-// particularly important for checkbox arrays whicgh should be formed like 
this:
+// particularly important for checkbox arrays which should be formed like this:
 //
 //    <input type="checkbox" name="foo[]" value="n">
 //    <input type="checkbox" name="foo[]" value="m">
@@ -231,7 +231,7 @@
 
 // If this is an Ajax request and we're being asked to commit the booking, then
 // we'll only have been supplied with parameters that need to be changed.  
Fill in
-// the rest from the existing boking information.
+// the rest from the existing booking information.
 // Note: we assume that 
 // (1) this is not a series (we can't cope with them yet)
 // (2) we always get passed start_seconds and end_seconds in the Ajax data
@@ -642,27 +642,38 @@
 }
 
 $just_check = $ajax && function_exists('json_encode') && !$commit;
-$this_id = (isset($id)) ? $id : NULL;
+
+// If this a "this and future" booking then we have to make two bookings,
+// a new one for the "this and future" part and a modified one for the "past"
+// part.   Otherwise just make the new booking.  And then we delete the 
original
+// booking.
+
+// Check to see whether this is the special case of a "this and future" booking
+// where this entry is at the start of the series, in which case it's really
+// the whole series we're dealing with
+if ($time_subset == THIS_AND_FUTURE)
+{
+  $original_booking = mrbsGetBooking($id);
+  if ($original_booking['start_time'] == $bookings[0]['start_time'])
+  {
+    $time_subset = WHOLE_SERIES;
+  }
+}
+
+$this_id = isset($id) ? $id : NULL;
 $result = mrbsMakeBookings($bookings, $this_id, $just_check, $skip, 
$original_room_id, $need_to_send_mail, $time_subset);
 
+if ($result['valid_booking'] && ($time_subset == THIS_AND_FUTURE))
+{
+  $original_booking['end_date'] = $bookings[0]['start_time'] - 1;  // Truncate 
the booking
+  $result = mrbsMakeBookings(array($original_booking), $id, $just_check, 
$skip, $original_room_id, $need_to_send_mail, WHOLE_SERIES);
+}
+
 // If we weren't just checking and this was a succesful booking and
 // we were editing an existing booking, then delete the old booking
 if (!$just_check && $result['valid_booking'] && isset($id))
 {
-  switch ($time_subset)
-  {
-    case THIS_ENTRY:
-      mrbsDelEntry($user, $id, FALSE);
-      break;
-    case THIS_AND_FUTURE:
-      mrbsDelEntry($user, $id, TRUE, TRUE, $booking['start_time']);
-      break;
-    case WHOLE_SERIES:
-      mrbsDelEntry($user, $id, TRUE, TRUE);
-      break;
-    default:
-      break;
-  }
+  mrbsDelEntry($user, $id, ($time_subset != THIS_ENTRY));
 }
 
 // If this is an Ajax request, output the result and finish

Modified: mrbs/branches/this_and_future/web/mrbs_sql.inc
===================================================================
--- mrbs/branches/this_and_future/web/mrbs_sql.inc      2013-03-07 16:35:52 UTC 
(rev 2713)
+++ mrbs/branches/this_and_future/web/mrbs_sql.inc      2013-03-10 10:12:40 UTC 
(rev 2714)
@@ -386,13 +386,12 @@
  * $id     - The entry to delete
  * $series - If set, delete the series, except user modified entries
  * $all    - If set, include user modified entries in the series delete
- * $from   - If set, delete all entries from and including this start_time
  *
  * Returns FALSE if an error occured, otherwise an array of start_times that
  * have been deleted.
  *
  */
-function mrbsDelEntry($user, $id, $series, $all=FALSE, $from=NULL)
+function mrbsDelEntry($user, $id, $series, $all=FALSE)
 {
   global $tbl_entry, $tbl_repeat;
   
@@ -423,11 +422,6 @@
 
   for ($i = 0; ($row = sql_row_keyed($res, $i)); $i++)
   {
-    if (isset($from) && ($row['start_time'] < $from))
-    {
-      continue;
-    }
-    
     if(!getWritable($row['create_by'], $user, $room_id))
     {
       continue;
@@ -491,13 +485,12 @@
     {
       switch ($key)
       {
-        // integers
+        // integers (not null)
         case 'start_time':
         case 'end_time':
         case 'entry_type':
         case 'repeat_id':
         case 'rep_type':
-        case 'month_absolute':
         case 'end_date':
         case 'room_id':
         case 'status':
@@ -505,6 +498,15 @@
           $sql_col[] = $key;
           $sql_val[] = $data[$key];
           break;
+          
+        // integers (nullable)
+        case 'month_absolute':
+          if (isset($data[$key]))
+          {
+            $sql_col[] = $key;
+            $sql_val[] = $data[$key];
+          }
+          break;
         
         // strings  
         case 'create_by':
@@ -673,6 +675,7 @@
  *                                  repeat day must be set)
  *                  rep_num_weeks   The repeat frequency for weekly repeats
  *                  month_absolute  The repeat day of the month for monthly 
repeats
+ *                  month_relative  The relative day of the month for monthly 
repeats
  * $n             Maximum number of entries to find
  *
  * Returns:
@@ -680,7 +683,7 @@
  *   an array  - This is a list of start times of each of the repeat entrys
  */
 
-function mrbsGetRepeatEntryList($time, $enddate, $rep_details, $n)
+function mrbsGetRepeatEntryList($time, $enddate, $rep_details, $n=PHP_INT_MAX)
 {
   $entries = array();
   
@@ -1106,8 +1109,7 @@
                  "A.area_admin_email",
                  "M.disabled AS room_disabled",
                  "A.disabled AS area_disabled",
-                 "A.enable_periods",
-                 "(end_time - start_time) AS duration");
+                 "A.enable_periods");
                  
   foreach ($table_fields as $field)
   {
@@ -1158,7 +1160,7 @@
            WHERE T.room_id = M.id
              AND M.area_id = A.id
              AND T.id=$id";
-
+  
   $res = sql_query($sql);
   if (! $res)
   {
@@ -1250,6 +1252,53 @@
   return $row;
 }
 
+
+// Gets the booking data for an entry in the entry table with id $id.
+// If the entry is part of a series it will also return the repeat details/
+// The start and end times will be those for the series as a whole (not this
+// particular member)
+function mrbsGetBooking($id)
+{
+  global $tbl_entry, $tbl_repeat;
+  
+  $sql = "SELECT *
+            FROM $tbl_entry
+           WHERE id=$id
+           LIMIT 1";
+  $res = sql_query($sql);
+  if ($res === FALSE)
+  {
+    trigger_error(sql_error(), E_USER_WARNING);
+    fatal_error(FALSE, get_vocab("fatal_db_error"));
+  }
+  if (sql_count($res) == 0)
+  {
+    trigger_error("Id $id does not exist", E_USER_NOTICE);
+    return NULL;
+  }
+  else
+  {
+    $row = sql_row_keyed($res, 0);
+    if (!empty($row['repeat_id']))
+    {
+      $sql = "SELECT start_time, end_time, rep_type, end_date, rep_opt, 
month_absolute, month_relative
+                FROM $tbl_repeat
+               WHERE id=" . $row['repeat_id'] . "
+               LIMIT 1";
+      $res = sql_query($sql);
+      if ($res === FALSE)
+      {
+        trigger_error(sql_error(), E_USER_WARNING);
+        fatal_error(FALSE, get_vocab("fatal_db_error"));
+      }
+      $repeat_row = sql_row_keyed($res, 0);
+      $row = array_merge($row, $repeat_row);
+    }
+  }
+  return $row;
+}
+
+
 function mrbsGetRoomArea($id)
 {
   global $tbl_room;
@@ -1397,17 +1446,17 @@
 // Makes bookings
 //    $bookings     an array of bookings
 //    $id           the id of the current booking when editing an existing 
entry
-function mrbsMakeBookings($bookings, $id=NULL, $just_check=FALSE, $skip=FALSE, 
$original_room_id=NULL, $send_mail=FALSE, $edit_type='')
+function mrbsMakeBookings($bookings, $id=NULL, $just_check=FALSE, $skip=FALSE, 
$original_room_id=NULL, $send_mail=FALSE, $time_subset=THIS_ENTRY)
 {
   global $max_rep_entrys, $enable_periods, $resolution, $mail_settings;
   global $tbl_entry, $tbl_room, $tbl_area;
-
+  
   // All the data, except for the status and room id, will be common
   // across the bookings
   $common = $bookings[0];
   // Work out the duration in seconds, but adjust it for DST changes so that
   // the user will still see, for example, "24 hours" when a booking goes from
-  // 1200 one day to 1200 the next, crosing a DST boundary.
+  // 1200 one day to 1200 the next, crossing a DST boundary.
   $duration_seconds = $common['end_time'] - $common['start_time'];
   $duration_seconds -= cross_dst($common['start_time'], $common['end_time']);
   // Now get the duration, which will be needed for email notifications
@@ -1564,7 +1613,7 @@
   $result['valid_booking'] = $valid_booking;
   $result['rules_broken'] = $rules_broken;
   $result['conflicts'] = $conflicts;
-  
+
   // If we've just been asked to check the bookings, or if it wasn't a valid
   // booking, then stop here and return the results
   if ($just_check || !$valid_booking)
@@ -1584,20 +1633,27 @@
   }
   foreach ($bookings as $booking)
   { 
-    // We need to work out whether this is the original booking being modified,
-    // because, if it is, we keep the ical_uid and increment the ical_sequence.
-    // We consider this to be the original booking if there was an original
-    // booking in the first place (in which case the original room id will be 
set) and
+    // If this is a "this_and_future" booking, it's always going to be a new
+    // booking.
+    if ($time_subset == THIS_AND_FUTURE)
+    {
+      $booking['ical_uid'] = generate_global_uid($booking['name']);
+      $booking['ical_sequence'] = 0;
+    }
+    // Otherwise we need to work out whether this is the original booking being
+    // modified, because, if it is, we keep the ical_uid and increment the
+    // ical_sequence.   We consider this to be the original booking if there 
was
+    // an original booking in the first place (in which case the original room 
id
+    // will be set) AND
     //      (a) this is the same room as the original booking
-    //   or (b) there is only one room in the new set of bookings, in which 
case
+    //   OR (b) there is only one room in the new set of bookings, in which 
case
     //          what has happened is that the booking has been changed to be in
     //          a new room
-    //   or (c) the new set of rooms does not include the original room, in 
which
+    //   OR (c) the new set of rooms does not include the original room, in 
which
     //          case we will make the arbitrary assumption that the original 
booking
     //          has been moved to the first room in the list and the bookings 
in the
     //          other rooms are clones and will be treated as new bookings.
-    
-    if (isset($original_room_id) && 
+    elseif (isset($original_room_id) &&
         (($original_room_id == $booking['room_id']) ||
          (count($rooms) == 1) ||
          (($rooms[0] == $booking['room_id']) && !in_array($original_room_id, 
$rooms))))
@@ -1680,13 +1736,13 @@
         // so that we can highlight the changes
         if (isset($id))
         {
-          if ($edit_type == "series")
+          if ($time_subset == THIS_ENTRY)
           {
-            $mail_previous = mrbsGetBookingInfo($repeat_id, TRUE);
+            $mail_previous = mrbsGetBookingInfo($id, FALSE);
           }
           else
           {
-            $mail_previous = mrbsGetBookingInfo($id, FALSE);
+            $mail_previous = mrbsGetBookingInfo($repeat_id, TRUE);
           }
         }
         else
@@ -1694,7 +1750,8 @@
           $mail_previous = array();
         }
         // Send the email
-        notifyAdminOnBooking($booking, $mail_previous, !isset($id), 
$is_repeat_table, $result['start_times']);
+        $is_new = !isset($id) || ($time_subset == THIS_AND_FUTURE);
+        notifyAdminOnBooking($booking, $mail_previous, $is_new, 
$is_repeat_table, $result['start_times']);
       }
     }   
   } // end foreach $bookings
------------------------------------------------------------------------------
Symantec Endpoint Protection 12 positioned as A LEADER in The Forrester  
Wave(TM): Endpoint Security, Q1 2013 and "remains a good choice" in the  
endpoint security space. For insight on selecting the right partner to 
tackle endpoint security challenges, access the full report. 
http://p.sf.net/sfu/symantec-dev2dev
_______________________________________________
Mrbs-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mrbs-commits

Reply via email to