Revision: 1726
          http://mrbs.svn.sourceforge.net/mrbs/?rev=1726&view=rev
Author:   cimorrison
Date:     2010-12-21 16:43:10 +0000 (Tue, 21 Dec 2010)

Log Message:
-----------
Moved the code for exporting a .ics file into a function which is now used by 
view_entry as well, meaning that the view_entry export correctly takes into 
account changed and cancelled members of a series.

Modified Paths:
--------------
    mrbs/branches/ics_attachments/web/functions_ical.inc
    mrbs/branches/ics_attachments/web/functions_view.inc
    mrbs/branches/ics_attachments/web/report.php
    mrbs/branches/ics_attachments/web/view_entry.php

Modified: mrbs/branches/ics_attachments/web/functions_ical.inc
===================================================================
--- mrbs/branches/ics_attachments/web/functions_ical.inc        2010-12-21 
14:00:46 UTC (rev 1725)
+++ mrbs/branches/ics_attachments/web/functions_ical.inc        2010-12-21 
16:43:10 UTC (rev 1726)
@@ -277,4 +277,159 @@
 }
 
 
+// outputs an iCalendar based on the data in $res, the result of an SQL query.
+//
+//    $res        resource  the result of an SQL query on the entry table
+//    $export_end int       a Unix timestamp giving the end limit for the 
export
+function export_icalendar($res, $keep_private, $export_end=PHP_INT_MAX)
+{
+  require_once "functions_view.inc";
+  require_once "mrbs_sql.inc";
+  
+  // Initialize an array to hold the events and a variable to keep track
+  // of the last repeat id we've seen
+  $ical_events = array();
+  $last_repeat_id = 0;
+  $private_text = "[" . get_vocab("private") . "]";
+  
+  for ($i = 0; ($row = sql_row_keyed($res, $i)); $i++)
+  {
+    $text_body = array();
+    $html_body = array();
+    // If this is an individual entry, then construct an event
+    if ($row['repeat_id'] == 0)
+    {
+      $text_body['content'] = create_details_body($row, FALSE, $keep_private);
+      $html_body['content'] = "<table>" . create_details_body($row, TRUE, 
$keep_private) . " </table>";
+      $ical_events[] = create_ical_event("REQUEST", $row, $text_body, 
$html_body, NULL, FALSE);
+    }
+    // Otherwise it's a series
+    else
+    {
+      // if it's a series that we haven't seen yet, then construct an event
+      if ($row['repeat_id'] != $last_repeat_id)
+      {
+        // Unless is the very first series, check to see whether we got
+        // all the entries we would have expected in the last series and
+        // issue cancellations for those that are missing
+        if (isset($actual_entries))
+        {
+          $missing_entries = array_diff($expected_entries, $actual_entries);
+          foreach ($missing_entries as $missing_entry)
+          {
+            if ($keep_private)
+            {
+              // We don't want private details appearing in the .ics file
+              $start_row['name'] = $private_text;
+              $start_row['create_by'] = $private_text;
+            }
+            $duration = $start_row['end_time'] - $start_row['start_time'];
+            $start_row['start_time'] = $missing_entry;
+            $start_row['end_time'] = $start_row['start_time'] + $duration;
+            $start_row['ical_recur_id'] = gmdate(RFC5545_FORMAT . '\Z', 
$missing_entry);
+            $ical_events[] = create_ical_event("CANCEL", $start_row, NULL, 
NULL, NULL, FALSE);
+          }
+        }
+        // Initialise for the new series
+        $last_repeat_id = $row['repeat_id'];
+        unset($replace_index);
+        // We need to set the repeat start and end dates because we've only 
been
+        // asked to export dates in the report range.  The end date will be 
the earlier
+        // of the series end date and the report end date.  The start date of 
the series
+        // will be the recurrence-id of the first entry in the series, which 
is this one
+        // thanks to the SQL query which ordered the entries by recurrence-id.
+        $start_row = $row;  // Make a copy of the data because we are going to 
tweak it.
+        $start_row['end_date'] = min($export_end, $start_row['end_date']);
+        $duration = $start_row['end_time'] - $start_row['start_time'];
+        $start_row['start_time'] = strtotime($row['ical_recur_id']);
+        $start_row['end_time'] = $start_row['start_time'] + $duration;
+        // However, if this is a series member that has been changed, then we 
+        // cannot trust the rest of the data (eg the description).   We will
+        // use this data for now in case we don't get anything better, but we
+        // will make a note that we really need an unchanged member of the 
series, 
+        // which will have the correct data for the series which we can use to
+        // replace this event.   (Of course, if we don't get an unchanged 
member
+        // then it doesn't matter because all the original members will have 
been
+        // changed and so there's no longer any need for the original data.)
+        if ($row['entry_type'] == ENTRY_RPT_CHANGED)
+        {
+          // Record the index number of the event that needs to be replaced.
+          // As we have not yet added that event to the array, it will be
+          // the current length of the array.
+          $replace_index = count($ical_events);
+        }
+        // Construct an array of the entries we'd expect to see in this series 
so that
+        // we can check whether any are missing and if so set their status to 
cancelled.
+        // (We use PHP_INT_MAX rather than $max_rep_entrys because 
$max_rep_entrys may
+        // have changed since the series was created.)
+        $expected_entries = mrbsGetRepeatEntryList($start_row['start_time'], 
+                                                   $start_row['end_date'],
+                                                   $start_row['rep_type'],
+                                                   $start_row['rep_opt'],
+                                                   PHP_INT_MAX,
+                                                   
$start_row['rep_num_weeks']);
+        // And keep an array of all the entries we actually see
+        $actual_entries = array();
+        // Create the series event
+        $text_body['content'] = create_details_body($start_row, FALSE, 
$keep_private);
+        $html_body['content'] = "<table>" . create_details_body($start_row, 
TRUE, $keep_private) . "</table>";
+        $ical_events[] = create_ical_event("REQUEST", $start_row, $text_body, 
$html_body, NULL, TRUE);
+      }
+      // Add this entry to the array of ones we've seen
+      $actual_entries[] = strtotime($row['ical_recur_id']);
+      // And if it's a series member that has been altered
+      if ($row['entry_type'] == ENTRY_RPT_CHANGED)
+      {
+        $text_body['content'] = create_details_body($row, FALSE, 
$keep_private);
+        $html_body['content'] = "<table>" . create_details_body($row, TRUE, 
$keep_private) . "</table>";
+        $ical_events[] = create_ical_event("REQUEST", $row, $text_body, 
$html_body, NULL, FALSE);
+      }
+      // Otherwise it must be an original member, in which case check
+      // to see if we were looking out for one
+      elseif (isset($replace_index))
+      {
+        // Use this row to define the sequence as it has got all the original
+        // data, except that we need to change the start and end times, keeping
+        // the original duration.   We get the start time from the recurrence
+        // id of the first member of the series, which we saved earlier on.
+        $duration = $row['end_time'] - $row['start_time'];
+        $row['start_time'] = $start_row['start_time'];
+        $row['end_time'] = $row['start_time'] + $duration;
+        $row['end_date'] = min($export_end, $row['end_date']);
+        $text_body['content'] = create_details_body($row, FALSE, 
$keep_private);
+        $html_body['content'] = "<table>" . create_details_body($row, TRUE, 
$keep_private) . "</table>";
+        $ical_events[$replace_index] = create_ical_event("REQUEST", $row, 
$text_body, $html_body, NULL, TRUE);
+        // Clear the $replace_index now that we've found an original entry
+        unset ($replace_index);
+      }
+    }
+  }
+  
+  // We've got to the end of the rows, so check to see whether there were
+  // any entries missing from the very last series, if there was one, and
+  // issue cancellations for the missing entries
+  if (isset($actual_entries))
+  {
+    $missing_entries = array_diff($expected_entries, $actual_entries);
+    foreach ($missing_entries as $missing_entry)
+    {
+      if ($keep_private)
+      {
+        // We don't want private details appearing in the .ics file
+        $start_row['name'] = $private_text;
+        $start_row['create_by'] = $private_text;
+      }
+      $duration = $start_row['end_time'] - $start_row['start_time'];
+      $start_row['start_time'] = $missing_entry;
+      $start_row['end_time'] = $start_row['start_time'] + $duration;
+      $start_row['ical_recur_id'] = gmdate(RFC5545_FORMAT . '\Z', 
$missing_entry);
+      $ical_events[] = create_ical_event("CANCEL", $start_row, NULL, NULL, 
NULL, FALSE);
+    }
+  }
+  // Build the iCalendar from the array of events and output it
+  $icalendar = create_icalendar("REQUEST", $ical_events);
+  echo $icalendar;
+  exit;
+}
+
 ?>
