ajax_register.module 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <?php
  2. /**
  3. * Implements hook_menu().
  4. */
  5. function ajax_register_menu() {
  6. $items['ajax_register/%/%ctools_js'] = array(
  7. 'page callback' => 'ajax_register_page_callback',
  8. 'page arguments' => array(1, 2),
  9. 'access callback' => 'ajax_register_page_access',
  10. 'access arguments' => array(1, 2),
  11. 'delivery callback' => 'ajax_deliver',
  12. 'theme callback' => 'ajax_base_page_theme',
  13. 'type' => MENU_CALLBACK,
  14. 'file' => 'ajax_register.pages.inc',
  15. );
  16. $items['admin/config/user-interface/ajax_register'] = array(
  17. 'title' => 'Ajax Register',
  18. 'page callback' => 'drupal_get_form',
  19. 'page arguments' => array('ajax_register_admin_form'),
  20. 'access arguments' => array('administer site configuration'),
  21. 'file' => 'ajax_register.admin.inc',
  22. );
  23. return $items;
  24. }
  25. /**
  26. * Check access to ajax page callback.
  27. */
  28. function ajax_register_page_access($form, $type) {
  29. $allowed_forms = array('login', 'register', 'password');
  30. $allowed_types = array('ajax', 'nojs');
  31. if (in_array($form, $allowed_forms) && in_array($type, $allowed_types) && user_is_anonymous()) {
  32. return TRUE;
  33. }
  34. return FALSE;
  35. }
  36. /**
  37. * Implements hook_block_info().
  38. */
  39. function ajax_register_block_info() {
  40. // Block with three links (login, register, restore password).
  41. $blocks['ajax_register_block'] = array(
  42. 'info' => t('Ajax Register links'),
  43. 'cache' => DRUPAL_CACHE_GLOBAL,
  44. );
  45. return $blocks;
  46. }
  47. /**
  48. * Implements hook_block_configure().
  49. */
  50. function ajax_register_block_configure() {
  51. // User allowed to disable ajax links.
  52. $form['ajax_register_enabled_links'] = array(
  53. '#type' => 'checkboxes',
  54. '#title' => t('Enabled links'),
  55. '#options' => array(
  56. 'login' => t('Login'),
  57. 'register' => t('Create new account'),
  58. 'password' => t('Request new password'),
  59. ),
  60. '#default_value' => variable_get('ajax_register_enabled_links', array('login', 'register', 'password')),
  61. );
  62. $form['ajax_register_show_links_inline'] = array(
  63. '#type' => 'checkbox',
  64. '#title' => t('Display links inline'),
  65. '#default_value' => variable_get('ajax_register_show_links_inline', TRUE),
  66. );
  67. return $form;
  68. }
  69. /**
  70. * Implements hook_block_save().
  71. */
  72. function ajax_register_block_save($delta = '', $edit = array()) {
  73. // Remove empty elements from array.
  74. $links = $edit['ajax_register_enabled_links'];
  75. foreach ($links as $key => $link) {
  76. if (!$link) {
  77. unset($links[$key]);
  78. }
  79. }
  80. // Save variables to database.
  81. variable_set('ajax_register_enabled_links', $links);
  82. variable_set('ajax_register_show_links_inline', $edit['ajax_register_show_links_inline']);
  83. }
  84. /**
  85. * Implements hook_block_view().
  86. */
  87. function ajax_register_block_view() {
  88. // Show links only to anonymous users.
  89. if (user_is_anonymous()) {
  90. // Get enabled ajax links.
  91. $enabled_links = variable_get('ajax_register_enabled_links', array('login', 'register', 'password'));
  92. if (!$enabled_links) {
  93. // Hide block if user didn't choose at least one link.
  94. return FALSE;
  95. }
  96. // Include css and js for modal dialog.
  97. _ajax_register_include_modal();
  98. // Add a links to the block.
  99. $block['content'] = array(
  100. '#theme' => 'item_list',
  101. '#items' => _ajax_register_ajax_links($enabled_links),
  102. '#attributes' => array('class' => array('ajax-register-links'))
  103. );
  104. // Display links inline.
  105. if (variable_get('ajax_register_show_links_inline', TRUE)) {
  106. $block['content']['#attributes']['class'][] = 'inline';
  107. }
  108. return $block;
  109. }
  110. }
  111. /**
  112. * Implements hook_menu_site_status_alter().
  113. */
  114. function ajax_register_menu_site_status_alter(&$menu_site_status, $path) {
  115. // Disable offline mode for ajax response for User Login and User Pass forms'.
  116. if ($menu_site_status == MENU_SITE_OFFLINE) {
  117. if (user_is_anonymous()) {
  118. switch ($path) {
  119. case 'system/ajax':
  120. if (isset($_POST['form_id']) && in_array($_POST['form_id'], array('user_login', 'user_pass'))) {
  121. $menu_site_status = MENU_SITE_ONLINE;
  122. break;
  123. }
  124. }
  125. }
  126. }
  127. }
  128. /**
  129. * Implements hook_form_alter().
  130. */
  131. function ajax_register_form_alter(&$form, &$form_state, $form_id) {
  132. // Create array with enabled ajax links.
  133. $enabled_links = array('login', 'register', 'password');
  134. switch ($form_id) {
  135. case 'user_login_block':
  136. // Add links processed with CTools modal.
  137. $form['links'] = array(
  138. '#theme' => 'item_list',
  139. '#items' => _ajax_register_ajax_links($enabled_links, $form_id),
  140. '#attributes' => array('class' => array('ajax-register-links')),
  141. );
  142. // Add html wrapper to form and #ajax to form submit.
  143. _ajax_register_add_ajax($form, $form_id);
  144. // Do not add to the user login block anything more.
  145. break;
  146. case 'user_login':
  147. case 'user_pass':
  148. case 'user_register_form':
  149. // Do not process form with AJAX that should be processed with CTools modal.
  150. $modal_links_enabled = variable_get('ajax_register_form_enable_modal_links', TRUE);
  151. if (!empty($form_state['ajax']) && $modal_links_enabled) {
  152. // Add links processed with CTools modal.
  153. $form['links'] = array(
  154. '#theme' => 'item_list',
  155. '#items' => _ajax_register_ajax_links($enabled_links, $form_id),
  156. '#attributes' => array('class' => array('ajax-register-links', 'inline')),
  157. '#weight' => -200,
  158. '#prefix' => '<div class="ajax-register-links-wrapper">',
  159. '#suffix' => '</div>',
  160. );
  161. // Unset captcha from modal form.
  162. $hide_captcha = variable_get('ajax_register_hide_captcha', FALSE);
  163. if ($hide_captcha) {
  164. unset($form['captcha']);
  165. }
  166. }
  167. else {
  168. // Add html wrapper to form and #ajax to form submit.
  169. _ajax_register_add_ajax($form, $form_id);
  170. }
  171. }
  172. }
  173. /**
  174. * Add form wrapper and #ajax attribute to forms.
  175. */
  176. function _ajax_register_add_ajax(&$form, $type) {
  177. // Add ajax wrapper to form.
  178. $html_id = 'ajax-register-' . str_replace('_', '-', $type) . '-wrapper';
  179. // Save previos form prefix if it is exists.
  180. if (!empty($form['#prefix'])) {
  181. $form['#prefix'] .= '<div id="' . $html_id . '">';
  182. }
  183. else {
  184. $form['#prefix'] = '<div id="' . $html_id . '">';
  185. }
  186. // Save previos form suffix if it is exists.
  187. if (!empty($form['#suffix'])) {
  188. $form['#suffix'] = '</div>' . $form['#suffix'];
  189. }
  190. else {
  191. $form['#suffix'] = '</div>';
  192. }
  193. // User login block and user login form have same ajax callback;
  194. if ($type == 'user_login_block') {
  195. $type = 'user_login';
  196. }
  197. // Add ajax functionality to form submit button.
  198. $form['actions']['submit']['#ajax'] = array(
  199. 'callback' => 'ajax_register_' . $type . '_ajax_callback',
  200. 'wrapper' => $html_id,
  201. 'event' => 'click',
  202. );
  203. // Add ctools modal style.
  204. $form['actions']['submit']['#attributes']['class'][] = 'ctools-modal-ctools-ajax-register-style';
  205. }
  206. /**
  207. * Ajax callback for USER LOGIN form.
  208. */
  209. function ajax_register_user_login_ajax_callback($form, $form_state) {
  210. if (!form_get_errors()) {
  211. $commands = _ajax_register_execute_form('login', $form_state);
  212. return array('#type' => 'ajax', '#commands' => $commands);
  213. }
  214. // Reload form if it didn't pass validation.
  215. return $form;
  216. }
  217. /**
  218. * Ajax callback for USER PASS form.
  219. */
  220. function ajax_register_user_pass_ajax_callback($form, $form_state) {
  221. if (!form_get_errors()) {
  222. $commands = _ajax_register_execute_form('password', $form_state);
  223. return array('#type' => 'ajax', '#commands' => $commands);
  224. }
  225. // Reload form if it didn't pass validation.
  226. return $form;
  227. }
  228. /**
  229. * Ajax callback for USER REGISTER form.
  230. */
  231. function ajax_register_user_register_form_ajax_callback($form, $form_state) {
  232. if (!form_get_errors()) {
  233. $commands = _ajax_register_execute_form('register', $form_state);
  234. return array('#type' => 'ajax', '#commands' => $commands);
  235. }
  236. // Reload form if it didn't pass validation.
  237. return $form;
  238. }
  239. /**
  240. * Executes form.
  241. */
  242. function _ajax_register_execute_form($form_type, $form_state) {
  243. // Include additinal ajax commands.
  244. ctools_include('ajax');
  245. ctools_include('modal');
  246. $redirect_behavior = variable_get('ajax_register_' . $form_type . '_redirect_behavior', 'default');
  247. $redirect_url = variable_get('ajax_register_' . $form_type . '_redirect_url', '');
  248. // Use the form state's redirect url for default behavior.
  249. if ($redirect_behavior == 'default' && !empty($form_state['redirect'])) {
  250. if (is_array($form_state['redirect'])) {
  251. $redirect_url = call_user_func_array('url', $form_state['redirect']);
  252. // Remove leading slash from the url.
  253. $redirect_url = drupal_substr($redirect_url, 1);
  254. }
  255. else {
  256. $redirect_url = $form_state['redirect'];
  257. }
  258. }
  259. elseif (($redirect_behavior == 'custom' && !empty($redirect_url)) || $redirect_behavior == 'none') {
  260. // Do nothing other than preventing the fallback.
  261. }
  262. // Refresh the page as a fallback redirect behavior.
  263. // This can happen if the form state does not have a redirect.
  264. else {
  265. $redirect_behavior = 'refresh';
  266. }
  267. // Provide additional logic and titles for different form types.
  268. switch ($form_type) {
  269. case 'password':
  270. $title = t('Successful password request');
  271. break;
  272. case 'register':
  273. $title = t('Successful registration');
  274. break;
  275. case 'login':
  276. $title = t('Successful login');
  277. $message = 'Login was successful. ';
  278. if ($redirect_behavior == 'refresh') {
  279. $message .= 'Page will now be reloaded.';
  280. }
  281. elseif ($redirect_behavior == 'default' || $redirect_behavior == 'custom') {
  282. $message .= 'Page will now be redirected.';
  283. }
  284. drupal_set_message(check_plain(t($message)));
  285. $commands[] = ctools_modal_command_display($title, theme('status_messages'));
  286. break;
  287. }
  288. // Send ajax command to modal based on redirect behavior.
  289. switch ($redirect_behavior) {
  290. case 'none':
  291. $commands[] = ctools_modal_command_display($title, theme('status_messages'));
  292. break;
  293. case 'refresh':
  294. $commands[] = ctools_ajax_command_reload();
  295. break;
  296. default:
  297. // Redirect to URL supplied from default or custom redirect behavior.
  298. $commands[] = ctools_ajax_command_redirect($redirect_url);
  299. break;
  300. }
  301. return $commands;
  302. }
  303. /**
  304. * Return ajax links for user login, register or password request.
  305. *
  306. * @param $links_enabled
  307. * Array with possible links could be enabled ('login', 'register', 'password).
  308. * @param null $form_id
  309. * Form where this links are builded.
  310. * @return array
  311. * List of ajax links.
  312. */
  313. function _ajax_register_ajax_links($links_enabled, $form_id = NULL) {
  314. // Include css and js for modal dialog.
  315. _ajax_register_include_modal();
  316. $links = array();
  317. // Build classes for ajax modal link.
  318. $classes = array();
  319. $classes[] = 'ctools-use-modal';
  320. $classes[] = 'ctools-modal-ctools-ajax-register-style';
  321. // Provide default options for ajax modal link.
  322. $options = array(
  323. 'attributes' => array(
  324. 'class' => $classes,
  325. 'rel' => 'nofollow',
  326. )
  327. );
  328. // Add login button.
  329. if (in_array('login', $links_enabled) && (stripos($form_id, 'user_login') === FALSE)) {
  330. $options['attributes']['title'] = t('Login');
  331. $links[] = l(t('Login'), 'ajax_register/login/nojs', $options);
  332. }
  333. // Add register button.
  334. if (in_array('register', $links_enabled) && $form_id != 'user_register_form') {
  335. $options['attributes']['title'] = t('Create new account');
  336. $links[] = l(t('Create new account'), 'ajax_register/register/nojs', $options);
  337. }
  338. // Add request password button.
  339. if (in_array('password', $links_enabled) && $form_id != 'user_pass') {
  340. $options['attributes']['title'] = t('Request new password');
  341. $links[] = l(t('Request new password'), 'ajax_register/password/nojs', $options);
  342. }
  343. return $links;
  344. }
  345. /**
  346. * Add css and javascript for modal dialog.
  347. */
  348. function _ajax_register_include_modal() {
  349. static $added = FALSE;
  350. if ($added == FALSE) {
  351. // Do not add css and scripts again.
  352. $added = TRUE;
  353. // Include the CTools tools that we need.
  354. ctools_include('modal');
  355. ctools_include('ajax');
  356. ctools_modal_add_js();
  357. // Create our own javascript that will be used to theme a modal.
  358. $ajax_register_style = array(
  359. 'ctools-ajax-register-style' => array(
  360. 'modalSize' => array(
  361. 'type' => 'fixed',
  362. 'width' => (int) trim(variable_get('ajax_register_modal_width', 550)),
  363. 'height' => 140,
  364. 'contentRight' => 30,
  365. 'contentBottom' => 0,
  366. ),
  367. 'modalOptions' => array(
  368. 'opacity' => (float) variable_get('ajax_register_modal_background_opacity', '0.8'),
  369. 'background-color' => '#' . variable_get('ajax_register_modal_background_color', '000000'),
  370. ),
  371. 'closeText' => '',
  372. 'throbber' => theme('image', array('path' => ctools_image_path('ajax-loader.gif', 'ajax_register'))),
  373. 'animation' => 'fadeIn',
  374. 'animationSpeed' => 'fast',
  375. ),
  376. );
  377. drupal_add_js($ajax_register_style, 'setting');
  378. // Add module css and js.
  379. ctools_add_css('ajax-register', 'ajax_register');
  380. ctools_add_js('ajax-register', 'ajax_register');
  381. }
  382. }