From 3171aa0c93275db57d2cddae3bd699a61e300435 Mon Sep 17 00:00:00 2001
From: aydreeihn <adriane@enhancesoft.com>
Date: Thu, 19 Apr 2018 13:49:02 -0500
Subject: [PATCH] Changes for email template recipient/staff variables:

- change recipient name vars to 'Collaborator' in tmpls if ticket user isn't a recipient
- for templates going to Bcc'd Collaborators, account for all name format variables that could be used
- new function getNameFormats returns an array of all name formats in templates for staff.name.* or recipient.name.*
- other minor cc/bcc fixes
---
 include/class.ticket.php | 39 ++++++++++++++++++++++-----------------
 include/class.user.php   | 10 ++++++++++
 2 files changed, 32 insertions(+), 17 deletions(-)

diff --git a/include/class.ticket.php b/include/class.ticket.php
index cdf4f22e0..167703aaf 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -1648,10 +1648,18 @@ implements RestrictedAccess, Threadable, Searchable {
         $collaborators['cc'] = $collaborators;
 
         //collaborator email sent out
-        if ($collaborators['cc']) {
+        if ($collaborators['cc']  || $owner_recip) {
           //say dear collaborator if the ticket user is not a recipient
-          if (!$owner_recip)
-            $cnotice = $this->replaceVars($msg, array('recipient.name.first' => __('Collaborator'), 'recipient' => $recipient));
+          if (!$owner_recip) {
+            $nameFormats = array_keys(PersonsName::allFormats());
+            $names = array();
+            foreach ($nameFormats as $key => $value) {
+              $names['recipient.name.' . $value] = __('Collaborator');
+            }
+            $names = array_merge($names, array('recipient' => $recipient));
+            $cnotice = $this->replaceVars($msg, $names);
+          }
+
           //otherwise address email to ticket user
           else
             $cnotice = $this->replaceVars($msg, array('recipient' => $owner));
@@ -2970,7 +2978,7 @@ implements RestrictedAccess, Threadable, Searchable {
             $collabsCc = array();
             if ($vars['ccs'] && $vars['emailcollab']) {
                 $collabsCc[] = Collaborator::getCollabList($vars['ccs']);
-                $collabsCc['cc'] = $collabsCc;
+                $collabsCc['cc'] = $collabsCc[0];
             }
 
             $email->send($user, $msg['subj'], $msg['body'], $attachments,
@@ -2985,10 +2993,9 @@ implements RestrictedAccess, Threadable, Searchable {
                     if (!($recipient = User::lookup($uid)))
                         continue;
 
-                    $msg = $this->replaceVars($bccmsg->asArray(), $variables + array(
-                                'recipient' => $user,
-                                'recipient.name.first' =>
-                                $recipient->getName()->getFirst()));
+                    $extraVars = UsersName::getNameFormats($recipient, 'recipient');
+                    $extraVars = array_merge($extraVars, array('recipient' => $user));
+                    $msg = $this->replaceVars($bccmsg->asArray(), $variables + $extraVars);
 
                     $email->send($recipient, $msg['subj'], $msg['body'], $attachments, $options);
                 }
@@ -4129,15 +4136,13 @@ implements RestrictedAccess, Threadable, Searchable {
                       && ($bccmsg=$tpl->getNewTicketNoticeMsgTemplate())
                       && ($email=$dept->getEmail())
                   )
-                  $bccmsg = $ticket->replaceVars($bccmsg->asArray(),
-                      array(
-                          'message'   => $message,
-                          'signature' => $signature,
-                          'response'  => ($response) ? $response->getBody() : '',
-                          'recipient' => $ticket->getOwner(),
-                          'recipient.name.first' => $recipient->getName()->getFirst(),
-                      )
-                  );
+                  $extraVars = UsersName::getNameFormats($recipient, 'recipient');
+                  $extraVars = array_merge($extraVars, array(
+                    'message'   => $message,
+                    'signature' => $signature,
+                    'response'  => ($response) ? $response->getBody() : '',
+                    'recipient' => $ticket->getOwner()));
+                  $bccmsg = $ticket->replaceVars($bccmsg->asArray(), $extraVars);
 
                   $email->send($recipient, $bccmsg['subj'], $bccmsg['body'], $attachments,
                       $options);
diff --git a/include/class.user.php b/include/class.user.php
index dcd04b9c5..083e0d5bf 100644
--- a/include/class.user.php
+++ b/include/class.user.php
@@ -813,6 +813,16 @@ implements TemplateVariable {
         return $this;
     }
 
+    function getNameFormats($user, $type) {
+      $nameFormats = array();
+
+      foreach (PersonsName::allFormats() as $format => $func) {
+          $nameFormats[$type . '.name.' . $format] = $user->getName()->$func[1]();
+      }
+
+      return $nameFormats;
+    }
+
     function asVar() {
         return $this->__toString();
     }
-- 
GitLab