Revision: 1656
          http://mrbs.svn.sourceforge.net/mrbs/?rev=1656&view=rev
Author:   cimorrison
Date:     2010-11-29 16:02:09 +0000 (Mon, 29 Nov 2010)

Log Message:
-----------
Very very basic first implementation of .ics notifications.   New single events 
can be entered onto a calendar and also deleted.    Changes are not yet 
supported, nor are repeat events.

Modified Paths:
--------------
    mrbs/branches/ics_attachments/web/edit_entry_handler.php
    mrbs/branches/ics_attachments/web/functions_mail.inc
    mrbs/branches/ics_attachments/web/systemdefaults.inc.php

Modified: mrbs/branches/ics_attachments/web/edit_entry_handler.php
===================================================================
--- mrbs/branches/ics_attachments/web/edit_entry_handler.php    2010-11-29 
08:51:33 UTC (rev 1655)
+++ mrbs/branches/ics_attachments/web/edit_entry_handler.php    2010-11-29 
16:02:09 UTC (rev 1656)
@@ -549,12 +549,14 @@
       $booking = mrbsCreateRepeatingEntrys($data);
       $new_id = $booking['id'];
       $is_repeat_table = $booking['series'];
+      $data['id'] = $new_id;  // Add in the id now we know it
     }
     else
     {
       // Create the entry:
       $new_id = mrbsCreateSingleEntry($data);
       $is_repeat_table = FALSE;
+      $data['id'] = $new_id;  // Add in the id now we know it
     }
     
     // Send an email if neccessary, provided that the entry creation was 
successful

Modified: mrbs/branches/ics_attachments/web/functions_mail.inc
===================================================================
--- mrbs/branches/ics_attachments/web/functions_mail.inc        2010-11-29 
08:51:33 UTC (rev 1655)
+++ mrbs/branches/ics_attachments/web/functions_mail.inc        2010-11-29 
16:02:09 UTC (rev 1656)
@@ -290,6 +290,50 @@
   }
   return $email;
 }
+
+
+// Creates an iCalendar object in RFC 5545 format
+function create_vcalendar($data, $method)
+{
+  require_once "version.inc";
+  
+  global $timezone;
+  
+  define ('RFC5545_FORMAT', 'Ymd\THis');
+  define ('EOL', "\r\n");
+  
+  $results = array();
+  $results[] = "BEGIN:VCALENDAR";
+  // Compulsory properties
+  $results[] = "PRODID:-//MRBS//NONSGML " . get_mrbs_version() . " //EN";
+  $results[] = "VERSION:2.0";
+  // Optional properties
+  $results[] = "CALSCALE:GREGORIAN";
+  $results[] = "METHOD:$method";
+  
+  // The event
+  $results[] = "BEGIN:VEVENT";
+  $results[] = "UID:" . $data['id'];  // STILL TO DO:  make this globally 
unique
+  $results[] = "DTSTAMP:" . gmdate(RFC5545_FORMAT . '\Z');
+  $results[] = "DTSTART;TZID=$timezone:" . date(RFC5545_FORMAT, 
$data['start_time']);
+  $results[] = "DTEND;TZID=$timezone:" . date(RFC5545_FORMAT, 
$data['end_time']);
+  $results[] = "SUMMARY:" . $data['name'];
+  $results[] = "DESCRIPTION:" . $data['description'];
+  $results[] = "LOCATION:" . $data['area_name'] . " - " . $data['room_name'];
+  // Set the Sequence number to be 0, unless we are cancelling in which case
+  // it will be 1 (the next in sequence).  This works because if an event is
+  // modified it gets a new id, rather than keeping the same id and having the
+  // sequence number incremented.
+  $results[] = "SEQUENCE:" . (($method=="CANCEL") ? "1" : "0");
+  $results[] = "END:VEVENT";
+  
+  $results[] = "END:VCALENDAR";
+  
+  $result = implode(EOL, $results);
+  $result .= EOL;
+  
+  return $result;
+}
       
 
 // }}}
@@ -639,6 +683,16 @@
     }
     $body .= "\n";
   }
+  
+  // Create the vcalendar if required
+  $attachment = array();
+  if ($mail_settings['ics'])
+  {
+    $attachment['method']   = "REQUEST";
+    $attachment['content']  = create_vcalendar($data, $attachment['method']);
+    $attachment['name']     = "meeting.ics";
+  }
+
   // If the subject contains any non-ASCII characters...
   if (!preg_match('/^[[:ascii:]]*$/', $subject))
   {
@@ -649,6 +703,7 @@
                      $subject,
                      $body,
                      NULL,
+                     $attachment,
                      get_mail_charset(),
                      $from,
                      $cc_list);
@@ -829,8 +884,18 @@
     $body .=  ": " . $mail_previous['end_date'] . "\n";
   }
   $body .= "\n";
+  
+  // Set up the attachment
+  $attachment = array();
+  if ($mail_settings['ics'])
+  {
+    $attachment['method']   = "CANCEL";
+    $attachment['content']  = create_vcalendar($mail_previous, 
$attachment['method']);
+    $attachment['name']     = "meeting.ics";
+  }
+  
   // End of mail details
