You are here

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

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

Implementation of hook_form().

File

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

Code

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

  // Build existing reserved items table on existing reservations.
  if (isset($node->nid)) {
    $form['existing_items'] = merci_build_reservation_table_form($form_state, $node, TRUE);
    $merci = $node->merci;
  }
  else {
    $merci['status'] = variable_get('merci_default_reservation_status', MERCI_STATUS_UNCONFIRMED);
  }

  // Choice adding code mostly stolen from poll module.
  if (isset($form_state['choice_count'])) {
    $choice_count = $form_state['choice_count'];
  }
  else {
    $choice_count = max(3, empty($node->choice) ? 3 : count($node->choice));
  }

  // 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>',
  );

  // Container for just the item selector.
  $form['choice_wrapper']['choice'] = array(
    '#prefix' => '<div id="merci-choices">',
    '#suffix' => '</div>',
    '#theme' => 'merci_choices',
  );
  if (isset($_GET['template'])) {
    $templates = db_query("SELECT nr.body FROM {node_revisions} nr WHERE nid = %d ORDER BY vid DESC LIMIT 1", intval($_GET['template']));
    while ($template_object = db_fetch_object($templates)) {
      $template = explode(',', $template_object->body);
    }

    // while
  }

  // if
  // Add the current choices to the form.
  for ($delta = 0; $delta < $choice_count || $delta < count($template); $delta++) {
    $default = isset($node->choice[$delta]['item']) ? $node->choice[$delta]['item'] : '';
    if ($default == '' && isset($template[$delta])) {
      $default = $template[$delta];
    }

    // if
    $form['choice_wrapper']['choice'][$delta] = _merci_choice_form($node, $form_state, $delta, $default);
  }

  // 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 (user_access('administer MERCI')) {
    $form['merci'] = array(
      '#type' => 'fieldset',
      '#title' => t('MERCI settings'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );
    $form['merci']['merci_status'] = array(
      '#title' => t('Status'),
      '#type' => 'radios',
      '#options' => merci_record_status(),
      '#default_value' => $merci['status'],
      '#description' => t('Finalized bookings can not have time conflicts with each other.'),
    );
  }
  else {
    $form['merci_status'] = array(
      '#type' => 'value',
      '#value' => $merci['status'],
    );
  }
  return $form;
}