You are here

function _support_status_form_attach in Support Ticketing System 7

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

2 calls to _support_status_form_attach()
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 2523
support.module

Code

function _support_status_form_attach(&$form, &$form_state, $node) {
  global $user;

  // Copy some dynamic values into the node object because we use them later.
  // @@@ Fix the usage instead, messing with $node in the form builder seems like
  // a bad idea.
  if (isset($form_state['values']['assigned'])) {
    $node->assigned = $form_state['values']['assigned'];
  }
  if (!isset($node->client)) {
    $node->client = _support_current_client();
  }
  if (isset($form_state['values']['client'])) {
    $node->client = $form_state['values']['client'];
  }

  // Make sure that assigned is always sane.
  if (!isset($node->assigned) || !empty($form_state['triggering_element']['#support_client_change'])) {

    // Discard the user input to force the dropdown to recognize our new default.
    if (isset($form_state['input']['assigned'])) {
      unset($form_state['input']['assigned']);
    }
    $autoassign = _support_autoassign($node->client, $user->uid);
    if ($autoassign) {

      // This ticket is being created or the client just changed, and this
      // module is configured to auto-assign new tickets.
      $node->assigned = $autoassign;
    }
    else {

      // If we are starting out or the client has just changed to one that doesn't have an autoassign...
      if (isset($node->assigned) && !_support_validate_assigned_user($node->assigned, $node->client) || !isset($node->assigned)) {

        // Does user have any ability to change assignment?
        if (!user_access('can assign tickets to self') && !user_access('can assign tickets to any user') && !user_access('administer support')) {

          // If not, force unassign.
          // @@@ Is there a better choice here?
          $node->assigned = 0;
        }
        else {

          // No ability to change assignment. We need to do it for them....
          // Is the user allowed to be assigned the ticket?
          if (_support_validate_assigned_user($user->uid, $node->client)) {

            // Assign the ticket to the user.
            $node->assigned = $user->uid;
          }
          else {

            // User isn't allowed to be assigned and there was no default,
            // fall back to unassigned.
            $node->assigned = 0;
          }
        }
      }
    }
  }

  // If we are editing a comment, use the state and priority from the comment.
  if (!empty($form['cid']['#value'])) {
    $comment = db_select('support_ticket_comment', 'c')
      ->condition('c.cid', $form['cid']['#value'])
      ->fields('c')
      ->execute()
      ->fetchObject();
    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();
  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,
    );
  }
  $priority = isset($node->priority) ? $node->priority : _support_priority_default();
  if (!user_access('can select priority') && !user_access('administer support')) {
    $form['support']['priority'] = array(
      '#type' => 'hidden',
      '#value' => $priority,
    );
  }
  else {
    $form['support']['priority'] = array(
      '#type' => 'select',
      '#prefix' => '&nbsp;&nbsp;',
      '#title' => t('Priority'),
      '#options' => _support_priorities(),
      '#default_value' => $priority,
    );
  }
  $clients = _support_available_clients();
  if (!isset($node->client) || empty($node->client)) {
    if (sizeof($clients) == 1) {
      $node->client = key($clients);
    }
    else {
      if (isset($_SESSION['support_client']) && 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;
  $client = isset($node->client) && is_numeric($node->client) ? $node->client : 0;
  if ($available == 1 || !user_access('can select client') && !user_access('administer support')) {
    $form['support']['client'] = array(
      '#type' => 'hidden',
      '#value' => $client,
    );
  }
  else {
    $form['support']['client'] = array(
      '#type' => 'select',
      '#required' => TRUE,
      '#prefix' => '&nbsp;&nbsp;',
      '#title' => t('Client'),
      '#options' => $clients,
      '#default_value' => $client,
      '#support_client_change' => TRUE,
      '#ajax' => array(
        'callback' => '_support_ajax_update_assigned_callback',
        'wrapper' => 'replace_support_client_dependencies',
      ),
    );
  }

  // Create wrapper group for elements dependent on client change.
  $form['support']['client_dependencies'] = array(
    '#prefix' => '<div id="replace_support_client_dependencies">',
    '#suffix' => '</div>',
  );
  if (!user_access('can assign tickets to self') && !user_access('can assign tickets to any user') && !user_access('administer support')) {
    $assigned = isset($node->assigned) ? $node->assigned : 0;
    $form['support']['client_dependencies']['assigned'] = array(
      '#type' => 'hidden',
      '#value' => $assigned,
    );
  }
  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($node->assigned);
          $assigned = $account->name;
        }
        else {
          $assigned = $node->assigned;
        }
      }
      else {
        $assigned = '';
      }
      $form['support']['client_dependencies']['assigned'] = array(
        '#type' => 'textfield',
        '#prefix' => '&nbsp;&nbsp;',
        '#title' => t('Assigned'),
        '#autocomplete_path' => 'support/autocomplete/assigned/' . $node->client,
        '#default_value' => $assigned,
        '#size' => '15',
      );
    }
    else {
      $assigned = isset($node->assigned) ? $node->assigned : 0;
      $form['support']['client_dependencies']['assigned'] = array(
        '#type' => 'select',
        '#prefix' => '&nbsp;&nbsp;',
        '#title' => t('Assigned'),
        '#options' => $options,
        '#default_value' => $assigned,
      );
    }
  }
}