\ No newline at end of file

Modified: mrbs/branches/ics_attachments/web/functions_view.inc
===================================================================
--- mrbs/branches/ics_attachments/web/functions_view.inc        2010-12-21 
14:00:46 UTC (rev 1725)
+++ mrbs/branches/ics_attachments/web/functions_view.inc        2010-12-21 
16:43:10 UTC (rev 1726)
@@ -54,6 +54,43 @@
     $data['rep_type'] = REP_NONE;
   }
   
+  // Go throuh 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 && $is_private_field["entry.$key"]) ? 
$private_text : $data[$key];
+        break;
+      
+      default:
+        if (!in_array($key, $standard_fields['entry']))
+        {
+          $data[$key] = ($keep_private && $is_private_field["entry.$key"]) ? 
$private_text : $data[$key];
+        }
+        break;
+    }
+  }
+
+  
   $tbody = '';
   $tbody .= ($as_html) ? "<tbody>\n" : "";
   // Description

Modified: mrbs/branches/ics_attachments/web/report.php
===================================================================
--- mrbs/branches/ics_attachments/web/report.php        2010-12-21 14:00:46 UTC 
(rev 1725)
+++ mrbs/branches/ics_attachments/web/report.php        2010-12-21 16:43:10 UTC 
(rev 1726)
@@ -730,8 +730,6 @@
 elseif ($output_as_ical)
 {
   require_once "functions_ical.inc";
-  require_once "functions_view.inc";
-  require_once "mrbs_sql.inc";
   header("Content-Type: application/ics;  charset=" . get_charset(). "; 
name=\"" . $mail_settings['ics_filename'] . ".ics\"");
   header("Content-Disposition: attachment; filename=\"" . 
$mail_settings['ics_filename'] . ".ics\"");
 }
