From 903636ec8c7162de235206868917611edeeff7a2 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Fri, 8 Nov 2013 03:32:03 +0000
Subject: [PATCH] Cleanup drafts on ticket processing

Before, the draft was only cleaned up when progress on the ticket was
tracked with the reply box.
---
 include/class.ticket.php |  7 +++++++
 scp/tickets.php          | 22 +++++++++-------------
 2 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/include/class.ticket.php b/include/class.ticket.php
index c652f8f3f..692c6e53e 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -728,6 +728,7 @@ class Ticket {
 
         $this->reload();
         $this->logEvent('closed');
+        $this->deleteDrafts();
 
         return true;
     }
@@ -1676,9 +1677,15 @@ class Ticket {
         foreach (DynamicFormEntry::forTicket($this->getId()) as $form)
             $form->delete();
 
+        $this->deleteDrafts();
+
         return true;
     }
 
+    function deleteDrafts() {
+        Draft::deleteForNamespace('ticket.%.' . $this->getId());
+    }
+
     function update($vars, &$errors) {
 
         global $cfg, $thisstaff;
diff --git a/scp/tickets.php b/scp/tickets.php
index 1058b80d9..c55c4f27f 100644
--- a/scp/tickets.php
+++ b/scp/tickets.php
@@ -70,13 +70,13 @@ if($_POST && !$errors):
                 $msg='Reply posted successfully';
                 $ticket->reload();
 
-                // Cleanup drafts for the ticket. If not closed, only clean
-                // for this staff. Else clean all drafts for the ticket.
-                Draft::deleteForNamespace('ticket.%.' . $ticket->getId(),
-                    $ticket->isClosed() ? false : $thisstaff->getId());
-
                 if($ticket->isClosed() && $wasOpen)
                     $ticket=null;
+                else
+                    // Still open -- cleanup response draft for this user
+                    Draft::deleteForNamespace(
+                        'ticket.response.' . $ticket->getId(),
+                        $thisstaff->getId());
 
             } elseif(!$errors['err']) {
                 $errors['err']='Unable to post the reply. Correct the errors below and try again!';
@@ -174,17 +174,13 @@ if($_POST && !$errors):
             $wasOpen = ($ticket->isOpen());
             if(($note=$ticket->postNote($vars, $errors, $thisstaff))) {
 
-                // Cleanup drafts for the ticket. If not closed, only clean
-                // note drafts for this staff. Else clean all drafts for the ticket.
-                Draft::deleteForNamespace(
-                        sprintf('ticket.%s.%d',
-                              $ticket->isClosed() ? '%' : 'note',
-                              $ticket->getId()),
-                        $ticket->isOpen() ? $thisstaff->getId() : false);
-
                 $msg='Internal note posted successfully';
                 if($wasOpen && $ticket->isClosed())
                     $ticket = null; //Going back to main listing.
+                else
+                    // Ticket is still open -- clear draft for the note
+                    Draft::deleteForNamespace('ticket.note.'.$ticket->getId(),
+                        $thisstaff->getId());
 
             } else {
 
-- 
GitLab