You are here

function pmtimetracking_form in Drupal PM (Project Management) 7

Same name and namespace in other branches
  1. 8 pmtimetracking/pmtimetracking.module \pmtimetracking_form()
  2. 7.3 pmtimetracking/pmtimetracking.module \pmtimetracking_form()
  3. 7.2 pmtimetracking/pmtimetracking.module \pmtimetracking_form()

Implements hook_form().

File

pmtimetracking/pmtimetracking.module, line 277
Main module functions for PM Timetracking.

Code

function pmtimetracking_form(&$node, $form_state) {
  $breadcrumb = array();
  $breadcrumb[] = l(t('Project Management'), 'pm');
  $breadcrumb[] = l(t('Timetrackings'), 'pm/timetrackings');
  drupal_set_breadcrumb($breadcrumb);
  $type = node_type_get_type($node);
  $info = field_info_extra_fields('node', 'pmtimetracking', 'form');
  $form['#attributes']['class'] = 'pmcomponent_node_form';
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => check_plain($type->title_label),
    '#required' => TRUE,
    '#default_value' => $node->title,
    '#weight' => $info['title']['weight'],
  );
  $form['group1'] = array(
    '#type' => 'markup',
    '#theme' => 'pm_form_group',
    '#weight' => $info['group1']['weight'],
  );
  $org_id = isset($node->organization_nid) ? $node->organization_nid : -1;
  $query = db_select('pmorganization', 'pmorg');
  $query
    ->innerJoin('node', 'n', 'pmorg.vid=n.vid');
  $records = $query
    ->fields('pmorg', array(
    'nid',
    'isactive',
  ))
    ->fields('n', array(
    'nid',
    'title',
  ))
    ->condition('n.status', '1')
    ->condition(db_or()
    ->condition('pmorg.isactive', '1')
    ->condition('n.nid', $org_id))
    ->condition('n.type', 'pmorganization')
    ->orderBy('title', 'ASC')
    ->addTag('node_access')
    ->execute();
  $organizations = array();
  foreach ($records as $organization) {
    $organizations[$organization->nid] = $organization->title;
    if (!isset($node->organization_nid)) {
      $node->organization_nid = $organization->nid;
    }
  }
  $organization_nid = isset($form_state['values']['organization_nid']) ? $form_state['values']['organization_nid'] : (isset($node->organization_nid) ? $node->organization_nid : -1);
  $form['group1']['organization_nid'] = array(
    '#type' => 'select',
    '#title' => t('Organization'),
    '#default_value' => $organization_nid,
    '#options' => $organizations,
    '#required' => TRUE,
    '#ajax' => array(
      'callback' => 'pmtimetracking_ajax_organization_nid',
      'wrapper' => 'pmtimetracking-project-nid',
    ),
  );
  if ($organization_nid == -1) {
    drupal_set_message(t('Please add an organization to the system before trying to add a timetracking.'), 'error');
  }
  $pro_query = db_select('node', 'n');
  $pro_query
    ->join('pmproject', 'pmpr', 'n.vid = pmpr.vid');
  $pro_result = $pro_query
    ->fields('n', array(
    'nid',
    'title',
  ))
    ->condition('n.status', 1)
    ->condition('pmpr.organization_nid', $organization_nid)
    ->condition('n.type', 'pmproject')
    ->orderBy('n.title', 'ASC')
    ->addTag('node_access')
    ->execute();
  $projects = array();
  foreach ($pro_result as $project) {
    $projects[$project->nid] = $project->title;
  }
  $form['group1']['project_nid'] = array(
    '#type' => 'select',
    '#title' => t('Project'),
    '#default_value' => isset($node->project_nid) ? $node->project_nid : 0,
    '#options' => array(
      0 => '-',
    ) + $projects,
    '#prefix' => '<div id="pmtimetracking-project-nid">',
    '#suffix' => '</div>',
    '#ajax' => array(
      'callback' => 'pmtimetracking_ajax_project_nid',
      'wrapper' => 'pmtimetracking-task-nid',
    ),
  );
  $project_nid = isset($form_state['values']['project_nid']) ? $form_state['values']['project_nid'] : (isset($node->project_nid) ? $node->project_nid : 0);
  $tree = isset($project_nid) ? _pmtask_get_tree($project_nid) : array();
  $tasks = _pmtask_plain_tree($tree);
  $form['group1']['task_nid'] = array(
    '#type' => 'select',
    '#title' => t('Task'),
    '#default_value' => isset($node->task_nid) ? $node->task_nid : 0,
    '#options' => array(
      0 => '-',
    ) + $tasks,
    '#prefix' => '<div id="pmtimetracking-task-nid">',
    '#suffix' => '</div>',
    '#ajax' => array(
      'callback' => 'pmtimetracking_ajax_task_nid',
      'wrapper' => 'pmtimetracking-ticket-nid',
    ),
  );
  $task_nid = isset($form_state['values']['task_nid']) ? $form_state['values']['task_nid'] : (isset($node->task_nid) ? $node->task_nid : 0);
  $ti_query = db_select('node', 'n');
  $ti_query
    ->join('pmticket', 'pmti', 'n.vid = pmti.vid');
  $ti_result = $ti_query
    ->fields('n', array(
    'nid',
    'title',
  ))
    ->condition('pmti.task_nid', $task_nid)
    ->condition('n.status', 1)
    ->condition('n.type', 'pmticket')
    ->orderBy('n.title', 'ASC')
    ->addTag('node_access')
    ->execute();
  $tickets = array();
  foreach ($ti_result as $ticket) {
    $tickets[$ticket->nid] = $ticket->title;
  }
  $form['group1']['ticket_nid'] = array(
    '#type' => 'select',
    '#title' => t('Ticket'),
    '#default_value' => isset($node->ticket_nid) ? $node->ticket_nid : 0,
    '#options' => array(
      0 => '-',
    ) + $tickets,
    '#prefix' => '<div id="pmtimetracking-ticket-nid">',
    '#suffix' => '</div>',
  );
  $form['group3'] = array(
    '#type' => 'markup',
    '#theme' => 'pm_form_group',
    '#weight' => $info['group3']['weight'],
  );
  $duration = isset($node->duration) ? sprintf("%01.2f", $node->duration) : sprintf("%01.2f", 0);
  $form['group3']['duration'] = array(
    '#type' => 'textfield',
    '#title' => t('Duration (h)'),
    '#default_value' => $duration,
    '#size' => 10,
    '#maxlength' => 5,
    '#weight' => 3,
    '#disabled' => TRUE,
  );
  $form['group3']['billing_duration'] = array(
    '#type' => 'textfield',
    '#title' => t('Billing duration (h)'),
    '#default_value' => sprintf("%01.2f", isset($node->billing_duration) ? $node->billing_duration : 0),
    '#size' => 10,
    '#maxlength' => 5,
    '#weight' => 4,
  );

  // Check to see if the body field is still there, if so, display it.
  $body = field_get_items('pmtimetracking', $node, 'body');
  if ($body) {
    $form['body_field'] = $body;
  }
  return $form;
}