@@ -1276,126 +1274,16 @@
     
     if ($output_as_ical)
     {
-      // If we're producing an iCalendar then initialize an array to hold the 
events
-      // and a variable to keep track of the last repeat id we've seen
-      $ical_events = array();
-      $last_repeat_id = 0;
+      // We set $keep_private to FALSE here because we excluded all private
+      // events in the SQL query
+      export_icalendar($res, FALSE, $report_end);
     }
 
     for ($i = 0; ($row = sql_row_keyed($res, $i)); $i++)
     {
       if ($summarize & REPORT)
       {
-        if ($output_as_ical)
-        {
-          $text_body = array();
-          $html_body = array();
-          // If this is an individual entry, then construct an event
-          if ($row['repeat_id'] == 0)
-          {
-            $text_body['content'] = create_details_body($row, FALSE);
-            $html_body['content'] = create_details_body($row, TRUE);
-            $ical_events[] = create_ical_event("REQUEST", $row, $text_body, 
$html_body, NULL, FALSE);
-          }
-          // Otherwise it's a series
-          else
-          {
-            // if it's a series that we haven't seen yet, then construct an 
event
-            if ($row['repeat_id'] != $last_repeat_id)
-            {
-              // Unless is the very first series, check to see whether we got
-              // all the entries we would have expected in the last series and
-              // issue cancellations for those that are missing
-              if (isset($actual_entries))
-              {
-                $missing_entries = array_diff($expected_entries, 
$actual_entries);
-                foreach ($missing_entries as $missing_entry)
-                {
-                  $duration = $start_row['end_time'] - 
$start_row['start_time'];
-                  $start_row['start_time'] = $missing_entry;
-                  $start_row['end_time'] = $start_row['start_time'] + 
$duration;
-                  $start_row['ical_recur_id'] = gmdate(RFC5545_FORMAT . '\Z', 
$missing_entry);
-                  $ical_events[] = create_ical_event("CANCEL", $start_row, 
NULL, NULL, NULL, FALSE);
-                }
-              }
-              // Initialise for the new series
-              $last_repeat_id = $row['repeat_id'];
-              unset($replace_index);
-              // We need to set the repeat start and end dates because we've 
only been
-              // asked to export dates in the report range.  The end date will 
be the earlier
-              // of the series end date and the report end date.  The start 
date of the series
-              // will be the recurrence-id of the first entry in the series, 
which is this one
-              // thanks to the SQL query which ordered the entries by 
recurrence-id.
-              $start_row = $row;  // Make a copy of the data because we are 
going to tweak it.
-              $start_row['end_date'] = min($report_end, 
$start_row['end_date']);
-              $duration = $start_row['end_time'] - $start_row['start_time'];
-              $start_row['start_time'] = strtotime($row['ical_recur_id']);
-              $start_row['end_time'] = $start_row['start_time'] + $duration;
-              // However, if this is a series member that has been changed, 
then we 
-              // cannot trust the rest of the data (eg the description).   We 
will
-              // use this data for now in case we don't get anything better, 
but we
-              // will make a note that we really need an unchanged member of 
the series, 
-              // which will have the correct data for the series which we can 
use to
-              // replace this event.   (Of course, if we don't get an 
unchanged member
-              // then it doesn't matter because all the original members will 
have been
-              // changed and so there's no longer any need for the original 
data.)
-              if ($row['entry_type'] == ENTRY_RPT_CHANGED)
-              {
-                // Record the index number of the event that needs to be 
replaced.
-                // As we have not yet added that event to the array, it will be
-                // the current length of the array.
-                $replace_index = count($ical_events);
-              }
-              // Construct an array of the entries we'd expect to see in this 
series so that
-              // we can check whether any are missing and if so set their 
status to cancelled.
-              // (We use PHP_INT_MAX rather than $max_rep_entrys because 
$max_rep_entrys may
-              // have changed since the series was created.)
-              $expected_entries = 
mrbsGetRepeatEntryList($start_row['start_time'], 
-                                                         
$start_row['end_date'],
-                                                         
$start_row['rep_type'],
-                                                         $start_row['rep_opt'],
-                                                         PHP_INT_MAX,
-                                                         
$start_row['rep_num_weeks']);
-              // And keep an array of all the entries we actually see
-              $actual_entries = array();
-              // Create the series event
-              $text_body['content'] = create_details_body($start_row, FALSE);
-              $html_body['content'] = create_details_body($start_row, TRUE);
-              $ical_events[] = create_ical_event("REQUEST", $start_row, 
$text_body, $html_body, NULL, TRUE);
-            }
-            // Add this entry to the array of ones we've seen
-            $actual_entries[] = strtotime($row['ical_recur_id']);
-            // And if it's a series member that has been altered
-            if ($row['entry_type'] == ENTRY_RPT_CHANGED)
-            {
-              $text_body['content'] = create_details_body($row, FALSE);
-              $html_body['content'] = create_details_body($row, TRUE);
-              $ical_events[] = create_ical_event("REQUEST", $row, $text_body, 
$html_body, NULL, FALSE);
-            }
-            // Otherwise it must be an original member, in which case check
-            // to see if we were looking out for one
-            elseif (isset($replace_index))
-            {
-              // Use this row to define the sequence as it has got all the 
original
-              // data, except that we need to change the start and end times, 
keeping
-              // the original duration.   We get the start time from the 
recurrence
-              // id of the first member of the series, which we saved earlier 
on.
-              $duration = $row['end_time'] - $row['start_time'];
-              $row['start_time'] = $start_row['start_time'];
-              $row['end_time'] = $row['start_time'] + $duration;
-              $row['end_date'] = min($report_end, $row['end_date']);
-              $text_body['content'] = create_details_body($row, FALSE);
-              $html_body['content'] = create_details_body($row, TRUE);
-              $ical_events[$replace_index] = create_ical_event("REQUEST", 
$row, $text_body, $html_body, NULL, TRUE);
-              // Clear the $replace_index now that we've found an original 
entry
-              unset ($replace_index);
-            }
-          }
-        }
-        else
-        {
-          reporton($row, $last_area_room, $last_date, $sortby, $display);
-        }
+        reporton($row, $last_area_room, $last_date, $sortby, $display);
       }
 
       if ($summarize & SUMMARY)
