Implement task deletion.
authorJan Schneider <jan@horde.org>
Mon, 16 Nov 2009 18:14:12 +0000 (19:14 +0100)
committerJan Schneider <jan@horde.org>
Mon, 16 Nov 2009 18:14:12 +0000 (19:14 +0100)
kronolith/ajax.php
kronolith/js/kronolith.js
kronolith/templates/index/task.inc
nag/lib/Api.php

index 0f753ef..9288db9 100644 (file)
@@ -370,6 +370,23 @@ try {
         $result->task = $task->toJson(true, $prefs->getValue('twentyFour') ? 'H:i' : 'h:i A');
         break;
 
+    case 'DeleteTask':
+        if (!$registry->hasMethod('tasks/deleteTask')) {
+            break;
+        }
+        if (is_null($id = Horde_Util::getFormData('id')) ||
+            is_null($list = Horde_Util::getFormData('list'))) {
+            break;
+        }
+        $result = $registry->tasks->deleteTask($list, $id);
+        if (is_a($result, 'PEAR_Error')) {
+            $notification->push($result, 'horde.error');
+            break;
+        }
+        $result = new stdClass;
+        $result->deleted = true;
+        break;
+
     case 'ToggleCompletion':
         if (!$registry->hasMethod('tasks/toggleCompletion')) {
             break;
index e16188c..bbb7c5e 100644 (file)
@@ -1782,6 +1782,7 @@ KronolithCore = {
             this.doAction('GetTask', { 'list': tasklist, 'id': id }, this._editTask.bind(this));
         } else {
             $('kronolithTaskId').clear();
+            $('kronolithTaskOldList').clear();
             $('kronolithTaskList').setValue(Kronolith.conf.default_tasklist);
             $('kronolithTaskDelete').hide();
             $('kronolithTaskDueDate').setValue(d.toString(Kronolith.conf.date_format));
@@ -1808,6 +1809,7 @@ KronolithCore = {
 
         /* Basic information */
         $('kronolithTaskId').setValue(task.id);
+        $('kronolithTaskOldList').setValue(task.l);
         $('kronolithTaskList').setValue(task.l);
         $('kronolithTaskTitle').setValue(task.n);
         //$('kronolithTaskLocation').setValue(task.l);
@@ -1853,6 +1855,21 @@ KronolithCore = {
     },
 
     /**
+     * Finally removes a task from the DOM and the cache.
+     *
+     * @param string task  A task id.
+     * @param string list  A task list name.
+     */
+    _removeTask: function(task, list)
+    {
+        this._deleteTasksCache(task, list);
+        $('kronolithViewTasksBody').select('tr').find(function(el) {
+            return el.retrieve('tasklist') == list &&
+                el.retrieve('taskid') == task;
+        }).remove();
+    },
+
+    /**
      * Parses a date attribute string into a Date object.
      *
      * For other strings use Date.parse().
@@ -2019,6 +2036,23 @@ KronolithCore = {
     },
 
     /**
+     * Deletes a task from the cache.
+     *
+     * @param string task  A task ID.
+     * @param string list  A task list string.
+     */
+    _deleteTasksCache: function(task, list)
+    {
+        this._deleteCache(task, [ 'external', 'tasks/' + list ]);
+        [ 'complete', 'incomplete' ].each(function(type) {
+            if (!Object.isUndefined(this.tcache.get(type)) &&
+                !Object.isUndefined(this.tcache.get(type).get(list))) {
+                this.tcache.get(type).get(list).unset(task);
+            }
+        }, this);
+    },
+
+    /**
      * Return all events for a single day from all displayed calendars merged
      * into a single hash.
      *
@@ -2249,6 +2283,30 @@ KronolithCore = {
                 e.stop();
                 return;
 
+            case 'kronolithTaskDelete':
+                var tasklist = $F('kronolithTaskOldList'),
+                    taskid = $F('kronolithTaskId');
+                this.doAction('DeleteTask',
+                              { 'list': tasklist, 'id': taskid },
+                              function(r) {
+                                  if (r.response.deleted) {
+                                      this._removeTask(taskid, tasklist);
+                                  } else {
+                                      $('kronolithViewTasksBody').select('tr').find(function(el) {
+                                          return el.retrieve('tasklist') == tasklist &&
+                                              el.retrieve('taskid') == taskid;
+                                      }).toggle();
+                                  }
+                              }.bind(this));
+                $('kronolithViewTasksBody').select('tr').find(function(el) {
+                    return el.retrieve('tasklist') == tasklist &&
+                        el.retrieve('taskid') == taskid;
+                }).hide();
+                this._closeRedBox();
+                window.history.back();
+                e.stop();
+                return;
+
             case 'kronolithEventCancel':
             case 'kronolithTaskCancel':
                 this._closeRedBox();
index b596feb..1f95610 100644 (file)
@@ -1,6 +1,7 @@
 <div id="kronolithTaskDialog" style="display:none">
 <form id="kronolithTaskForm" action="">
 <input id="kronolithTaskId" type="hidden" name="task_id" />
+<input id="kronolithTaskOldList" type="hidden" name="old_tasklist" />
 
 <div>
   <label for="kronolithTaskTitle"><?php echo _("Name") ?>:</label><br />
index 8db58c6..48aad71 100644 (file)
@@ -1036,14 +1036,34 @@ class Nag_Api extends Horde_Registry_Api
 
         if (!Horde_Auth::isAdmin() &&
             !array_key_exists($task->tasklist,
-                Nag::listTasklists(false, PERMS_DELETE))) {
-                    return PEAR::raiseError(_("Permission Denied"));
-                }
+                              Nag::listTasklists(false, PERMS_DELETE))) {
+            return PEAR::raiseError(_("Permission Denied"));
+        }
 
         return $storage->delete($task->id);
     }
 
     /**
+     * Deletes a task identified by tasklist and ID.
+     *
+     * @param string $tasklist  A tasklist id.
+     * @param string $id        A task id.
+     */
+    public function deleteTask($tasklist, $id)
+    {
+        require_once dirname(__FILE__) . '/base.php';
+
+        if (!Horde_Auth::isAdmin() &&
+            !array_key_exists($tasklist,
+                              Nag::listTasklists(false, PERMS_DELETE))) {
+            return PEAR::raiseError(_("Permission Denied"));
+        }
+
+        $storage = Nag_Driver::singleton($tasklist);
+        return $storage->delete($id);
+    }
+
+    /**
      * Replaces the task identified by UID with the content represented in the
      * specified content type.
      *