From dea47d4e3448a35bf5707b1f14ac474fb47985c8 Mon Sep 17 00:00:00 2001
From: JediKev <kevin@enhancesoft.com>
Date: Mon, 29 Oct 2018 11:19:20 -0500
Subject: [PATCH] queue: Top-Level Ticket Counts

This adds a setting in the Ticket Settings to give the option of showing the
ticket counts on Top-Level queues. If enabled, this will populate the ticket
counts next to the Top-Level queue names like they used to be. The only
caveat is the counts are triggered by mousing-over any of the queue names as
we only load the counts when we need to display the sub-queues (for better
performance).

This also adds a new format function called `number` (Phase 1). As of now it
will format any integer to a comma separated number (eg. `1337 -> 1,337`).
---
 include/ajax.search.php                          |  2 +-
 include/class.config.php                         |  5 +++++
 include/class.format.php                         | 16 ++++++++++++++++
 include/i18n/en_US/config.yaml                   |  1 +
 .../i18n/en_US/help/tips/settings.ticket.yaml    |  6 ++++++
 include/staff/settings-tickets.inc.php           |  6 ++++++
 .../staff/templates/queue-navigation.tmpl.php    | 11 ++++++++++-
 scp/js/scp.js                                    |  3 ++-
 8 files changed, 47 insertions(+), 3 deletions(-)

diff --git a/include/ajax.search.php b/include/ajax.search.php
index 2cf552151..8ac463aa0 100644
--- a/include/ajax.search.php
+++ b/include/ajax.search.php
@@ -396,6 +396,6 @@ class SearchAjaxAPI extends AjaxController {
             $criteria = array('id__in' => $ids);
         $counts = SavedQueue::counts($thisstaff, true, $criteria);
         Http::response(200, false, 'application/json');
-        return $this->encode($counts);
+        return $this->encode(Format::number($counts));
     }
 }
diff --git a/include/class.config.php b/include/class.config.php
index d7518a91f..8cdf808e8 100644
--- a/include/class.config.php
+++ b/include/class.config.php
@@ -767,6 +767,10 @@ class OsticketConfig extends Config {
         return $sequence;
     }
 
+    function showTopLevelTicketCounts() {
+        return ($this->get('queue_bucket_counts'));
+    }
+
     function getDefaultTicketNumberFormat() {
         return $this->get('ticket_number_format');
     }