-  $result = sendMail($recipient_list, $subject, $body, NULL, 
get_mail_charset(), $from, $cc_list);
+  $result = sendMail($recipient_list, $subject, $body, NULL, $attachment, 
get_mail_charset(), $from, $cc_list);
   return $result;
 }
 
@@ -854,6 +919,7 @@
   $data = mrbsGetBookingInfo($id, $series);
   
   // Now process some special fields
+  $data['id']         = $id;
   $data['namebooker'] = $data['name'];
   $data['createdby']  = $data['create_by'];
   $data['updated']    = getMailTimeDateString($data['last_updated']);
@@ -968,17 +1034,22 @@
  * @param string  $subject          email subject
  * @param string  $text_body        text part of body
  * @param string  $text_html        HTML part of body
+ * @param string  $attachment       file to attach.   An array consisting of
+ *                                    'content' the file or data to attach
+ *                                    'method'  the vcalendar METHOD
+ *                                    'name'    the name to give it
  * @param string  $charset          character set used in body
  * @param string  $cc               Carbon Copy
  * @param string  $bcc              Blind Carbon Copy
  * @param string  $from             from field
  * @return bool                     TRUE or PEAR error object if fails
  */
-function sendMail($recipients, $subject, $text_body, $html_body,
+function sendMail($recipients, $subject, $text_body, $html_body, $attachment,
                   $charset = 'us-ascii', $from, $cc = NULL, $bcc = NULL)
 {
   require_once "Mail.php";
   require_once "Mail/mime.php";
+  require_once "Mail/mimePart.php";
   
   global $mail_settings, $sendmail_settings, $smtp_settings;
   
@@ -1033,27 +1104,45 @@
   (NULL != $cc) ? $headers['Cc'] = $cc : '';
   (NULL != $bcc) ? $headers['Bcc'] = $bcc : '';
   $headers['Subject'] = $subject;
+  $headers['Mime-Version'] = '1.0';
   
-  // Parameters for the get()
-  $get_params = array();
-  $get_params['text_encoding'] = 'quoted-printable';
-  $get_params['text_charset'] = $charset;
+  // Build the email
+  $mime_params = array();
+  $mime_params['content_type'] = "multipart/alternative";
+  $mime = new Mail_mimePart('', $mime_params);
   
-  // Build the mime object
-  $mime = new Mail_mime("\n");
-  $mime->setTXTBody($text_body);
+  // Add the text part
+  $mime_params['content_type'] = "text/plain";
+  $mime_params['encoding']     = "7bit";
+  $mime_params['charset']      = $charset;
+  $text = $mime->addSubPart($text_body, $mime_params);
+  
+  if ($mail_settings['ics'])
+  {
+    // Add the text version of the vcalendar
+    $mime_params['content_type'] = "text/calendar; method=" . 
$attachment['method'];
+    $text = $mime->addSubPart($attachment['content'], $mime_params);
+    // And add it as a file as well
+    $mime_params['content_type'] = "application/ics";
+    $mime_params['encoding']     = "base64";
+    $mime_params['disposition']  = "attachment";
+    $mime_params['dfilename']    = $attachment['name'];
+    unset($mime_params['charset']);
+    $text = $mime->addSubPart($attachment['content'], $mime_params);
+  }
+
   if (!empty($html_body))
   {
-    $mime->setHTMLBody($html_body);
+    // do something
   }
-  
-  // get() must be called before headers()
-  $body = $mime->get($get_params);
-  $hdrs = $mime->headers($headers);
 
+  $mime = $mime->encode();
+  // Add in the extra headers
+  $mime['headers'] = array_merge($mime['headers'], $headers);
+
   // Call to the PEAR::Mail class
   $mail_object =& Mail::factory($backend, $params);
-  $result = $mail_object->send($recipients, $hdrs, $body);
+  $result = $mail_object->send($recipients, $mime['headers'], $mime['body']);
 
   if (is_object($result))
   {

Modified: mrbs/branches/ics_attachments/web/systemdefaults.inc.php
===================================================================
--- mrbs/branches/ics_attachments/web/systemdefaults.inc.php    2010-11-29 
08:51:33 UTC (rev 1655)
+++ mrbs/branches/ics_attachments/web/systemdefaults.inc.php    2010-11-29 
16:02:09 UTC (rev 1656)
@@ -771,6 +771,8 @@
 // Set to TRUE or FALSE as required
 $mail_settings['details'] = FALSE;  // Set to TRUE if you want full booking 
details;
                                     // otherwise you just get a link to the 
entry
+$mail_settings['ics'] = FALSE;      // Set to TRUE to include a .ics attachment
+                                    // which can be imported into a calendar
 
 // HOW TO EMAIL - CHARACTER SET AND LANGUAGE
 // -----------------------------------------


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

------------------------------------------------------------------------------
Increase Visibility of Your 3D Game App & Earn a Chance To Win $500!
Tap into the largest installed PC base & get more eyes on your game by
optimizing for Intel(R) Graphics Technology. Get started today with the
Intel(R) Software Partner Program. Five $500 cash prizes are up for grabs.
http://p.sf.net/sfu/intelisp-dev2dev
_______________________________________________
Mrbs-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mrbs-commits

Reply via email to