MFB: Add option to delete individual ticket history entries.
authorJan Schneider <jan@horde.org>
Fri, 30 Apr 2010 10:06:39 +0000 (12:06 +0200)
committerJan Schneider <jan@horde.org>
Fri, 30 Apr 2010 10:07:06 +0000 (12:07 +0200)
whups/docs/CHANGES
whups/lib/Driver/sql.php
whups/lib/Renderer/Comment.php
whups/ticket/delete_history.php [new file with mode: 0644]

index e86ffb0..ff32d5b 100644 (file)
@@ -9,7 +9,7 @@ v2.0-cvs
 v1.0.2-cvs
 ----------
 
-
+[jan] Add option to delete individual ticket history entries.
 
 
 ------
index 3fca21e..5357257 100644 (file)
@@ -1174,6 +1174,86 @@ class Whups_Driver_sql extends Whups_Driver {
     }
 
     /**
+     * Deletes all changes of a transaction.
+     *
+     * @param integer $transaction  A transaction id.
+     */
+    function deleteHistory($transaction)
+    {
+        $transaction = (int)$transaction;
+
+        /* Deleting comments. */
+        $query = 'SELECT log_value FROM whups_logs WHERE log_type = ? AND transaction_id = ?';
+        $values = array('comment', $transaction);
+        Horde::logMessage(
+            sprintf('Whups_Driver_sql::deleteTransaction(): query="%s"; values="%s"',
+                    $query, implode(',', $values)), 'DEBUG');
+        $comments = $this->_db->getCol($query, 'log_value', $values);
+        if (is_a($comments, 'PEAR_Error')) {
+            Horde::logMessage($comments, 'ERR');
+            return $comments;
+        }
+
+        if ($comments) {
+            $query = sprintf('DELETE FROM whups_comments WHERE comment_id IN (%s)',
+                             implode(',', $comments));
+            Horde::logMessage(
+                sprintf('Whups_Driver_sql::deleteTransaction(): query="%s"', $query), 'DEBUG');
+            $result = $this->_write_db->query($query);
+            if (is_a($result, 'PEAR_Error')) {
+                Horde::logMessage($result, 'ERR');
+                return $result;
+            }
+        }
+
+        /* Deleting attachments. */
+        if (isset($GLOBALS['conf']['vfs']['type'])) {
+            $query = 'SELECT ticket_id, log_value FROM whups_logs WHERE log_type = ? AND transaction_id = ?';
+            $values = array('attachment', $transaction);
+            Horde::logMessage(
+                sprintf('Whups_Driver_sql::deleteTransaction(): query="%s"; values="%s"',
+                        $query, implode(',', $values)), 'DEBUG');
+            $attachments = $this->_db->query($query, $values);
+            if (is_a($attachments, 'PEAR_Error')) {
+                Horde::logMessage($attachments, 'ERR');
+                return $attachments;
+            }
+            require_once 'VFS.php';
+            $vfs = &VFS::singleton($GLOBALS['conf']['vfs']['type'],
+                                   Horde::getDriverConfig('vfs'));
+            if (is_a($vfs, 'PEAR_Error')) {
+                return $vfs;
+            }
+
+            while ($attachment = $attachments->fetchRow(DB_FETCHMODE_ASSOC)) {
+                $dir = WHUPS_VFS_ATTACH_PATH . '/' . $attachment['ticket_id'];
+                if ($vfs->exists($dir, $attachment['log_value'])) {
+                    try {
+                        $result = $vfs->deleteFile($dir, $attachment['log_value']);
+                    } catch (VFS_Exception $e) {
+                        return PEAR::raiseError($e->getMessage());
+                    }
+                } else {
+                    Horde::logMessage(sprintf(_("Attachment %s not found."),
+                                              $attachment['log_value']),
+                                      'WARN');
+                }
+            }
+        }
+
+        $query = 'DELETE FROM whups_logs WHERE transaction_id = ?';
+        Horde::logMessage(
+            sprintf('Whups_Driver_sql::deleteTransaction(): query="%s"; values="%s"',
+                    $query, implode(',', $values)),
+           'DEBUG');
+        $result = $this->_write_db->query($query, array($transaction));
+        if (is_a($result, 'PEAR_Error')) {
+            Horde::logMessage($result, 'ERR');
+        }
+        return $result;
+    }
+
+    /**
      * Return a list of queues with open tickets, and the number of
      * open tickets in each.
      *
index 7614261..13c96df 100755 (executable)
@@ -167,6 +167,18 @@ class Horde_Form_Renderer_Comment extends Horde_Form_Renderer {
         }
 
         if (count($changes)) {
+            // Admins can delete entries.
+            $delete_link = '';
+            if (Whups::hasPermission($vars->get('queue'), 'queue', Horde_Perms::DELETE)) {
+                $delete_link = Horde::applicationUrl('ticket/delete_history.php')
+                    ->add(array('transaction' => $transaction,
+                                'id' => $vars->get('ticket_id'),
+                                'url' => Horde::selfUrl(true, false, true)))
+                    ->link(array('title' => _("Delete entry"), 'onclick' => 'return window.confirm(\'' . addslashes(_("Permanently delete entry?")) . '\');'))
+                    . Horde::img('delete.png', _("Delete entry"))
+                    . '</a>';
+            }
+
             ob_start();
             $class = $private ? 'pc' : 'c';
 ?>
@@ -176,7 +188,7 @@ class Horde_Form_Renderer_Comment extends Horde_Form_Renderer {
   <td width="20%" class="<?php echo $class ?>_l nowrap" valign="top"><?php echo strftime($prefs->getValue('date_format'), $vars->get('timestamp')) ?></td>
   <td width="20%" class="<?php echo $class ?>_m" valign="top"><?php echo $vars->get('user_id') ? Whups::formatUser($vars->get('user_id'), false, true, true) : '&nbsp;' ?></td>
   <td width="30%" class="<?php echo $class ?>_m" valign="top"><?php echo implode('<br />', $changes) ?></td>
-  <td width="30%" class="<?php echo $class ?>_r rightAlign" valign="top"><?php if ($comment && !$private) echo $reply ?></td>
+  <td width="30%" class="<?php echo $class ?>_r rightAlign" valign="top"><?php if ($comment && !$private) echo $reply . ' '; echo $delete_link; ?></td>
  </tr>
 <?php if ($comment): ?>
  <tr><td colspan="4" class="<?php echo $class ?>_b">
diff --git a/whups/ticket/delete_history.php b/whups/ticket/delete_history.php
new file mode 100644 (file)
index 0000000..b7973d3
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+/**
+ * Deletes a history entry from the ticket.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file LICENSE for license information (BSD). If you
+ * did not receive this file, see http://www.horde.org/licenses/bsdl.php.
+ */
+
+require_once dirname(__FILE__) . '/../lib/Application.php';
+Horde_Registry::appInit('whups');
+
+$ticket = Whups::getCurrentTicket();
+if (!Whups::hasPermission($ticket->get('queue'), 'queue', Horde_Perms::DELETE)) {
+    $notification->push(_("Permission Denied"), 'horde.error');
+    header('Location: ' . Horde::applicationUrl($prefs->getValue('whups_default_view') . '.php', true));
+    exit;
+}
+
+$vars = Horde_Variables::getDefaultVariables();
+$result = $whups_driver->deleteHistory($vars->get('transaction'));
+if (is_a($result, 'PEAR_Error')) {
+    $notification->push($result, 'horde.error');
+} else {
+    $notification->push(_("Entry deleted."), 'horde.success');
+}
+
+if ($url = Horde_Util::getFormData('url')) {
+    header('Location: ' . $url);
+} else {
+    header('Location: ' . Horde::applicationUrl($prefs->getValue('whups_default_view') . '.php', true));
+}