From 8b9eb44f7ea2151b658b99917afa3f5b089e5345 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Wed, 7 Oct 2015 21:20:55 -0400
Subject: [PATCH] queue: Implement manual sorting

---
 include/class.config.php            | 10 ++++++
 include/class.orm.php               |  2 +-
 include/class.queue.php             |  1 -
 include/staff/queue.inc.php         |  3 +-
 include/staff/queues-ticket.inc.php | 53 ++++++++++++++++++++---------
 5 files changed, 49 insertions(+), 20 deletions(-)

diff --git a/include/class.config.php b/include/class.config.php
index 8f10d777a..bab8ecb7b 100644
--- a/include/class.config.php
+++ b/include/class.config.php
@@ -1244,6 +1244,16 @@ class OsticketConfig extends Config {
         if(!Validator::process($f, $vars, $errors) || $errors)
             return false;
 
+        // Sort ticket queues
+        require_once INCLUDE_DIR . 'class.queue.php';
+        $queues = CustomQueue::objects()->all();
+        foreach ($vars['qsort'] as $queue_id => $sort) {
+            if ($q = $queues->findFirst(array('id' => $queue_id))) {
+                $q->sort = $sort;
+                $q->save();
+            }
+        }
+
         return $this->updateAll(array(
             'ticket_number_format'=>$vars['ticket_number_format'] ?: '######',
             'ticket_sequence_id'=>$vars['ticket_sequence_id'] ?: 0,
diff --git a/include/class.orm.php b/include/class.orm.php
index a5b335dc6..8b0fdedef 100644
--- a/include/class.orm.php
+++ b/include/class.orm.php
@@ -1882,7 +1882,7 @@ implements Iterator {
      * database.
      */
     function findAll(array $criteria, $limit=false) {
-        $records = array();
+        $records = new ListObject();
         foreach ($this as $record) {
             $matches = true;
             foreach ($criteria as $field => $check) {
diff --git a/include/class.queue.php b/include/class.queue.php
index 7f3facb5d..05f0c75ea 100644
--- a/include/class.queue.php
+++ b/include/class.queue.php
@@ -19,7 +19,6 @@ 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',
diff --git a/include/staff/queue.inc.php b/include/staff/queue.inc.php
index 3cc19582d..9a23e1f3e 100644
--- a/include/staff/queue.inc.php
+++ b/include/staff/queue.inc.php
@@ -29,7 +29,8 @@ else {
   <input type="hidden" name="id" value="<?php echo $info['id']; ?>">
   <input type="hidden" name="root" value="<?php echo Format::htmlchars($_REQUEST['t']); ?>">
 
-  <h2><?php echo __('Ticket Queues'); ?> // <?php echo $title; ?>
+  <h2><a href="settings.php?t=tickets#queues"><?php echo __('Ticket Queues'); ?></a>
+      // <?php echo $title; ?>
       <?php if (isset($queue->id)) { ?><small>
       — <?php echo $queue->getName(); ?></small>
       <?php } ?>
diff --git a/include/staff/queues-ticket.inc.php b/include/staff/queues-ticket.inc.php
index c4b43397a..dfbfbf8b5 100644
--- a/include/staff/queues-ticket.inc.php
+++ b/include/staff/queues-ticket.inc.php
@@ -1,7 +1,6 @@
 <?php
 require_once INCLUDE_DIR . 'class.queue.php';
 ?>
-<form action="queues.php?t=tickets" method="POST" name="keys">
     <div>
         <div class="pull-right">
             <a href="queues.php?t=tickets&amp;a=add" class="green button action-button"><i class="icon-plus-sign"></i> <?php echo __('Add New Queue');?></a>
@@ -37,28 +36,48 @@ require_once INCLUDE_DIR . 'class.queue.php';
     <div class="clear"></div>
  <?php csrf_token(); ?>
  <input type="hidden" name="do" value="mass_process" >
-<input type="hidden" id="action" name="a" value="" >
+ <input type="hidden" id="action" name="a" value="" >
  <table class="list" border="0" cellspacing="1" cellpadding="0" width="940">
     <thead>
         <tr>
             <th width="4%">&nbsp;</th>
-            <th width="46%"><a <?php echo $key_sort; ?> href="queues.php?t=tickets&amp;<?php echo $qstr; ?>&sort=name#queues"><?php echo __('Name');?></a></th>
-            <th width="12%"><a <?php echo $ip_sort; ?> href="queues.php?t=tickets&amp;<?php echo $qstr; ?>&sort=creator#queues"><?php echo __('Creator');?></a></th>
-            <th width="8%"><a  <?php echo $status_sort; ?> href="queues.php?t=tickets&amp;<?php echo $qstr; ?>&sort=status#queues"><?php echo __('Status');?></a></th>
-            <th width="10%" nowrap><a  <?php echo $date_sort; ?>href="queues.php?t=tickets&amp;<?php echo $qstr; ?>&sort=date#queues"><?php echo __('Created');?></a></th>
+            <th colspan="5" width="46%"><?php echo __('Name');?></th>
+            <th width="12%"><?php echo __('Creator');?></th>
+            <th width="8%"><?php echo __('Status');?></th>
+            <th width="10%" nowrap><?php echo __('Created');?></th>
         </tr>
     </thead>
-    <tbody>
-<?php foreach (CustomQueue::objects() as $q) { ?>
-    <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->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>
-    </tr>
+    <tbody class="sortable-rows" data-sort="qsort">
+<?php
+$all_queues = CustomQueue::objects()->all();
+$emitLevel = function($queues, $level=0) use ($all_queues, &$emitLevel) { 
+    $queues->sort(function($a) { return $a->sort; });
+    foreach ($queues as $q) { ?>
+      <tr>
+<?php if ($level) { ?>
+        <td colspan="<?php echo max(1, $level); ?>"></td>
 <?php } ?>
+        <td>
+          <input type="checkbox" class="checkbox" name="ckb[]">
+          <input type="hidden" name="qsort[<?php echo $q->id; ?>]"
+            value="<?php echo $q->sort; ?>"/>
+        </td>
+        <td colspan="<?php echo max(1, 5-$level); ?>"><a
+          href="queues.php?id=<?php echo $q->getId(); ?>"><?php
+          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>
+      </tr>
+<?php
+        $children = $all_queues->findAll(array('parent_id' => $q->id));
+        if (count($children)) {
+            $emitLevel($children, $level+1);
+        }
+    }
+};
+
+$emitLevel($all_queues->findAll(array('parent_id' => 0)));
+?>
     </tbody>
 </table>
-</form>
-- 
GitLab