<php
/**
* Implements hook_theme().
*/
function popup_theme($existing, $type, $theme, $path) {
$hooks['popup_form'] = array(
'render element' => 'form',
'path' => path_to_theme() . '/templates/forms',
'template' => 'popup-form',
);
$hooks += drupal_find_theme_templates($hooks, '.tpl.php', $path);
return $hooks;
}
/**
* Implements hook_menu().
*/
function popup_menu() {
$items = array();
$items['popup/reset-password'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array('popup_pass_reset_form'),
'access arguments' => array('access content'),
'delivery callback' => 'popup_form_ajax_popup_callback',
'type' => MENU_CALLBACK,
);
$items['form-pass-reset-ajax-callback'] = array(
'page callback' => 'popup_form_pass_reset_ajax_callback',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'delivery callback' => 'ajax_deliver',
);
return $items;
}
/**
* Check if request is an AJAX call
*/
function check_is_ajax() {
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
return TRUE;
}
return FALSE;
}
/**
* Delivery callback function for ajax popups
*/
function popup_form_ajax_popup_callback($form) {
if (!check_is_ajax()) {
drupal_deliver_html_page(MENU_ACCESS_DENIED);
module_invoke_all('exit');
drupal_exit();
}
drupal_alter('form_' . $form['#form_id'] . '_ajax_popup_callback', $form);
$form_html = render($form);
$scripts = drupal_add_js();
if (!empty($scripts['settings']['data'])) {
$data = array();
foreach($scripts['settings']['data'] as $ajax) {
if (!empty($ajax['ajax'])) {
$data[] = $ajax['ajax'];
}
}
$json = drupal_json_encode(drupal_array_merge_deep_array($data));
$js_element = array(
'#tag' => 'script',
'#value' => 'jQuery.extend(Drupal.settings.ajax, ' . $json . ');',
'#attributes' => array(
'type' => 'text/javascript',
),
);
$tag = theme('html_tag', array('element' => $js_element));
}
print $form_html;
drupal_page_footer();
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function popup_form_popup_pass_reset_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'popup_pass_reset_form' && $_GET['q'] == 'popup/reset-password') {
$form['actions']['submit']['#ajax'] = array(
'path' => 'form-pass-reset-ajax-callback',
'progress' => array('message' => NULL),
);
}
}
/**
* Implements hook_form_FORM_ID_ajax_popup_callback_alter().
*/
function popup_form_popup_pass_reset_form_ajax_popup_callback_alter(&$form) {
array_unshift($form['#theme'], "popup_form__popup_pass_reset_form");
}
/**
* Ajax callback for the form popup_pass_reset_form
*/
function popup_form_pass_reset_ajax_callback() {
list($form, $form_state) = ajax_get_form();
drupal_process_form($form['#form_id'], $form, $form_state);
$commands = array();
$element['#parents'][] = 'name';
$error = form_get_error($element);
if (empty($error)) {
drupal_set_message(t('Email was sent.'));
}
$messages = theme('status_messages');
$commands[] = ajax_command_html('#messages-popup-mot-de-passe', $messages);
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Preprocess form, threat forms like entities.
*/
function popup_preprocess_popup_form(&$variables) {
$preprocess = array();
$preprocess_entity_loaded = &drupal_static('popup_preprocess_loaded', FALSE);
if (!$preprocess_entity_loaded) {
$preprocess_entity_loaded = TRUE;
$file = path_to_theme() . '/popup.preprocess.inc';
if (is_file($file)) {
require_once $file;
}
}
foreach ($variables['form']['#theme'] as $theme_name) {
if (strstr($theme_name, 'form_popup__')) {
$preprocess[] = str_replace('popup_form__', 'popup_preprocess_popup_form__', $theme_name);
}
}
foreach ($preprocess as $preproces) {
if (function_exists($preproces)) {
$preproces($variables);
}
}
}
?>