@@ -1409,29 +1297,6 @@
       }
     }
     
-    if ($output_as_ical)
-    {
-      // We've got to the end of the rows, so check to see whether there were
-      // any entries missing from the very last series, if there was one, and
-      // issue cancellations for the missing entries
-      if (isset($actual_entries))
-      {
-        $missing_entries = array_diff($expected_entries, $actual_entries);
-        foreach ($missing_entries as $missing_entry)
-        {
-          $duration = $start_row['end_time'] - $start_row['start_time'];
-          $start_row['start_time'] = $missing_entry;
-          $start_row['end_time'] = $start_row['start_time'] + $duration;
-          $start_row['ical_recur_id'] = gmdate(RFC5545_FORMAT . '\Z', 
$missing_entry);
-          $ical_events[] = create_ical_event("CANCEL", $start_row, NULL, NULL, 
NULL, FALSE);
-        }
-      }
-      // Build the iCalendar from the array of events and output it
-      $icalendar = create_icalendar("REQUEST", $ical_events);
-      echo $icalendar;
-      exit;
-    }
-    
     if ($summarize & SUMMARY)
     {
       do_summary($count, $hours, $room_hash, $name_hash);

Modified: mrbs/branches/ics_attachments/web/view_entry.php
===================================================================
--- mrbs/branches/ics_attachments/web/view_entry.php    2010-12-21 14:00:46 UTC 
(rev 1725)
+++ mrbs/branches/ics_attachments/web/view_entry.php    2010-12-21 16:43:10 UTC 
(rev 1726)
@@ -133,45 +133,11 @@
 $private = $row['status'] & STATUS_PRIVATE;
 $writeable = getWritable($row['create_by'], $user, $row['room_id']);
 $keep_private = (is_private_event($private) && !$writeable);
-$private_text = "[" . get_vocab("private") . "]";
+
 // Work out when the last reminder was sent
 $last_reminded = (empty($row['reminded'])) ? $row['last_updated'] : 
$row['reminded'];
 
-// Go throuh 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
-foreach ($row 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':
-      $row[$key] = ($keep_private && $is_private_field["entry.$key"]) ? 
$private_text : $row[$key];
-      break;
-      
-    default:
-      if (!in_array($key, $standard_fields['entry']))
-      {
-        $row[$key] = ($keep_private && $is_private_field["entry.$key"]) ? 
$private_text : $row[$key];
-      }
-      break;
-  }
-}
 
