From a361e75b73f9d9d9215c53fc8826dba31128db5a Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@enhancesoft.com>
Date: Wed, 27 Nov 2013 16:17:23 +0000
Subject: [PATCH] Add email recepients as collaborators

---
 include/ajax.tickets.php    | 145 +++++-------------------------------
 include/api.tickets.php     |   2 +-
 include/class.mailfetch.php |  14 ++--
 include/class.mailparse.php |   6 +-
 include/class.ticket.php    |   7 ++
 5 files changed, 35 insertions(+), 139 deletions(-)

diff --git a/include/ajax.tickets.php b/include/ajax.tickets.php
index 93e90758c..c9c98b040 100644
--- a/include/ajax.tickets.php
+++ b/include/ajax.tickets.php
@@ -457,34 +457,23 @@ class TicketsAjaxAPI extends AjaxController {
                 || !$ticket->checkStaffAccess($thisstaff))
             Http::response(404, 'No such ticket');
 
-        //If not a post then assume new collaborator form
-        if(!$_POST)
-            return self::_addcollaborator($ticket);
-
-        $user = $form = null;
-        if (isset($_POST['id']) && $_POST['id']) { //Existing user/
-            $user =  User::lookup($_POST['id']);
-        } else { //We're creating a new user!
-            $form = UserForm::getUserForm()->getForm($_POST);
-            $user = User::fromForm($form);
-        }
-
         $errors = $info = array();
