From 0a63231d7666c62697f1741a6f17f81c7e11c111 Mon Sep 17 00:00:00 2001
From: Javier Castro <javier.alejandro.castro@gmail.com>
Date: Mon, 17 Sep 2012 22:19:51 -0300
Subject: [PATCH 340/340] Fix for 507974

---
 support.module | 99 ++++++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 72 insertions(+), 27 deletions(-)

diff --git a/support.module b/support.module
index 8b35c55..5bb3441 100644
--- a/support.module
+++ b/support.module
@@ -344,7 +344,6 @@ function support_menu() {
     'access arguments' => array(1),
     'file' => 'support.user.inc',
   );
-  unset($states['my open']);
   foreach ($states as $sid => $state) {
     $items["support/%user_uid_optional/assigned/$state"] = array(
       'title' => "$state",
@@ -369,6 +368,25 @@ function support_menu() {
     'access callback' => TRUE,
     'type' => MENU_CALLBACK,
   );
+  /* list all clients tickets */
+  $items["support/all"] = array(
+    'title' => t("All clients"),
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('support_page_form', 'all'),
+    'access arguments' => array('administer support'),
+    'weight' => -1,
+  );
+  unset($states[-3]);
+  foreach ($states as $sid => $state) {
+    $items["support/all/$state"] = array(
+      'title' => "$state",
+      'page callback' => 'drupal_get_form',
+      'page arguments' => array('support_page_form', 'all', $state),
+      'access arguments' => array('administer support'),
+      'weight' => $sid,
+      'type' => $sid == -2 ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
+     );
+  }
   $items['admin/support/clients/%support_client/fetch'] = array(
     'title' => 'Fetch mail',
     'type' => MENU_CALLBACK,
@@ -3147,7 +3165,8 @@ function _support_get_state($state) {
     }
     return $states;
   }
-  $sid = db_query("SELECT sid FROM {support_states} WHERE state = :state", array(':state' => $state))->fetchField();
+  $states = _support_states();
+  $sid = array_search($state, $states);
   if (!$sid) {
     $sid = _support_state_default();
   }
@@ -3167,27 +3186,20 @@ function support_page_form($form, &$form_state, $client = NULL, $state = NULL) {
   if (isset($client) && is_numeric($client)) {
     $client = support_client_load($client);
   }
-
+  
   // Be sure a client is selected. If not, select the last visited client.
   if (empty($client)) {
     if (isset($_SESSION['support_client']) && $client = support_client_load($_SESSION['support_client'])) {
       unset($_SESSION['support_client']);
       drupal_goto(support_queue_url($client));
     }
-    $clients = _support_available_clients();
-    if (count($clients)) {
-      foreach ($clients as $key => $name) {
-        if ($client = support_client_load($key)) {
-          drupal_goto(support_queue_url($client));
-        }
-      }
-    }
+    $client = 'all';
   }
   else {
     if (isset($client->clid)) {
       $_SESSION['support_client'] = $client->clid;
     }
-    else {
+    elseif ($client !== 'all') {
       drupal_set_message(t('Client does not exist or is not enabled.'), 'error');
       if (isset($_SESSION['support_client'])) {
         unset($_SESSION['support_client']);
@@ -3199,7 +3211,11 @@ function support_page_form($form, &$form_state, $client = NULL, $state = NULL) {
   if (!$state) {
     $state = 'all open';
   }
-  $state = _support_get_state($state);
+  $state_value = _support_get_state($state);
+
+  if ($client === 'all') {
+    drupal_set_title(t("Support tickets for all clients"), PASS_THROUGH);
+  }
 
   $form['post-ticket'] = array(
     '#markup' => l(t('Post new support ticket'), 'node/add/support-ticket'),
@@ -3233,49 +3249,68 @@ function support_page_form($form, &$form_state, $client = NULL, $state = NULL) {
       $headers[$key] = $array;
     }
   }
-  $form['header'] = array(
-    '#type' => 'value',
-    '#value' => array(
+  
+  $columns = array(
       $checkboxes,
       $headers[SUPPORT_SORT_NID],
       array('data' => t('Ticket'), 'field' => 'n.title'),
       $headers[SUPPORT_SORT_UPDATE],
       array('data' => t('Reported by'), 'field' => 'n.uid'),
       array('data' => t('Assigned to'), 'field' => 't.assigned'),
+    );
+
+  if ($client === 'all') {
+    $columns = array_merge($columns, array(
+      array('data' => t('Client'), 'field' => 'c.name'),
+      ));
+  }
+  
+  $columns = array_merge($columns, array(
       $headers[SUPPORT_SORT_STATE],
       $headers[SUPPORT_SORT_PRIORITY],
       array('data' => t('Updates'), 'field' => 's.comment_count'),
-    ),
+    ));
+    
+  $form['header'] = array(
+    '#type' => 'value',
+    '#value' => $columns,
   );
-
+  
   $query = db_select('node', 'n')
     ->extend('PagerDefault')
     ->extend('TableSort')
     ->orderByHeader($form['header']['#value']);
   $query->leftjoin('support_ticket', 't', 't.nid = n.nid');
+  
+  if ($client === 'all') {
+    $query->leftjoin('support_client', 'c', 'c.clid = t.client');
+  }
+  else {
+    $query->condition('t.client', $client->clid)
+          ->addMetaData('support_client', $client);
+  }
+  
   $query->join('node_comment_statistics', 's', 's.nid = n.nid');
   $query->join('users', 'u', 'u.uid = n.uid');
   $query->condition('n.status', NODE_PUBLISHED)
         ->condition('n.type', 'support_ticket')
-        ->condition('t.client', $client->clid)
-        ->addMetaData('support_client', $client)
-        ->addTag('support_pager');
-
+        ->addTag('support_pager');
+    
   if (!user_access('view other users tickets') && !user_access('administer support') && !user_access('edit any support_ticket content') && !user_access('delete any support_ticket content')) {
     $query->condition(db_or()
       ->condition('n.uid', $user->uid)
       ->condition('t.assigned', $user->uid));
   }
 
-  if ($state == -2)
+  if ($state_value == -2)
     $query->condition('t.assigned', $user->uid);
 
-  if ($state < 0) {
+  if ($state_value < 0) {
     $states = _support_get_state(SUPPORT_STATE_CLOSED);
     $query->condition('t.state', $states, 'NOT IN');
   }
-  else if ($state) {
-    $query->condition('t.state', $state);
+  else if ($state_value) {
+    $query->condition('t.state', $state_value);
   }
 
   if (variable_get('support_secondary_sort_order', SUPPORT_SORT_DESC) == SUPPORT_SORT_DESC) {
@@ -3304,6 +3339,11 @@ function support_page_form($form, &$form_state, $client = NULL, $state = NULL) {
       ->fields('s', array('comment_count'))
       ->fields('t', array('client', ' state', 'priority', 'assigned'))
       ->addExpression('GREATEST(n.changed, s.last_comment_timestamp)', 'last_updated');
+  
+  if ($client === 'all') {
+    $query->addField('c', 'name', 'client_name');
+    $query->addField('c','path','client_path');
+  }
 
   $query->limit(50);
 
@@ -3325,6 +3365,9 @@ function support_page_form($form, &$form_state, $client = NULL, $state = NULL) {
     $form['title'][$ticket->nid] = array('#markup' => l(_support_truncate($ticket->title), "node/$ticket->nid", array('attributes' => array('class' => array('ticket-title')))));
     $form['updated'][$ticket->nid] = array('#markup' => format_date($ticket->last_updated, 'short', array('attributes' => array('class' => array('ticket-updated')))));
     $form['reported'][$ticket->nid] = array('#markup' => l(_support_truncate($account->name, 24), "user/$account->uid", array('attributes' => array('class' => array('ticket-reported')))));
+    if ($client === 'all')
+      $form['client'][$ticket->nid] = array('#markup' => l(_support_truncate($ticket->client_name, 24), "support/$ticket->client_path", array('attributes' => array('class' => array('ticket-client')))));
+      
     // Assigned to
     if ((user_access('edit multiple tickets') && user_access('can assign tickets to any user')) || user_access('administer support')) {
       $node = node_load($ticket->nid);
@@ -3482,6 +3525,8 @@ function theme_support_page_form($variables) {
       $row[] = drupal_render($form['updated'][$key]);
       $row[] = drupal_render($form['reported'][$key]);
       $row[] = drupal_render($form['assigned']["assigned-$key"]);
+      if (array_key_exists('client', $form))
+        $row[] = drupal_render($form['client'][$key]);
       $row[] = drupal_render($form['state']["state-$key"]);
       $row[] = drupal_render($form['priority']["priority-$key"]);
       $row[] = drupal_render($form['updates'][$key]);
@@ -3495,7 +3540,7 @@ function theme_support_page_form($variables) {
   else {
     $rows[] = array(array(
         'data' => t('No tickets available.'),
-        'colspan' => '9',
+        'colspan' => count($form['header']['#value']),
         ));
   }
   if ($form['pager']['#markup']) {
-- 
1.7.11.4

