You are here

function casetracker_case_form_common in Case Tracker 7

Same name and namespace in other branches
  1. 5 casetracker.module \casetracker_case_form_common()
  2. 6 casetracker.module \casetracker_case_form_common()

Common form elements for cases, generic enough for use either in a full node display, or in comment displays and updating. Default values are calculated based on an existing $form['nid']['#value'].

Parameters

$form: A Forms API $form, as received from a hook_form_alter().

$default_project: The project ID that should be pre-selected.

Return value

$form A modified Forms API $form.

2 calls to casetracker_case_form_common()
casetracker_form_alter in ./casetracker.module
Implements hook_form_alter().
casetracker_form_comment_form_alter in ./casetracker.module
Implements hook_form_comment_form_alter().

File

./casetracker.module, line 562
Enables the handling of projects and their cases.

Code

function casetracker_case_form_common(&$form, $default_project = NULL) {
  global $user;
  $node = $form['#node'];

  // On preview the case will be an array, we want an object.
  if (isset($node->build_mode) && $node->build_mode == NODE_BUILD_PREVIEW) {
    $node->casetracker = (object) $node->casetracker;
  }

  // project to set as the default is based on how the user got here.
  if (empty($default_project) && !empty($node->casetracker->pid)) {
    $default_project = $node->casetracker->pid;
  }
  $project_options = casetracker_project_options();
  $form['casetracker'] = array(
    '#type' => 'fieldset',
    '#title' => t('Case information'),
    '#weight' => -10,
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#tree' => TRUE,
    '#theme' => 'casetracker_case_form_common',
  );

  // if there's no project ID from the URL, or more than one project,
  // we'll create a select menu for the user; otherwise, we'll save
  // the passed (or only) project ID into a hidden field.
  if (count($project_options) > 1) {
    if (casetracker_is_project($node->type)) {
      $project_options[0] = NULL;
      $form['casetracker']['pid'] = array(
        '#title' => t('Project'),
        '#type' => 'select',
        '#description' => 'Select self if leave empty.',
        '#default_value' => $default_project,
        '#options' => $project_options,
        '#disabled' => !user_access('change any case project') && !(user_access('change any case project') && $node->uid == $user->uid),
      );
    }
    else {
      $form['casetracker']['pid'] = array(
        '#title' => t('Project'),
        '#type' => 'select',
        '#default_value' => $default_project,
        '#options' => $project_options,
        '#disabled' => !user_access('change any case project') && !(user_access('change any case project') && $node->uid == $user->uid),
      );
    }
  }
  else {
    $form['casetracker']['pid'] = array(
      '#type' => 'value',
      // default value, or the only the project ID in the project_options array.
      '#value' => !empty($default_project) ? $default_project : key($project_options),
    );
  }

  // Retrieve the assign_to default value.
  if (isset($node->casetracker->assign_to)) {
    $default_assign_to = is_numeric($node->casetracker->assign_to) ? casetracker_get_name($node->casetracker->assign_to) : $node->casetracker->assign_to;
  }
  else {
    $default_assign_to = casetracker_default_assign_to();
  }

  // Only show this element if the user has access.
  $form['casetracker']['assign_to'] = array(
    '#title' => t('Assign to'),
    '#required' => TRUE,
    '#access' => user_access('assign cases'),
  );

  // Use different widgets based on the potential assignees.
  $options = drupal_map_assoc(casetracker_user_options());
  $assign_to_widget = variable_get('casetracker_assign_to_widget', 'flexible');
  if ($assign_to_widget == 'flexible' && count($options) < 25 || $assign_to_widget == 'radios') {
    $form['casetracker']['assign_to']['#type'] = 'radios';
    $form['casetracker']['assign_to']['#options'] = $options;
  }
  else {
    if ($assign_to_widget == 'flexible' && count($options) < 50 || $assign_to_widget == 'select') {
      $form['casetracker']['assign_to']['#type'] = 'select';
      $form['casetracker']['assign_to']['#options'] = $options;
    }
    else {
      $form['casetracker']['assign_to']['#type'] = 'textfield';
      $form['casetracker']['assign_to']['#autocomplete_path'] = 'casetracker_autocomplete';
      $form['casetracker']['assign_to']['#size'] = 12;
    }
  }

  // Set the default value if it is valid.
  $form['casetracker']['assign_to']['#default_value'] = in_array($default_assign_to, $options, TRUE) ? $default_assign_to : NULL;
  $case_status_options = casetracker_realm_load('status');
  $default_status = !empty($node->casetracker->case_status_id) ? $node->casetracker->case_status_id : variable_get('casetracker_default_case_status', key($case_status_options));
  $form['casetracker']['case_status_id'] = array(
    '#type' => 'select',
    '#title' => t('Status'),
    '#options' => $case_status_options,
    '#default_value' => $default_status,
    '#disabled' => !user_access('change any case status') && !(user_access('change own case status') && $node->uid == $user->uid),
  );
  $case_priority_options = casetracker_realm_load('priority');
  $default_priority = !empty($node->casetracker->case_priority_id) ? $node->casetracker->case_priority_id : variable_get('casetracker_default_case_priority', key($case_priority_options));
  $form['casetracker']['case_priority_id'] = array(
    '#type' => 'select',
    '#title' => t('Priority'),
    '#options' => $case_priority_options,
    '#default_value' => $default_priority,
    '#disabled' => !user_access('change any case priority') && !(user_access('change own case priority') && $node->uid == $user->uid),
  );
  $case_type_options = casetracker_realm_load('type');
  $default_type = !empty($node->casetracker->case_type_id) ? $node->casetracker->case_type_id : variable_get('casetracker_default_case_type', key($case_type_options));
  $form['casetracker']['case_type_id'] = array(
    '#type' => 'select',
    '#title' => t('Type'),
    '#options' => $case_type_options,
    '#default_value' => $default_type,
    '#disabled' => !user_access('change any case type') && !(user_access('change own case type') && $node->uid == $user->uid),
  );
  return $form;
}