You are here

function pmticket_form in Drupal PM (Project Management) 7

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

Implements hook_form().

File

pmticket/pmticket.module, line 233
1: Hooks 2: Access functions

Code

function pmticket_form(&$node, $form_state) {
  $breadcrumb = array(
    l(t('Project Management'), 'pm'),
    l(t('Tickets'), 'pm/tickets'),
  );
  drupal_set_breadcrumb($breadcrumb);
  $type = node_type_get_type($node);
  $info = field_info_extra_fields('node', 'pmticket', 'form');
  $form['#attributes']['class'] = 'pmcomponent_node_form';
  $form['#prefix'] = '<div id="pmticket-form">';
  $form['#suffix'] = '</div>';
  $form['group1'] = array(
    '#type' => 'markup',
    '#theme' => 'pm_form_group',
    '#weight' => $info['group1']['weight'],
  );
  $org_query = db_select('node', 'n');
  $org_query
    ->join('pmorganization', 'sor', 'n.vid = sor.vid');
  $org_query
    ->fields('n', array(
    'nid',
    'title',
  ))
    ->condition('n.status', 1)
    ->condition('n.type', 'pmorganization')
    ->addTag('node_access')
    ->orderBy('n.title', 'ASC');
  $org_result = $org_query
    ->execute();
  $organizations = array();
  foreach ($org_result as $organization) {
    $organizations[$organization->nid] = $organization->title;
    if (!isset($node->organization_nid)) {
      $node->organization_nid = $organization->nid;
    }
  }
  $form['group1']['organization_nid'] = array(
    '#type' => 'select',
    '#title' => t('Organization'),
    '#default_value' => isset($node->organization_nid) ? $node->organization_nid : NULL,
    '#options' => $organizations,
    '#required' => TRUE,
    '#ajax' => array(
      'callback' => 'pmticket_form_ajax',
      'wrapper' => 'pmticket-form',
    ),
  );
  if (isset($node->organization_nid)) {
    $organization_nid = isset($form_state['values']['organization_nid']) ? $form_state['values']['organization_nid'] : $node->organization_nid;
  }
  else {
    drupal_set_message(t('Please add an organization to the system before trying to add a ticket.'), 'error');
  }
  $pro_query = db_select('node', 'n');
  $pro_query
    ->join('pmproject', 'spr', 'n.vid = spr.vid');
  $pro_result = $pro_query
    ->fields('n', array(
    'nid',
    'title',
  ))
    ->condition('spr.organization_nid', $organization_nid)
    ->condition('n.status', 1)
    ->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="pmticket-project-nid">',
    '#suffix' => '</div>',
    '#ajax' => array(
      'callback' => 'pmticket_form_ajax',
      'wrapper' => 'pmticket-form',
    ),
  );
  $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="pmticket-task-nid">',
    '#suffix' => '</div>',
  );
  $form['group1']['task_nid_old'] = array(
    '#type' => 'hidden',
    '#default_value' => isset($node->task_nid) ? $node->task_nid : NULL,
  );
  $form['group2'] = array(
    '#type' => 'markup',
    '#theme' => 'pm_form_group',
    '#weight' => $info['group2']['weight'],
  );
  $category_list = pm_attributes_bydomain('Ticket category');
  $form['group2']['ticketcategory'] = array(
    '#type' => 'select',
    '#title' => t('Category'),
    '#default_value' => isset($node->ticketcategory) ? $node->ticketcategory : $category_list['default'],
    '#options' => $category_list['values'],
  );
  $status_list = pm_attributes_bydomain('Ticket status');
  $form['group2']['ticketstatus'] = array(
    '#type' => 'select',
    '#title' => t('Status'),
    '#default_value' => isset($node->ticketstatus) ? $node->ticketstatus : $status_list['default'],
    '#options' => $status_list['values'],
  );
  $priority_list = pm_attributes_bydomain('Ticket priority');
  $form['group2']['ticketpriority'] = array(
    '#type' => 'select',
    '#title' => t('Priority'),
    '#default_value' => isset($node->ticketpriority) ? $node->ticketpriority : $priority_list['default'],
    '#options' => $priority_list['values'],
  );
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => check_plain($type->title_label),
    '#required' => TRUE,
    '#default_value' => $node->title,
    '#weight' => $info['title']['weight'],
  );
  $form['group3'] = array(
    '#type' => 'markup',
    '#theme' => 'pm_form_group',
    '#weight' => $info['group3']['weight'],
  );
  $durationunits = pm_attributes_bydomain('Duration unit');
  $form['group3']['durationunit'] = array(
    '#type' => 'select',
    '#title' => t('Duration unit'),
    '#default_value' => isset($node->durationunit) ? $node->durationunit : $durationunits['default'],
    '#options' => $durationunits['values'],
  );
  $form['group3']['duration'] = array(
    '#type' => 'textfield',
    '#title' => t('Duration'),
    '#size' => 20,
    '#default_value' => isset($node->duration) ? $node->duration : NULL,
  );
  $form['group4'] = array(
    '#type' => 'markup',
    '#theme' => 'pm_form_group',
    '#weight' => 3,
    '#weight' => $info['group4']['weight'],
  );
  $pricemodes = pm_attributes_bydomain('Price mode');
  $form['group4']['pricemode'] = array(
    '#type' => 'select',
    '#title' => t('Price mode'),
    '#default_value' => isset($node->pricemode) ? $node->pricemode : $pricemodes['default'],
    '#options' => array(
      '-' => '-',
    ) + $pricemodes['values'],
  );
  $form['group4']['price'] = array(
    '#title' => t('Price'),
    '#type' => 'textfield',
    '#size' => 15,
    '#default_value' => isset($node->price) ? $node->price : NULL,
  );
  $currencies = pm_attributes_bydomain('Currency');
  $form['group4']['currency'] = array(
    '#type' => 'select',
    '#title' => t('Price currency'),
    '#default_value' => isset($node->currency) ? $node->currency : $currencies['default'],
    '#options' => array(
      '-' => '-',
    ) + $currencies['values'],
  );
  $form['group5'] = array(
    '#type' => 'markup',
    '#theme' => 'pm_form_group',
    '#weight' => $info['group5']['weight'],
  );
  $options = isset($project_nid) ? pm_get_assignment_options($organization_nid, $project_nid) : pm_get_assignment_options($organization_nid);
  $form['group5']['assigned_nid'] = array(
    '#type' => 'select',
    '#title' => t('Assigned to'),
    '#options' => $options,
    '#default_value' => isset($node->assigned_nid) ? $node->assigned_nid : NULL,
  );

  // Check to see if the body field is still there, if so, display it.
  $body = field_get_items('pmticket', $node, 'body');
  if ($body) {
    $form['body_field'] = $body;
  }
  $form['title_old'] = array(
    '#type' => 'hidden',
    '#default_value' => isset($node->title_old) ? $node->title_old : NULL,
  );
  return $form;
}