You are here

function pmnote_form in Drupal PM (Project Management) 7

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

Implements hook_form().

File

pmnote/pmnote.module, line 200
Functions for the PM Note module.

Code

function pmnote_form(&$node, $form_state) {
  $breadcrumb = array();
  $breadcrumb[] = l(t('Project Management'), 'pm');
  $breadcrumb[] = l(t('Notes'), 'pm/notes');
  drupal_set_breadcrumb($breadcrumb);
  $type = node_type_get_type($node);
  $info = field_info_extra_fields('node', 'pmnote', 'form');
  $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_result = $org_query
    ->fields('n', array(
    'nid',
    'title',
  ))
    ->condition('n.status', 1)
    ->condition('n.type', 'pmorganization')
    ->condition('sor.isactive', 1)
    ->orderBy('n.title', 'ASC')
    ->addTag('node_access')
    ->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' => 'pmnote_ajax_organization_nid',
      'wrapper' => 'pmnote-project-nid',
    ),
  );
  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 note.'), 'error');
    $organization_nid = NULL;
  }
  $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('n.status', 1)
    ->condition('spr.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;
  }
  $project_nid = isset($form_state['values']['project_nid']) ? $form_state['values']['project_nid'] : (isset($node->project_nid) ? $node->project_nid : NULL);
  $form['group1']['project_nid'] = array(
    '#type' => 'select',
    '#title' => t('Project'),
    '#default_value' => $project_nid,
    '#options' => array(
      0 => '-',
    ) + $projects,
    '#prefix' => '<div id="pmnote-project-nid">',
    '#suffix' => '</div>',
    '#ajax' => array(
      'callback' => 'pmnote_ajax_project_nid',
      'wrapper' => 'pmnote-task-nid',
    ),
  );
  $tree = is_null($project_nid) ? array() : _pmtask_get_tree($project_nid);
  $tasks = _pmtask_plain_tree($tree);
  $form['group1']['task_nid'] = array(
    '#type' => 'select',
    '#title' => t('Task'),
    '#default_value' => isset($node->task_nid) ? $node->task_nid : NULL,
    '#options' => array(
      0 => '-',
    ) + $tasks,
    '#prefix' => '<div id="pmnote-task-nid">',
    '#suffix' => '</div>',
  );
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => check_plain($type->title_label),
    '#required' => TRUE,
    '#default_value' => $node->title,
    '#weight' => $info['title']['weight'],
  );

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