modal_forms.pages.inc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. /**
  3. * @file
  4. * Page callbacks for the modal_forms module.
  5. */
  6. /*
  7. * A modal user login callback.
  8. */
  9. function modal_forms_login($js = NULL) {
  10. // Fall back if $js is not set.
  11. if (!$js) {
  12. return drupal_get_form('user_login');
  13. }
  14. ctools_include('modal');
  15. ctools_include('ajax');
  16. $form_state = array(
  17. // 'title' => t('Log in'),
  18. 'ajax' => TRUE,
  19. );
  20. $output = ctools_modal_form_wrapper('user_login', $form_state);
  21. if (!empty($form_state['executed'])) {
  22. // We'll just overwrite the form output if it was successful.
  23. $output = array();
  24. ctools_add_js('ajax-responder');
  25. $output[] = ctools_modal_command_dismiss(t('Login success'));
  26. if (isset($_GET['destination'])) {
  27. $output[] = ctools_ajax_command_redirect($_GET['destination']);
  28. }
  29. elseif(module_exists('login_destination')) {
  30. $destination = login_destination_get_destination('login');
  31. $output[] = ctools_ajax_command_redirect($destination['path']);
  32. }
  33. else {
  34. $output[] = ctools_ajax_command_reload();
  35. }
  36. }
  37. print ajax_render($output);
  38. }
  39. /**
  40. * A modal user register callback.
  41. */
  42. function modal_forms_register($js = NULL) {
  43. // Fall back if $js is not set.
  44. if (!$js) {
  45. return drupal_get_form('user_register_form');
  46. }
  47. ctools_include('modal');
  48. ctools_include('ajax');
  49. $form_state = array(
  50. // 'title' => t('Create new account'),
  51. 'ajax' => TRUE,
  52. );
  53. $output = ctools_modal_form_wrapper('user_register_form', $form_state);
  54. if (!empty($form_state['executed'])) {
  55. // We'll just overwrite the form output if it was successful.
  56. $output = array();
  57. ctools_add_js('ajax-responder');
  58. if (isset($_GET['destination'])) {
  59. $output[] = ctools_ajax_command_redirect($_GET['destination']);
  60. }
  61. elseif(module_exists('login_destination')) {
  62. $destination = login_destination_get_destination('login');
  63. $output[] = ctools_ajax_command_redirect($destination['path']);
  64. }
  65. else {
  66. $output[] = ctools_ajax_command_reload();
  67. }
  68. }
  69. print ajax_render($output);
  70. }
  71. /**
  72. * A modal user password callback.
  73. */
  74. function modal_forms_password($js = NULL) {
  75. module_load_include('inc', 'user', 'user.pages');
  76. // Fall back if $js is not set.
  77. if (!$js) {
  78. return drupal_get_form('user_pass');
  79. }
  80. ctools_include('modal');
  81. ctools_include('ajax');
  82. $form_state = array(
  83. // 'title' => t('Request new password'),
  84. 'ajax' => TRUE,
  85. );
  86. $output = ctools_modal_form_wrapper('user_pass', $form_state);
  87. if (!empty($form_state['executed'])) {
  88. // We'll just overwrite the form output if it was successful.
  89. $output = array();
  90. ctools_add_js('ajax-responder');
  91. if (isset($_GET['destination'])) {
  92. $output[] = ctools_ajax_command_redirect($_GET['destination']);
  93. }
  94. else {
  95. $output[] = ctools_ajax_command_reload();
  96. }
  97. }
  98. print ajax_render($output);
  99. }
  100. /**
  101. * A modal contact callback.
  102. */
  103. function modal_forms_contact($js = NULL) {
  104. module_load_include('inc', 'contact', 'contact.pages');
  105. // Fall back if $js is not set.
  106. if (!$js) {
  107. return drupal_get_form('contact_site_form');
  108. }
  109. ctools_include('modal');
  110. ctools_include('ajax');
  111. $form_state = array(
  112. // 'title' => t('Contact'),
  113. 'ajax' => TRUE,
  114. );
  115. $output = ctools_modal_form_wrapper('contact_site_form', $form_state);
  116. if (!empty($form_state['executed'])) {
  117. // We'll just overwrite the form output if it was successful.
  118. $output = array();
  119. ctools_add_js('ajax-responder');
  120. if (isset($_GET['destination'])) {
  121. $output[] = ctools_ajax_command_redirect($_GET['destination']);
  122. }
  123. else {
  124. $output[] = ctools_ajax_command_reload();
  125. }
  126. }
  127. print ajax_render($output);
  128. }
  129. /**
  130. * A modal personal contact form
  131. */
  132. function modal_forms_personal_contact($recipient, $js = NULL) {
  133. module_load_include('inc', 'contact', 'contact.pages');
  134. // Fall back if $js is not set.
  135. if (!$js) {
  136. return drupal_get_form('contact_personal_form', $recipient);
  137. }
  138. ctools_include('modal');
  139. ctools_include('ajax');
  140. $form_state = array(
  141. // 'title' => t('Contact'),
  142. 'build_info' => array(
  143. 'args' => array($recipient)
  144. ),
  145. 'ajax' => TRUE,
  146. );
  147. $output = ctools_modal_form_wrapper('contact_personal_form', $form_state);
  148. if (!empty($form_state['executed'])) {
  149. // We'll just overwrite the form output if it was successful.
  150. $output = array();
  151. ctools_add_js('ajax-responder');
  152. if (isset($_GET['destination'])) {
  153. $output[] = ctools_ajax_command_redirect($_GET['destination']);
  154. }
  155. else {
  156. $output[] = ctools_ajax_command_reload();
  157. }
  158. }
  159. print ajax_render($output);
  160. }
  161. /**
  162. * A modal comment callback.
  163. */
  164. function modal_forms_comment_reply($node, $js = NULL, $pid = NULL) {
  165. $output = array();
  166. $comment = array(
  167. 'pid' => $pid,
  168. 'nid' => $node->nid,
  169. );
  170. // Fall back if $js is not set.
  171. if (!$js) {
  172. return drupal_get_form('comment_node_' . $node->type . '_form', (object) $comment);
  173. }
  174. ctools_include('modal');
  175. ctools_include('ajax');
  176. $form_state = array(
  177. 'build_info' => array(
  178. 'args' => array(
  179. (object) $comment,
  180. ),
  181. ),
  182. // 'title' => t('Comment'),
  183. 'ajax' => TRUE,
  184. 're_render' => FALSE,
  185. 'no_redirect' => TRUE,
  186. );
  187. // Should we show the reply box?
  188. if ($node->comment != COMMENT_NODE_OPEN) {
  189. drupal_set_message(t('This discussion is closed: you can\'t post new comments.'), 'error');
  190. drupal_goto('node/' . $node->nid);
  191. }
  192. else {
  193. $output = drupal_build_form('comment_node_' . $node->type . '_form', $form_state);
  194. // Remove output bellow the comment.
  195. unset($output['comment_output_below']);
  196. }
  197. if (!$form_state['executed'] || $form_state['rebuild']) {
  198. $output = ctools_modal_form_render($form_state, $output);
  199. }
  200. else {
  201. // We'll just overwrite the form output if it was successful.
  202. $output = array();
  203. // @todo: Uncomment once http://drupal.org/node/1587916 is fixed.
  204. //if (is_array($form_state['redirect'])) {
  205. // list($path, $options) = $form_state['redirect'];
  206. // $output[] = ctools_ajax_command_redirect($path, 0, $options);
  207. //}
  208. if (isset($_GET['destination'])) {
  209. $output[] = ctools_ajax_command_redirect($_GET['destination']);
  210. }
  211. else {
  212. $output[] = ctools_ajax_command_reload();
  213. }
  214. }
  215. print ajax_render($output);
  216. }
  217. /**
  218. * Modal display of the node's webform.
  219. *
  220. * @param $node
  221. * A node object.
  222. */
  223. function modal_forms_view_webform($node, $js = NULL) {
  224. $output = array();
  225. // Fall back if $js is not set.
  226. if (!$js) {
  227. return drupal_get_form('webform_client_form_' . $node->nid, $node, FALSE);
  228. }
  229. ctools_include('modal');
  230. ctools_include('ajax');
  231. if (empty($node->webform['components'])) {
  232. // No webform or no components.
  233. $output[] = ctools_modal_command_display(t('Webform'), t('No webform found.'));
  234. print ajax_render($output);
  235. exit;
  236. }
  237. // Get webform title.
  238. $title = check_plain($node->title);
  239. $form_state = array(
  240. 'title' => $title,
  241. 'ajax' => TRUE,
  242. );
  243. // Prevent webform redirect.
  244. $GLOBALS['conf']['webform_blocks']['client-block-' . $node->nid]['pages_block'] = 1;
  245. $node->webform_block = TRUE;
  246. // Pass required parameters.
  247. $form_state['build_info']['args'] = array($node, FALSE);
  248. // Render the Webform.
  249. $output = ctools_modal_form_wrapper('webform_client_form_' . $node->nid, $form_state);
  250. if (!empty($form_state['executed'])) {
  251. if (!isset($form_state['storage'])) {
  252. ctools_add_js('ajax-responder');
  253. // Handle confirmation message or redirect.
  254. if ('<confirmation>' == $node->webform['redirect_url']) {
  255. // Use confirmation message if set, default text otherwise.
  256. // See webform/templates/webform-confirmation.tpl.php.
  257. $message = (empty($node->webform['confirmation'])) ? t('Thank you, your submission has been received.') : check_markup($node->webform['confirmation'], $node->webform['confirmation_format'], '', TRUE);
  258. $confirmation = '<div class="webform-confirmation">' . $message . '</div>';
  259. $confirmation .= ctools_ajax_text_button(t('Close'), 'modal_forms/nojs/dismiss', t('Close'));
  260. $output[] = ctools_modal_command_display($title, $confirmation);
  261. }
  262. elseif ('<none>' == $node->webform['redirect_url']) {
  263. // Reload webform node itself.
  264. $output[] = ctools_ajax_command_reload();
  265. }
  266. else {
  267. // Redirect to URL.
  268. // Note: Redirect code in webform_client_form_submit() is more complex.
  269. $url = trim(ltrim($node->webform['redirect_url'], '/'));
  270. $output[] = ctools_ajax_command_redirect($url);
  271. }
  272. }
  273. }
  274. print ajax_render($output);
  275. }
  276. /**
  277. * Closes modal windows.
  278. */
  279. function modal_forms_dismiss($js = NULL) {
  280. if (!$js) {
  281. // we should never be here out of ajax context
  282. return MENU_NOT_FOUND;
  283. }
  284. ctools_include('modal');
  285. ctools_include('ajax');
  286. $output = array(ctools_modal_command_dismiss());
  287. print ajax_render($output);
  288. }