From 6be198876950cf5acc5b865d7732610a2a81fcf4 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Tue, 6 Oct 2015 12:04:56 -0500
Subject: [PATCH] queue: Add ability to inherit criteria

---
 include/ajax.search.php     |  7 +------
 include/class.queue.php     | 15 +++++++++++++--
 include/class.search.php    | 17 ++++++++++-------
 include/staff/queue.inc.php |  3 +++
 4 files changed, 27 insertions(+), 15 deletions(-)

diff --git a/include/ajax.search.php b/include/ajax.search.php
index d670d6caf..2d9b3cea8 100644
--- a/include/ajax.search.php
+++ b/include/ajax.search.php
@@ -227,12 +227,7 @@ class SearchAjaxAPI extends AjaxController {
             $queue = CustomQueue::create();
         }
 
-        // Update queue columns (but without save)
-        foreach ($_POST['columns'] as $colid) {
-            $col = QueueColumn::create(array("id" => $colid, "queue" => $queue));
-            $col->update($_POST);
-            $queue->addColumn($col);
-        }
+        $queue->update($_POST);
 
         $form = $queue->getForm($_POST);
         $tickets = $queue->getQuery($form);
diff --git a/include/class.queue.php b/include/class.queue.php
index 3a6372432..8bf73879a 100644
--- a/include/class.queue.php
+++ b/include/class.queue.php
@@ -18,6 +18,7 @@ require_once INCLUDE_DIR . 'class.search.php';
 
 class CustomQueue extends SavedSearch {
     static $meta = array(
+        'select_related' => array('parent'),
         'joins' => array(
             'columns' => array(
                 'reverse' => 'QueueColumn.queue',
@@ -148,9 +149,18 @@ class CustomQueue extends SavedSearch {
         return 'tickets.php?queue='.$this->getId();
     }
 
+    function inheritCriteria() {
+        return $this->flags & self::FLAG_INHERIT_CRITERIA;
+    }
+
     function getBasicQuery($form=false) {
-        $root = $this->getRoot();
-        $query = $root::objects();
+        if ($this->parent && $this->inheritCriteria()) {
+            $query = $this->parent->getBasicQuery();
+        }
+        else {
+            $root = $this->getRoot();
+            $query = $root::objects();
+        }
         $form = $form ?: $this->loadFromState($this->getCriteria());
         return $this->mangleQuerySet($query, $form);
     }
@@ -210,6 +220,7 @@ class CustomQueue extends SavedSearch {
         $this->title = $vars['name'];
         $this->parent_id = $vars['parent_id'];
         $this->filter = $vars['filter'];
+        $this->setFlag(self::FLAG_INHERIT_CRITERIA, isset($vars['inherit']));
 
         // Update queue columns (but without save)
         if (isset($vars['columns'])) {
diff --git a/include/class.search.php b/include/class.search.php
index eea827a83..06a728424 100644
--- a/include/class.search.php
+++ b/include/class.search.php
@@ -664,9 +664,10 @@ class SavedSearch extends VerySimpleModel {
         'ordering' => array('sort'),
     );
 
-    const FLAG_PUBLIC =     0x0001;   // Shows up in e'eryone's saved searches
-    const FLAG_QUEUE =      0x0002;   // Shows up in queue navigation
-    const FLAG_CONTAINER =  0x0004;   // Container for other queues ('Open')
+    const FLAG_PUBLIC =         0x0001; // Shows up in e'eryone's saved searches
+    const FLAG_QUEUE =          0x0002; // Shows up in queue navigation
+    const FLAG_CONTAINER =      0x0004; // Container for other queues ('Open')
+    const FLAG_INHERIT_CRITERIA = 0x0008; // Include criteria from parent
 
     var $criteria;
 
@@ -1106,15 +1107,17 @@ class SavedSearch extends VerySimpleModel {
     }
 
     protected function hasFlag($flag) {
-        return $this->get('flag') & $flag !== 0;
+        return $this->get('flags') & $flag !== 0;
     }
 
     protected function clearFlag($flag) {
-        return $this->set('flag', $this->get('flag') & ~$flag);
+        return $this->set('flags', $this->get('flag') & ~$flag);
     }
 
-    protected function setFlag($flag) {
-        return $this->set('flag', $this->get('flag') | $flag);
+    protected function setFlag($flag, $value=true) {
+        return $value
+            ? $this->flags |= $flag
+            : $this->clearFlag($flag);
     }
 
     static function create($vars=array()) {
diff --git a/include/staff/queue.inc.php b/include/staff/queue.inc.php
index bfe6b4476..47df03bfb 100644
--- a/include/staff/queue.inc.php
+++ b/include/staff/queue.inc.php
@@ -56,6 +56,9 @@ else {
         <br/>
         <br/>
         <div><strong><?php echo __("Queue Search Criteria"); ?></strong></div>
+        <div><input type="checkbox" class="checkbox" name="inherit" <?php
+            if ($queue->inheritCriteria()) echo 'checked="checked"';
+            ?>/> <?php echo __('Include parent search criteria'); ?></div>
         <hr/>
         <div class="error"><?php echo $errors['criteria']; ?></div>
         <div class="advanced-search">
-- 
GitLab