modal_forms.module 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /**
  3. * @file
  4. * Modal Forms allows you to activate a Ctools based
  5. * ajax modal on common Drupal forms
  6. */
  7. /**
  8. * Implements hook_init().
  9. */
  10. function modal_forms_init() {
  11. // Do not load modal_forms during the Drupal installation process, e.g. if part
  12. // of installation profiles.
  13. if (!drupal_installation_attempted()) {
  14. _modal_forms_doheader();
  15. }
  16. }
  17. /**
  18. * Implements hook_menu().
  19. */
  20. function modal_forms_menu() {
  21. $items['admin/config/development/modal_forms'] = array(
  22. 'title' => 'Modal forms',
  23. 'description' => 'Adjust Modal forms settings.',
  24. 'page callback' => 'drupal_get_form',
  25. 'page arguments' => array('modal_forms_admin_settings'),
  26. 'access arguments' => array('administer site configuration'),
  27. 'file' => 'modal_forms.admin.inc',
  28. );
  29. $items['modal_forms/%ctools_js/login'] = array(
  30. 'title' => 'Log in',
  31. 'page callback' => 'modal_forms_login',
  32. 'page arguments' => array(1),
  33. 'access callback' => TRUE,
  34. 'file' => 'modal_forms.pages.inc',
  35. 'type' => MENU_CALLBACK,
  36. );
  37. $items['modal_forms/%ctools_js/register'] = array(
  38. 'title' => 'Register',
  39. 'page callback' => 'modal_forms_register',
  40. 'page arguments' => array(1),
  41. 'access callback' => 'user_register_access',
  42. 'file' => 'modal_forms.pages.inc',
  43. 'type' => MENU_CALLBACK,
  44. );
  45. $items['modal_forms/%ctools_js/password'] = array(
  46. 'title' => 'Password',
  47. 'page callback' => 'modal_forms_password',
  48. 'page arguments' => array(1),
  49. 'access callback' => 'user_is_anonymous',
  50. 'file' => 'modal_forms.pages.inc',
  51. 'type' => MENU_CALLBACK,
  52. );
  53. $items['modal_forms/%ctools_js/contact'] = array(
  54. 'title' => 'Contact',
  55. 'page callback' => 'modal_forms_contact',
  56. 'page arguments' => array(1),
  57. 'access arguments' => array('access site-wide contact form'),
  58. 'file' => 'modal_forms.pages.inc',
  59. 'type' => MENU_CALLBACK,
  60. );
  61. $items['modal_forms/%ctools_js/user/%user/contact'] = array(
  62. 'title' => 'Contact',
  63. 'page callback' => 'modal_forms_personal_contact',
  64. 'page arguments' => array(3, 1),
  65. 'access arguments' => array('access user contact forms'),
  66. 'file' => 'modal_forms.pages.inc',
  67. 'type' => MENU_CALLBACK,
  68. );
  69. $items['modal_forms/%ctools_js/comment/reply/%node'] = array(
  70. 'title' => 'Comment',
  71. 'page callback' => 'modal_forms_comment_reply',
  72. 'page arguments' => array(4, 1),
  73. 'access arguments' => array('post comments'),
  74. 'file' => 'modal_forms.pages.inc',
  75. 'type' => MENU_CALLBACK,
  76. );
  77. $items['modal_forms/%ctools_js/webform/%node'] = array(
  78. 'title' => 'Webform',
  79. 'page callback' => 'modal_forms_view_webform',
  80. 'page arguments' => array(3, 1),
  81. 'access callback' => 'node_access',
  82. 'access arguments' => array('view', 3),
  83. 'file' => 'modal_forms.pages.inc',
  84. 'type' => MENU_CALLBACK,
  85. );
  86. $items['modal_forms/%ctools_js/dismiss'] = array(
  87. 'title' => 'Dismiss',
  88. 'page callback' => 'modal_forms_dismiss',
  89. 'page arguments' => array(1),
  90. 'access callback' => TRUE,
  91. 'file' => 'modal_forms.pages.inc',
  92. 'type' => MENU_CALLBACK,
  93. );
  94. return $items;
  95. }
  96. /**
  97. * Implements hook_form_alter().
  98. */
  99. function modal_forms_form_alter(&$form, &$form_state, $form_id) {
  100. switch ($form_id) {
  101. case 'user_login':
  102. if (arg(0) == 'modal_forms') {
  103. $form['name']['#size'] = 25;
  104. $form['pass']['#size'] = 25;
  105. // Add links as needed.
  106. if (variable_get('modal_forms_login_links', 0)) {
  107. $items = array();
  108. // Add standard links.
  109. if (variable_get('modal_forms_login_links', 0) == 1) {
  110. if (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)) {
  111. $items[] = l(t('Create new account'), 'user/register', array('attributes' => array('title' => t('Create a new user account.'))));
  112. }
  113. $items[] = l(t('Request new password'), 'user/password', array('attributes' => array('title' => t('Request new password via e-mail.'))));
  114. }
  115. // Add links that opens in a modal_forms.
  116. if (variable_get('modal_forms_login_links', 0) == 2) {
  117. if (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)) {
  118. $items[] = ctools_modal_text_button(t('Create new account'), 'modal_forms/nojs/register', t('Create a new user account'), 'ctools-modal-modal-popup-medium');
  119. }
  120. $items[] = ctools_modal_text_button(t('Request new password'), 'modal_forms/nojs/password', t('Request new password via e-mail.'), 'ctools-modal-modal-popup-small');
  121. }
  122. $form['links'] = array('#markup' => theme('item_list', array('items' => $items)));
  123. }
  124. }
  125. break;
  126. case 'user_register_form':
  127. if (arg(0) == 'modal_forms') {
  128. $form['account']['name']['#size'] = 30;
  129. $form['account']['mail']['#size'] = 30;
  130. }
  131. break;
  132. case 'user_pass':
  133. if (arg(0) == 'modal_forms') {
  134. $form['name']['#size'] = 30;
  135. }
  136. break;
  137. }
  138. }
  139. /**
  140. * Implements hook_form_BASE_FROM_ID_alter().
  141. */
  142. function modal_forms_form_comment_form_alter(&$form, &$form_state, $form_id) {
  143. if (variable_get('modal_forms_comment', 0)) {
  144. $comment = $form_state['comment'];
  145. // If not replying to a comment, use our dedicated page callback for new
  146. // comments on nodes.
  147. if (empty($comment->cid) && empty($comment->pid)) {
  148. // Change the action to call our function.
  149. $form['#action'] = url('modal_forms/nojs/comment/reply/' . $comment->nid);
  150. }
  151. }
  152. }
  153. /**
  154. * Check if modal_forms should be active for the current URL.
  155. *
  156. * @return
  157. * TRUE if modal_forms should be active for the current page.
  158. */
  159. function _modal_forms_active() {
  160. // Code from the block_list funtion in block.module.
  161. $path = drupal_get_path_alias($_GET['q']);
  162. $modal_forms_pages = variable_get('modal_forms_pages', "admin*\nimagebrowser*\nimg_assist*\nimce*\nnode/add/*\nnode/*/edit\nprint/*\nprintpdf/*\nsystem/ajax\nsystem/ajax/*");
  163. // Compare with the internal and path alias (if any).
  164. $page_match = drupal_match_path($path, $modal_forms_pages);
  165. if ($path != $_GET['q']) {
  166. $page_match = $page_match || drupal_match_path($_GET['q'], $modal_forms_pages);
  167. }
  168. $page_match = variable_get('modal_forms_visibility', 0) == 0 ? !$page_match : $page_match;
  169. return $page_match;
  170. }
  171. /**
  172. * Loads the various js and css files.
  173. */
  174. function _modal_forms_doheader() {
  175. static $already_added = FALSE;
  176. if ($already_added) {
  177. return; // Don't add the JavaScript and CSS multiple times.
  178. }
  179. if (!_modal_forms_active()) {
  180. return; // Don't add the JavaScript and CSS on specified paths.
  181. }
  182. // Include the CTools tools that we need.
  183. ctools_include('ajax');
  184. ctools_include('modal');
  185. // Add CTools' javascript to the page.
  186. ctools_modal_add_js();
  187. $throbber = theme('image', array('path' => ctools_image_path('loading_animation.gif', 'modal_forms'), 'alt' => t('Loading...'), 'title' => t('Loading')));
  188. $js_settings = array(
  189. 'modal-popup-small' => array(
  190. 'modalSize' => array(
  191. 'type' => variable_get('modal_forms_popup_small_type', 'fixed'),
  192. 'width' => floatval(variable_get('modal_forms_popup_small_width', 300)),
  193. 'height' => floatval(variable_get('modal_forms_popup_small_height', 300)),
  194. ),
  195. 'modalOptions' => array(
  196. 'opacity' => floatval(variable_get('modal_forms_opacity', 0.85)),
  197. 'background' => variable_get('modal_forms_background_color', '#000'),
  198. ),
  199. 'animation' => 'fadeIn',
  200. 'modalTheme' => 'ModalFormsPopup',
  201. 'throbber' => $throbber,
  202. 'closeText' => t('Close'),
  203. ),
  204. 'modal-popup-medium' => array(
  205. 'modalSize' => array(
  206. 'type' => variable_get('modal_forms_popup_medium_type', 'fixed'),
  207. 'width' => floatval(variable_get('modal_forms_popup_medium_width', 550)),
  208. 'height' => floatval(variable_get('modal_forms_popup_medium_height', 450)),
  209. ),
  210. 'modalOptions' => array(
  211. 'opacity' => floatval(variable_get('modal_forms_opacity', 0.85)),
  212. 'background' => variable_get('modal_forms_background_color', '#000'),
  213. ),
  214. 'animation' => 'fadeIn',
  215. 'modalTheme' => 'ModalFormsPopup',
  216. 'throbber' => $throbber,
  217. 'closeText' => t('Close'),
  218. ),
  219. 'modal-popup-large' => array(
  220. 'modalSize' => array(
  221. 'type' => variable_get('modal_forms_popup_large_type', 'scale'),
  222. 'width' => floatval(variable_get('modal_forms_popup_large_width', 0.8)),
  223. 'height' => floatval(variable_get('modal_forms_popup_large_height', 0.8)),
  224. ),
  225. 'modalOptions' => array(
  226. 'opacity' => floatval(variable_get('modal_forms_opacity', 0.85)),
  227. 'background' => variable_get('modal_forms_background_color', '#000'),
  228. ),
  229. 'animation' => 'fadeIn',
  230. 'modalTheme' => 'ModalFormsPopup',
  231. 'throbber' => $throbber,
  232. 'closeText' => t('Close'),
  233. ),
  234. );
  235. drupal_add_js($js_settings, 'setting');
  236. // Add modal_forms own js and CSS.
  237. ctools_add_css('modal_forms_popup', 'modal_forms');
  238. ctools_add_js('modal_forms_popup', 'modal_forms');
  239. $path = drupal_get_path('module', 'modal_forms');
  240. // Automatically rewrite selected links to open in a modal.
  241. if ($GLOBALS['user']->uid == 0) {
  242. if (variable_get('modal_forms_login', 0)) {
  243. drupal_add_js($path . '/js/modal_forms_login.js', array('weight' => -20));
  244. }
  245. if (variable_get('modal_forms_register', 0)) {
  246. drupal_add_js($path . '/js/modal_forms_register.js', array('weight' => -20));
  247. }
  248. if (variable_get('modal_forms_password', 0)) {
  249. drupal_add_js($path . '/js/modal_forms_password.js', array('weight' => -20));
  250. }
  251. }
  252. if (variable_get('modal_forms_contact', 0)) {
  253. drupal_add_js($path . '/js/modal_forms_contact.js', array('weight' => -20));
  254. }
  255. if (user_access('post comments') && variable_get('modal_forms_comment', 0)) {
  256. drupal_add_js($path . '/js/modal_forms_comment.js', array('weight' => -20));
  257. }
  258. $already_added = TRUE;
  259. }