From 5d54e8c342765f1c0586e21cc2311d186d0b4e84 Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@enhancesoft.com>
Date: Sun, 24 Nov 2013 20:57:14 +0000
Subject: [PATCH] Add routine to change ticket owner to ticket class Add
 ability to change ticket owner on ticket view Add get last updated date to
 user class

---
 include/class.ticket.php          | 27 +++++++++++++++++++++++++++
 include/staff/ticket-view.inc.php | 22 ++++++++++++++++++++++
 scp/tickets.php                   | 11 +++++++++++
 3 files changed, 60 insertions(+)

diff --git a/include/class.ticket.php b/include/class.ticket.php
index 647a246e4..a2283c242 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -1327,6 +1327,33 @@ class Ticket {
         return $this->unassign();
     }
 
+    //Change ownership
+    function changeOwner($user) {
+        global $thisstaff;
+
+        if (!$user
+                || ($user->getId() == $this->getOwnerId())
+                || !$thisstaff->canEditTickets())
+            return false;
+
+        $sql ='UPDATE '.TICKET_TABLE.' SET updated = NOW() '
+            .', user_id = '.db_input($user->getId())
+            .' WHERE ticket_id = '.db_input($this->getId());
+
+        if (!db_query($sql) || !db_affected_rows())
+            return false;
+
+        $this->ht['user_id'] = $user->getId();
+        $this->user = null;
+
+        $this->logNote('Ticket ownership changed',
+                Format::htmlchars( sprintf('%s changed ticket ownership to %s',
+                    $thisstaff->getName(), $user->getName()))
+                );
+
+        return true;
+    }
+
     //Insert message from client
     function postMessage($vars, $origin='', $alerts=true) {
         global $cfg;
diff --git a/include/staff/ticket-view.inc.php b/include/staff/ticket-view.inc.php
index d60bd2c73..719d19f9e 100644
--- a/include/staff/ticket-view.inc.php
+++ b/include/staff/ticket-view.inc.php
@@ -67,6 +67,24 @@ if($ticket->isOverdue())
             } ?>
             <?php
             if($thisstaff->canEditTickets()) { ?>
+            <a class="action-button" href="#"
+                onclick="javascript:
+                    $.userLookup('ajax.php/tickets/<?php echo $ticket->getId(); ?>/change-user',
+                        function(user) {
+                            var cid = <?php echo $ticket->getOwnerId(); ?>;
+                            if(cid!=user.id && $('.dialog#confirm-action #changeuser-confirm').length) {
+                                $('#newuser').html(user.name +' <'+user.email+'>');
+                                $('.dialog#confirm-action #action').val('changeuser');
+                                $('#confirm-form').append('<input type=hidden name=user_id value='+user.id+' />');
+                                $('#overlay').show();
+                                $('.dialog#confirm-action .confirm-action').hide();
+                                $('.dialog#confirm-action p#changeuser-confirm')
+                                .show()
+                                .parent('div').show().trigger('click');
+                            }
+                       });
+                    return false;
+                "><i class="icon-edit"></i> Change Owner</a>
                 <a class="action-button" href="tickets.php?id=<?php echo $ticket->getId(); ?>&a=edit"><i class="icon-edit"></i> Edit</a>
             <?php
             } ?>
@@ -881,6 +899,10 @@ $tcount+= $ticket->getNumNotes();
     <p class="confirm-action" style="display:none;" id="release-confirm">
         Are you sure want to <b>unassign</b> ticket from <b><?php echo $ticket->getAssigned(); ?></b>?
     </p>
+    <p class="confirm-action" style="display:none;" id="changeuser-confirm">
+        Are you sure want to <b>change</b> ticket owner to <b><span id="newuser">this guy</span></b>?
+        <br><br><b><?php echo $ticket->getName(); ?></b> will no longer have access to the ticket.
+    </p>
     <p class="confirm-action" style="display:none;" id="delete-confirm">
         <font color="red"><strong>Are you sure you want to DELETE this ticket?</strong></font>
         <br><br>Deleted tickets CANNOT be recovered, including any associated attachments.
diff --git a/scp/tickets.php b/scp/tickets.php
index 8630ce3a0..b7ac97fe1 100644
--- a/scp/tickets.php
+++ b/scp/tickets.php
@@ -332,6 +332,17 @@ if($_POST && !$errors):
                         $errors['err']='Unable to remove the email from banlist. Try again.';
                     }
                     break;
+                case 'changeuser':
+                    if (!$thisstaff->canEditTickets()) {
+                        $errors['err'] = 'Perm. Denied. You are not allowed to EDIT tickets!!';
+                    } elseif (!$_POST['user_id'] || !($user=User::lookup($_POST['user_id']))) {
+                        $errors['err'] = 'Unknown user selected!';
+                    } elseif ($ticket->changeOwner($user)) {
+                        $msg = 'Ticket ownership changed to '.$user->getName();
+                    } else {
+                        $errors['err'] = 'Unable to change tiket ownership. Try again';
+                    }
+                    break;
                 case 'delete': // Dude what are you trying to hide? bad customer support??
                     if(!$thisstaff->canDeleteTickets()) {
                         $errors['err']='Perm. Denied. You are not allowed to DELETE tickets!!';
-- 
GitLab