+        $user = null;
+        $form = UserForm::getInstance();
+        if ($form->isValid())
+            $user = User::fromForm($form->getClean());
+
         if ($user && ($c=$ticket->addCollaborator($user, $errors))) {
-            $info =array('msg' => sprintf('%s added as a collaborator',
+            $info +=array('msg' => sprintf('%s added as a collaborator',
                         $c->getName()));
-
-            return self::_collaborators($ticket, $info);
-        }
-
-        if($errors && $errors['err']) {
-            $info +=array('error' => $errors['err']);
+            $form = null;
+        } elseif($errors && $errors['err']) {
+            $info +=array('add_error' => $errors['err']);
         } else {
-            $info +=array('error' =>'Unable to add collaborator - try again');
+            $info +=array('add_error' =>'Errors occurred - try again');
         }
 
-
-        return self::_addcollaborator($ticket, $user, $form, $info);
+        return self::_collaborators($ticket, $form, $info);
     }
 
     function updateCollaborator($cid) {
@@ -504,7 +493,7 @@ class TicketsAjaxAPI extends AjaxController {
         $info = array('msg' => sprintf('%s updated successfully',
                     $c->getName()));
 
-        return self::_collaborators($ticket, $info);
+        return self::_collaborators($ticket, null, $info);
     }
 
     function viewCollaborator($cid) {
@@ -525,22 +514,7 @@ class TicketsAjaxAPI extends AjaxController {
                 || !$ticket->checkStaffAccess($thisstaff))
             Http::response(404, 'No such ticket');
 
-        if($ticket->getCollaborators())
-            return self::_collaborators($ticket);
-
-        return self::_addcollaborator($ticket);
-    }
-
-
-
-    function _addcollaborator($ticket, $user=null, $form=null, $info=array()) {
-
-        $info += array(
-                    'title' => sprintf('Ticket #%s: Add a collaborator', $ticket->getNumber()),
-                    'action' => sprintf('#tickets/%d/add-collaborator', $ticket->getId())
-                    );
-
-        return self::_userlookup($user, $form, $info);
+        return self::_collaborators($ticket);
     }
 
 
@@ -558,26 +532,22 @@ class TicketsAjaxAPI extends AjaxController {
             $info +=array('error' => $errors['err']);
         }
 
-        return self::_collaborators($ticket, $info);
+        return self::_collaborators($ticket, null, $info);
     }
 
 
 
-    function _collaborator($collaborator, $form=null, $info=array()) {
-
-        $info += array('action' => '#collaborators/'.$collaborator->getId());
-
-        $user = $collaborator->getUser();
+    function _collaborator($collaborator, $form=null, $errors=array()) {
 
         ob_start();
-        include(STAFFINC_DIR . 'templates/user.tmpl.php');
+        include(STAFFINC_DIR . 'templates/collaborator.tmpl.php');
         $resp = ob_get_contents();
         ob_end_clean();
 
         return $resp;
     }
 
-    function _collaborators($ticket, $info=array()) {
+    function _collaborators($ticket, $form=null, $info=array()) {
 
         ob_start();
         include(STAFFINC_DIR . 'templates/collaborators.tmpl.php');
@@ -586,86 +556,5 @@ class TicketsAjaxAPI extends AjaxController {
 
         return $resp;
     }
-
-    function viewUser($tid) {
-        global $thisstaff;
-
-        if(!$thisstaff
-                || !($ticket=Ticket::lookup($tid))
-                || !$ticket->checkStaffAccess($thisstaff))
-            Http::response(404, 'No such ticket');
-
-
-        if(!($user = $ticket->getOwner()))
-            Http::response(404, 'Unknown user');
-
-
-        $info = array(
-                'title' => sprintf('Ticket #%s: %s', $ticket->getNumber(), $user->getName())
-                );
-
-        ob_start();
-        include(STAFFINC_DIR . 'templates/user.tmpl.php');
-        $resp = ob_get_contents();
-        ob_end_clean();
-        return $resp;
-
-    }
-
-    function updateUser($tid) {
-
-        global $thisstaff;
-
-        if(!$thisstaff
-                || !($ticket=Ticket::lookup($tid))
-                || !$ticket->checkStaffAccess($thisstaff)
-                || ! ($user = $ticket->getOwner()))
-            Http::response(404, 'No such ticket/user');
-
-        $errors = array();
-        if($user->updateInfo($_POST, $errors))
-             Http::response(201, $user->to_json());
-
-        $forms = $user->getForms();
-
-        $info = array(
-                'title' => sprintf('Ticket #%s: %s', $ticket->getNumber(), $user->getName())
-                );
-
-        ob_start();
-        include(STAFFINC_DIR . 'templates/user.tmpl.php');
-        $resp = ob_get_contents();
-        ob_end_clean();
-        return $resp;
-    }
-
-    function changeUserForm($tid) {
-        global $thisstaff;
-
-        if(!$thisstaff
-                || !($ticket=Ticket::lookup($tid))
-                || !$ticket->checkStaffAccess($thisstaff))
-            Http::response(404, 'No such ticket');
-
-
-        $user = $ticket->getOwner();
-
-        $info = array(
-                'title' => sprintf('Change user for ticket #%s', $ticket->getNumber())
-                );
-
-        return self::_userlookup($user, $info);
-    }
-
-    function _userlookup($user, $form, $info) {
-
-        ob_start();
-        include(STAFFINC_DIR . 'templates/user-lookup.tmpl.php');
-        $resp = ob_get_contents();
-        ob_end_clean();
-        return $resp;
-
-    }
-
 }
 ?>
diff --git a/include/api.tickets.php b/include/api.tickets.php
index e0a761e27..45cbc2db9 100644
--- a/include/api.tickets.php
+++ b/include/api.tickets.php
@@ -40,7 +40,7 @@ class TicketApiController extends ApiController {
             $supported = array_merge($supported, array('header', 'mid',
                 'emailId', 'ticketId', 'reply-to', 'reply-to-name',
                 'in-reply-to', 'references',
-                'collaborators' => array("*" => array("name", "email"))
+                'recipients' => array("*" => array("name", "email"))
                 ));
 
             $supported['attachments']['*'][] = 'cid';
diff --git a/include/class.mailfetch.php b/include/class.mailfetch.php
index 97805cd52..2b05c3a59 100644
--- a/include/class.mailfetch.php
+++ b/include/class.mailfetch.php
@@ -290,10 +290,10 @@ class MailFetcher {
         if($headerinfo->cc)
             $tolist = array_merge($tolist, $headerinfo->cc);
 
-        $header['collaborators'] = array();
+        $header['recipients'] = array();
         foreach($tolist as $addr) {
             if(!($emailId=Email::getIdByEmail(strtolower($addr->mailbox).'@'.$addr->host))) {
-                $header['collaborators'][] = array(
+                $header['recipients'][] = array(
                         'name' => $this->mime_decode(@$addr->personal),
                         'email' => strtolower($addr->mailbox).'@'.$addr->host);
             } elseif(!$header['emailId']) {
@@ -303,12 +303,12 @@ class MailFetcher {
 
         //BCCed?
         if(!$header['emailId']) {
-            unset($header['collaborators']); //Nuke the recipients - we were bcced
-            if($headerinfo->bcc)
+            unset($header['recipients']); //Nuke the recipients - we were bcced
+            if ($headerinfo->bcc) {
                 foreach($headerinfo->bcc as $addr)
-                    if(!$header['emailId']
-                            && ($emailId=Email::getIdByEmail(strtolower($addr->mailbox).'@'.$addr->host)))
-                        $header['emailId'] = $emailId;
+                    if (($header['emailId'] = Email::getIdByEmail(strtolower($addr->mailbox).'@'.$addr->host)))
+                        break;
+            }
         }
 
         // Ensure we have a message-id. If unable to read it out of the
diff --git a/include/class.mailparse.php b/include/class.mailparse.php
index c1f4700c4..dfc4c6b3f 100644
--- a/include/class.mailparse.php
+++ b/include/class.mailparse.php
@@ -386,7 +386,7 @@ class EmailDataParser {
 
         //TO Address:Try to figure out the email address... associated with the incoming email.
         $data['emailId'] = 0;
-        $data['collaborators'] = array();
+        $data['recipients'] = array();
         $tolist = array();
         if(($to = $parser->getToAddressList()))
             $tolist = array_merge($tolist, $to);
@@ -396,7 +396,7 @@ class EmailDataParser {
 
         foreach ($tolist as $addr) {
             if(!($emailId=Email::getIdByEmail(strtolower($addr->mailbox).'@'.$addr->host))) {
-                $data['collaborators'][] = array(
+                $data['recipients'][] = array(
                           'name' => trim(@$addr->personal, '"'),
                           'email' => strtolower($addr->mailbox).'@'.$addr->host);
             } elseif(!$data['emailId']) {
@@ -406,7 +406,7 @@ class EmailDataParser {
 
         //maybe we got BCC'ed??
         if(!$data['emailId']) {
-            unset($data['collaborators']);
+            unset($data['recipients']);
             $emailId =  0;
             if($bcc = $parser->getBccAddressList())
                 foreach ($bcc as $addr) {
diff --git a/include/class.ticket.php b/include/class.ticket.php
index e61bdf05d..648424ca6 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -1456,6 +1456,13 @@ class Ticket {
 
         $this->setLastMsgId($message->getId());
 
+        //Add email recipients as collaborators
+        if($vars['recipients']) {
+            foreach($vars['recipients'] as $recipient)
+                if(($user=User::fromVars($recipient)))
+                    $this->addCollaborator($user, $errors);
+        }
+
         if(!$alerts) return $message; //Our work is done...
 
         $autorespond = true;
-- 
GitLab