You are here

function support_status_form in Support Ticketing System 6

Generate form for adding update to ticket. Enhances comment_form adding a ticket status bar.

2 calls to support_status_form()
support_form in ./support.module
Implementation of hook_form().
support_form_alter in ./support.module
Customize comment form for ticket followups.

File

./support.module, line 2161
support.module

Code

function support_status_form(&$form_state, $edit, $title) {
  global $user;
  $cid = 0;
  if (is_array($edit)) {
    if (is_array($edit['nid'])) {
      $node = node_load($edit['nid']['#value']);
      $cid = $edit['cid']['#value'];
    }
    else {
      $node = node_load($edit['nid']);
    }
  }
  else {
    $node = $edit;
  }
  $autoassign = _support_autoassign(_support_current_client(), $user->uid);
  if ($autoassign && !isset($node->assigned) && (!isset($node->nid) || !$node->nid)) {

    // This ticket is being created, and this module is configured to
    // auto-assign new tickets.
    $node->assigned = $autoassign;
  }
  if ($cid) {
    $comment = db_fetch_object(db_query('SELECT state, priority FROM {support_ticket_comment} WHERE cid = %d', $cid));
    if ($comment->state && $comment->priority) {
      $node->state = $comment->state;
      $node->priority = $comment->priority;
    }
  }
  if (user_access('can select state') || user_access('can select priority') || user_access('can select client') || user_access('can assign tickets to self') || user_access('can assign tickets to any user') || user_access('administer support') || user_access('can administer state')) {
    $form['support'] = array(
      '#type' => 'fieldset',
      '#prefix' => '<div class="container-inline">',
      '#suffix' => '</div>',
      '#title' => t('Ticket properties'),
    );
  }
  $default = isset($node->state) ? $node->state : _support_state_default();

  /**
   * Disable auto-changing ticket from "new" to "active" state.
    if ($node->uid != $user->uid && $default == _support_state_default()) {
      // We did not create this ticket, but we're updating it.  Suggest that it
      // no longer be marked as new.
      $default = _support_state_secondary();
    }
  **/
  if (!user_access('can select state') && !user_access('administer support') && !user_access('can administer state')) {
    $form['support']['state'] = array(
      '#type' => 'hidden',
      '#value' => $default,
    );
  }
  else {
    if (isset($node->nid) && $node->nid && isset($node->state)) {
      $state = $node->state;
    }
    else {
      $state = 0;
    }
    $form['support']['state'] = array(
      '#type' => 'select',
      '#title' => t('State'),
      '#options' => _support_states(FALSE, $state),
      '#default_value' => $default,
    );
  }
  if (!user_access('can select priority') && !user_access('administer support')) {
    $form['support']['priority'] = array(
      '#type' => 'hidden',
      '#value' => $node->priority ? $node->priority : _support_priority_default(),
    );
  }
  else {
    $form['support']['priority'] = array(
      '#type' => 'select',
      '#prefix' => '&nbsp;&nbsp;',
      '#title' => t('Priority'),
      '#options' => _support_priorities(),
      '#default_value' => isset($node->priority) ? $node->priority : _support_priority_default(),
    );
  }
  $clients = _support_available_clients();
  if (!isset($node->client) || empty($node->client)) {
    if (sizeof($clients) == 1) {
      $node->client = key($clients);
    }
    else {
      if (is_numeric($_SESSION['support_client'])) {
        $node->client = $_SESSION['support_client'];
      }
      else {
        if (!user_access('can select client')) {

          // TODO: It should be possible to set a default client.  Perhaps allow
          // a weight to be assigned to clients -- then we select the heaviest
          // matching client...?
          $node->client = key($clients);
        }
      }
    }
  }
  $available = count($clients);
  if (!$available) {
    drupal_set_message(t('A site administrator must !create a client before you can create support tickets.', array(
      '!create' => l(t('create and enable'), 'admin/support/clients/add'),
    )), 'error');
  }
  $clients = array(
    '- select client -',
  ) + $clients;
  if ($available == 1 || !user_access('can select client') && !user_access('administer support')) {
    $form['support']['client'] = array(
      '#type' => 'hidden',
      '#value' => is_numeric($node->client) ? $node->client : 0,
    );
  }
  else {
    $form['support']['client'] = array(
      '#type' => 'select',
      '#required' => TRUE,
      '#prefix' => '&nbsp;&nbsp;',
      '#title' => t('Client'),
      '#options' => $clients,
      '#default_value' => is_numeric($node->client) ? $node->client : 0,
    );
  }
  if (!user_access('can assign tickets to self') && !user_access('can assign tickets to any user') && !user_access('administer support')) {
    $form['support']['assigned'] = array(
      '#type' => 'hidden',
      '#value' => $node->assigned ? $node->assigned : 0,
    );
  }
  else {
    $options = _support_assigned(isset($node->assigned) ? $node->assigned : 0, $node, variable_get('support_autocomplete_limit', 15));
    if ($options === FALSE) {
      if (isset($node->assigned)) {
        if (is_numeric($node->assigned)) {
          $account = user_load(array(
            'uid' => $node->assigned,
          ));
          $assigned = $account->name;
        }
        else {
          $assigned = $node->assigned;
        }
      }
      else {
        $assigned = '';
      }
      $form['support']['assigned'] = array(
        '#type' => 'textfield',
        '#prefix' => '&nbsp;&nbsp;',
        '#title' => t('Assigned'),
        '#autocomplete_path' => 'support/autocomplete/assigned/' . $node->client,
        '#default_value' => $assigned,
        '#size' => '15',
      );
    }
    else {
      $form['support']['assigned'] = array(
        '#type' => 'select',
        '#prefix' => '&nbsp;&nbsp;',
        '#title' => t('Assigned'),
        '#options' => $options,
        '#default_value' => isset($node->assigned) ? $node->assigned : 0,
      );
    }
  }
  return $form;
}