You are here

function commerce_registration_line_item_registrations_refresh in Commerce Registration 7.2

AJAX callback for registration sections on the order edit ui.

1 string reference to 'commerce_registration_line_item_registrations_refresh'
commerce_registration_form_commerce_order_ui_order_form_alter in ./commerce_registration.module
Implements hook_form_FORM_ID_alter().

File

./commerce_registration.module, line 1404
Commerce Registration module code.

Code

function commerce_registration_line_item_registrations_refresh($form, $form_state) {
  $refresh_line_items = FALSE;
  $commands = array();

  // Reverse the array parents of the triggering element, because we know the
  // parts of the form to return.
  $parents = array_reverse($form_state['triggering_element']['#array_parents']);

  // AJAX commands if triggered by a remove checkbox.
  if ($parents[0] == 'registration_remove') {
    $commands[] = ajax_command_replace('#line-item-reg-' . $parents[3], render($form['commerce_registration']['line_item_registrations'][$parents[3]]['registrations']));

    // Since the registration has been deleted we want to re-focus the client on
    // the registrations table for the current line item.
    $commands[] = ajax_command_invoke('#line-item-reg-' . $parents[3], 'focus');
    $refresh_line_items = TRUE;
  }
  elseif ($parents[0] == 'registration_control_add') {

    // Refresh the registration controls to display the new registration form.
    $commands[] = ajax_command_replace('#reg-new-' . $parents[2], render($form['commerce_registration']['line_item_registrations'][$parents[2]]['registration_control']));

    // Set focus on the first element on the new registration form.
    if (isset($form['commerce_registration']['line_item_registrations'][$parents[2]]['registration_control']['form']) && ($children_ids = element_children($form['commerce_registration']['line_item_registrations'][$parents[2]]['registration_control']['form'], TRUE))) {
      $first_child = reset($children_ids);
      $element_id = $form['commerce_registration']['line_item_registrations'][$parents[2]]['registration_control']['form'][$first_child]['#id'];
    }
    else {
      $element_id = 'line-item-reg-' . $parents[2];
    }
    $commands[] = ajax_command_invoke('#' . $element_id, 'focus');
  }
  elseif (isset($parents[3]) && $parents[3] == 'registration_control' && $parents[0] == 'registration_control_submit') {

    // Reset the registration form controls.
    $commands[] = ajax_command_replace('#li-reg-' . $parents[4], render($form['commerce_registration']['line_item_registrations'][$parents[4]]));

    // Since a new registration was added we want to re-focus the client on
    // the registrations table for the current line item.
    $commands[] = ajax_command_invoke('#line-item-reg-' . $parents[4], 'focus');
    $refresh_line_items = TRUE;
  }
  elseif (isset($parents[3]) && $parents[3] == 'registration_control' && $parents[0] == 'registration_control_cancel') {

    // Reset the registration form controls.
    $commands[] = ajax_command_replace('#reg-new-' . $parents[4], render($form['commerce_registration']['line_item_registrations'][$parents[4]]['registration_control']));

    // Set focus back to the "Add registration" button.
    $commands[] = ajax_command_invoke('#' . $form['commerce_registration']['line_item_registrations'][$parents[4]]['registration_control']['registration_control_add']['#id'], 'focus');
  }
  elseif ($parents[0] == 'sync_registrations') {
    $refresh_line_items = TRUE;

    // For accessibility purposes return focus back on the sync checkbox.
    $commands[] = ajax_command_invoke('#' . $form_state['triggering_element']['#id'], 'focus');
  }

  // AJAX commands for updating the line items if affected by registrations.
  if ($refresh_line_items) {

    // Also if possible refresh the line items since the quantity should have
    // been updated.
    if (isset($form['commerce_line_items']) && isset($form['commerce_line_items']['#language']) && isset($form['commerce_line_items'][$form['commerce_line_items']['#language']])) {
      $commands[] = ajax_command_replace('#line-item-manager', render($form['commerce_line_items'][$form['commerce_line_items']['#language']]));
    }
  }

  // Add the status messages inside the new content's wrapper element, so that
  // on subsequent Ajax requests, it is treated as old content.
  $commands[] = ajax_command_replace('#commerce-registration-messages', '<div id="commerce-registration-messages">' . theme('status_messages') . '</div>');
  return array(
    '#type' => 'ajax',
    '#commands' => $commands,
  );
}