From a08de3be6d651bf4c963814d0d4771094cd45713 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Thu, 10 Apr 2014 17:02:41 -0500
Subject: [PATCH] orgs: Auto add collaborators to new tickets

---
 include/class.organization.php | 12 ++++++++++++
 include/class.ticket.php       | 22 ++++++++++++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/include/class.organization.php b/include/class.organization.php
index 908638685..5592aa95f 100644
--- a/include/class.organization.php
+++ b/include/class.organization.php
@@ -44,6 +44,18 @@ class OrganizationModel extends VerySimpleModel {
         return $this->staff_id;
     }
 
+    function autoAddCollabs() {
+        return $this->check(self::COLLAB_ALL_MEMBERS | self::COLLAB_PRIMARY_CONTACT);
+    }
+
+    function autoAddPrimaryContactsAsCollabs() {
+        return $this->check(self::COLLAB_PRIMARY_CONTACT);
+    }
+
+    function autoAddMembersAsCollabs() {
+        return $this->check(self::COLLAB_ALL_MEMBERS);
+    }
+
     function getUpdateDate() {
         return $this->updated;
     }
diff --git a/include/class.ticket.php b/include/class.ticket.php
index 57235d31b..663ecc57f 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -2415,6 +2415,28 @@ class Ticket {
 
         $dept = $ticket->getDept();
 
+        // Add organizational collaborators
+        if ($org && $org->autoAddCollabs()) {
+            $pris = $org->autoAddPrimaryContactsAsCollabs();
+            $members = $org->autoAddMembersAsCollabs();
+            $settings = array('isactive' => true);
+            $collabs = array();
+            foreach ($org->allMembers() as $u) {
+                if ($members || ($pris && $u->isPrimaryContact())) {
+                    if ($c = $this->addCollaborator($u, $settings, $errors)) {
+                        $collabs[] = (string) $c;
+                    }
+                }
+            }
+            //TODO: Can collaborators add others?
+            if ($collabs) {
+                //TODO: Change EndUser to name of  user.
+                $this->logNote(sprintf('Collaborators for %s organization added',
+                        $org->getName()),
+                    implode("<br>", $collabs), $org->getName(), false);
+            }
+        }
+
         //post the message.
         unset($vars['cannedattachments']); //Ticket::open() might have it set as part of  open & respond.
         $vars['title'] = $vars['subject']; //Use the initial subject as title of the post.
-- 
GitLab