From a0b0bf31cd6465e666d393e5cbf3a38211a7b789 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared.hancock@cleco.com>
Date: Thu, 29 Dec 2016 10:41:41 -0600
Subject: [PATCH] queue: Add options for easy addition of queues

Adds the ability to "Clone" a queue, which would be nice to create a queue
similar to the current one with a couple distinct settings, but also not a
child of the queue. Also adds the ability to add a "Sub Queue" to the current
queue, which will allow easy creation of queues which are pre-configured for
inheritance.
---
 include/class.orm.php                         | 17 ++++++++++++++++-
 include/class.queue.php                       |  2 ++
 include/staff/queue.inc.php                   |  2 ++
 .../staff/templates/queue-tickets.tmpl.php    | 14 ++++++++++++++
 scp/queues.php                                | 19 +++++++++++++++++++
 5 files changed, 53 insertions(+), 1 deletion(-)

diff --git a/include/class.orm.php b/include/class.orm.php
index 63510fade..3e8f9830c 100644
--- a/include/class.orm.php
+++ b/include/class.orm.php
@@ -727,6 +727,21 @@ class VerySimpleModel {
     function getDbFields() {
         return $this->ht;
     }
+
+    /**
+     * Create a new clone of this model. The primary key will be unset and the
+     * object will be set as __new__. The __clone() magic method is reserved
+     * by the buildModel() system, because it clone's a single instance when
+     * hydrating objects from the database.
+     */
+    function copy() {
+        // Drop the PK and set as unsaved
+        $dup = clone $this;
+        foreach ($dup::getMeta('pk') as $f)
+            $dup->__unset($f);
+        $dup->__new__ = true;
+        return $dup;
+    }
 }
 
 /**
@@ -814,7 +829,7 @@ trait WriteableAnnotatedModelTrait {
     }
 
     function __isset($what) {
-        if ($this->__overlay__->__isset($what))
+        if (isset($this->__overlay__) && $this->__overlay__->__isset($what))
             return true;
         return parent::__isset($what);
     }
diff --git a/include/class.queue.php b/include/class.queue.php
index 92ad23ae5..f0994e7b7 100644
--- a/include/class.queue.php
+++ b/include/class.queue.php
@@ -60,6 +60,8 @@ class CustomQueue extends VerySimpleModel {
     const FLAG_INHERIT_SORTING =  0x0020; // Inherit advanced sorting from parent
     const FLAG_INHERIT_DEF_SORT = 0x0040; // Inherit default selected sort
 
+    const FLAG_INHERIT_EVERYTHING = 0x78; // Maskf or all INHERIT flags
+
     var $criteria;
 
     static function queues() {
diff --git a/include/staff/queue.inc.php b/include/staff/queue.inc.php
index 53afb43d5..25ef7faa9 100644
--- a/include/staff/queue.inc.php
+++ b/include/staff/queue.inc.php
@@ -9,6 +9,8 @@ if (!$queue) {
     $queue = CustomQueue::create(array(
         'flags' => CustomQueue::FLAG_QUEUE,
     ));
+}
+if ($queue->__new__) {
     $title=__('Add New Queue');
     $action='create';
     $submit_text=__('Create');
diff --git a/include/staff/templates/queue-tickets.tmpl.php b/include/staff/templates/queue-tickets.tmpl.php
index 69189caf3..f87afe61e 100644
--- a/include/staff/templates/queue-tickets.tmpl.php
+++ b/include/staff/templates/queue-tickets.tmpl.php
@@ -175,6 +175,20 @@ else {
                         </li>
 <?php
 }
+if ($thisstaff->isAdmin()) { ?>
+                        <li>
+                            <a class="no-pjax"
+                            href="queues.php?a=sub&amp;id=<?php echo $queue->id; ?>"><i
+                            class="icon-fixed-width icon-level-down"></i>
+                            <?php echo __('Add Sub Queue'); ?></a>
+                        </li>
+                        <li>
+                            <a class="no-pjax"
+                            href="queues.php?a=clone&amp;id=<?php echo $queue->id; ?>"><i
+                            class="icon-fixed-width icon-copy"></i>
+                            <?php echo __('Clone'); ?></a>
+                        </li>
+<?php }
 if (
     ($thisstaff->isAdmin() && $queue->parent_id)
     || $queue->isPrivate()
diff --git a/scp/queues.php b/scp/queues.php
index 5c41f79d4..c2a641614 100644
--- a/scp/queues.php
+++ b/scp/queues.php
@@ -90,6 +90,25 @@ if ($_POST) {
         Http::redirect('settings.php?t=tickets#queues');
     }
 }
+elseif (isset($_GET['a'])
+    && isset($queue) && $queue instanceof CustomQueue
+) {
+    switch (strtolower($_GET['a'])) {
+    case 'clone':
+        $queue = $queue->copy();
+        // Require a new name for the queue
+        unset($queue->title);
+        break;
+    case 'sub':
+        $q = new CustomQueue([
+            'parent' => $queue,
+            'flags' => CustomQueue::FLAG_QUEUE
+                     | CustomQueue::FLAG_INHERIT_EVERYTHING,
+        ]);
+        $queue = $q;
+        break;
+    }
+}
 
 require_once(STAFFINC_DIR.'header.inc.php');
 include_once(STAFFINC_DIR."queue.inc.php");
-- 
GitLab