webform_ajax.module in Webform Ajax 6
Same filename and directory in other branches
Makes webform pages load by AJAX instead of causing a full page refresh.
File
webform_ajax.moduleView source
<?php
/**
* @file
* Makes webform pages load by AJAX instead of causing a full page refresh.
*/
/**
* Implementation of hook_menu().
*/
function webform_ajax_menu() {
$items['webform_ajax/%/%'] = array(
'title' => t('Ajax Handling of Webforms'),
'type' => MENU_CALLBACK,
'access callback' => '_webform_ajax_perm',
'access arguments' => array(
1,
),
'page callback' => '_webform_ajax_page',
'page arguments' => array(
1,
2,
),
);
return $items;
}
/**
* The access callback for hook_menu item 'webform_ajax/%/%'.
* @param $nid: the node id of the current webform.
* @return TRUE if user has access to use this webform, FALSE if not.
*/
function _webform_ajax_perm($nid = NULL) {
if (!$nid) {
return FALSE;
}
$node = node_load($nid);
global $user;
$enabled = TRUE;
// Check if the user's role can submit this webform.
if (variable_get('webform_submission_access_control', 1)) {
$allowed_roles = array();
foreach ($node->webform['roles'] as $rid) {
$allowed_roles[$rid] = isset($user->roles[$rid]) ? TRUE : FALSE;
}
if (array_search(TRUE, $allowed_roles) === FALSE && $user->uid != 1) {
$enabled = FALSE;
}
}
// Check if the user can add another submission.
if ($node->webform['submit_limit'] != -1) {
// -1: Submissions are never throttled.
module_load_include('inc', 'webform', 'includes/webform.submissions');
// Disable the form if the limit is exceeded and page cache is not active.
if (($limit_exceeded = _webform_submission_limit_check($node)) && ($user->uid != 0 || variable_get('cache', CACHE_DISABLED) == CACHE_DISABLED)) {
$enabled = FALSE;
}
}
// If editing a submission, load the submission object
$sid = $_POST['details']['sid'];
if ($sid) {
$submission = webform_menu_submission_load($sid, $nid);
$enabled = $enabled && webform_submission_access($node, $submission, 'edit');
}
return $enabled;
}
/**
* The access callback for hook_menu item 'webform_ajax/%/%'.
* @param $nid: the node id of the current webform.
* @param $op: the operation to handle. Can be either 'next' or 'previous'.
* @return The rendered HTML of the next/previous page of the webform.
*/
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;
}
/**
* Implementation of hook_form_alter().
*/
function webform_ajax_form_alter(&$form, &$form_state, $form_id) {
if (is_array($form['details']) && isset($form['details']['nid']) && $form_id == 'webform_client_form_' . $form['details']['nid']['#value']) {
global $base_url;
drupal_add_js('var drupalBasePath = "' . $base_url . '";', 'inline');
drupal_add_js(drupal_get_path('module', 'webform_ajax') . '/js/webform_ajax.js', 'module');
}
}
Functions
Name![]() |
Description |
---|---|
webform_ajax_form_alter | Implementation of hook_form_alter(). |
webform_ajax_menu | Implementation of hook_menu(). |
_webform_ajax_page | The access callback for hook_menu item 'webform_ajax/%/%'. |
_webform_ajax_perm | The access callback for hook_menu item 'webform_ajax/%/%'. |