You are here

function support_page_form in Support Ticketing System 7

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

Display tickets

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

File

./support.module, line 3170
support.module

Code

function support_page_form($form, &$form_state, $client = NULL, $state = NULL) {
  global $user;
  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));
        }
      }
    }
  }
  else {
    if (isset($client->clid)) {
      $_SESSION['support_client'] = $client->clid;
    }
    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 (!$state) {
    $state = 'all open';
  }
  $state = _support_get_state($state);
  $form['post-ticket'] = array(
    '#markup' => l(t('Post new support ticket'), 'node/add/support-ticket'),
  );

  // TODO: Use a tableselect element instead. (Requires rethinking "edit multiple tickets".)
  $checkboxes = array();
  if (user_access('edit multiple tickets') || user_access('administer support')) {
    $checkboxes = array(
      'data' => '',
      'class' => array(
        'select-all',
      ),
    );
    $form['#attached']['js']['misc/tableselect.js'] = 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' => 't.priority',
    ),
  ) 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' => 's.comment_count',
      ),
    ),
  );
  $query = db_select('node', 'n')
    ->extend('PagerDefault')
    ->extend('TableSort')
    ->orderByHeader($form['header']['#value']);
  $query
    ->leftjoin('support_ticket', 't', 't.nid = n.nid');
  $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) {
    $query
      ->condition('t.assigned', $user->uid);
  }
  if ($state < 0) {
    $states = _support_get_state(SUPPORT_STATE_CLOSED);
    $query
      ->condition('t.state', $states, 'NOT IN');
  }
  else {
    if ($state) {
      $query
        ->condition('t.state', $state);
    }
  }
  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:
      $query
        ->orderBy('last_updated', $order);
      break;
    case SUPPORT_SORT_NID:
      $query
        ->orderBy('n.nid', $order);
      break;
    case SUPPORT_SORT_STATE:
      $query
        ->orderBy('t.state', $order);
      break;
    case SUPPORT_SORT_PRIORITY:
      $query
        ->orderBy('t.priority', $order);
      break;
  }
  $query
    ->fields('n', array(
    'nid',
    'title',
    'type',
    'changed',
    'uid',
  ))
    ->fields('u', array(
    'name',
  ))
    ->fields('s', array(
    'comment_count',
  ))
    ->fields('t', array(
    'client',
    ' state',
    'priority',
    'assigned',
  ))
    ->addExpression('GREATEST(n.changed, s.last_comment_timestamp)', 'last_updated');
  $query
    ->limit(50);
  $result = $query
    ->execute();
  $rows = array();
  $tickets = array();
  foreach ($result as $ticket) {
    drupal_alter('support_page_list_ticket', $ticket);
    $account = user_load($ticket->uid);
    $assigned = user_load($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(
      '#markup' => l($ticket->nid, "node/{$ticket->nid}", array(
        'attributes' => array(
          'class' => array(
            'ticket-id',
          ),
        ),
      )),
    );
    $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',
          ),
        ),
      )),
    );

    // 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($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' => array(
              'ticket-assigned',
            ),
          ),
        );
      }
      else {
        $form['assigned']["assigned-{$ticket->nid}"] = array(
          '#type' => 'select',
          '#options' => $options,
          '#default_value' => isset($ticket->assigned) ? $ticket->assigned : 0,
          '#attributes' => array(
            'class' => array(
              'ticket-assigned',
            ),
          ),
        );
      }
    }
    else {
      $form['assigned']["assigned-{$ticket->nid}"] = array(
        '#markup' => l(_support_truncate($assigned->name, 24), "user/{$assigned->uid}", array(
          'attributes' => array(
            'class' => array(
              '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' => array(
            'ticket-state',
          ),
        ),
      );
    }
    else {
      $form['state']["state-{$ticket->nid}"] = array(
        '#markup' => _support_state($ticket->state),
        '#attributes' => array(
          'class' => array(
            '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' => array(
            'ticket-priority',
          ),
        ),
      );
    }
    else {
      $form['priority']["priority-{$ticket->nid}"] = array(
        '#markup' => _support_priorities($ticket->priority),
        '#attributes' => array(
          'class' => array(
            'ticket-priority',
          ),
        ),
      );
    }
    $form['updates'][$ticket->nid] = array(
      '#markup' => $comments,
      '#attributes' => array(
        'class' => array(
          "state-{$ticket->state}",
          "priority-{$ticket->priority}",
        ),
      ),
    );
    $form['class'][$ticket->nid] = array(
      '#value' => array(
        "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',
      '#required' => variable_get('support_require_comment', TRUE),
      '#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' => 0,
      );
    }
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Update ticket'),
    );
  }
  $form['pager'] = array(
    '#markup' => theme('pager', array(
      'tags' => NULL,
      'element' => 0,
    )),
  );
  return $form;
}