From e04778d145952db786f53127c7ff8dea2815fad7 Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@enhancesoft.com>
Date: Sun, 21 Oct 2018 04:19:11 +0000
Subject: [PATCH] Queue: Hierarchical Queues

The commit fixes queues display and sorting.
---
 include/class.queue.php                       | 26 ++++++++++++++-----
 include/class.search.php                      |  4 +++
 include/i18n/en_US/queue.yaml                 |  4 +--
 .../queue-savedsearches-nav.tmpl.php          | 15 +++++------
 scp/tickets.php                               |  5 +---
 5 files changed, 33 insertions(+), 21 deletions(-)

diff --git a/include/class.queue.php b/include/class.queue.php
index 812f9da30..b35f33b85 100644
--- a/include/class.queue.php
+++ b/include/class.queue.php
@@ -1339,23 +1339,35 @@ class CustomQueue extends VerySimpleModel {
      *      visible queues.
      * $pid - <int> parent_id of root queue. Default is zero (top-level)
      */
-    static function getHierarchicalQueues(Staff $staff, $pid=0) {
-        $all = static::objects()
+    static function getHierarchicalQueues(Staff $staff, $pid=0,
+            $primary=true) {
+        $query = static::objects()
+            ->annotate(array('_sort' =>  SqlCase::N()
+                        ->when(array('sort' => 0), 999)
+                        ->otherwise(new SqlField('sort'))))
             ->filter(Q::any(array(
                 'flags__hasbit' => self::FLAG_PUBLIC,
                 'flags__hasbit' => static::FLAG_QUEUE,
                 'staff_id' => $staff->getId(),
             )))
             ->exclude(['flags__hasbit' => self::FLAG_DISABLED])
-            ->asArray();
-
+            ->order_by('parent_id', '_sort', 'title');
+        $all = $query->asArray();
         // Find all the queues with a given parent
-        $for_parent = function($pid) use ($all, &$for_parent) {
+        $for_parent = function($pid) use ($primary, $all, &$for_parent) {
             $results = [];
             foreach (new \ArrayIterator($all) as $q) {
-                if ($q->parent_id == $pid)
-                    $results[] = [ $q, $for_parent($q->getId()) ];
+                if ($q->parent_id != $pid)
+                    continue;
+
+                if ($pid == 0 && (
+                            ($primary &&  !$q->isAQueue())
+                            || (!$primary && $q->isAQueue())))
+                    continue;
+
+                $results[] = [ $q, $for_parent($q->getId()) ];
             }
+
             return $results;
         };
 
diff --git a/include/class.search.php b/include/class.search.php
index f76aacf16..eb720bcc2 100644
--- a/include/class.search.php
+++ b/include/class.search.php
@@ -701,6 +701,10 @@ class SavedQueue extends CustomQueue {
         return $this->_columns;
     }
 
+    static function getHierarchicalQueues(Staff $staff) {
+        return CustomQueue::getHierarchicalQueues($staff, 0, false);
+    }
+
     /**
      * Fetch an AdvancedSearchForm instance for use in displaying or
      * configuring this search in the user interface.
diff --git a/include/i18n/en_US/queue.yaml b/include/i18n/en_US/queue.yaml
index b7325620c..34a35c287 100644
--- a/include/i18n/en_US/queue.yaml
+++ b/include/i18n/en_US/queue.yaml
@@ -118,7 +118,7 @@
   parent_id: 1
   flags: 0x2b
   root: T
-  sort: 1
+  sort: 2
   sort_id: 4
   config: '{"criteria":[["isanswered","set",null]],"conditions":[]}'
   columns:
@@ -158,7 +158,7 @@
   parent_id: 1
   flags: 0x2b
   root: T
-  sort: 2
+  sort: 3
   sort_id: 4
   config: '{"criteria":[["isoverdue","set",null]],"conditions":[]}'
   columns:
diff --git a/include/staff/templates/queue-savedsearches-nav.tmpl.php b/include/staff/templates/queue-savedsearches-nav.tmpl.php
index ef06cf06c..34a8aff16 100644
--- a/include/staff/templates/queue-savedsearches-nav.tmpl.php
+++ b/include/staff/templates/queue-savedsearches-nav.tmpl.php
@@ -4,6 +4,10 @@
 // $searches = All visibile saved searches
 // $child_selected - <bool> true if the selected queue is a descendent
 // $adhoc - not FALSE if an adhoc advanced search exists
+
+$searches = SavedQueue::getHierarchicalQueues($thisstaff);
+if ($queue && !$queue->parent_id && $queue->staff_id)
+    $child_selected = true;
 ?>
 <li class="primary-only item <?php if ($child_selected) echo 'active'; ?>">
 <?php
@@ -16,14 +20,9 @@
   <div class="customQ-dropdown">
     <ul class="scroll-height">
       <!-- Start Dropdown and child queues -->
-      <?php foreach ($searches->findAll(array(
-            'staff_id' => $thisstaff->getId(),
-            'parent_id' => 0,
-            Q::not(array(
-                'flags__hasbit' => CustomQueue::FLAG_PUBLIC
-            ))
-      )) as $q) {
-        if ($q->checkAccess($thisstaff))
+      <?php foreach ($searches as $search) {
+          list($q, $children) = $search;
+          if ($q->checkAccess($thisstaff))
             include 'queue-subnavigation.tmpl.php';
       } ?>
      <?php
diff --git a/scp/tickets.php b/scp/tickets.php
index 41ba48538..86fec81f2 100644
--- a/scp/tickets.php
+++ b/scp/tickets.php
@@ -466,7 +466,7 @@ foreach ($queues as $_) {
                 || false !== strpos($queue->getPath(), "/{$q->getId()}/"));
         include STAFFINC_DIR . 'templates/queue-navigation.tmpl.php';
 
-        return ($child_selected || $selected);
+        return $child_selected;
     });
 }
 
@@ -477,10 +477,7 @@ $nav->addSubMenu(function() use ($queue) {
     // A queue is selected if it is the one being displayed. It is
     // "child" selected if its ID is in the path of the one selected
     $child_selected = $queue instanceof SavedSearch;
-    $searches = SavedSearch::forStaff($thisstaff)->getIterator();
-
     include STAFFINC_DIR . 'templates/queue-savedsearches-nav.tmpl.php';
-
     return ($child_selected || $selected);
 });
 
-- 
GitLab