From 5ec1098162c85a0f4fec1e9cc4909e3a634c2808 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Tue, 6 Oct 2015 20:06:03 -0400
Subject: [PATCH] queue: Fix sorting on hierarchical queues

---
 include/class.queue.php             | 29 +++++++++++++++++++++++++++++
 include/staff/queues-ticket.inc.php |  2 +-
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/include/class.queue.php b/include/class.queue.php
index 510b4c466..c17e8e91e 100644
--- a/include/class.queue.php
+++ b/include/class.queue.php
@@ -19,6 +19,7 @@ require_once INCLUDE_DIR . 'class.search.php';
 class CustomQueue extends SavedSearch {
     static $meta = array(
         'select_related' => array('parent'),
+        'ordering' => array('title', 'path'),
         'joins' => array(
             'columns' => array(
                 'reverse' => 'QueueColumn.queue',
@@ -143,6 +144,28 @@ class CustomQueue extends SavedSearch {
         ));
     }
 
+    function getPath() {
+        return $this->path ?: $this->buildPath();
+    }
+
+    function buildPath() {
+        if (!$this->id)
+            return;
+
+        $path = $this->id;
+        if ($this->parent) {
+            $path = sprintf('%s/%d', $this->parent->getPath(), $path);
+        }
+        return $path;
+    }
+
+    function getFullName() {
+        $base = $this->getName();
+        if ($this->parent)
+            $base = sprintf("%s / %s", $this->parent->getFullName(), $base);
+        return $base;
+    }
+
     function getHref() {
         // TODO: Get base page from getRoot();
         $root = $this->getRoot();
@@ -225,6 +248,7 @@ class CustomQueue extends SavedSearch {
         $this->title = $vars['name'];
         $this->parent_id = $vars['parent_id'];
         $this->filter = $vars['filter'];
+        $this->path = $this->buildPath();
         $this->setFlag(self::FLAG_INHERIT_CRITERIA, isset($vars['inherit']));
 
         // Update queue columns (but without save)
@@ -246,9 +270,14 @@ class CustomQueue extends SavedSearch {
     }
 
     function save($refetch=false) {
+        $wasnew = !isset($this->id);
         if (!($rv = parent::save($refetch)))
             return $rv;
 
+        if ($wasnew) {
+            $this->path = $this->buildPath();
+            $this->save();
+        }
         return $this->columns->saveAll();
     }
 
diff --git a/include/staff/queues-ticket.inc.php b/include/staff/queues-ticket.inc.php
index 620ae9158..c4b43397a 100644
--- a/include/staff/queues-ticket.inc.php
+++ b/include/staff/queues-ticket.inc.php
@@ -53,7 +53,7 @@ require_once INCLUDE_DIR . 'class.queue.php';
     <tr>
       <td><input type="checkbox" class="checkbox" name="ckb[]"></td>
       <td><a href="queues.php?id=<?php echo $q->getId(); ?>"><?php
-        echo Format::htmlchars($q->getName()); ?></a></td>
+        echo Format::htmlchars($q->getFullName()); ?></a></td>
       <td><?php echo Format::htmlchars($q->staff->getName()); ?></td>
       <td><?php echo Format::htmlchars($q->getStatus()); ?></td>
       <td><?php echo Format::date($q->created); ?></td>
-- 
GitLab