From 2accf58751ab38b2ca6fd7186684a64df7c193da Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Mon, 1 Sep 2014 18:31:47 +0000
Subject: [PATCH] Consolidate and unify change status buttons

---
 include/ajax.tickets.php                      |  4 +-
 include/class.list.php                        |  2 +-
 .../staff/templates/status-options.tmpl.php   | 96 +++++++++----------
 include/staff/ticket-view.inc.php             | 24 +----
 include/staff/tickets.inc.php                 |  9 +-
 scp/ajax.php                                  |  2 +-
 6 files changed, 64 insertions(+), 73 deletions(-)

diff --git a/include/ajax.tickets.php b/include/ajax.tickets.php
index 01a537117..5cf3f24e1 100644
--- a/include/ajax.tickets.php
+++ b/include/ajax.tickets.php
@@ -701,7 +701,7 @@ class TicketsAjaxAPI extends AjaxController {
         return $canned->getFormattedResponse($format, $varReplacer);
     }
 
-    function changeTicketStatus($tid, $status) {
+    function changeTicketStatus($tid, $status, $id=0) {
         global $thisstaff;
 
         if (!$thisstaff)
@@ -748,7 +748,7 @@ class TicketsAjaxAPI extends AjaxController {
         $info['title'] = sprintf('%s %s #%s',
                 $verb ?: $status,
                 __('Ticket'), $ticket->getNumber());
-        $info['status_id'] = $_REQUEST['status_id'] ?: $ticket->getStatusId();
+        $info['status_id'] = $_REQUEST['status_id'] ?: $id ?: $ticket->getStatusId();
 
         return self::_setStatus($state, $info);
     }
diff --git a/include/class.list.php b/include/class.list.php
index 3decd37f9..b4423c3aa 100644
--- a/include/class.list.php
+++ b/include/class.list.php
@@ -1036,7 +1036,7 @@ class TicketStatus  extends VerySimpleModel implements CustomListItem {
         return $status;
     }
 
-    static function options() {
+    static function status_options() {
         include(STAFFINC_DIR . 'templates/status-options.tmpl.php');
     }
 }
diff --git a/include/staff/templates/status-options.tmpl.php b/include/staff/templates/status-options.tmpl.php
index ebc5db6cb..19235f19b 100644
--- a/include/staff/templates/status-options.tmpl.php
+++ b/include/staff/templates/status-options.tmpl.php
@@ -1,60 +1,60 @@
 <?php
+global $thisstaff, $ticket;
+// Map states to actions
 $actions= array(
-        'close' => array(
+        'closed' => array(
             'icon'  => 'icon-repeat',
-            'state' => 'closed'
+            'action' => 'close'
             ),
-        'resolve' => array(
+        'resolved' => array(
             'icon'  => 'icon-ok-circle',
-            'state' => 'resolved'
+            'action' => 'resolve'
             ),
-        'reopen' => array(
+        'open' => array(
             'icon'  => 'icon-undo',
-            'state' => 'open'
+            'action' => 'reopen'
             ),
         );
+?>
 
-foreach($actions as $k => $v) {
-    $criteria = array('states' => array($v['state']));
-    if (!($statuses = TicketStatusList::getStatuses($criteria)->all()))
-        continue;
-
-    if ($statuses && count($statuses) > 1) {
-    ?>
-        <span
-            class="action-button"
-            data-dropdown="#action-dropdown-<?php echo $k; ?>">
-            <a id="tickets-<?php echo $k; ?>"
-                class="tickets-action"
-                href="#tickets/status/<?php echo $k; ?>"><i
-                class="<?php echo $v['icon']; ?>"></i> <?php
-                echo TicketStateField::getVerb($v['state']); ?></a>
-            <i class="icon-caret-down"></i>
-        </span>
-        <div id="action-dropdown-<?php echo $k; ?>"
-            class="action-dropdown anchor-right">
-          <ul>
-            <?php
-            foreach ($statuses as $s) {
-                ?>
+<span
+    class="action-button"
+    data-dropdown="#action-dropdown-statuses">
+    <a class="tickets-action"
+        href="#statuses"><i
+        class="icon-flag"></i> <?php
+        echo __('Change Status'); ?></a>
+    <i class="icon-caret-down"></i>
+</span>
+<div id="action-dropdown-statuses"
+    class="action-dropdown anchor-right">
+    <ul>
+    <?php
+    $states = array('open');
+    if ($thisstaff->canCloseTickets())
+        $states = array_merge($states,
+                array('resolved', 'closed'));
 
-             <li>
-                 <a class="no-pjax tickets-action"
-                    href="#tickets/status/<?php echo $k; ?>/<?php
-                    echo $s->getId(); ?>"> <i
-                        class="icon-tag"></i> <?php echo __($s->getName()); ?></a> </li>
-            <?php
-            } ?>
-          </ul>
-        </div>
+    $statusId = $ticket ? $ticket->getStatusId() : 0;
+    foreach (TicketStatusList::getStatuses(
+                array('states' => $states))->all() as $status) {
+        if (!isset($actions[$status->getState()])
+                || $statusId == $status->getId())
+            continue;
+        ?>
+        <li>
+            <a class="no-pjax <?php
+                echo $ticket? 'ticket-action' : 'tickets-action'; ?>"
+                href="<?php
+                    echo sprintf('#%s/status/%s/%d',
+                            $ticket ? ('tickets/'.$ticket->getId()) : 'tickets',
+                            $actions[$status->getState()]['action'],
+                            $status->getId()); ?>"><i class=" aaa <?php
+                        echo $actions[$status->getState()]['icon'] ?: 'icon-tag';
+                    ?>"></i> <?php
+                        echo __($status->getName()); ?></a>
+        </li>
     <?php
-    } else {
-    ?>
-        <a id="tickets-<?php echo $k; ?>" class="action-button tickets-action"
-            href="#tickets/status/<?php echo $k; ?>"><i
-            class="<?php echo $v['icon']; ?>"></i> <?php
-            echo TicketStateField::getVerb($v['state']); ?></a>
-<?php
-    }
-}
-?>
+    } ?>
+    </ul>
+</div>
diff --git a/include/staff/ticket-view.inc.php b/include/staff/ticket-view.inc.php
index addb9aa3b..6d0295017 100644
--- a/include/staff/ticket-view.inc.php
+++ b/include/staff/ticket-view.inc.php
@@ -56,29 +56,15 @@ if($ticket->isOverdue())
             </span>
             <?php
             }
-            if($thisstaff->canCloseTickets()) {
-                if($ticket->isOpen()) {?>
-                <a class="action-button ticket-action"
-                    href="#tickets/<?php echo $ticket->getId()
-                    ?>/status/close"><i class="icon-repeat"></i> <?php echo __('Close');?></a>
-                <a class="action-button ticket-action"
-                    href="#tickets/<?php echo $ticket->getId()
-                    ?>/status/resolve"><i class="icon-ok-circle"></i> <?php echo __('Resolve');?></a>
-                <?php
-                } else { ?>
-                <a class="action-button ticket-action"
-                    href="#tickets/<?php echo $ticket->getId()
-                    ?>/status/reopen"><i class="icon-undo"></i> <?php echo
-                    __('Reopen');?></a>
-                <?php
-                }
-            }
-            if($thisstaff->canEditTickets()) { ?>
+            // Status change options
+            echo TicketStatus::status_options();
+
+            if ($thisstaff->canEditTickets()) { ?>
                 <a class="action-button" href="tickets.php?id=<?php echo $ticket->getId(); ?>&a=edit"><i class="icon-edit"></i> <?php
                     echo __('Edit'); ?></a>
             <?php
             }
-            if($ticket->isOpen() && !$ticket->isAssigned() && $thisstaff->canAssignTickets()) {?>
+            if ($ticket->isOpen() && !$ticket->isAssigned() && $thisstaff->canAssignTickets()) {?>
                 <a id="ticket-claim" class="action-button confirm-action" href="#claim"><i class="icon-user"></i> <?php
                     echo __('Claim'); ?></a>
 
diff --git a/include/staff/tickets.inc.php b/include/staff/tickets.inc.php
index 899f54e2a..1ebcf5c4c 100644
--- a/include/staff/tickets.inc.php
+++ b/include/staff/tickets.inc.php
@@ -335,12 +335,17 @@ if ($results) {
                 $results_type.$showing; ?></a></h2>
         </div>
         <div class="pull-right flush-right">
+
+            <?php
+            if ($thisstaff->canDeleteTickets()) { ?>
             <a id="tickets-delete" class="action-button tickets-action"
                 href="#tickets/status/delete"><i
             class="icon-trash"></i> <?php echo __('Delete'); ?></a>
             <?php
-            if ($res && $results && $thisstaff->canManageTickets()) {
-                echo TicketStatus::options();
+            } ?>
+            <?php
+            if ($thisstaff->canManageTickets()) {
+                echo TicketStatus::status_options();
             }
             ?>
         </div>
diff --git a/scp/ajax.php b/scp/ajax.php
index 7cfe289d5..7ce76dc97 100644
--- a/scp/ajax.php
+++ b/scp/ajax.php
@@ -140,7 +140,7 @@ $dispatcher = patterns('',
         url_get('^(?P<tid>\d+)/forms/manage$', 'manageForms'),
         url_post('^(?P<tid>\d+)/forms/manage$', 'updateForms'),
         url_get('^(?P<tid>\d+)/canned-resp/(?P<cid>\w+).(?P<format>json|txt)', 'cannedResponse'),
-        url_get('^(?P<tid>\d+)/status/(?P<status>\w+)$', 'changeTicketStatus'),
+        url_get('^(?P<tid>\d+)/status/(?P<status>\w+)(?:/(?P<sid>\d+))?$', 'changeTicketStatus'),
         url_post('^(?P<tid>\d+)/status/(?P<status>\w+)$', 'setTicketStatus'),
         url_get('^status/(?P<status>\w+)(?:/(?P<sid>\d+))?$', 'changeTicketsStatus'),
         url_post('^status/(?P<state>\w+)$', 'setTicketsStatus'),
-- 
GitLab