From 4867615ae0466d0e7dc63a3f543372d917940f7f Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Thu, 13 Aug 2015 14:19:13 -0500
Subject: [PATCH] client: Show open/closed stats for help topic search

This patch makes the open and closed ticket counts on the client portal
relative to the help topic search. It also hides the counts when doing text
searches so that an extra count query isn't necessary.
---
 include/class.client.php       | 15 ++++++++++++++-
 include/client/tickets.inc.php | 32 +++++++++++++++++++++++++++-----
 2 files changed, 41 insertions(+), 6 deletions(-)

diff --git a/include/class.client.php b/include/class.client.php
index 886c89342..e7d5adecf 100644
--- a/include/class.client.php
+++ b/include/class.client.php
@@ -253,7 +253,6 @@ class  EndUser extends BaseAuthenticatedUser {
 
     function getNumTopicTickets($topic_id, $forMyOrg=false) {
         $stats = $this->getTicketStats();
-        $count = 0;
         $section = $forMyOrg ? 'myorg' : 'mine';
         if (!isset($this->topic_stats)) {
             $this->topic_stats = array();
@@ -264,6 +263,20 @@ class  EndUser extends BaseAuthenticatedUser {
         return $this->topic_stats[$topic_id];
     }
 
+    function getNumTopicTicketsInState($topic_id, $state=false, $forMyOrg=false) {
+        $stats = $this->getTicketStats();
+        $count = 0;
+        $section = $forMyOrg ? 'myorg' : 'mine';
+        foreach ($stats[$section] as $row) {
+            if ($topic_id != $row['topic_id'])
+                continue;
+            if ($state && $state != $row['status__state'])
+                continue;
+            $count += $row['count'];
+        }
+        return $count;
+    }
+
     function getNumOrganizationTickets() {
         return $this->getNumTickets(true);
     }
diff --git a/include/client/tickets.inc.php b/include/client/tickets.inc.php
index 12908ece2..dfc7673e0 100644
--- a/include/client/tickets.inc.php
+++ b/include/client/tickets.inc.php
@@ -6,12 +6,28 @@ $settings = &$_SESSION['client:Q'];
 // Unpack search, filter, and sort requests
 if (isset($_REQUEST['clear']))
     $settings = array();
-if (isset($_REQUEST['keywords']))
+if (isset($_REQUEST['keywords'])) {
     $settings['keywords'] = $_REQUEST['keywords'];
-if (isset($_REQUEST['topic_id']))
+}
+if (isset($_REQUEST['topic_id'])) {
     $settings['topic_id'] = $_REQUEST['topic_id'];
-if (isset($_REQUEST['status']))
+}
+if (isset($_REQUEST['status'])) {
     $settings['status'] = $_REQUEST['status'];
+}
+
+if ($settings['keywords']) {
+    // Don't show stat counts for searches
+    $openTickets = $closedTickets = -1;
+}
+elseif ($settings['topic_id']) {
+    $openTickets = $thisclient->getNumTopicTicketsInState($settings['topic_id'], 'open');
+    $closedTickets = $thisclient->getNumTopicTicketsInState($settings['topic_id'], 'closed');
+}
+else {
+    $openTickets = $thisclient->getNumOpenTickets();
+    $closedTickets = $thisclient->getNumClosedTickets();
+}
 
 $tickets = TicketModel::objects();
 
@@ -136,19 +152,25 @@ $tickets->values(
 
 <div class="pull-right states">
     <small>
+<?php if ($openTickets) { ?>
     <i class="icon-file-alt"></i>
     <a class="state <?php if ($status == 'open') echo 'active'; ?>"
         href="?<?php echo Http::build_query(array('a' => 'search', 'status' => 'open')); ?>">
-    <?php echo sprintf('%s (%d)', _P('ticket-status', 'Open'), $thisclient->getNumOpenTickets()); ?>
+    <?php echo _P('ticket-status', 'Open'); if ($openTickets > 0) echo sprintf(' (%d)', $openTickets); ?>
     </a>
+    <?php if ($closedTickets) { ?>
     &nbsp;
     <span style="color:lightgray">|</span>
+    <?php }
+}
+if ($closedTickets) {?>
     &nbsp;
     <i class="icon-file-text"></i>
     <a class="state <?php if ($status == 'closed') echo 'active'; ?>"
         href="?<?php echo Http::build_query(array('a' => 'search', 'status' => 'closed')); ?>">
-    <?php echo sprintf('%s (%d)', __('Closed'), $thisclient->getNumClosedTickets()); ?>
+    <?php echo __('Closed'); if ($closedTickets > 0) echo sprintf(' (%d)', $closedTickets); ?>
     </a>
+<?php } ?>
     </small>
 </div>
 </h1>
-- 
GitLab