@@ -1277,6 +1281,7 @@ class OsticketConfig extends Config {
         return $this->updateAll(array(
             'ticket_number_format'=>$vars['ticket_number_format'] ?: '######',
             'ticket_sequence_id'=>$vars['ticket_sequence_id'] ?: 0,
+            'queue_bucket_counts'=>isset($vars['queue_bucket_counts'])?1:0,
             'default_priority_id'=>$vars['default_priority_id'],
             'default_help_topic'=>$vars['default_help_topic'],
             'default_ticket_status_id'=>$vars['default_ticket_status_id'],
diff --git a/include/class.format.php b/include/class.format.php
index d3d9d7ae9..399feaf0a 100644
--- a/include/class.format.php
+++ b/include/class.format.php
@@ -503,6 +503,22 @@ class Format {
         return implode( $separator, $string );
     }
 
+    function number($number, $locale=false) {
+        if (is_array($number))
+            return array_map(array('Format','number'), $number);
+
+        if (!is_numeric($number))
+            return $number;
+
+        if (extension_loaded('intl') && class_exists('NumberFormatter')) {
+            $nf = NumberFormatter::create($locale ?: Internationalization::getCurrentLocale(),
+                NumberFormatter::DECIMAL);
+            return $nf->format($number);
+        }
+
+        return number_format((int) $number);
+    }
+
     /* elapsed time */
     function elapsedTime($sec) {
 
diff --git a/include/i18n/en_US/config.yaml b/include/i18n/en_US/config.yaml
index 731e51eb2..e0c47f714 100644
--- a/include/i18n/en_US/config.yaml
+++ b/include/i18n/en_US/config.yaml
@@ -77,6 +77,7 @@ core:
     email_attachments: 1
     ticket_number_format: '######'
     ticket_sequence_id: 0
+    queue_bucket_counts: 0
     task_number_format: '#'
     task_sequence_id: 2
     log_level: 2
diff --git a/include/i18n/en_US/help/tips/settings.ticket.yaml b/include/i18n/en_US/help/tips/settings.ticket.yaml
index 3b0c1cea9..a736db8e0 100644
--- a/include/i18n/en_US/help/tips/settings.ticket.yaml
+++ b/include/i18n/en_US/help/tips/settings.ticket.yaml
@@ -32,6 +32,12 @@ sequence_id:
         in the <span class="doc-desc-title">Ticket Number
         Format</span> configuration for help topics.
 
+queue_bucket_counts:
+    title: Top-Level Ticket Counts
+    content: >
+        This setting is used to hide or show the ticket counts on Main-Level
+        queues. Get back to the way things used to be.
+
 default_ticket_status:
     title: Default Status for new Tickets
     content: >
diff --git a/include/staff/settings-tickets.inc.php b/include/staff/settings-tickets.inc.php
index 76db039c0..95636299e 100644
--- a/include/staff/settings-tickets.inc.php
+++ b/include/staff/settings-tickets.inc.php
@@ -65,6 +65,12 @@ if(!($maxfileuploads=ini_get('max_file_uploads')))
                 <i class="help-tip icon-question-sign" href="#sequence_id"></i>
             </td>
         </tr>
+        <tr><td width="220"><?php echo __('Top-Level Ticket Counts'); ?>:</td>
+            <td>
+                <input type="checkbox" name="queue_bucket_counts" <?php echo $config['queue_bucket_counts']?'checked="checked"':''; ?>>
+                <?php echo __('Enable'); ?>&nbsp;<i class="help-tip icon-question-sign" href="#queue_bucket_counts"></i>
+            </td>
+        </tr>
         <tr>
             <td width="180" class="required">
                 <?php echo __('Default Status'); ?>:
diff --git a/include/staff/templates/queue-navigation.tmpl.php b/include/staff/templates/queue-navigation.tmpl.php
index 380e03af9..f408c2184 100644
--- a/include/staff/templates/queue-navigation.tmpl.php
+++ b/include/staff/templates/queue-navigation.tmpl.php
@@ -4,6 +4,7 @@
 // $q - <CustomQueue> object for this navigation entry
 // $selected - <bool> true if this queue is currently active
 // $child_selected - <bool> true if the selected queue is a descendent
+global $cfg;
 $childs = $children;
 $this_queue = $q;
 $selected = (!isset($_REQUEST['a'])  && $_REQUEST['queue'] == $this_queue->getId());
@@ -11,7 +12,15 @@ $selected = (!isset($_REQUEST['a'])  && $_REQUEST['queue'] == $this_queue->getId
 <li class="top-queue item <?php if ($child_selected) echo 'child active';
     elseif ($selected) echo 'active'; ?>">
   <a href="<?php echo $this_queue->getHref(); ?>"
-    class="Ticket"><i class="small icon-sort-down pull-right"></i><?php echo $this_queue->getName(); ?></a>
+    class="Ticket"><i class="small icon-sort-down pull-right"></i><?php echo $this_queue->getName(); ?>
+<?php if ($cfg->showTopLevelTicketCounts()) { ?>
+    <span id="queue-count-bucket" class="hidden">
+      (<span class="queue-count"
+        data-queue-id="<?php echo $this_queue->id; ?>"><span class="faded-more"></span>
+      </span>)
+    </span>
+<?php } ?>
+  </a>
   <div class="customQ-dropdown">
     <ul class="scroll-height">
       <!-- Add top-level queue (with count) -->
diff --git a/scp/js/scp.js b/scp/js/scp.js
index cbccaed04..c15d2330f 100644
--- a/scp/js/scp.js
+++ b/scp/js/scp.js
@@ -534,9 +534,10 @@ var scp_prep = function() {
         url: 'ajax.php/queue/counts',
         dataType: 'json',
         success: function(json) {
-          $('li > span.queue-count').each(function(i, e) {
+          $('li span.queue-count').each(function(i, e) {
             var $e = $(e);
             $e.text(json['q' + $e.data('queueId')]);
+            $(e).parents().find('#queue-count-bucket').show();
           });
         }
       });
-- 
GitLab