From 0b91a9ca53643f4010049fbe299738a67fcf2487 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Fri, 26 Dec 2014 13:52:43 -0600
Subject: [PATCH] dept: Add function to retrieve alert email mailbox

This patch adds a ::getAlertEmail() method to the Department class, which
allows administrators to use the department email address (normally for
replies) as the alert email address. If not specified, the system alert
email address is retrieved instead, automatically.
---
 include/class.dept.php   | 18 ++++++++++++++++++
 include/class.ticket.php | 12 ++++++------
 2 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/include/class.dept.php b/include/class.dept.php
index 7abb372d1..d9d4c6309 100644
--- a/include/class.dept.php
+++ b/include/class.dept.php
@@ -92,6 +92,24 @@ class Dept {
         return $this->email;
     }
 
+    /**
+     * getAlertEmail
+     *
+     * Fetches either the department email (for replies) if configured.
+     * Otherwise, the system alert email address is used.
+     */
+    function getAlertEmail() {
+        global $cfg;
+
+        if (!$this->email && ($id = $this->getEmailId())) {
+            $this->email = Email::lookup($id);
+        }
+        if (!$this->email && $cfg) {
+            $this->email = $cfg->getAlertEmail();
+        }
+        return $this->email;
+    }
+
     function getNumStaff() {
         return $this->ht['users'];
     }
diff --git a/include/class.ticket.php b/include/class.ticket.php
index 7aeabf55a..cb9d23dc4 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -993,7 +993,7 @@ class Ticket {
         //Send alert to out sleepy & idle staff.
         if ($alertstaff
                 && $cfg->alertONNewTicket()
-                && ($email=$dept->getEmail())
+                && ($email=$dept->getAlertEmail())
                 && ($msg=$tpl->getNewTicketAlertMsgTemplate())) {
 
             $msg = $this->replaceVars($msg->asArray(), array('message' => $message));
@@ -1219,7 +1219,7 @@ class Ticket {
         $dept = $this->getDept();
         if(!$dept
                 || !($tpl = $dept->getTemplate())
-                || !($email = $dept->getEmail()))
+                || !($email = $dept->getAlertEmail()))
             return true;
 
         //recipients
@@ -1276,7 +1276,7 @@ class Ticket {
         //Get the message template
         if(($tpl = $dept->getTemplate())
                 && ($msg=$tpl->getOverdueAlertMsgTemplate())
-                && ($email = $dept->getEmail())) {
+                && ($email = $dept->getAlertEmail())) {
 
             $msg = $this->replaceVars($msg->asArray(),
                 array('comments' => $comments));
@@ -1473,7 +1473,7 @@ class Ticket {
         if(!$alert || !$cfg->alertONTransfer() || !($dept=$this->getDept()))
             return true; //no alerts!!
 
-         if (($email = $dept->getEmail())
+         if (($email = $dept->getAlertEmail())
                      && ($tpl = $dept->getTemplate())
                      && ($msg=$tpl->getTransferAlertMsgTemplate())) {
 
@@ -1701,7 +1701,7 @@ class Ticket {
                 'thread'=>$message);
         //If enabled...send alert to staff (New Message Alert)
         if($cfg->alertONNewMessage()
-                && ($email = $dept->getEmail())
+                && ($email = $dept->getAlertEmail())
                 && ($tpl = $dept->getTemplate())
                 && ($msg = $tpl->getNewMessageAlertMsgTemplate())) {
 
@@ -1958,7 +1958,7 @@ class Ticket {
         if(!$alert || !$cfg->alertONNewNote() || !($dept=$this->getDept()))
             return $note;
 
-        if (($email = $dept->getEmail())
+        if (($email = $dept->getAlertEmail())
                 && ($tpl = $dept->getTemplate())
                 && ($msg=$tpl->getNoteAlertMsgTemplate())) {
 
-- 
GitLab