You are here

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

Same name and namespace in other branches
  1. 6.2 includes/menu.inc \merci_choice_js()
  2. 7.2 merci.module \merci_choice_js()

Menu callback for AHAH additions.

1 string reference to 'merci_choice_js'
merci_menu in ./merci.module
Implementation of hook_menu().

File

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

Code

function merci_choice_js() {
  $delta = count($_POST['choice']);
  $nid = isset($_POST['nid']) ? $_POST['nid'] : 0;
  if ((int) $nid) {
    $node = node_load($nid);
  }
  else {
    $node = new stdClass();
  }
  $dates = $_POST['field_merci_date'][0];

  // If a start and end date exist, we have to massage them
  // into the proper format from user input.
  // TODO: is there a more elegant way to do this?
  if ($dates['value']['date'] && $dates['value']['time'] && $dates['value2']['date'] && $dates['value2']['time']) {
    module_load_include('inc', 'date_api', 'date_api_elements');
    $date_timezone = date_default_timezone_name();
    $date_format = 'm/d/Y g:ia';
    $start = array(
      '#value' => array(
        'date' => $dates['value']['date'],
        'time' => $dates['value']['time'],
      ),
      '#date_timezone' => $date_timezone,
      '#date_format' => $date_format,
    );
    $end = array(
      '#value' => array(
        'date' => $dates['value2']['date'],
        'time' => $dates['value2']['time'],
      ),
      '#date_timezone' => $date_timezone,
      '#date_format' => $date_format,
    );
    $form_state['values']['field_merci_date'][0]['value'] = date_popup_input_value($start);
    $form_state['values']['field_merci_date'][0]['value2'] = date_popup_input_value($end);
  }
  else {
    $form_state = array();
  }

  // Build our new form element.
  $form_element = _merci_choice_form($node, $form_state, $delta);
  drupal_alter('form', $form_element, array(), 'merci_choice_js');

  // Build the new form.
  $form_state = array(
    'submitted' => FALSE,
  );
  $form_build_id = $_POST['form_build_id'];

  // Add the new element to the stored form. Without adding the element to the
  // form, Drupal is not aware of this new elements existence and will not
  // process it. We retreive the cached form, add the element, and resave.
  if (!($form = form_get_cache($form_build_id, $form_state))) {
    exit;
  }
  $form['choice_wrapper']['choice'][$delta] = $form_element;
  form_set_cache($form_build_id, $form, $form_state);
  $form += array(
    '#post' => $_POST,
    '#programmed' => FALSE,
  );

  // Rebuild the form.
  $form = form_builder('merci_reservation_node_form', $form, $form_state);

  // Render the new output.
  $choice_form = $form['choice_wrapper']['choice'];
  unset($choice_form['#prefix'], $choice_form['#suffix']);

  // Prevent duplicate wrappers.
  $choice_form[$delta]['#attributes']['class'] = empty($choice_form[$delta]['#attributes']['class']) ? 'ahah-new-content' : $choice_form[$delta]['#attributes']['class'] . ' ahah-new-content';
  $output = theme('status_messages') . drupal_render($choice_form);
  drupal_json(array(
    'status' => TRUE,
    'data' => $output,
  ));
}