--- support.module	Sat Sep 22 23:49:34 2012
+++ support.module	Mon Sep 24 12:13:45 2012
@@ -344,7 +344,6 @@
     '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 @@
     '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 callback' => 'support_access_all_clients_tickets_page',
+    '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 callback' => 'support_access_all_clients_tickets_page',
+      '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,
@@ -617,6 +635,22 @@
   drupal_goto('');
 }
 
+
+/**
+ * Custom permissions function.
+ */
+function support_access_all_clients_tickets_page() {
+  global $user;
+  $result = db_query('SELECT clid, name FROM {support_client} WHERE status = :status', array(':status' => 1));
+  foreach ($result as $client_info) {
+    $client = support_client_load($client_info->clid);
+	if (support_access_clients($client, $user) && $client) {
+      return TRUE;
+	}
+  }
+  return FALSE;
+}
+
 /**
  * Custom permissions function.
  */
@@ -3153,7 +3187,8 @@
     }
     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();
   }
@@ -3173,27 +3208,20 @@
   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']);
@@ -3205,7 +3233,11 @@
   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'),
@@ -3239,49 +3271,82 @@
       $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');
+	$_result = db_query('SELECT clid, name FROM {support_client} WHERE status = :status', array(':status' => 1));
+	$_client_ids = array();
+	$_clients = array();
+    foreach ($_result as $_client) {
+	  $ticket_client = support_client_load($_client->clid);
+	  if (support_access_clients($ticket_client, $user) && $ticket_client) {
+	    $_client_ids[] = $ticket_client->clid;
+	    $_clients[] = $ticket_client;
+	  }
+	}
+    $query->condition('t.client', $_client_ids, 'IN', $_client_ids)
+          ->addMetaData('support_clients', $_clients);
+    unset($_clients);
+    unset($_client_ids);
+  }
+  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');
-
+    
   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) {
@@ -3310,6 +3375,11 @@
       ->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);
 
@@ -3331,6 +3401,9 @@
     $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);
@@ -3488,6 +3561,8 @@
       $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]);
@@ -3501,7 +3576,7 @@
   else {
     $rows[] = array(array(
         'data' => t('No tickets available.'),
-        'colspan' => '9',
+        'colspan' => count($form['header']['#value']),
         ));
   }
   if ($form['pager']['#markup']) {
