You are here

function merci_form in MERCI (Manage Equipment Reservations, Checkout and Inventory) 6.2

Same name and namespace in other branches
  1. 6 merci.module \merci_form()
  2. 7.2 merci.module \merci_form()

Implementation of hook_form().

File

./merci.module, line 305
MERCI - Managed Equipment Reservation Checkout and Inventory

Code

function merci_form(&$node, $form_state) {
  $form = node_content_form($node, $form_state);
  if (isset($form_state['node'])) {
    $node = $form_state['node'] + (array) $node;
  }
  $node = (object) $node;

  // Add a wrapper for the choices and more button.
  $form['choice_wrapper'] = array(
    '#tree' => FALSE,
    '#prefix' => '<div class="clear-block" id="merci-choice-wrapper">',
    '#suffix' => '</div>',
  );

  // Build existing reserved items table on existing reservations.

  //if (isset($node->nid)) {
  $form['choice_wrapper']['merci_reservation_items'] = merci_build_reservation_table_form($form_state, $node, TRUE);

  //}

  // Choice adding code mostly stolen from poll module.
  $choice_count = empty($node->choice_count) ? 3 : $node->choice_count;
  if (array_key_exists('op', $form_state['post']) and $form_state['post']['op'] == "Add more items") {
    $choice_count += 3;
  }
  $form['choice_wrapper']['choice_count'] = array(
    '#type' => 'value',
    '#value' => $choice_count,
  );

  // Add the current choices to the form.
  for ($delta = 1; $delta <= $choice_count; $delta++) {
    $default = isset($node->merci_reservation_items["choice_" . $delta]['merci_item_nid']) ? $node->merci_reservation_items["choice_" . $delta]['merci_item_nid'] : '';
    $form['choice_wrapper']['merci_reservation_items']["choice_" . $delta]['merci_item_nid'] = _merci_choice_form($node, $form_state, $delta, $default);
  }
  $options = array();
  for ($i = 1; $i < 20; $i++) {
    $options[$i] = $i;
  }

  // We name our button 'merci_more' to avoid conflicts with other modules using
  // AHAH-enabled buttons with the id 'more'.
  $form['choice_wrapper']['merci_more'] = array(
    '#type' => 'submit',
    '#value' => t('Add more items'),
    '#description' => t("If the number of items above isn't enough, click here to add more items."),
    '#weight' => 1,
    '#submit' => array(
      'merci_more_choices_submit',
    ),
    // If no javascript action.
    '#ahah' => array(
      'path' => 'merci/js',
      'wrapper' => 'merci-choice-wrapper',
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );
  if (user_access('manage reservations')) {
    $form['merci_reservation_status'] = array(
      '#title' => t('Status'),
      '#type' => 'radios',
      '#options' => merci_record_status(),
      '#default_value' => $node->merci_reservation_status,
      '#description' => t('Finalized bookings can not have time conflicts with each other.'),
    );
  }
  else {
    $form['merci_reservation_status'] = array(
      '#type' => 'value',
      '#value' => $node->merci_reservation_status,
    );
  }
  $form['merci_original_reservation_status'] = array(
    '#type' => 'value',
    '#value' => isset($node->merci_original_reservation_status) ? $node->merci_original_reservation_status : $node->merci_reservation_status,
  );

  // Since hook_validate is broken in 6.x, we add our own
  // custom validation here.
  // TODO check if this fixed.
  $form['#validate'][] = 'merci_node_validate';
  $form['#after_build'][] = '_merci_after_build';
  $form['#cache'] = TRUE;

  // Make sure the form is cached.
  // Pull the correct action out of form_state if it's there to avoid AHAH+Validation action-rewrite.
  if (isset($form_state['action'])) {
    $form['#action'] = $form_state['action'];
  }
  return $form;
}