You are here

function support_page_form in Support Ticketing System 6

Same name and namespace in other branches
  1. 7 support.module \support_page_form()

Display tickets

3 string references to 'support_page_form'
support_menu in ./support.module
Implementation of hook_menu().
support_pm_menu in support_pm/support_pm.module
Implementation of hook_menu(). TODO: Include date in 'view' and 'edit' tabs
support_substatus_form_alter in support_substatus/support_substatus.module

File

./support.module, line 2903
support.module

Code

function support_page_form(&$form_state, $client = NULL, $state = NULL) {
  global $user;
  $form = array();

  // Be sure a client is selected.  If not, select the last visited client.
  if (!$client) {
    if (isset($_SESSION['support_client']) && ($client = support_client_load($_SESSION['support_client']))) {
      unset($_SESSION['support_client']);
      if ($client->parent) {
        $parent = support_client_load($client->parent);
        drupal_goto("support/{$parent->path}/{$client->path}");
      }
      else {
        drupal_goto("support/{$client->path}");
      }
    }
    $clients = _support_available_clients();
    if (count($clients)) {
      foreach ($clients as $key => $name) {
        $client = support_client_load($key);
        if ($client->parent) {
          $parent = support_client_load($client->parent);
          drupal_goto("support/{$parent->path}/{$client->path}");
        }
        else {
          drupal_goto("support/{$client->path}");
        }
      }
    }
  }
  else {
    if (is_numeric($client)) {
      $_SESSION['support_client'] = $client;
    }
    else {
      drupal_set_message(t('Client does not exist or is not enabled.'), 'error');
      if (isset($_SESSION['support_client'])) {
        unset($_SESSION['support_client']);
      }
      drupal_goto('');
    }
  }
  if ($client == SUPPORT_ALL_CLIENTS) {
    $client = array_keys(_support_access_tickets(FALSE));
    $all_clients = TRUE;
  }
  else {
    $client = array(
      $client,
    );
    $all_clients = FALSE;
  }
  if (!$state) {
    $state = 'all open';
  }
  $state = _support_get_state($state);

  // Optionally filter ticket listing.
  $available = array(
    'tid',
    'uid',
    'sid',
    'pid',
    'clid',
  );
  $filters = array();
  foreach ($available as $filter) {
    $to_filter = isset($_GET[$filter]) ? $_GET[$filter] : '';
    $unsanitized = explode(',', $to_filter);
    foreach ($unsanitized as $element) {
      $element = (int) $element;
      if ($element) {
        $filters[$filter][$element] = $element;
      }
    }
  }
  $join = $where = $or = array();
  if (!empty($filters)) {
    if (isset($filters['tid']) && !empty($filters['tid'])) {
      $join[] = 'LEFT JOIN {term_node} tn ON n.nid = tn.nid';
      $where[] = strtr('tn.tid IN (!tid)', array(
        '!tid' => implode(',', $filters['tid']),
      ));
    }
    if (isset($filters['uid']) && !empty($filters['uid'])) {
      if (isset($filters['uid'][-1])) {
        $filters['uid'][-1] = 0;
      }
      $where[] = strtr('t.assigned IN (!uid)', array(
        '!uid' => implode(',', $filters['uid']),
      ));
      if (isset($filters['uid'][-1])) {
        $filters['uid'][-1] = -1;
      }
    }
    if (isset($filters['sid']) && !empty($filters['sid'])) {
      $where[] = strtr('t.state IN (!sid)', array(
        '!sid' => implode(',', $filters['sid']),
      ));
    }
    if (isset($filters['pid']) && !empty($filters['pid'])) {
      $where[] = strtr('t.priority IN (!pid)', array(
        '!pid' => implode(',', $filters['pid']),
      ));
    }
    if (isset($filters['clid']) && !empty($filters['clid'])) {
      $where[] = strtr('t.client IN (!clid)', array(
        '!clid' => implode(',', $filters['clid']),
      ));
    }
  }
  $filters['join'] = $join;
  $filters['where'] = $where;
  $filters['or'] = $or;
  drupal_alter('support_ticket_listing_filter', $filters);
  if (!empty($filters['where'])) {
    array_unshift($filters['where'], ' ');
  }

  // Allow filtering of ticket listing.
  $form += support_filter_form($client, $filters, $state);
  $form['post-ticket'] = array(
    '#type' => 'markup',
    '#value' => l(t('Post new support ticket'), 'node/add/support-ticket'),
  );
  $checkboxes = user_access('edit multiple tickets') || user_access('administer support') ? theme('table_select_header_cell') : array();
  $sort = variable_get('support_default_sort_tickets', SUPPORT_SORT_UPDATE);
  if (variable_get('support_default_sort_order', SUPPORT_SORT_DESC) == SUPPORT_SORT_DESC) {
    $order = 'desc';
  }
  else {
    $order = 'asc';
  }
  foreach (array(
    SUPPORT_SORT_UPDATE => array(
      'data' => t('Updated'),
      'field' => 'last_updated',
    ),
    SUPPORT_SORT_NID => array(
      'data' => t('Id'),
      'field' => 'n.nid',
    ),
    SUPPORT_SORT_STATE => array(
      'data' => t('State'),
      'field' => 't.state',
    ),
    SUPPORT_SORT_PRIORITY => array(
      'data' => t('Priority'),
      'field' => 'spri.weight',
    ),
  ) as $key => $array) {
    if ($sort == $key) {
      $headers[$key] = $array + array(
        'sort' => $order,
      );
    }
    else {
      $headers[$key] = $array;
    }
  }
  $form['header'] = array(
    '#type' => 'value',
    '#value' => 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',
      ),
      $headers[SUPPORT_SORT_STATE],
      $headers[SUPPORT_SORT_PRIORITY],
      array(
        'data' => t('Updates'),
        'field' => 'l.comment_count',
      ),
    ),
  );
  if ($all_clients) {
    $form['header']['#value'][] = array(
      'data' => t('Client'),
      'field' => 'sc.name',
    );
    $form['all_clients']['#value'] = TRUE;
  }
  if ($state == -2) {
    $my_open = "AND t.assigned = {$user->uid}";
  }
  else {
    $my_open = '';
  }
  if ($state < 0) {
    $state = 'AND t.state NOT IN (' . implode(', ', _support_get_state(SUPPORT_STATE_CLOSED)) . ')';
  }
  else {
    $state = $state ? "AND t.state = {$state}" : '';
  }
  $sql = "SELECT DISTINCT(n.nid), n.title, n.type, n.changed, n.uid, u.name, GREATEST(n.changed, l.last_comment_timestamp) AS last_updated, l.comment_count, t.state, t.priority, t.assigned, t.client, sc.name FROM {node} n LEFT JOIN {support_ticket} t ON n.nid = t.nid LEFT JOIN {support_client} sc ON t.client = sc.clid LEFT JOIN {support_priority} spri ON t.priority = spri.pid INNER JOIN {node_comment_statistics} l ON n.nid = l.nid INNER JOIN {users} u ON n.uid = u.uid " . implode(' ', $filters['join']) . " WHERE (n.status = 1 AND n.type = 'support_ticket' AND client IN(" . implode(',', $client) . ") {$state} {$my_open} " . implode(' AND ', $filters['where']);
  if (!user_access('view other users tickets') && !user_access('administer support') && !user_access('edit any ticket') && !user_access('delete any ticket')) {
    $sql .= " AND (n.uid = {$user->uid} OR t.assigned = {$user->uid})";
  }
  $sql .= ')';
  if (!empty($filters['or'])) {
    $sql .= ' OR (' . implode(' OR ', $filters['or']) . ')';
  }
  $sql = db_rewrite_sql($sql);
  $sql .= tablesort_sql($form['header']['#value']);
  if (variable_get('support_secondary_sort_order', SUPPORT_SORT_DESC) == SUPPORT_SORT_DESC) {
    $order = 'desc';
  }
  else {
    $order = 'asc';
  }
  switch (variable_get('support_secondary_sort_tickets', SUPPORT_SORT_NONE)) {
    case SUPPORT_SORT_UPDATE:
      $sql .= ", last_updated {$order}";
      break;
    case SUPPORT_SORT_NID:
      $sql .= ", n.nid {$order}";
      break;
    case SUPPORT_SORT_STATE:
      $sql .= ", t.state {$order}";
      break;
    case SUPPORT_SORT_PRIORITY:
      $sql .= ", t.priority {$order}";
      break;
  }
  $sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n LEFT JOIN {comments} c ON n.nid = c.nid AND (c.status = 0 OR c.status IS NULL) LEFT JOIN {support_ticket} t ON n.nid = t.nid ' . implode(' ', $filters['join']) . ' WHERE n.status = 1 AND client IN(' . implode(',', $client) . ") {$state} {$my_open} " . implode(' AND ', $filters['where']);
  $sql_count = db_rewrite_sql($sql_count);
  $result = pager_query($sql, 50, 0, $sql_count);
  $rows = array();
  $tickets = array();
  while ($ticket = db_fetch_object($result)) {
    drupal_alter('support_page_list_ticket', $ticket);
    $account = user_load(array(
      'uid' => $ticket->uid,
    ));
    $assigned = user_load(array(
      'uid' => $ticket->assigned,
    ));
    $comments = l($ticket->comment_count, "node/{$ticket->nid}", array(
      'fragment' => 'comments',
    ));
    if ($new = comment_num_new($ticket->nid)) {
      $node = node_load($ticket->nid);
      $comments .= '&nbsp;(' . l(format_plural($new, '1 new', '@count new'), "node/{$ticket->nid}", array(
        'query' => comment_new_page_count($node->comment_count, $new, $node),
        'fragment' => 'new',
      )) . ')';
    }
    $tickets[$ticket->nid] = '';
    $form['id'][$ticket->nid] = array(
      '#value' => l($ticket->nid, "node/{$ticket->nid}", array(
        'attributes' => array(
          'class' => 'ticket-id',
        ),
      )),
    );
    $form['title'][$ticket->nid] = array(
      '#value' => l(_support_truncate($ticket->title), "node/{$ticket->nid}", array(
        'attributes' => array(
          'class' => 'ticket-title',
        ),
      )),
    );
    $form['updated'][$ticket->nid] = array(
      '#value' => format_date($ticket->last_updated, 'small', array(
        'attributes' => array(
          'class' => 'ticket-updated',
        ),
      )),
    );
    $form['reported'][$ticket->nid] = array(
      '#value' => l(_support_truncate($account->name, 24), "user/{$account->uid}", array(
        'attributes' => array(
          'class' => 'ticket-reported',
        ),
      )),
    );

    // 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);
      $options = _support_assigned(isset($assigned->uid) ? $assigned->uid : 0, $node, variable_get('support_autocomplete_limit', 15));
      if ($options === FALSE) {
        if (isset($ticket->assigned)) {
          if (is_numeric($ticket->assigned)) {
            $account = user_load(array(
              'uid' => $ticket->assigned,
            ));
            $assigned = $account->name;
          }
          else {
            $assigned = $ticket->assigned;
          }
        }
        else {
          $assigned = '';
        }
        $form['assigned']["assigned-{$ticket->nid}"] = array(
          '#type' => 'textfield',
          '#autocomplete_path' => 'support/autocomplete/assigned/' . $ticket->client,
          '#default_value' => $assigned,
          '#size' => '15',
          '#attributes' => array(
            'class' => 'ticket-assigned',
          ),
        );
      }
      else {
        $form['assigned']["assigned-{$ticket->nid}"] = array(
          '#type' => 'select',
          '#options' => $options,
          '#default_value' => isset($ticket->assigned) ? $ticket->assigned : 0,
          '#attributes' => array(
            'class' => 'ticket-assigned',
          ),
        );
      }
    }
    else {
      $form['assigned']["assigned-{$ticket->nid}"] = array(
        '#value' => l(_support_truncate($assigned->name, 24), "user/{$assigned->uid}", array(
          'attributes' => array(
            'class' => 'ticket-assigned',
          ),
        )),
      );
    }

    // State
    $states = _support_states(FALSE);
    if ((user_access('edit multiple tickets') || user_access('administer support')) && sizeof($states) > 1) {
      $form['state']["state-{$ticket->nid}"] = array(
        '#type' => 'select',
        '#options' => $states,
        '#default_value' => $ticket->state,
        '#attributes' => array(
          'class' => 'ticket-state',
        ),
      );
    }
    else {
      $form['state']["state-{$ticket->nid}"] = array(
        '#value' => _support_state($ticket->state),
        '#attributes' => array(
          'class' => 'ticket-state',
        ),
      );
    }

    // Priority
    if (user_access('administer support') || user_access('edit multiple tickets') && user_access('can select priority')) {
      $form['priority']["priority-{$ticket->nid}"] = array(
        '#type' => 'select',
        '#options' => _support_priorities(),
        '#default_value' => $ticket->priority,
        '#attributes' => array(
          'class' => 'ticket-priority',
        ),
      );
    }
    else {
      $form['priority']["priority-{$ticket->nid}"] = array(
        '#value' => _support_priorities($ticket->priority),
        '#attributes' => array(
          'class' => 'ticket-priority',
        ),
      );
    }
    $form['updates'][$ticket->nid] = array(
      '#value' => $comments,
      '#attributes' => array(
        'class' => "state-{$ticket->state} priority-{$ticket->priority}",
      ),
    );
    $form['class'][$ticket->nid] = array(
      '#value' => "state-{$ticket->state} priority-{$ticket->priority}",
    );
    if ($all_clients) {
      $form['client'][$ticket->nid] = array(
        '#value' => _support_client($ticket->client),
        '#attributes' => array(
          'class' => "state-{$ticket->state} priority-{$ticket->priority}",
        ),
      );
    }
  }
  $form['tickets'] = array(
    '#type' => 'checkboxes',
    '#options' => count($tickets) && (user_access('edit multiple tickets') || user_access('administer support')) ? $tickets : array(),
  );
  if (count($tickets) && (user_access('edit multiple tickets') || user_access('administer support'))) {
    $form['update'] = array(
      '#title' => t('Update'),
      '#type' => 'textarea',
      '#description' => t('This text will be added to all selected tickets.'),
    );
    if (user_access('can suppress notification') && variable_get('support_notifications', TRUE)) {
      $form['suppress'] = array(
        '#type' => 'checkbox',
        '#title' => t('Suppress notification'),
        '#description' => t('By checking this box you will prevent notification emails from being sent for this update.'),
        '#default_value' => isset($edit->suppress) ? $edit->suppress : 0,
      );
    }
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Update ticket'),
    );
  }
  $form['pager'] = array(
    '#value' => theme('pager', NULL, 50, 0),
  );
  return $form;
}