Branch 'roundcubemail-plugins-kolab-format2' - 3 commits - plugins/libkolab

Thomas Brüderli bruederli at kolabsys.com
Tue Nov 13 15:51:39 CET 2012


 plugins/libkolab/lib/kolab_format.php                  |   80 -------
 plugins/libkolab/lib/kolab_format_configuration.php    |    2 
 plugins/libkolab/lib/kolab_format_contact.php          |    2 
 plugins/libkolab/lib/kolab_format_distributionlist.php |    2 
 plugins/libkolab/lib/kolab_format_event.php            |  184 ++++++++++++++++-
 5 files changed, 181 insertions(+), 89 deletions(-)

New commits:
commit b20c816e83481c7ed94ac34f0238b0da480ed7b7
Author: Thomas Bruederli <bruederli at kolabsys.com>
Date:   Tue Nov 13 15:51:21 2012 +0100

    Improve is_valid() methods for kolab objects

diff --git a/plugins/libkolab/lib/kolab_format_configuration.php b/plugins/libkolab/lib/kolab_format_configuration.php
index a099c59..10c9eb9 100644
--- a/plugins/libkolab/lib/kolab_format_configuration.php
+++ b/plugins/libkolab/lib/kolab_format_configuration.php
@@ -53,7 +53,7 @@ class kolab_format_configuration extends kolab_format
      */
     public function is_valid()
     {
-        return $this->data || (is_object($this->obj) && $this->obj->isValid());
+        return !empty($this->data['uid']) && !empty($this->data['type']);
     }
 
     /**
diff --git a/plugins/libkolab/lib/kolab_format_contact.php b/plugins/libkolab/lib/kolab_format_contact.php
index cab7ded..a7dcd5c 100644
--- a/plugins/libkolab/lib/kolab_format_contact.php
+++ b/plugins/libkolab/lib/kolab_format_contact.php
@@ -238,7 +238,7 @@ class kolab_format_contact extends kolab_format
      */
     public function is_valid()
     {
-        return strlen($this->data['uid']);
+        return !empty($this->data['uid']);
     }
 
     /**
diff --git a/plugins/libkolab/lib/kolab_format_distributionlist.php b/plugins/libkolab/lib/kolab_format_distributionlist.php
index b6fc566..67822df 100644
--- a/plugins/libkolab/lib/kolab_format_distributionlist.php
+++ b/plugins/libkolab/lib/kolab_format_distributionlist.php
@@ -63,7 +63,7 @@ class kolab_format_distributionlist extends kolab_format
 
     public function is_valid()
     {
-        return !empty($this->data['uid']);
+        return !empty($this->data['uid']) && !empty($this->data['name']);
     }
 
     /**


commit f3663b403f596fe832a4dd42fed61601d65732f2
Author: Thomas Bruederli <bruederli at kolabsys.com>
Date:   Tue Nov 13 15:47:07 2012 +0100

    Implement saving of events using Horde

diff --git a/plugins/libkolab/lib/kolab_format_event.php b/plugins/libkolab/lib/kolab_format_event.php
index 0a960b1..505b617 100644
--- a/plugins/libkolab/lib/kolab_format_event.php
+++ b/plugins/libkolab/lib/kolab_format_event.php
@@ -30,6 +30,17 @@ class kolab_format_event extends kolab_format
 
     public static $fulltext_cols = array('title', 'description', 'location', 'attendees:name', 'attendees:email', 'categories');
 
+    // old Kolab 2 format field map
+    private $kolab2_fieldmap = array(
+      // kolab       => roundcube
+      'summary'      => 'title',
+      'location'     => 'location',
+      'body'         => 'description',
+      'categories'   => 'categories',
+      'sensitivity'  => 'sensitivity',
+      'show-time-as' => 'free_busy',
+      'priority'     => 'priority',
+    );
     private $kolab2_rolemap = array(
         'required' => 'REQ-PARTICIPANT',
         'optional' => 'OPT-PARTICIPANT',
@@ -42,6 +53,7 @@ class kolab_format_event extends kolab_format
         'accepted'  => 'ACCEPTED',
         'declined'  => 'DECLINED',
     );
+    private $kolab2_weekdaymap = array('MO'=>'monday', 'TU'=>'tuesday', 'WE'=>'wednesday', 'TH'=>'thursday', 'FR'=>'friday', 'SA'=>'saturday', 'SU'=>'sunday');
     private $kolab2_monthmap = array('', 'january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december');
 
 
@@ -64,7 +76,168 @@ class kolab_format_event extends kolab_format
     {
         $this->init();
 
-        // TODO: implement this
+        if ($object['uid'])
+            $this->kolab_object['uid'] = $object['uid'];
+
+        $this->kolab_object['last-modification-date'] = time();
+
+        // map basic fields rcube => $kolab
+        foreach ($this->kolab2_fieldmap as $kolab => $rcube) {
+            $this->kolab_object[$kolab] = $object[$rcube];
+        }
+
+        // all-day event
+        if (intval($object['allday'])) {
+            // shift times from user's timezone to server's timezone
+            // because Horde_Kolab_Format_Date::encodeDate() uses strftime() which operates in server tz
+            $server_tz = new DateTimeZone(date_default_timezone_get());
+            $start = clone $object['start'];
+            $end = clone $object['end'];
+
+            $start->setTimezone($server_tz);
+            $end->setTimezone($server_tz);
+            $start->setTime(0,0,0);
+            $end->setTime(0,0,0);
+
+            // create timestamps at exactly 00:00. This is also needed for proper re-interpretation in _to_rcube_event() after updating an event
+            $this->kolab_object['start-date'] = mktime(0,0,0, $start->format('n'), $start->format('j'), $start->format('Y'));
+            $this->kolab_object['end-date']   = mktime(0,0,0, $end->format('n'),   $end->format('j'),   $end->format('Y')) + 86400;
+
+            // sanity check: end date is same or smaller than start
+            if (date('Y-m-d', $this->kolab_object['end-date']) <= date('Y-m-d', $this->kolab_object['start-date']))
+              $this->kolab_object['end-date'] = mktime(13,0,0, $start->format('n'), $start->format('j'), $start->format('Y')) + 86400;
+
+            $this->kolab_object['_is_all_day'] = 1;
+        }
+        else {
+            $this->kolab_object['start-date'] = $object['start']->format('U');
+            $this->kolab_object['end-date']   = $object['end']->format('U');
+        }
+
+        // handle alarms
+        if ($object['alarms']) {
+            $alarmbase = explode(":", $object['alarms']);
+            $avalue = preg_replace('/[^0-9]/', '', $alarmbase[0]); 
+
+            if (preg_match("/H/",$alarmbase[0])) {
+                $this->kolab_object['alarm'] = $avalue*60;
+            }
+            else if (preg_match("/D/",$alarmbase[0])) {
+                $this->kolab_object['alarm'] = $avalue*24*60;
+            }
+            else {
+                $this->kolab_object['alarm'] = $avalue;
+            }
+        }
+
+        // recurr object/array
+        if (count($object['recurrence']) > 1) {
+            $ra = $object['recurrence'];
+
+            // frequency and interval
+            $this->kolab_object['recurrence'] = array(
+                'cycle' => strtolower($ra['FREQ']),
+                'interval' => intval($ra['INTERVAL']),
+            );
+
+            // range Type
+            if ($ra['UNTIL']) {
+                $this->kolab_object['recurrence']['range-type'] = 'date';
+                $this->kolab_object['recurrence']['range'] = $ra['UNTIL']->format('U');
+            }
+            if ($ra['COUNT']) {
+                $this->kolab_object['recurrence']['range-type'] = 'number';
+                $this->kolab_object['recurrence']['range'] = $ra['COUNT'];
+            }
+
+            // WEEKLY
+            if ($ra['FREQ'] == 'WEEKLY') {
+                if ($ra['BYDAY']) {
+                    foreach (explode(',', $ra['BYDAY']) as $day)
+                        $this->kolab_object['recurrence']['day'][] = $this->kolab2_weekdaymap[$day];
+                }
+                else {
+                    // use weekday of start date if empty
+                    $this->kolab_object['recurrence']['day'][] = strtolower($object['start']->format('l'));
+                }
+            }
+
+            // MONTHLY (temporary hack to follow Horde logic)
+            if ($ra['FREQ'] == 'MONTHLY') {
+                if ($ra['BYDAY'] && preg_match('/(-?[1-4])([A-Z]+)/', $ra['BYDAY'], $m)) {
+                    $this->kolab_object['recurrence']['daynumber'] = $m[1];
+                    $this->kolab_object['recurrence']['day'] = array($this->kolab2_weekdaymap[$m[2]]);
+                    $this->kolab_object['recurrence']['cycle'] = 'monthly';
+                    $this->kolab_object['recurrence']['type']  = 'weekday';
+                }
+                else {
+                    $this->kolab_object['recurrence']['daynumber'] = preg_match('/^\d+$/', $ra['BYMONTHDAY']) ? $ra['BYMONTHDAY'] : $object['start']->format('j');
+                    $this->kolab_object['recurrence']['cycle'] = 'monthly';
+                    $this->kolab_object['recurrence']['type']  = 'daynumber';
+                }
+            }
+
+            // YEARLY
+            if ($ra['FREQ'] == 'YEARLY') {
+                if (!$ra['BYMONTH'])
+                    $ra['BYMONTH'] = $object['start']->format('n');
+
+                $this->kolab_object['recurrence']['cycle'] = 'yearly';
+                $this->kolab_object['recurrence']['month'] = $this->month_map[intval($ra['BYMONTH'])];
+
+                if ($ra['BYDAY'] && preg_match('/(-?[1-4])([A-Z]+)/', $ra['BYDAY'], $m)) {
+                    $this->kolab_object['recurrence']['type'] = 'weekday';
+                    $this->kolab_object['recurrence']['daynumber'] = $m[1];
+                    $this->kolab_object['recurrence']['day'] = array($this->kolab2_weekdaymap[$m[2]]);
+                }
+                else {
+                    $this->kolab_object['recurrence']['type'] = 'monthday';
+                    $this->kolab_object['recurrence']['daynumber'] = $object['start']->format('j');
+                }
+            }
+
+            // exclusions
+            foreach ((array)$ra['EXDATE'] as $excl) {
+                $this->kolab_object['recurrence']['exclusion'][] = $excl->format('Y-m-d');
+            }
+        }
+        else if (isset($object['recurrence']))
+            unset($this->kolab_object['recurrence']);
+
+        // process event attendees
+        $status_map = array_flip($this->kolab2_statusmap);
+        $role_map = array_flip($this->kolab2_rolemap);
+        $this->kolab_object['attendee'] = array();
+        foreach ((array)$object['attendees'] as $attendee) {
+            $role = $attendee['role'];
+            if ($role == 'ORGANIZER') {
+                $this->kolab_object['organizer'] = array(
+                    'display-name' => $attendee['name'],
+                    'smtp-address' => $attendee['email'],
+                );
+            }
+            else {
+                $this->kolab_object['attendee'][] = array(
+                    'display-name' => $attendee['name'],
+                    'smtp-address' => $attendee['email'],
+                    'status'       => $status_map[$attendee['status']],
+                    'role'         => $role_map[$role],
+                    'request-response' => $attendee['rsvp'],
+                );
+            }
+        }
+
+        // clear old cid: list attachments
+        $links = array();
+        foreach ((array)$this->kolab_object['link-attachment'] as $i => $url) {
+            if (strpos($url, 'cid:') !== 0)
+                $links[] = $url;
+        }
+        foreach ((array)$object['_attachments'] as $key => $attachment) {
+            if ($attachment)
+                $links[] = 'cid:' . $key;
+        }
+        $this->kolab_object['link-attachment'] = $links;
 
         // cache this data
         $this->data = $object;
@@ -76,7 +249,7 @@ class kolab_format_event extends kolab_format
      */
     public function is_valid()
     {
-        return $this->data;
+        return !empty($this->data['uid']) && $this->data['start'] && $this->data['end'];
     }
 
     /**
@@ -121,7 +294,7 @@ class kolab_format_event extends kolab_format
             $rec['start-date'] -= $gmt_offset - date('Z', $rec['start-date']);  // because generated with mktime() in Horde_Kolab_Format_Date::decodeDate()
             // sanity check
             if ($rec['end-date'] <= $rec['start-date'])
-              $rec['end-date'] += 86400;
+                $rec['end-date'] += 86400;
         }
 
         // convert alarm time into internal format
@@ -207,8 +380,7 @@ class kolab_format_event extends kolab_format
             'allday' => $allday,
             'recurrence' => $rrule,
             'alarms' => $alarm_value . $alarm_unit,
-            'categories' => explode(',', $rec['categories']),
-            'attachments' => $attachments,
+            'categories' => $rec['categories'],
             'attendees' => $attendees,
             'free_busy' => $rec['show-time-as'],
             'priority' => $rec['priority'],
@@ -216,7 +388,7 @@ class kolab_format_event extends kolab_format
             'changed' => $rec['last-modification-date'],
         );
 
-        // assign current timezone to event start/end
+        // assign current timezone to event start/end which are in UTC
         $this->data['start']->setTimezone(self::$timezone);
         $this->data['end']->setTimezone(self::$timezone);
     }


commit 94da991b05fd96d24dc3c7ca2467e25185747cad
Author: Thomas Bruederli <bruederli at kolabsys.com>
Date:   Tue Nov 13 15:46:02 2012 +0100

    Remove unused utility functions

diff --git a/plugins/libkolab/lib/kolab_format.php b/plugins/libkolab/lib/kolab_format.php
index d976328..afa43ce 100644
--- a/plugins/libkolab/lib/kolab_format.php
+++ b/plugins/libkolab/lib/kolab_format.php
@@ -86,86 +86,6 @@ abstract class kolab_format
     }
 
     /**
-     * Convert the given date/time value into a cDateTime object
-     *
-     * @param mixed         Date/Time value either as unix timestamp, date string or PHP DateTime object
-     * @param DateTimeZone  The timezone the date/time is in. Use global default if Null, local time if False
-     * @param boolean       True of the given date has no time component
-     * @return object       The libkolabxml date/time object
-     */
-    public static function get_datetime($datetime, $tz = null, $dateonly = false)
-    {
-        // use timezone information from datetime of global setting
-        if (!$tz && $tz !== false) {
-            if ($datetime instanceof DateTime)
-                $tz = $datetime->getTimezone();
-            if (!$tz)
-                $tz = self::$timezone;
-        }
-        $result = new cDateTime();
-
-        // got a unix timestamp (in UTC)
-        if (is_numeric($datetime)) {
-            $datetime = new DateTime('@'.$datetime, new DateTimeZone('UTC'));
-            if ($tz) $datetime->setTimezone($tz);
-        }
-        else if (is_string($datetime) && strlen($datetime))
-            $datetime = new DateTime($datetime, $tz ?: null);
-
-        if ($datetime instanceof DateTime) {
-            $result->setDate($datetime->format('Y'), $datetime->format('n'), $datetime->format('j'));
-
-            if (!$dateonly)
-                $result->setTime($datetime->format('G'), $datetime->format('i'), $datetime->format('s'));
-
-            if ($tz && $tz->getName() == 'UTC')
-                $result->setUTC(true);
-            else if ($tz !== false)
-                $result->setTimezone($tz->getName());
-        }
-
-        return $result;
-    }
-
-    /**
-     * Convert the given cDateTime into a PHP DateTime object
-     *
-     * @param object cDateTime  The libkolabxml datetime object
-     * @return object DateTime  PHP datetime instance
-     */
-    public static function php_datetime($cdt)
-    {
-        if (!is_object($cdt) || !$cdt->isValid())
-            return null;
-
-        $d = new DateTime;
-        $d->setTimezone(self::$timezone);
-
-        try {
-            if ($tzs = $cdt->timezone()) {
-                $tz = new DateTimeZone($tzs);
-                $d->setTimezone($tz);
-            }
-            else if ($cdt->isUTC()) {
-                $d->setTimezone(new DateTimeZone('UTC'));
-            }
-        }
-        catch (Exception $e) { }
-
-        $d->setDate($cdt->year(), $cdt->month(), $cdt->day());
-
-        if ($cdt->isDateOnly()) {
-            $d->_dateonly = true;
-            $d->setTime(12, 0, 0);  // set time to noon to avoid timezone troubles
-        }
-        else {
-            $d->setTime($cdt->hour(), $cdt->minute(), $cdt->second());
-        }
-
-        return $d;
-    }
-
-    /**
      * Parse the X-Kolab-Type header from MIME messages and return the object type in short form
      *
      * @param string X-Kolab-Type header value





More information about the commits mailing list