From 4c511c8dc490f8070298880b59f561edfadaf6e3 Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Fri, 10 Jan 2014 21:42:08 +0000
Subject: [PATCH] Add ability to preview the list of collaborators

---
 include/ajax.tickets.php                      | 20 ++++++++++++-
 .../templates/collaborators-preview.tmpl.php  | 28 +++++++++++++++++++
 include/staff/ticket-view.inc.php             |  8 ++++--
 scp/ajax.php                                  |  3 +-
 scp/js/tips.js                                | 22 +++++++++++++++
 5 files changed, 76 insertions(+), 5 deletions(-)
 create mode 100644 include/staff/templates/collaborators-preview.tmpl.php

diff --git a/include/ajax.tickets.php b/include/ajax.tickets.php
index e9bca0f6a..68aaf24ae 100644
--- a/include/ajax.tickets.php
+++ b/include/ajax.tickets.php
@@ -568,7 +568,23 @@ class TicketsAjaxAPI extends AjaxController {
         return self::_addcollaborator($ticket);
     }
 
+    function previewCollaborators($tid) {
+        global $thisstaff;
+
+        if (!($ticket=Ticket::lookup($tid))
+                || !$ticket->checkStaffAccess($thisstaff))
+            Http::response(404, 'No such ticket');
+
+        if (!$ticket->getCollaborators())
+            Http::response(404, 'No such ticket');
 
+        ob_start();
+        include STAFFINC_DIR . 'templates/collaborators-preview.tmpl.php';
+        $resp = ob_get_contents();
+        ob_end_clean();
+
+        return $resp;
+    }
 
     function _addcollaborator($ticket, $user=null, $form=null, $info=array()) {
 
@@ -590,7 +606,9 @@ class TicketsAjaxAPI extends AjaxController {
 
         $errors = $info = array();
         if ($ticket->updateCollaborators($_POST, $errors))
-            Http::response(201, sprintf('Recipients (%d)', $ticket->getNumActiveCollaborators()));
+            Http::response(201, sprintf('Recipients (%d of %d)',
+                        $ticket->getNumActiveCollaborators(),
+                        $ticket->getNumCollaborators()));
 
         if($errors && $errors['err'])
             $info +=array('error' => $errors['err']);
diff --git a/include/staff/templates/collaborators-preview.tmpl.php b/include/staff/templates/collaborators-preview.tmpl.php
new file mode 100644
index 000000000..45cfecca2
--- /dev/null
+++ b/include/staff/templates/collaborators-preview.tmpl.php
@@ -0,0 +1,28 @@
+<div>
+<table border="0" cellspacing="" cellpadding="1">
+<colgroup><col style="min-width: 250px;"></col></colgroup>
+<?php
+if (($users=$ticket->getCollaborators())) {?>
+<?php
+    foreach($users as $user) {
+        echo sprintf('<tr><td %s><i class="icon-%s"></i> %s <em>&lt;%s&gt;</em></td></tr>',
+                ($user->isActive()? 'class="faded"' : ''),
+                ($user->isActive()? 'comments' :  'comment-alt'),
+                $user->getName(),
+                $user->getEmail());
+    }
+}  else {
+    echo "Bro, not sure how you got here!";
+}?>
+</table>
+<?php
+$options = array();
+//TODO: Add options to manage collaborators
+if ($options) {
+    echo '<ul class="tip_menu">';
+    foreach($options as $option)
+        echo sprintf('<li><a href="%s">%s</a></li>', $option['url'], $option['action']);
+    echo '</ul>';
+}
+?>
+</div>
diff --git a/include/staff/ticket-view.inc.php b/include/staff/ticket-view.inc.php
index 3ac6b4efd..6fee37da6 100644
--- a/include/staff/ticket-view.inc.php
+++ b/include/staff/ticket-view.inc.php
@@ -453,10 +453,12 @@ $tcount+= $ticket->getNumNotes();
                     <?php
                     $recipients = 'Add Recipients';
                     if ($ticket->getNumCollaborators())
-                        $recipients = sprintf('Recipients (%d)', $ticket->getNumActiveCollaborators());
+                        $recipients = sprintf('Recipients (%d of %d)',
+                                $ticket->getNumActiveCollaborators(),
+                                $ticket->getNumCollaborators());
 
-                    echo sprintf('<span><a class="collaborators"
-                            href="#tickets/%d/collaborators/manage"><span id="recipients">%s</span></a></span>',
+                    echo sprintf('<span><a class="collaborators" id="manageCollab"
+                            href="#tickets/%d/collaborators"><span id="recipients">%s</span></a></span>',
                             $ticket->getId(),
                             $recipients);
                    ?>
diff --git a/scp/ajax.php b/scp/ajax.php
index 7e990934b..8c321ead8 100644
--- a/scp/ajax.php
+++ b/scp/ajax.php
@@ -79,7 +79,8 @@ $dispatcher = patterns('',
         url_post('^(?P<tid>\d+)/lock$', 'acquireLock'),
         url_post('^(?P<tid>\d+)/lock/(?P<id>\d+)/renew', 'renewLock'),
         url_post('^(?P<tid>\d+)/lock/(?P<id>\d+)/release', 'releaseLock'),
-        url_get('^(?P<tid>\d+)/collaborators/manage$', 'showCollaborators'),
+        url_get('^(?P<tid>\d+)/collaborators/preview$', 'previewCollaborators'),
+        url_get('^(?P<tid>\d+)/collaborators$', 'showCollaborators'),
         url_post('^(?P<tid>\d+)/collaborators$', 'updateCollaborators'),
         url_get('^(?P<tid>\d+)/add-collaborator/(?P<uid>\d+)$', 'addCollaborator'),
         url_get('^(?P<tid>\d+)/add-collaborator/auth:(?P<bk>\w+):(?P<id>.+)$', 'addRemoteCollaborator'),
diff --git a/scp/js/tips.js b/scp/js/tips.js
index 205eb6fc9..db94a1acb 100644
--- a/scp/js/tips.js
+++ b/scp/js/tips.js
@@ -160,6 +160,28 @@ jQuery(function() {
         clearTimeout($(this).data('timer'));
     });
 
+
+    $('a#manageCollab').live('mouseover', function(e) {
+        e.preventDefault();
+        var elem = $(this);
+
+        var url = 'ajax.php/'+elem.attr('href').substr(1)+'/preview';
+        var xoffset = 100;
+        elem.data('timer', 0);
+
+        if (e.type=='mouseover') {
+            elem.data('timer',setTimeout(function() { showtip(url, elem, xoffset);},750))
+        } else {
+            showtip(url,elem,xoffset);
+        }
+    }).live('mouseout', function(e) {
+        clearTimeout($(this).data('timer'));
+    }).live('click', function(e) {
+        clearTimeout($(this).data('timer'));
+        $('.tip_box').remove();
+    });
+
+
     //Ticket preview
     $('.ticketPreview').live('mouseover', function(e) {
         e.preventDefault();
-- 
GitLab