From cccc621749cda1f624a2a8dea96c7bbce48b7bc5 Mon Sep 17 00:00:00 2001
From: aydreeihn <adriane@enhancesoft.com>
Date: Fri, 25 May 2018 10:25:29 -0500
Subject: [PATCH] New Ticket UI Changes + Some Post Reply UI Changes

New Ticket UI:
- Clean up recipient section (adding user and collaborators)
- Give agents the option of who to send the new ticket alert to:
	- Alert to All
	- Alert to User
	- Alert to CC + User
	- Alert to BCC
	- Do not send an Alert
- Added option of adding new Users for cc/bcc section using the 'Add New' button

Post Reply UI:
- Instead of having to type to get the option of adding a new user in the cc/bcc boxes, put an Add New button
- Clean up Add New routine
	- Now when adding a new collaborator using the Add New button, the User's name will appear in the cc or bcc box as specified and the Agent can go ahead and write their reply rather than pulling up the manage collaborators popup and forcing the agent to have to save the collaborator changes before being able to type a reply
- Change the option "Reply to Collaborators" to "Reply to CC + User" to avoid confusion
---
 include/class.thread.php                      |   8 +-
 include/class.ticket.php                      |  15 +-
 .../i18n/en_US/help/tips/tickets.queue.yaml   |   2 +-
 include/staff/ticket-open.inc.php             | 274 ++++++++++--------
 include/staff/ticket-view.inc.php             | 102 ++++---
 5 files changed, 213 insertions(+), 188 deletions(-)

