You are here

function casetracker_case_form_common in Case Tracker 5

Same name and namespace in other branches
  1. 6 casetracker.module \casetracker_case_form_common()
  2. 7 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 (ie., no select box).

Return value

$form A modified Forms API $form.

1 call to casetracker_case_form_common()
casetracker_form_alter in ./casetracker.module
Implementation of hook_form_alter().

File

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

Code

function casetracker_case_form_common(&$form, $default_project = NULL) {

  // we need the user so we need the evil "user global"
  global $user;

  // we use CSS to make an inline display of the case states.
  drupal_add_css(drupal_get_path('module', 'casetracker') . '/casetracker.css');
  $node = isset($form['nid']['#value']) ? node_load($form['nid']['#value']) : NULL;

  // project to set as the default is based on how the user got here.
  $default_project = isset($default_project) ? $default_project : $node->pid;

  // get a list of all nodes that have been assigned as projects. we first
  // check our settings for all node types assigned as projects, count them,
  // and add that many %s's to our SQL so as to grab all nodes of those types.
  $project_options = array();

  // stores all found projects from set node types.
  $results = db_query(db_rewrite_sql("SELECT n.nid, n.title FROM {node} n WHERE n.type IN (" . str_pad('', count(array_filter(variable_get('casetracker_project_node_types', array(
    'casetracker_basic_project',
  )))) * 5 - 1, "'%s',") . ") AND n.status = 1 ORDER BY n.title"), array_filter(variable_get('casetracker_project_node_types', array(
    'casetracker_basic_project',
  ))));
  while ($result = db_fetch_array($results)) {
    $project_options[$result['nid']] = $result['title'];
  }

  // we predefine if the user has assign user access
  $assignAccess = false;
  if (user_access('assign case to user')) {
    $assignAccess = true;
  }
  elseif ($user->uid == $node->assign_to && user_access('assign case to user if logged in user is assigned') || $user->uid == $node->uid && user_access('assign cases if user is creator')) {
    $assignAccess = true;
  }

  // we predefine if the user has set status access
  $setStatusAccess = false;
  if (user_access('set case status')) {
    $setStatusAccess = true;
  }
  elseif ($user->uid == $node->assign_to && user_access('set status if user is assigned') || $user->uid == $node->uid && user_access('set status if user is creator')) {
    $setStatusAccess = true;
  }

  // predefine the priority set access
  $setPriorityAccess = false;
  if (user_access('set case priority')) {
    $setPriorityAccess = true;
  }
  elseif ($user->uid == $node->assign_to && user_access('set case priority if user is assigned') || $user->uid == $node->uid && user_access('set case priority if user is creator')) {
    $setPriorityAccess = true;
  }

  // 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) {
    $form['casetracker_project_information'] = array(
      '#type' => 'fieldset',
      '#title' => t('Project information'),
      '#weight' => -10,
      '#collapsible' => TRUE,
      '#collapsed' => isset($default_project) ? TRUE : FALSE,
      '#prefix' => '<div id="project-information">',
      '#suffix' => '</div>',
    );
    $form['casetracker_project_information']['pid'] = array(
      '#title' => t('Project'),
      '#type' => 'select',
      '#default_value' => $default_project,
      '#options' => $project_options,
    );
  }
  else {
    $tempKeys = array_keys($project_options);
    $form['casetracker_project_information']['pid'] = array(
      '#type' => 'hidden',
      // default value, or the only the project ID in the project_options array.
      '#default_value' => isset($default_project) ? $default_project : array_shift($tempKeys),
      '#options' => $project_options,
    );
  }
  $form['casetracker_case_information'] = array(
    '#type' => 'fieldset',
    '#title' => t('Case information'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => -9,
    '#prefix' => '<div id="case-information">',
    '#suffix' => '</div>',
  );
  $form['casetracker_case_information']['assign_to'] = array(
    '#type' => $assignAccess ? 'textfield' : 'hidden',
    '#title' => t('Assign to'),
    '#autocomplete_path' => 'casetracker/autocomplete',
    '#required' => TRUE,
    '#size' => 25,
    '#default_value' => $assignAccess ? isset($node->assign_to) ? casetracker_get_name($node->assign_to) : $node->name : isset($node->assign_to) ? casetracker_get_name($node->assign_to) : variable_get('casetracker_default_assign_to', variable_get('anonymous', t('Anonymous'))),
  );
  $case_status_options = casetracker_case_state_load('status');
  $tempKeys = array_keys($case_status_options);
  $form['casetracker_case_information']['case_status_id'] = array(
    '#type' => $setStatusAccess ? 'select' : 'hidden',
    '#title' => t('Status'),
    '#options' => $case_status_options,
    '#default_value' => isset($node->case_status_id) ? $node->case_status_id : variable_get('casetracker_default_case_status', array_shift($tempKeys)),
  );
  $case_priority_options = casetracker_case_state_load('priority');
  $tempKeys = array_keys($case_priority_options);
  $form['casetracker_case_information']['case_priority_id'] = array(
    '#type' => 'select',
    '#type' => $setPriorityAccess ? 'select' : 'hidden',
    '#title' => t('Priority'),
    '#options' => $case_priority_options,
    '#default_value' => isset($node->case_priority_id) ? $node->case_priority_id : variable_get('casetracker_default_case_priority', array_shift($tempKeys)),
  );
  $case_type_options = casetracker_case_state_load('type');
  $tempKeys = array_keys($case_type_options);
  $form['casetracker_case_information']['case_type_id'] = array(
    '#type' => 'select',
    '#title' => t('Type'),
    '#options' => $case_type_options,
    '#default_value' => isset($node->case_type_id) ? $node->case_type_id : variable_get('casetracker_default_case_type', array_shift($tempKeys)),
  );
  return $form;
}