You are here

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

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

Implementation of hook_form().

File

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

Code

function merci_form($node, &$form_state) {
  $form = node_content_form($node, $form_state);

  // During initial form build, add the node entity to the form state for use
  // during form building and processing. During a rebuild, use what is in the
  // form state.
  if (!isset($form_state['node'])) {
    if (!isset($node->title)) {
      $node->title = NULL;
    }
    node_object_prepare($node);
    $form_state['node'] = $node;
  }
  else {
    $node = $form_state['node'];
  }

  /*
    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, $form_state, $node, TRUE);

  //}

  // Choice adding code mostly stolen from poll module.
  $choice_count = isset($form_state['values']['choice_count']) ? $form_state['values']['choice_count'] : 3;
  if (isset($form_state['triggering_element']['#value']) && $form_state['triggering_element']['#value'] == t('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 usinm
  // 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',
    ),
    '#limit_validation_errors' => array(
      array(
        'choice_count',
      ),
      array(
        'merci_reservation_items',
      ),
    ),
    // No need to validate when submitting this.
    '#validate' => array(),
    '#ajax' => array(
      'callback' => 'merci_choice_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_reservation_node_validate';
  $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;
}