diff --git a/include/class.thread.php b/include/class.thread.php
index 306cedf52..034eb3505 100644
--- a/include/class.thread.php
+++ b/include/class.thread.php
@@ -1547,7 +1547,7 @@ implements TemplateVariable {
         $ticketUser = Ticket::objects()->filter(array('ticket_id'=>$ticket[0]))->values_flat('user_id')->first();
 
         //User
-        if ($ticketUser && (Ticket::checkReply('user', $vars['emailreply']) || $vars['do'] == 'create')) {
+        if ($ticketUser && Ticket::checkReply('user', $vars['emailreply'])) {
           $uEmail = UserEmailModel::objects()->filter(array('user_id'=>$ticketUser[0]))->values_flat('address')->first();
           $u = array();
           $u[$ticketUser[0]] = $uEmail[0];
@@ -1561,18 +1561,18 @@ implements TemplateVariable {
         self::setReplyFlag($entry, $vars['emailreply']);
 
         //Cc collaborators
-        if ($vars['ccs'] && (Ticket::checkReply('cc', $vars['emailreply']) || $vars['do'] == 'create')) {
+        if ($vars['ccs'] && Ticket::checkReply('cc', $vars['emailreply'])) {
           $cc = Collaborator::getCollabList($vars['ccs']);
           $recipients['cc'] = $cc;
         }
 
         //Bcc Collaborators
-        if($vars['bccs'] && (Ticket::checkReply('bcc', $vars['emailreply']) || $vars['do'] == 'create')) {
+        if($vars['bccs'] && Ticket::checkReply('bcc', $vars['emailreply'])) {
           $bcc = Collaborator::getCollabList($vars['bccs']);
           $recipients['bcc'] = $bcc;
         }
 
-        if (($vars['do'] == 'create' || $vars['emailreply'] != '0') && $recipients)
+        if ($vars['emailreply'] != '0' && $recipients)
           $entry->recipients = json_encode($recipients);
 
         if ($entry->format == 'html')
diff --git a/include/class.ticket.php b/include/class.ticket.php
index 8ec533177..df25a4a0d 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -4161,7 +4161,6 @@ implements RestrictedAccess, Threadable, Searchable {
         }
 
         if (!$cfg->notifyONNewStaffTicket()
-            || !isset($vars['alertuser'])
             || !($dept=$ticket->getDept())
         ) {
             return $ticket; //No alerts.
@@ -4208,12 +4207,15 @@ implements RestrictedAccess, Threadable, Searchable {
             );
 
             //ticket created on user's behalf
-            if($vars['emailcollab'] == 1) {
-
+            if (Ticket::checkReply('cc', $vars['emailreply'])) {
               $email->send($ticket->getOwner(), $msg['subj'], $msg['body'], $attachments,
                   $options, $collabsCc);
+            }
+            elseif (Ticket::checkReply('user', $vars['emailreply']))
+              $email->send($ticket->getOwner(), $msg['subj'], $msg['body'], $attachments,
+                  $options);
 
-              if ($collabsBcc) {
+              if ($collabsBcc && Ticket::checkReply('bcc', $vars['emailreply'])) {
                 foreach ($collabsBcc as $recipient) {
                   if (($tpl=$dept->getTemplate())
                       && ($bccmsg=$tpl->getNewTicketNoticeBCCMsgTemplate())
@@ -4231,10 +4233,7 @@ implements RestrictedAccess, Threadable, Searchable {
                       $options);
                 }
               }
-            }
-            else
-              $email->send($ticket->getOwner(), $msg['subj'], $msg['body'], $attachments,
-                  $options);
+
         }
         return $ticket;
     }
diff --git a/include/i18n/en_US/help/tips/tickets.queue.yaml b/include/i18n/en_US/help/tips/tickets.queue.yaml
index a6238b0e7..2b799b166 100644
--- a/include/i18n/en_US/help/tips/tickets.queue.yaml
+++ b/include/i18n/en_US/help/tips/tickets.queue.yaml
@@ -84,7 +84,7 @@ reply_types:
           This reply is sent to the User and the Collaborators (CC and BCC) you choose to include.</br>
         <b>Reply to User:</b>
           This reply is sent to the User only, no Collaborators.</br>
-        <b>Reply to Collaborators:</b>
+        <b>Reply to CC + User:</b>
           This reply is sent to only the User and CC'd Collaborators</br>
         <b>Reply to BCC:</b>
           This is sent only to the BCC'd Collaborators.
diff --git a/include/staff/ticket-open.inc.php b/include/staff/ticket-open.inc.php
index 923e19b41..dc03024d9 100644
--- a/include/staff/ticket-open.inc.php
+++ b/include/staff/ticket-open.inc.php
@@ -75,113 +75,129 @@ if ($_POST)
     <tbody>
         <tr>
             <th colspan="2">
-                <em><strong><?php echo __('Recipient Information'); ?></strong>: </em>
+                <em><strong><?php echo __('User and Collaborators'); ?></strong>: </em>
                 <div class="error"><?php echo $errors['user']; ?></div>
             </th>
         </tr>
-        <?php
-        if ($user) { ?>
-        <tr><td><?php echo __('User'); ?>:</td><td>
-            <div id="user-info">
-                <input type="hidden" name="uid" id="uid" value="<?php echo $user->getId(); ?>" />
-            <a href="#" onclick="javascript:
-                $.userLookup('ajax.php/users/<?php echo $user->getId(); ?>/edit',
-                        function (user) {
-                            $('#user-name').text(user.name);
-                            $('#user-email').text(user.email);
-                        });
-                return false;
-                "><i class="icon-user"></i>
-                <span id="user-name"><?php echo Format::htmlchars($user->getName()); ?></span>
-                &lt;<span id="user-email"><?php echo $user->getEmail(); ?></span>&gt;
-                </a>
-                <a class="inline button" style="overflow:inherit" href="#"
+        <tr>
+          <td>
+            <table class="form_table" width="940" border="0" cellspacing="0" cellpadding="2">
+              <?php
+              if ($user) { ?>
+                  <tr><td><?php echo __('User'); ?>:</td><td>
+                    <div id="user-info">
+                      <input type="hidden" name="uid" id="uid" value="<?php echo $user->getId(); ?>" />
+                      <a href="#" onclick="javascript:
+                      $.userLookup('ajax.php/users/<?php echo $user->getId(); ?>/edit',
+                      function (user) {
+                        $('#user-name').text(user.name);
+                        $('#user-email').text(user.email);
+                      });
+                      return false;
+                      "><i class="icon-user"></i>
+                      <span id="user-name"><?php echo Format::htmlchars($user->getName()); ?></span>
+                      &lt;<span id="user-email"><?php echo $user->getEmail(); ?></span>&gt;
+                    </a>
+                    <a class="inline button" style="overflow:inherit" href="#"
                     onclick="javascript:
-                        $.userLookup('ajax.php/users/select/'+$('input#uid').val(),
-                            function(user) {
-                                $('input#uid').val(user.id);
-                                $('#user-name').text(user.name);
-                                $('#user-email').text('<'+user.email+'>');
-                        });
-                        return false;
+                    $.userLookup('ajax.php/users/select/'+$('input#uid').val(),
+                    function(user) {
+                      $('input#uid').val(user.id);
+                      $('#user-name').text(user.name);
+                      $('#user-email').text('<'+user.email+'>');
+                    });
+                    return false;
                     "><i class="icon-retweet"></i> <?php echo __('Change'); ?></a>
-            </div>
-        </td></tr>
-        <?php
-        } else { //Fallback: Just ask for email and name
-            ?>
-        <tr>
-            <td width="160" class="required"> <?php echo __('Email Address'); ?>: </td>
-            <td>
-                <div class="attached input">
-                    <input type="text" size=45 name="email" id="user-email" class="attached"
-                        autocomplete="off" autocorrect="off" value="<?php echo $info['email']; ?>" /> </span>
-                <a href="?a=open&amp;uid={id}" data-dialog="ajax.php/users/lookup/form"
-                    class="attached button"><i class="icon-search"></i></a>
-                </div>
+                  </div>
+                </td>
+              </tr>
+              <?php
+            } else { //Fallback: Just ask for email and name
+              ?>
+              <tr id="userRow">
+                <td width="120"><?php echo __('User'); ?>:</td>
+                <td>
+                  <span>
+                    <select class="userSelection" name="name" id="user-name"
+                    data-placeholder="<?php echo __('Select User'); ?>">
+                  </select>
+                </span>
+
+                <a class="inline button" style="overflow:inherit" href="#"
+                onclick="javascript:
+                $.userLookup('ajax.php/users/lookup/form', function (user) {
+                  var newUser = new Option(user.email + ' - ' + user.name, user.id, true, true);
+                  return $(&quot;#user-name&quot;).append(newUser).trigger('change');
+                });
+                "><i class="icon-plus"></i> <?php echo __('Add New'); ?></a>
+
                 <span class="error">*</span>
-                <div class="error"><?php echo $errors['email']; ?></div>
-            </td>
-        </tr>
-        <tr>
-            <td width="160" class="required"> <?php echo __('Full Name'); ?>: </td>
+                <br/><span class="error"><?php echo $errors['name']; ?></span>
+              </td>
+              <div>
+                <input type="hidden" size=45 name="email" id="user-email" class="attached"
+                placeholder="<?php echo __('User Email'); ?>"
+                autocomplete="off" autocorrect="off" value="<?php echo $info['email']; ?>" />
+              </div>
+            </tr>
+            <?php
+          } ?>
+          <tr id="ccRow">
+            <td width="160"><?php echo __('Cc'); ?>:</td>
             <td>
-                <span style="display:inline-block;">
-                    <input type="text" size=45 name="name" id="user-name" value="<?php echo $info['name']; ?>" /> </span>
-                <span class="error">*</span>
-                <div class="error"><?php echo $errors['name']; ?></div>
-            </td>
-        </tr>
-        <?php
-        } ?>
+              <span>
+                <select class="collabSelections" name="ccs[]" id="cc_users_open" multiple="multiple"
+                ref="tags" data-placeholder="<?php echo __('Select Contacts'); ?>">
+              </select>
+            </span>
 
-        <?php
-        if($cfg->notifyONNewStaffTicket()) {  ?>
-        <tr>
-            <td width="160"><?php echo __('Ticket Notice'); ?>:</td>
-            <td>
-            <input type="checkbox" name="alertuser" <?php echo (!$errors || $info['alertuser'])? 'checked="checked"': ''; ?>><?php
-                echo __('Send alert to user.'); ?>
-            </td>
+            <a class="inline button" style="overflow:inherit" href="#"
+            onclick="javascript:
+            $.userLookup('ajax.php/users/lookup/form', function (user) {
+              var newUser = new Option(user.name, user.id, true, true);
+              return $(&quot;#cc_users_open&quot;).append(newUser).trigger('change');
+            });
+            "><i class="icon-plus"></i> <?php echo __('Add New'); ?></a>
+
+            <br/><span class="error"><?php echo $errors['ccs']; ?></span>
+          </td>
         </tr>
-        <?php
-        } ?>
-          <tr>
-            <td>
-              <table border="0">
-                <tr class="no_border">
-                  <td width="120">
-                      <label><strong><?php echo __('Collaborators'); ?>:</strong></label>
-                  </td>
-                  <td>
-                      <input type='checkbox' value='1' name="emailcollab"
-                      id="emailcollab"
-                          <?php echo ((!$info['emailcollab'] && !$errors) || isset($info['emailcollab']))?'checked="checked"':''; ?>
+        <tr id="bccRow">
+          <td width="160"><?php echo __('Bcc'); ?>:</td>
+          <td>
+            <span>
+              <select class="collabSelections" name="bccs[]" id="bcc_users_open" multiple="multiple"
+              data-placeholder="<?php echo __('Select Contacts'); ?>">
+            </select>
+          </span>
+
+          <a class="inline button" style="overflow:inherit" href="#"
+          onclick="javascript:
+          $.userLookup('ajax.php/users/lookup/form', function (user) {
+            var newUser = new Option(user.name, user.id, true, true);
+            return $(&quot;#bcc_users_open&quot;).append(newUser).trigger('change');
+          });
+          "><i class="icon-plus"></i> <?php echo __('Add New'); ?></a>
 
-                          >
-                      <?php
-                     ?>
-                  </td>
-                </tr>
-                <tr class="no_border" id="ccRow">
-                  <td width="160"><?php echo __('Cc'); ?>:</td>
-                  <td>
-                      <select class="collabSelections" name="ccs[]" id="cc_users_open" multiple="multiple"
-                          data-placeholder="<?php echo __('Select Contacts'); ?>">
-                      </select>
-                      <br/><span class="error"><?php echo $errors['ccs']; ?></span>
-                  </td>
-                </tr>
-                <tr class="no_border" id="bccRow">
-                  <td width="160"><?php echo __('Bcc'); ?>:</td>
-                  <td>
-                      <select class="collabSelections" name="bccs[]" id="bcc_users_open" multiple="multiple"
-                          data-placeholder="<?php echo __('Select Contacts'); ?>">
-                      </select>
-                      <br/><span class="error"><?php echo $errors['ccs']; ?></span>
-                  </td>
-                </tr>
-              </table>
+          <br/><span class="error"><?php echo $errors['bccs']; ?></span>
+        </td>
+      </tr>
+      <tr class="no_border">
+        <td>
+          <?php echo __('Ticket Notice');?>:
+        </td>
+        <td>
+          <select id="emailreply" name="emailreply">
+            <option value="reply-all"><?php echo __('Alert All'); ?></option>
+            <option value="reply-user"><?php echo __('Alert to User'); ?></option>
+            <option value="reply-collab" <?php echo 'selected="selected"'; ?>><?php echo __('Alert to CC + User'); ?></option>
+            <option value="reply-bcc"><?php echo __('Alert to BCC'); ?></option>
+            <option value="reply-bcc"><?php echo __('Alert to BCC'); ?></option>
+            <option value="0">&mdash; <?php echo __('Do Not Send Alert'); ?> &mdash;</option>
+          </select>
+        </td>
+      </tr>
+    </table>
           </td>
         </tr>
     </tbody>
@@ -529,8 +545,8 @@ $(function() {
         return false;
     });
 
-    $('.collabSelections').select2({
-      width: '350px',
+    $('.userSelection').select2({
+      width: '450px',
       minimumInputLength: 3,
       ajax: {
         url: "ajax.php/users/local",
@@ -544,40 +560,54 @@ $(function() {
           return {
             results: $.map(data, function (item) {
               return {
-                text: item.name,
+                text: item.email + ' - ' + item.name,
                 slug: item.slug,
+                email: item.email,
                 id: item.id
               }
             })
           };
+          $('#user-email').val(item.name);
         }
       }
     });
 
-  });
+    $('.userSelection').on('select2:select', function (e) {
+      var data = e.params.data;
+      $('#user-email').val(data.email);
+    });
 
-$(document).ready(function () {
-    $('#emailcollab').on('change', function(){
-      var value = $("#cc_users_open").val();
-        if ($(this).prop('checked')) {
-            $('#ccRow').show();
-            $('#bccRow').show();
-        }
-        else {
-            $('#ccRow').hide();
-            $('#bccRow').hide();
+    $('.userSelection').on("change", function (e) {
+      var data = $('.userSelection').select2('data');
+      var data = data[0].text;
+      var email = data.substr(0,data.indexOf(' '));
+      $('#user-email').val(data.substr(0,data.indexOf(' ')));
+     });
+
+    $('.collabSelections').select2({
+      width: '450px',
+      minimumInputLength: 3,
+      ajax: {
+        url: "ajax.php/users/local",
+        dataType: 'json',
+        data: function (params) {
+          return {
+            q: params.term,
+          };
+        },
+        processResults: function (data) {
+          return {
+            results: $.map(data, function (item) {
+              return {
+                text: item.name,
+                slug: item.slug,
+                id: item.id
+              }
+            })
+          };
         }
+      }
     });
-});
 
-$("form").submit(function(event) {
-  var value = $("#emailcollab").val();
-  if ($("#emailcollab").prop('checked')) {
-    //do nothing
-  }
-  else {
-    $("#cc_users_open").val(null).change();
-    $("#bcc_users_open").val(null).change();
-  }
-});
+  });
 </script>
diff --git a/include/staff/ticket-view.inc.php b/include/staff/ticket-view.inc.php
index 8715462fb..5a1a90e2e 100644
--- a/include/staff/ticket-view.inc.php
+++ b/include/staff/ticket-view.inc.php
@@ -762,7 +762,7 @@ if ($errors['err'] && isset($_POST['a'])) {
                     <select id="emailreply" name="emailreply">
                         <option value="reply-all"><?php echo __('Reply All'); ?></option>
                         <option value="reply-user"><?php echo __('Reply to User'); ?></option>
-                        <option value="reply-collab" <?php echo ($ccUser || $ticketUser ) ?  'selected="selected"' : ''; ?>><?php echo __('Reply to Collaborators'); ?></option>
+                        <option value="reply-collab" <?php echo ($ccUser || $ticketUser ) ?  'selected="selected"' : ''; ?>><?php echo __('Reply to CC + User'); ?></option>
                         <option value="reply-bcc" <?php echo $bccUser || $errors['bccs'] ?  'selected="selected"' : ''; ?>><?php echo __('Reply to BCC'); ?></option>
                         <option value="0" <?php echo !$emailReply ? 'selected="selected"' : ''; ?>
                         >&mdash; <?php echo __('Do Not Email Reply'); ?> &mdash;</option>
@@ -798,38 +798,60 @@ if ($errors['err'] && isset($_POST['a'])) {
              <tr id="cc-row">
                  <td width="160" style="padding-left:20px;"><?php echo __('Cc'); ?></td>
                  <td>
-                     <select class="collabSelections" name="ccs[]" id="cc_users" multiple="multiple"
-                         data-placeholder="<?php echo __('Select Contacts'); ?>">
-                         <?php
-                         foreach ($cc_cids as $u) {
-                           if($u != $ticket->user_id && !in_array($u, $bcc_cids)) {
-                             ?>
-                             <option value="<?php echo $u; ?>" <?php
-                             if (in_array($u, $cc_cids))
-                             echo 'selected="selected"'; ?>><?php echo User::lookup($u); ?>
-                           </option>
-                         <?php } } ?>
-                         ?>
-                     </select>
+                    <span>
+                      <select class="collabSelections" name="ccs[]" id="cc_users" multiple="multiple"
+                          data-placeholder="<?php echo __('Select Contacts'); ?>">
+                          <?php
+                          foreach ($cc_cids as $u) {
+                            if($u != $ticket->user_id && !in_array($u, $bcc_cids)) {
+                              ?>
+                              <option value="<?php echo $u; ?>" <?php
+                              if (in_array($u, $cc_cids))
+                              echo 'selected="selected"'; ?>><?php echo User::lookup($u); ?>
+                            </option>
+                          <?php } } ?>
+                          ?>
+                      </select>
+                    </span>
+
+                         <a class="inline button" style="overflow:inherit" href="#"
+                         onclick="javascript:
+                         $.userLookup('ajax.php/users/lookup/form', function (user) {
+                           var newUser = new Option(user.name, user.id, true, true);
+                           return $(&quot;#cc_users&quot;).append(newUser).trigger('change');
+                         });
+                         "><i class="icon-plus"></i> <?php echo __('Add New'); ?></a>
+
                      <br/><span class="error"><?php echo $errors['ccs']; ?></span>
                  </td>
              </tr>
              <tr id="bcc-row">
                <td width="160" style="padding-left:20px;"><?php echo __('Bcc'); ?></td>
                <td>
-                   <select class="collabSelections" name="bccs[]" id="bcc_users" multiple="multiple"
-                       data-placeholder="<?php echo __('Select Contacts'); ?>">
-                       <?php
-                       foreach ($bcc_cids as $u) {
-                         if($u != $ticket->user_id && !in_array($u, $cc_cids)) {
-                           ?>
-                           <option value="<?php echo $u; ?>" <?php
-                           if (in_array($u, $bcc_cids))
-                           echo 'selected="selected"'; ?>><?php echo User::lookup($u); ?>
-                         </option>
-                       <?php } } ?>
-                       ?>
-                   </select>
+                  <span>
+                    <select class="collabSelections" name="bccs[]" id="bcc_users" multiple="multiple"
+                        data-placeholder="<?php echo __('Select Contacts'); ?>">
+                        <?php
+                        foreach ($bcc_cids as $u) {
+                          if($u != $ticket->user_id && !in_array($u, $cc_cids)) {
+                            ?>
+                            <option value="<?php echo $u; ?>" <?php
+                            if (in_array($u, $bcc_cids))
+                            echo 'selected="selected"'; ?>><?php echo User::lookup($u); ?>
+                          </option>
+                        <?php } } ?>
+                        ?>
+                    </select>
+                  </span>
+
+                    <a class="inline button" style="overflow:inherit" href="#"
+                    onclick="javascript:
+                    $.userLookup('ajax.php/users/lookup/form', function (user) {
+                      var newUser = new Option(user.name, user.id, true, true);
+                      return $(&quot;#bcc_users&quot;).append(newUser).trigger('change');
+                    });
+                    "><i class="icon-plus"></i> <?php echo __('Add New'); ?></a>
+
                    <br/><span class="error"><?php echo $errors['bccs']; ?></span>
                </td>
              </tr>
@@ -1236,31 +1258,6 @@ $(function() {
 });
 
 $(function() {
-  $('.collabSelections').on("select2:select", function(e) {
-    var el = $(this);
-    var tid = <?php echo $ticket->getThreadId(); ?>;
-    var target = e.currentTarget.id;
-    var addTo = (target == 'cc_users') ? 'addcc' : 'addbcc';
-
-   if(el.val().includes("NEW")) {
-     $("li[title='— Add New —']").remove();
-     var url = 'ajax.php/thread/' + tid + '/add-collaborator/' + addTo ;
-      $.userLookup(url, function(user) {
-        e.preventDefault();
-         if($('.dialog#confirm-action').length) {
-             $('.dialog#confirm-action #action').val(addTo);
-             $('#confirm-form').append('<input type=hidden name=user_id value='+user.id+' />');
-             $('#overlay').show();
-         }
-      });
-         var arr = el.val();
-         var removeStr = "NEW";
-
-         arr.splice($.inArray(removeStr, arr),1);
-         $(this).val(arr);
-    }
- });
-
  $('.collabSelections').on("select2:unselecting", function(e) {
    var el = $(this);
    var target = '#' + e.currentTarget.id;
@@ -1289,7 +1286,6 @@ $(function() {
        };
      },
      processResults: function (data) {
-       data[0] = {name: "\u2014 Add New \u2014", id: "NEW"};
        return {
          results: $.map(data, function (item) {
            return {
-- 
GitLab