You are here

function _webform_ajax_page in Webform Ajax 6

The access callback for hook_menu item 'webform_ajax/%/%'.

Parameters

$nid: the node id of the current webform.:

$op: the operation to handle. Can be either 'next' or 'previous'.:

Return value

The rendered HTML of the next/previous page of the webform.

1 string reference to '_webform_ajax_page'
webform_ajax_menu in ./webform_ajax.module
Implementation of hook_menu().

File

./webform_ajax.module, line 78
Makes webform pages load by AJAX instead of causing a full page refresh.

Code

function _webform_ajax_page($nid = NULL, $op = NULL) {
  global $user;

  // Loading the node object
  $node = node_load($nid);

  // If editing a submission, load the submission object
  $sid = $_POST['details']['sid'];
  if ($sid) {
    $submission = webform_menu_submission_load($sid, $nid);
  }
  else {
    $submission = array();
  }

  // Checking on the chosen operation
  $operation = '';
  switch ($op) {
    case 'next':
      $operation = 'next';
      break;
    case 'previous':
      $operation = 'previous';
      break;
  }

  // Stop if the operation entered is invalid
  if ($operation == '') {
    return '';
  }

  // Store the $_POST because some form API functions clear it out
  $local_POST = $_POST;
  $form_build_id = $_POST['form_build_id'];
  $form_id = 'webform_client_form_' . $nid;

  // Building $form_state to process according to the clicked button
  $form_state = array();
  $form = form_get_cache($form_build_id, $form_state);
  $form_array = drupal_rebuild_form($form_id, $form_state, array(
    $form_state,
    $node,
    $submission,
  ), $form_build_id);
  $form_state['clicked_button'] = $form_array['actions'][$operation];
  if (is_array($local_POST['submitted'])) {
    foreach ($local_POST['submitted'] as $submit_index => $submit) {
      $form_state['storage']['submitted'][$submit_index] = $submit;
      $form_state['values']['submitted'][$submit_index] = $submit;
    }
  }

  // Clearing empty values from $form_state
  if (is_array($form_state['values']['submitted'])) {
    foreach ($form_state['values']['submitted'] as $value_index => $value) {
      if (!$value) {
        unset($form_state['values']['submitted'][$value_index]);
      }
    }
  }

  // Executing the pressed button action
  drupal_execute($form_id, $form_state, $node, array());

  // Get the HTML for the error messages
  $error_html = theme('status_messages', 'error');

  // Building the resulting form after the processing of the button
  $form_array = drupal_rebuild_form($form_id, $form_state, array(
    $form_state,
    $node,
    $submission,
  ), $form_build_id);
  $form = drupal_render_form($form_id, $form_array);
  echo '<span id="' . $form_array['#id'] . '-errors">' . $error_html . '</span>' . $form;
}