From 80acffa5cad95ecfca648ed6ce34acfa64359e88 Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Tue, 25 Feb 2014 20:33:33 +0000
Subject: [PATCH] Use up-to-date user/staff name instead of poster when
 displaying ticket thread

Join to staff/user tables to get current name when fetching thread entries.
---
 include/class.pdf.php             |  2 +-
 include/class.staff.php           | 15 +++++++++++----
 include/class.thread.php          | 10 +++++++++-
 include/staff/ticket-view.inc.php |  2 +-
 4 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/include/class.pdf.php b/include/class.pdf.php
index 8346ee3c7..51a752a15 100644
--- a/include/class.pdf.php
+++ b/include/class.pdf.php
@@ -291,7 +291,7 @@ class Ticket2PDF extends mPDF
                 $this->WriteCell($w/2, 7, Format::db_datetime($entry['created']), 'LTB', 0, 'L', true);
                 $this->SetFont('Arial', '', 10);
                 $this->WriteCell($w, 7, Format::truncate($entry['title'], 50), 'TB', 0, 'L', true);
-                $this->WriteCell($w/2, 7, $entry['poster'], 'TBR', 1, 'L', true);
+                $this->WriteCell($w/2, 7, $entry['name'] ?: $entry['poster'], 'TBR', 1, 'L', true);
                 $this->SetFont('');
                 $text= $entry['body'];
                 if($entry['attachments']
diff --git a/include/class.staff.php b/include/class.staff.php
index 1334a785d..819c68d28 100644
--- a/include/class.staff.php
+++ b/include/class.staff.php
@@ -569,16 +569,23 @@ class Staff extends AuthenticatedUser {
     function delete() {
         global $thisstaff;
 
-        if(!$thisstaff || !($id=$this->getId()) || $id==$thisstaff->getId())
+        if (!$thisstaff || $this->getId() == $thisstaff->getId())
             return 0;
 
-        $sql='DELETE FROM '.STAFF_TABLE.' WHERE staff_id='.db_input($id).' LIMIT 1';
+        $sql='DELETE FROM '.STAFF_TABLE
+            .' WHERE staff_id = '.db_input($this->getId()).' LIMIT 1';
         if(db_query($sql) && ($num=db_affected_rows())) {
             // DO SOME HOUSE CLEANING
             //Move remove any ticket assignments...TODO: send alert to Dept. manager?
-            db_query('UPDATE '.TICKET_TABLE.' SET staff_id=0 WHERE status=\'open\' AND staff_id='.db_input($id));
+            db_query('UPDATE '.TICKET_TABLE.' SET staff_id=0 WHERE staff_id='.db_input($this->getId()));
+
+            //Update the poster and clear staff_id on ticket thread table.
+            db_query('UPDATE '.TICKET_THREAD_TABLE
+                    .' SET staff_id=0, poster= '.db_input($this->getName()->getOriginal())
+                    .' WHERE staff_id='.db_input($this->getId()));
+
             //Cleanup Team membership table.
-            db_query('DELETE FROM '.TEAM_MEMBER_TABLE.' WHERE staff_id='.db_input($id));
+            db_query('DELETE FROM '.TEAM_MEMBER_TABLE.' WHERE staff_id='.db_input($this->getId()));
         }
 
         Signal::send('model.deleted', $this);
diff --git a/include/class.thread.php b/include/class.thread.php
index fec7c52c4..1ab319640 100644
--- a/include/class.thread.php
+++ b/include/class.thread.php
@@ -113,9 +113,17 @@ class Thread {
         if(!$order || !in_array($order, array('DESC','ASC')))
             $order='ASC';
 
-        $sql='SELECT thread.* '
+        $sql='SELECT thread.*
+               , COALESCE(user.name,
+                    IF(staff.staff_id,
+                        CONCAT_WS(" ", staff.firstname, staff.lastname),
+                        NULL)) as name '
             .' ,count(DISTINCT attach.attach_id) as attachments '
             .' FROM '.TICKET_THREAD_TABLE.' thread '
+            .' LEFT JOIN '.USER_TABLE.' user
+                ON (thread.user_id=user.id) '
+            .' LEFT JOIN '.STAFF_TABLE.' staff
+                ON (thread.staff_id=staff.staff_id) '
             .' LEFT JOIN '.TICKET_ATTACHMENT_TABLE.' attach
                 ON (thread.ticket_id=attach.ticket_id
                         AND thread.id=attach.ref_id) '
diff --git a/include/staff/ticket-view.inc.php b/include/staff/ticket-view.inc.php
index 7449197a1..1a17a054e 100644
--- a/include/staff/ticket-view.inc.php
+++ b/include/staff/ticket-view.inc.php
@@ -351,7 +351,7 @@ $tcount+= $ticket->getNumNotes();
                         <span style="vertical-align:middle;" class="textra"></span>
                         <span style="vertical-align:middle;"
                             class="tmeta faded title"><?php
-                            echo Format::htmlchars($entry['poster']); ?></span>
+                            echo Format::htmlchars($entry['name'] ?: $entry['poster']); ?></span>
                     </span>
                 </div>
                 </th>
-- 
GitLab