From 231a2fa2b2676d1ac3ee0820d6fcbdbf07b65491 Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Wed, 15 Jan 2014 15:49:35 +0000
Subject: [PATCH] Add iteratable UserList class Add routine to get ticket's
 recipients (owner + active collaborators)

---
 include/class.ticket.php | 27 +++++++++++++++++----
 include/class.user.php   | 51 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+), 5 deletions(-)

diff --git a/include/class.ticket.php b/include/class.ticket.php
index cd3a649ec..2e98d8654 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -598,6 +598,23 @@ class Ticket {
         return $this->collaborators;
     }
 
+    //UserList of recipients  (owner + collaborators)
+    function getRecipients() {
+
+        if (!isset($this->recipients)) {
+            $list = new UserList();
+            $list->add($this->getOwner());
+            if ($collabs = $this->getActiveCollaborators()) {
+                foreach ($collabs as $c)
+                    $list->add($c);
+            }
+            $this->recipients = $list;
+        }
+
+        return $this->recipients;
+    }
+
+
     function addCollaborator($user, $vars, &$errors) {
 
         if (!$user || $user->getId()==$this->getOwnerId())
@@ -610,6 +627,7 @@ class Ticket {
             return null;
 
         $this->collaborators = null;
+        $this->recipients = null;
 
         return $c;
     }
@@ -958,14 +976,13 @@ class Ticket {
 
         if (!$vars
                 || !$vars['variables']
-                || !($collaborators=$this->getActiveCollaborators())
+                || !($recipients=$this->getRecipients())
                 || !($dept=$this->getDept())
                 || !($tpl=$dept->getTemplate())
                 || !($msg=$tpl->getActivityNoticeMsgTemplate())
                 || !($email=$dept->getEmail()))
             return;
 
-
         $msg = $this->replaceVars($msg->asArray(), $vars['variables']);
 
         if($cfg->stripQuotedReply() && ($tag=$cfg->getReplySeparator()))
@@ -978,9 +995,9 @@ class Ticket {
         if($vars['references'])
             $options['references'] = $vars['references'];
 
-        foreach($collaborators as $collaborator) {
-            $notice = $this->replaceVars($msg, array('recipient' => $collaborator));
-            $email->send($collaborator->getEmail(), $notice['subj'], $notice['body'], $attachments,
+        foreach($recipients as $recipient) {
+            $notice = $this->replaceVars($msg, array('recipient' => $recipient));
+            $email->send($recipient->getEmail(), $notice['subj'], $notice['body'], $attachments,
                 $options);
         }
 
diff --git a/include/class.user.php b/include/class.user.php
index 1212627e5..e74e922a5 100644
--- a/include/class.user.php
+++ b/include/class.user.php
@@ -468,4 +468,55 @@ class UserEmail extends UserEmailModel {
     }
 }
 
+
+/*
+ *  Generic user list.
+ */
+class UserList implements  IteratorAggregate, ArrayAccess {
+    private $users;
+
+    function __construct($list = array()) {
+        $this->users = $list;
+    }
+
+    function add($user) {
+        $this->offsetSet(null, $user);
+    }
+
+    function offsetSet($offset, $value) {
+
+        if (is_null($offset))
+            $this->users[] = $value;
+        else
+            $this->users[$offset] = $value;
+    }
+
+    function offsetExists($offset) {
+        return isset($this->users[$offset]);
+    }
+
+    function offsetUnset($offset) {
+        unset($this->users[$offset]);
+    }
+
+    function offsetGet($offset) {
+        return isset($this->users[$offset]) ? $this->users[$offset] : null;
+    }
+
+    function getIterator() {
+        return new ArrayIterator($this->users);
+    }
+
+    function __toString() {
+
+        $list = array();
+        foreach($this->users as $user) {
+            if (is_object($user))
+                $list [] = $user->getName();
+        }
+
+        return $list ? implode(', ', $list) : '';
+    }
+}
+
 ?>
-- 
GitLab