From 8c707b5d55c9b2cb8253ab7cfebcd248646e43a2 Mon Sep 17 00:00:00 2001
From: aydreeihn <adriane@enhancesoft.com>
Date: Fri, 20 Jul 2018 13:01:32 -0500
Subject: [PATCH] Recipients Icon + View Email Recipients for Users

This commit puts the recipient icon (for one recipient or multiple) on Thread Entries created by Users. It also stores email recipients so you can view them in the Thread Entry Actions.

For Emails:
All recipients in the email are tracked

For front end entries:
All recipients who received an alert are tracked
---
 include/class.thread.php | 20 +++++++++++---------
 include/class.ticket.php | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+), 9 deletions(-)

diff --git a/include/class.thread.php b/include/class.thread.php
index b1a0f1bf7..d371f3da2 100644
--- a/include/class.thread.php
+++ b/include/class.thread.php
@@ -1505,8 +1505,18 @@ implements TemplateVariable {
         ));
 
         //add recipients to thread entry
-        if ($vars['thread_entry_recipients'])
+        if ($vars['thread_entry_recipients']) {
+            $count = 0;
+            foreach ($vars['thread_entry_recipients'] as $key => $value)
+                $count = $count + count($value);
+
+            if ($count > 1)
+                $entry->flags |= ThreadEntry::FLAG_REPLY_ALL;
+            else
+                $entry->flags |= ThreadEntry::FLAG_REPLY_USER;
+
             $entry->recipients = json_encode($vars['thread_entry_recipients']);
+        }
 
 
         if (Collaborator::getIdByUserId($vars['userId'], $vars['threadId']))
@@ -2866,14 +2876,6 @@ implements TemplateVariable {
         $vars['pid'] = $this->getLastMessage()->getId();
 
         $vars['flags'] = 0;
-        switch ($vars['reply-to']) {
-            case 'all':
-                $vars['flags'] |= ThreadEntry::FLAG_REPLY_ALL;
-            break;
-            case 'user':
-                $vars['flags'] |= ThreadEntry::FLAG_REPLY_USER;
-            break;
-        }
 
         if (!($resp = ResponseThreadEntry::add($vars, $errors)))
             return $resp;
diff --git a/include/class.ticket.php b/include/class.ticket.php
index 615931fa0..40d0edf55 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -2667,6 +2667,42 @@ implements RestrictedAccess, Threadable, Searchable {
         if ($vars['userId'] == $this->user_id)
           $isMsg = true;
 
+      // Get active recipients of the response
+      // Initial Message from Tickets created by Agent
+      if ($vars['reply-to']) {
+          if ($vars['reply-to'] == 'all' || $vars['reply-to'] == 'user') {
+              if ($user = User::lookup($this->user_id))
+                $vars['thread_entry_recipients']['to'][] = $user->getEmail()->address;
+          }
+          if ($vars['ccs'] && $vars['reply-to'] == 'all') {
+              foreach ($vars['ccs'] as $key => $uid) {
+                  if (!$cc = User::lookup($uid))
+                    continue;
+                  else
+                    $vars['thread_entry_recipients']['cc'][] = $cc->getEmail()->address;
+              }
+          }
+      }
+      // Messages from User front end
+      elseif (strtolower($origin) != 'email') {
+          $recipients = $this->getRecipients('all');
+          foreach ($recipients as $key => $recipient) {
+              $recipientContact = $recipient->getContact();
+              // Do not put the Ticket Owner as a recipient if they are opening a new Ticket
+              if(get_class($recipientContact) == 'TicketOwner' && !is_null($this->getLastMsgDate())) {
+                  if ($recipientContact->getId() == $vars['userId'])
+                      unset($recipients[$key]);
+              }
+              // Do not put a Collaborator as a recipient if they are replying from Web Portal
+              elseif (get_class($recipientContact) == 'Collaborator') {
+                  if ($recipient->getContact()->getUserId() == $vars['userId'])
+                      unset($recipients[$key]);
+              }
+          }
+          if ($recipients && $recipients instanceof MailingList)
+              $vars['thread_entry_recipients'] = $recipients->getEmailAddresses();
+      }
+
         if (!($message = $this->getThread()->addMessage($vars, $errors)))
             return null;
 
-- 
GitLab