Fix various issues with changing the owner of a calendar or task list.
authorJan Schneider <jan@horde.org>
Tue, 7 Dec 2010 14:45:59 +0000 (15:45 +0100)
committerJan Schneider <jan@horde.org>
Tue, 7 Dec 2010 14:45:59 +0000 (15:45 +0100)
kronolith/js/kronolith.js
kronolith/lib/Ajax/Application.php

index 9cff592..104faf8 100644 (file)
@@ -3438,6 +3438,12 @@ KronolithCore = {
     {
         var type = form.id.replace(/kronolithCalendarForm/, '');
 
+        // If saving the calendar changed the owner, we need to delete
+        // and re-insert the calendar.
+        if (r.response.deleted) {
+            this.deleteCalendar(type, data.calendar);
+            delete data.calendar;
+        }
         if (r.response.saved) {
             if ($F('kronolithCalendarinternalImport')) {
                 var name = 'kronolithIframe' + Math.round(Math.random() * 1000),
@@ -3447,21 +3453,11 @@ KronolithCore = {
                 form.submit();
             }
             if (data.calendar) {
-                var cal = Kronolith.conf.calendars[type][data.calendar],
+                var cal = r.response.calendar,
                     color = {
-                        backgroundColor: data.color,
-                        color: r.response.color
+                        backgroundColor: cal.bg,
+                        color: cal.fg
                     };
-                cal.bg = data.color;
-                cal.fg = r.response.color;
-                cal.name = data.name;
-                cal.desc = data.description;
-                if (r.response.perms) {
-                    cal.perms = r.response.perms;
-                }
-                if (data.tags) {
-                    cal.tg = data.tags.split(',');
-                }
                 this.getCalendarList(type, cal.owner).select('div').each(function(element) {
                     if (element.retrieve('calendar') == data.calendar) {
                         element
@@ -3476,28 +3472,14 @@ KronolithCore = {
                         el.setStyle(color);
                     }
                 });
+                Kronolith.conf.calendars[type][data.calendar] = cal;
             } else {
-                var cal = {
-                    bg: data.color,
-                    fg: r.response.color,
-                    name: data.name,
-                    desc: data.description,
-                    edit: true,
-                    owner: true,
-                    show: true
-                };
-                if (r.response.perms) {
-                    cal.perms = r.response.perms;
-                }
-                if (data.tags) {
-                    cal.tg = data.tags.split(',');
-                }
                 if (!Kronolith.conf.calendars[type]) {
                     Kronolith.conf.calendars[type] = [];
                 }
-                Kronolith.conf.calendars[type][r.response.calendar] = cal;
-                this.insertCalendarInList(type, r.response.calendar, cal);
-                this.storeCache($H(), [type, r.response.calendar], this.viewDates(this.date, this.view), true);
+                Kronolith.conf.calendars[type][r.response.id] = r.response.calendar;
+                this.insertCalendarInList(type, r.response.id, r.response.calendar);
+                this.storeCache($H(), [type, r.response.id], this.viewDates(this.date, this.view), true);
             }
         }
         form.down('.kronolithCalendarSave').enable();
@@ -3506,6 +3488,37 @@ KronolithCore = {
     },
 
     /**
+     * Deletes a calendar and all its events from the interface and cache.
+     *
+     * @param string type      The calendar type.
+     * @param string calendar  The calendar id.
+     */
+    deleteCalendar: function(type, calendar)
+    {
+        var container = this.getCalendarList(type, Kronolith.conf.calendars[type][calendar].owner),
+            noItems = container.previous(),
+            div = container.select('div').find(function(element) {
+                return element.retrieve('calendar') == calendar;
+            }),
+            arrow = div.previous('span');
+        arrow.purge();
+        arrow.remove();
+        div.purge();
+        div.remove();
+        if (noItems &&
+            noItems.tagName == 'DIV' &&
+            noItems.className == 'kronolithDialogInfo' &&
+            !container.childElements().size()) {
+            noItems.show();
+        }
+        this.deleteCache(null, [type, calendar]);
+        this.kronolithBody.select('div.kronolithEvent').findAll(function(el) {
+            return el.retrieve('calendar') == type + '|' + calendar;
+        }).invoke('remove');
+        delete Kronolith.conf.calendars[type][calendar];
+    },
+
+    /**
      * Parses a date attribute string into a Date object.
      *
      * For other strings use Date.parse().
@@ -4562,27 +4575,7 @@ KronolithCore = {
                               { type: type, calendar: calendar },
                               function(r) {
                                   if (r.response.deleted) {
-                                      var container = this.getCalendarList(type, Kronolith.conf.calendars[type][calendar].owner),
-                                          noItems = container.previous(),
-                                          div = container.select('div').find(function(element) {
-                                              return element.retrieve('calendar') == calendar;
-                                          }),
-                                          arrow = div.previous('span');
-                                      arrow.purge();
-                                      arrow.remove();
-                                      div.purge();
-                                      div.remove();
-                                      if (noItems &&
-                                          noItems.tagName == 'DIV' &&
-                                          noItems.className == 'kronolithDialogInfo' &&
-                                          !container.childElements().size()) {
-                                          noItems.show();
-                                      }
-                                      this.deleteCache(null, [type, calendar]);
-                                      this.kronolithBody.select('div.kronolithEvent').findAll(function(el) {
-                                          return el.retrieve('calendar') == type + '|' + calendar;
-                                      }).invoke('remove');
-                                      delete Kronolith.conf.calendars[type][calendar];
+                                      this.deleteCalendar(type, calendar);
                                   }
                                   this.closeRedBox();
                                   this.go(this.lastLocation);
index a067a0f..bcaa79f 100644 (file)
@@ -621,14 +621,18 @@ class Kronolith_Ajax_Application extends Horde_Core_Ajax_Application
                 try {
                     $calendar = Kronolith::addShare($info);
                     Kronolith::readPermsForm($calendar);
-                    $result->perms = Kronolith::permissionToJson($calendar->getPermission());
+                    $tagger->tag($result->calendar, $this->_vars->tags, $calendar->get('owner'), 'calendar');
+                    if ($calendar->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::SHOW)) {
+                        $wrapper = new Kronolith_Calendar_Internal(array('share' => $calendar));
+                        $result->saved = true;
+                        $result->id = $calendar->getName();
+                        $result->calendar = $wrapper->toHash();
+                    }
                 } catch (Exception $e) {
                     $GLOBALS['notification']->push($e, 'horde.error');
                     return $result;
                 }
                 $GLOBALS['notification']->push(sprintf(_("The calendar \"%s\" has been created."), $info['name']), 'horde.success');
-                $result->calendar = $calendar->getName();
-                $tagger->tag($result->calendar, $this->_vars->tags, $calendar->get('owner'), 'calendar');
                 break;
             }
 
@@ -636,16 +640,23 @@ class Kronolith_Ajax_Application extends Horde_Core_Ajax_Application
             try {
                 $calendar = $GLOBALS['kronolith_shares']->getShare($calendar_id);
                 $original_name = $calendar->get('name');
+                $original_owner = $calendar->get('owner');
                 Kronolith::updateShare($calendar, $info);
                 Kronolith::readPermsForm($calendar);
-                $result->perms = Kronolith::permissionToJson($calendar->getPermission());
+                if ($calendar->get('owner') != $original_owner) {
+                    $result->deleted = true;
+                }
+                if ($calendar->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::SHOW)) {
+                    $wrapper = new Kronolith_Calendar_Internal(array('share' => $calendar));
+                    $result->saved = true;
+                    $result->calendar = $wrapper->toHash();
+                }
+                $tagger->replaceTags($calendar->getName(), $this->_vars->tags, $calendar->get('owner'), 'calendar');
             } catch (Exception $e) {
                 $GLOBALS['notification']->push($e, 'horde.error');
                 return $result;
 
             }
-            $tagger->replaceTags($calendar->getName(), $this->_vars->tags, $calendar->get('owner'), 'calendar');
-
             if ($calendar->get('name') != $original_name) {
                 $GLOBALS['notification']->push(sprintf(_("The calendar \"%s\" has been renamed to \"%s\"."), $original_name, $calendar->get('name')), 'horde.success');
             } else {
@@ -673,17 +684,27 @@ class Kronolith_Ajax_Application extends Horde_Core_Ajax_Application
                         return $result;
                     }
                     $tasklist = $tasklists[$tasklistId];
-                    //$GLOBALS['session']['kronolith:all_external_calendars']['tasks/' . $tasklistId] = new Kronolith_Calendar_External(array('api' => 'tasks', 'name' => $tasklist->get('name')));
-                    $GLOBALS['display_external_calendars'][] = 'tasks/' . $tasklistId;
-                    $GLOBALS['prefs']->setValue('display_external_cals', serialize($GLOBALS['display_external_calendars']));
                     Kronolith::readPermsForm($tasklist);
-                    $result->perms = Kronolith::permissionToJson($tasklist->getPermission());
+                    if ($tasklist->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::SHOW)) {
+                        $wrapper = new Kronolith_Calendar_External_Tasks(array('api' => 'tasks', 'name' => $tasklistId, 'share' => $tasklist));
+
+                        // Update external calendars caches.
+                        $all_external = $GLOBALS['session']->get('kronolith', 'all_external_calendars');
+                        $all_external[] = array('a' => 'tasks', 'n' => $tasklistId, 'd' => $tasklist->get('name'));
+                        $GLOBALS['session']->set('kronolith', 'all_external_calendars', $all_external);
+                        $GLOBALS['display_external_calendars'][] = 'tasks/' . $tasklistId;
+                        $GLOBALS['prefs']->setValue('display_external_cals', serialize($GLOBALS['display_external_calendars']));
+                        $GLOBALS['all_external_calendars']['tasks/' . $tasklistId] = $wrapper;
+
+                        $result->saved = true;
+                        $result->id = 'tasks/' . $tasklistId;
+                        $result->calendar = $wrapper->toHash();
+                    }
                 } catch (Exception $e) {
                     $GLOBALS['notification']->push($e, 'horde.error');
                     return $result;
                 }
                 $GLOBALS['notification']->push(sprintf(_("The task list \"%s\" has been created."), $calendar['name']), 'horde.success');
-                $result->calendar = $tasklist;
                 break;
             }
 
@@ -692,16 +713,25 @@ class Kronolith_Ajax_Application extends Horde_Core_Ajax_Application
             try {
                 $GLOBALS['registry']->tasks->updateTasklist($calendar_id, $calendar);
                 $tasklists = $GLOBALS['registry']->tasks->listTasklists(true, Horde_Perms::EDIT);
-                Kronolith::readPermsForm($tasklists[$calendar_id]);
-                $result->perms = Kronolith::permissionToJson($tasklists[$calendar_id]->getPermission());
+                $tasklist = $tasklists[$calendar_id];
+                $original_owner = $tasklist->get('owner');
+                Kronolith::readPermsForm($tasklist);
+                if ($tasklist->get('owner') != $original_owner) {
+                    $result->deleted = true;
+                }
+                if ($tasklist->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::SHOW)) {
+                    $wrapper = new Kronolith_Calendar_External_Tasks(array('api' => 'tasks', 'name' => $calendar_id, 'share' => $tasklist));
+                    $result->saved = true;
+                    $result->calendar = $wrapper->toHash();
+                }
             } catch (Exception $e) {
                 $GLOBALS['notification']->push($e, 'horde.error');
                 return $result;
             }
-            if ($tasklists[$calendar_id]->get('name') != $calendar['name']) {
-                $GLOBALS['notification']->push(sprintf(_("The task list \"%s\" has been renamed to \"%s\"."), $tasklists[$calendar_id]->get('name'), $calendar['name']), 'horde.success');
+            if ($tasklist->get('name') != $calendar['name']) {
+                $GLOBALS['notification']->push(sprintf(_("The task list \"%s\" has been renamed to \"%s\"."), $tasklist->get('name'), $calendar['name']), 'horde.success');
             } else {
-                $GLOBALS['notification']->push(sprintf(_("The task list \"%s\" has been saved."), $tasklists[$calendar_id]->get('name')), 'horde.success');
+                $GLOBALS['notification']->push(sprintf(_("The task list \"%s\" has been saved."), $tasklist->get('name')), 'horde.success');
             }
             break;
 
@@ -720,14 +750,13 @@ class Kronolith_Ajax_Application extends Horde_Core_Ajax_Application
                 $GLOBALS['notification']->push(sprintf(_("The calendar \"%s\" has been saved."), $calendar['name']), 'horde.success');
             } else {
                 $GLOBALS['notification']->push(sprintf(_("You have been subscribed to \"%s\" (%s)."), $calendar['name'], $calendar['url']), 'horde.success');
-                $result->calendar = $calendar['url'];
+                $result->id = $calendar['url'];
             }
+            $result->saved = true;
+            $result->color = Kronolith::foregroundColor($calendar);
             break;
         }
 
-        $result->saved = true;
-        $result->color = Kronolith::foregroundColor($calendar);
-
         return $result;
     }