You are here

function _webform_ajax_callback_completed in Webform Ajax 7

Same name and namespace in other branches
  1. 7.2 webform_ajax.module \_webform_ajax_callback_completed()

AJAX callback helper for a completed webform.

Generates a redirect if needed, or displays the appropriate content.

1 call to _webform_ajax_callback_completed()
webform_ajax_callback in ./webform_ajax.module
AJAX callback for Webform Prev/Next page and Submit buttons.

File

./webform_ajax.module, line 162
Webform AJAX module file.

Code

function _webform_ajax_callback_completed($form, $form_state) {
  $output = '';
  if (isset($form_state['redirect'])) {

    // If a redirect is set, then use it to send a redirect instruction via AJAX.
    ctools_include('ajax');
    ctools_add_js('ajax-responder');
    $redirect = is_array($form_state['redirect']) ? $form_state['redirect'] : array(
      $form_state['redirect'],
      array(),
    );

    // Send two AJAX commands:
    // The first disables the form's buttons, to avoid extra click while waiting for redirect.
    // The second is a redirect command, giving the browser the URL where to go next.
    $output = array(
      '#type' => 'ajax',
      '#commands' => array(
        ajax_command_invoke('#' . $form_state['values']['webform_ajax_wrapper_id'] . ' input.form-submit', 'attr', array(
          'disabled',
          'disabled',
        )),
        ctools_ajax_command_redirect($redirect[0], 0, $redirect[1]),
      ),
    );
  }
  elseif ($form['#node']->webform['webform_ajax'] == WEBFORM_AJAX_CONFIRM) {

    // Display webform confirmation.
    $output = array(
      '#type' => 'markup',
      '#markup' => theme(array(
        'webform_confirmation_' . $form['#node']->nid,
        'webform_confirmation',
      ), array(
        'node' => $form['#node'],
        'sid' => $form_state['values']['details']['sid'],
      )),
      '#prefix' => '<div id="' . $form_state['values']['webform_ajax_wrapper_id'] . '">',
      '#suffix' => '</div>',
    );

    // Unset confirmation message previously set with drupal_set_message()
    // by Webform module, as we displayed it themed.
    // Get messages without clearing queue.
    $status_messages = drupal_get_messages('status', FALSE);

    // If there are status messages, we may want to clear one.
    if (isset($status_messages['status'])) {

      // Load node and submission to pass to webform_replace_tokens()
      $node = node_load($form_state['values']['details']['nid']);
      $submission = webform_get_submission($form_state['values']['details']['nid'], $form_state['values']['details']['sid']);

      // This is the message we want to erase.
      $confirmation = webform_replace_tokens($form['#node']->webform['confirmation'], $node, $submission, NULL, $form['#node']->webform['confirmation_format']);
      $index = array_search($confirmation, $status_messages['status']);

      // If message found, then remove it from the list, clear the messages queue,
      // then reset all messages (except the one we removed).
      if ($index !== FALSE) {
        unset($status_messages['status'][$index]);
        drupal_get_messages('status');
        foreach ($status_messages['status'] as $message) {
          drupal_set_message($message);
        }
      }
    }

    // Add Javascript which will AJAXify "Return to form" link in webform_confirmation.
    $ajax_setting = array(
      'webform_ajax' => array(
        'wrappers' => array(
          $form_state['values']['webform_ajax_wrapper_id'] => array(
            'wrapper_id' => $form_state['values']['webform_ajax_wrapper_id'],
            'nid' => $form['#node']->nid,
            'sid' => $form_state['values']['details']['sid'],
          ),
        ),
      ),
    );
    $ajax_setting['webform_ajax']['url'] = url('webform_ajax/return_webform');
    drupal_add_js($ajax_setting, 'setting');
    drupal_add_js(drupal_get_path('module', 'webform_ajax') . '/js/webform_ajax.js', 'file');
  }
  elseif ($form['#node']->webform['webform_ajax'] == WEBFORM_AJAX_NO_CONFIRM) {

    // We can't build a new form on the same request that submitted it (don't know why).
    // So, as we need to rebuild the Webform, as AJAX response, tell the client to
    // build another AJAX response to another URL (webform_ajax.js).
    $ajax_setting = array(
      'webform_ajax' => array(
        'reload' => array(
          'wrapper_id' => $form_state['values']['webform_ajax_wrapper_id'],
          'nid' => $form['#node']->nid,
          'sid' => $form_state['values']['details']['sid'],
          'button_id' => $form['actions']['submit']['#id'],
        ),
      ),
    );
    $ajax_setting['webform_ajax']['url'] = url('webform_ajax/return_webform');
    drupal_add_js($ajax_setting, 'setting');
    drupal_add_js(drupal_get_path('module', 'webform_ajax') . '/js/webform_ajax.js', 'file');

    // This output does not alter the page, and ensures Drupal messages are kept for the next request.
    $output = array(
      '#type' => 'ajax',
      '#commands' => array(),
    );
  }
  return $output;
}