* $form['#ajaxsubmit'] = TRUE;
*
*
* Optional settings
*
* Target: Element, indentified by a jQuery selector, that submit response should render into.
* Trigger this behavior as follows:
*
*
* $form['#ajaxsubmit_target'] = '#myid';
*
*
* Progress: ajaxsubmit can monitor for progress of a form submit.
* Trigger this behavior as follows:
*
*
* $form['#ajaxsubmit_progress'] = TRUE;
*
*/
/**
* Implementation of hook_form_alter().
*/
function ajaxsubmit_form_alter($form_id, &$form) {
if (in_array($form_id, array_filter(variable_get('ajaxsubmit_forms', array())))) {
$form['#ajaxsubmit'] = TRUE;
}
if ($form['#ajaxsubmit']) {
// If we're using ajaxsubmit and the form's already been submitted, redirect.
if (!empty($_POST) && !empty($_POST['ajaxsubmit']) && ($_POST['form_id'] == $form_id)) {
// Redirect on successful submit, i.e., if there are no errors.
$form['#redirect'] = 'ajaxsubmit/dispatch';
if (is_array($form['#pre_render'])) {
$form['#pre_render'][] = 'ajaxsubmit_dispatch';
}
else {
$form['#pre_render'] = array('ajaxsubmit_dispatch');
}
}
else {
// In case a destination was set by a previous transaction, unset it
// so that this one can set its own as needed.
unset($_SESSION['ajaxsubmit_destination']);
ajaxsubmit_load();
$form['#attributes']['class'] .= ' ajaxsubmit';
if ($form['#ajaxsubmit_target']) {
$form['ajaxsubmit_target'] = array(
'#type' => 'hidden',
'#value' => $form['#ajaxsubmit_target']
);
}
if ($form['#ajaxsubmit_progress']) {
// Value is the path to the progress monitoring.
$form['ajaxsubmit_progress'] = array(
'#type' => 'hidden',
'#value' => url(drupal_get_path('module', 'ajaxsubmit') . '/ajaxsubmit_update')
);
}
if ($form['#ajaxsubmit_error_message']) {
$form['ajaxsubmit_error_message'] = array(
'#type' => 'hidden',
'#value' => $form['#ajaxsubmit_error_message']
);
}
if ($form['#ajaxsubmit_error_redirect']) {
$form['ajaxsubmit_error_redirect'] = array(
'#type' => 'hidden',
'#value' => $form['#ajaxsubmit_error_redirect']
);
}
}
}
}
/**
* Implementation of hook_menu().
*/
function ajaxsubmit_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/settings/ajaxsubmit',
'title' => t('AJAX submit'),
'description' => t('Configuration for AJAX submit'),
'callback' => 'drupal_get_form',
'callback arguments' => array('ajaxsubmit_admin_settings')
);
$items[] = array(
'path' => 'ajaxsubmit/dispatch',
'title' => t('ajaxsubmit'),
'access' => user_access('access content'),
'type' => MENU_CALLBACK,
'callback' => 'ajaxsubmit_dispatch'
);
$items[] = array(
'path' => 'ajaxsubmit/progress',
'title' => t('ajaxsubmit progress'),
'access' => user_access('access content'),
'type' => MENU_CALLBACK,
'callback' => 'ajaxsubmit_progress'
);
}
return $items;
}
/**
* Menu callback for admin settings form.
*/
function ajaxsubmit_admin_settings() {
$form = array();
$known_forms = form_store_get_all();
$options = array();
foreach ($known_forms as $item) {
$options[$item->form_id] = $item->form_id .' - '. $item->description;
}
$form['ajaxsubmit_forms'] = array(
'#type' => 'checkboxes',
'#title' => t('Forms'),
'#description' => t('Select the forms to apply the AJAX Submit behavior to.'),
'#default_value' => array_filter(variable_get('ajaxsubmit_forms', array())),
'#options' => $options,
);
$output = system_settings_form($form);
return $output;
}
/**
* Load needed files.
*/
function ajaxsubmit_load() {
$path = drupal_get_path('module', 'ajaxsubmit');
module_invoke('jstools', 'add_js', array($path . '/ajaxsubmit.js'));
drupal_add_js('misc/progress.js');
}
/**
* Return form submit result.
*/
function ajaxsubmit_dispatch($form_id = NULL, $form = NULL) {
$result = array(
'status' => TRUE,
'data' => array(
'errors' => form_get_errors(),
'message' => theme('status_messages'),
'preview' => isset($form[arg(0) . '_preview']) && !empty($form[arg(0) . '_preview']['#value']) ? $form[arg(0) . '_preview']['#value'] : NULL,
'destination' => $_SESSION['ajaxsubmit_destination'] ? $_SESSION['ajaxsubmit_destination'] : NULL
)
);
print drupal_to_js($result);
exit();
}
/**
* Return progress percentage and message.
*
* The _submit function must set two session variables:
* - $_SESSION['{form_id}_total'], the total number of actions to be taken during this submit, and
* - $_SESSION['{form_id}_remaining'], the number of actions remaining.
* In each of these, the 'form_id' value should be the id of the form in question.
*/
function ajaxsubmit_progress() {
$form_id = $_REQUEST['form_id'];
$percentage = floor(($_SESSION[$form_id . '_total'] - $_SESSION[$form_id . '_remaining']) / $_SESSION[$form_id . '_total'] * 100);
$message = ($percentage == 100) ? t('Submit complete') : t('Submit in progress');
print drupal_to_js(array('status' => TRUE, 'percentage' => $percentage, 'message' => $message));
}