-
 if ($series == 1)
 {
   $repeat_id = $id;  // Save the repeat_id
@@ -225,32 +191,33 @@
     require_once "functions_ical.inc";
     header("Content-Type: application/ics;  charset=" . get_charset(). "; 
name=\"" . $mail_settings['ics_filename'] . ".ics\"");
     header("Content-Disposition: attachment; filename=\"" . 
$mail_settings['ics_filename'] . ".ics\"");
-    $text_body = array();
-    $text_body['content'] = create_details_body($row, FALSE, $keep_private, 
$room_disabled);
-    $html_body = array();
-    $html_body['content'] = "<table>\n" . create_details_body($row, TRUE, 
$keep_private, $room_disabled) . "</table>\n";
-    $addresses = array();
-    $ical_components = array();
-    $ical_components[] = create_ical_event("REQUEST", $row, $text_body, 
$html_body, $addresses, $series);
-    // If it's a series we need to find out which of the individual entries 
have been changed
-    // and include them in the iCalendar object
+    
+    // Construct the SQL query
+    $sql = "SELECT E.*, "
+         .  sql_syntax_timestamp_to_unix("E.timestamp") . " AS last_updated, "
+         . "A.area_name, R.room_name, "
+         . "A.approval_enabled, A.confirmation_enabled";
     if ($series)
     {
-      $sql = "SELECT id FROM $tbl_entry WHERE repeat_id=$repeat_id AND 
entry_type=" . ENTRY_RPT_CHANGED;
-      $res = sql_query($sql);
-      if ($res && (sql_count($res) > 0))
-      {
-        for ($i = 0; ($row = sql_row_keyed($res, $i)); $i++)
-        {
-          $data = mrbsGetBookingInfo($row['id'], FALSE);
-          $text_body['content'] = create_details_body($data, FALSE, 
$keep_private, $room_disabled);
-          $html_body['content'] = "<table>\n" . create_details_body($data, 
TRUE, $keep_private, $room_disabled) . "</table>\n";
-          $ical_components[] = create_ical_event("REQUEST", $data, $text_body, 
$html_body, $addresses, FALSE);
-        }
-      }
+      // If it's a series we want the repeat information
+      $sql .= ", T.rep_type, T.end_date, T.rep_opt, T.rep_num_weeks";
     }
-    $icalendar = create_icalendar("REQUEST", $ical_components);
-    echo $icalendar;
+    $sql .= " FROM $tbl_area A, $tbl_room R, $tbl_entry E";
+    if ($series)
+    {
+      $sql .= ", $tbl_repeat T"
+            . " WHERE E.repeat_id=$repeat_id"
+            . " AND E.repeat_id=T.id"
+            . " ORDER BY E.ical_recur_id";
+    }
+    else
+    {
+      $sql .= " WHERE E.id=$id";
+    }
+    $res = sql_query($sql);
+    
+    // Export the calendar
+    export_icalendar($res, $keep_private);
     exit;
   }
 }


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

------------------------------------------------------------------------------
Forrester recently released a report on the Return on Investment (ROI) of
Google Apps. They found a 300% ROI, 38%-56% cost savings, and break-even
within 7 months.  Over 3 million businesses have gone Google with Google Apps:
an online email calendar, and document program that's accessible from your 
browser. Read the Forrester report: http://p.sf.net/sfu/googleapps-sfnew
_______________________________________________
Mrbs-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mrbs-commits

Reply via email to