modal.inc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. /**
  3. * @file
  4. * Implement a modal form using AJAX.
  5. *
  6. * The modal form is implemented primarily from mc.js; this contains the
  7. * Drupal specific stuff to use it. The modal is fairly generic and can
  8. * be activated mostly by setting up the right classes, but if you are
  9. * using the modal you must include links to the images in settings,
  10. * because the javascript does not inherently know where the images are.
  11. *
  12. * You can accomplish this with this PHP code:
  13. * @code {
  14. * ctools_include('modal');
  15. * ctools_modal_add_js();
  16. * }
  17. *
  18. * You can have links and buttons bound to use the modal by adding the
  19. * class ctools-use-modal.
  20. *
  21. * For links, the href of the link will be the destination, with any
  22. * appearance of /nojs/ converted to /ajax/.
  23. *
  24. * For submit buttons, however, the URL is found a different, slightly
  25. * more complex way. The ID of the item is taken and -url is appended to
  26. * it to derive a class name. Then, all form elements that contain that
  27. * class name are founded and their values put together to form a URL.
  28. *
  29. * For example, let's say you have an 'add' button, and it has a select
  30. * form item that tells your system what widget it is adding. If the id
  31. * of the add button is edit-add, you would place a hidden input with
  32. * the base of your URL in the form and give it a class of 'edit-add-url'.
  33. * You would then add 'edit-add-url' as a class to the select widget
  34. * allowing you to convert this value to the form without posting.
  35. *
  36. * If no URL is found, the action of the form will be used and the entire
  37. * form posted to it.
  38. */
  39. function ctools_modal_add_js() {
  40. // Provide a gate so we only do this once.
  41. static $done = FALSE;
  42. if ($done) {
  43. return;
  44. }
  45. $settings = array(
  46. 'CToolsModal' => array(
  47. 'loadingText' => t('Loading...'),
  48. 'closeText' => t('Close Window'),
  49. 'closeImage' => theme('image', array(
  50. 'path' => ctools_image_path('icon-close-window.png'),
  51. 'title' => t('Close window'),
  52. 'alt' => t('Close window'),
  53. )),
  54. 'throbber' => theme('image', array(
  55. 'path' => ctools_image_path('throbber.gif'),
  56. 'title' => t('Loading...'),
  57. 'alt' => t('Loading'),
  58. )),
  59. ),
  60. );
  61. drupal_add_js($settings, 'setting');
  62. drupal_add_library('system', 'jquery.form');
  63. drupal_add_library('system', 'drupal.progress');
  64. drupal_add_library('system', 'drupal.ajax');
  65. ctools_add_js('modal');
  66. ctools_add_css('modal');
  67. $done = TRUE;
  68. }
  69. /**
  70. * @todo this is deprecated
  71. */
  72. function ctools_modal_add_plugin_js($plugins) {
  73. $css = array();
  74. $js = array(drupal_get_path('module', 'ctools') . '/js/dependent.js' => TRUE);
  75. foreach ($plugins as $subtype) {
  76. if (isset($subtype['js'])) {
  77. foreach ($subtype['js'] as $file) {
  78. if (file_exists($file)) {
  79. $js[$file] = TRUE;
  80. }
  81. else if (file(exists($subtype['path'] . '/' . $file))) {
  82. $js[$subtype['path'] . '/' . $file] = TRUE;
  83. }
  84. }
  85. }
  86. if (isset($subtype['css'])) {
  87. foreach ($subtype['css'] as $file) {
  88. if (file_exists($file)) {
  89. $css[$file] = TRUE;
  90. }
  91. else if (file(exists($subtype['path'] . '/' . $file))) {
  92. $css[$subtype['path'] . '/' . $file] = TRUE;
  93. }
  94. }
  95. }
  96. }
  97. foreach (array_keys($js) as $file) {
  98. drupal_add_js($file);
  99. }
  100. foreach (array_keys($css) as $file) {
  101. drupal_add_css($file);
  102. }
  103. }
  104. /**
  105. * Place HTML within the modal.
  106. *
  107. * @param $title
  108. * The title of the modal.
  109. * @param $html
  110. * The html to place within the modal.
  111. */
  112. function ctools_modal_command_display($title, $html) {
  113. if (is_array($html)) {
  114. $html = drupal_render($html);
  115. }
  116. return array(
  117. 'command' => 'modal_display',
  118. 'title' => $title,
  119. 'output' => $html,
  120. );
  121. }
  122. /**
  123. * Dismiss the modal.
  124. */
  125. function ctools_modal_command_dismiss() {
  126. return array(
  127. 'command' => 'modal_dismiss',
  128. );
  129. }
  130. /**
  131. * Display loading screen in the modal
  132. */
  133. function ctools_modal_command_loading() {
  134. return array(
  135. 'command' => 'modal_loading',
  136. );
  137. }
  138. /**
  139. * Render an image as a button link. This will automatically apply an AJAX class
  140. * to the link and add the appropriate javascript to make this happen.
  141. *
  142. * @param $image
  143. * The path to an image to use that will be sent to theme('image') for rendering.
  144. * @param $dest
  145. * The destination of the link.
  146. * @param $alt
  147. * The alt text of the link.
  148. * @param $class
  149. * Any class to apply to the link. @todo this should be a options array.
  150. */
  151. function ctools_modal_image_button($image, $dest, $alt, $class = '') {
  152. return ctools_ajax_text_button(theme('image', array('path' => $image)), $dest, $alt, $class, 'ctools-use-modal');
  153. }
  154. /**
  155. * Render text as a link. This will automatically apply an AJAX class
  156. * to the link and add the appropriate javascript to make this happen.
  157. *
  158. * Note: 'html' => true so be sure any text is vetted! Chances are these kinds of buttons will
  159. * not use user input so this is a very minor concern.
  160. *
  161. * @param $text
  162. * The text that will be displayed as the link.
  163. * @param $dest
  164. * The destination of the link.
  165. * @param $alt
  166. * The alt text of the link.
  167. * @param $class
  168. * Any class to apply to the link. @todo this should be a options array.
  169. */
  170. function ctools_modal_text_button($text, $dest, $alt, $class = '') {
  171. return ctools_ajax_text_button($text, $dest, $alt, $class, 'ctools-use-modal');
  172. }
  173. /**
  174. * Wrap a form so that we can use it properly with AJAX. Essentially if the
  175. * form wishes to render, it automatically does that, otherwise it returns
  176. * the render array so we can see submission results.
  177. * @param array $form
  178. * An associative array containing the structure of the form.
  179. * @param array $form_state
  180. * An associative array containing the current state of the form.
  181. * If the 'reset_html_ids' key is set to TRUE, it will prevent HTML IDs in
  182. * forms from being incremented.
  183. *
  184. * @return mixed
  185. * The output of the form, if it was rendered. If $form_state['ajax']
  186. * is set, this will use ctools_modal_form_render so it will be
  187. * a $command object suitable for ajax_render already.
  188. *
  189. * If the form was not rendered, the raw render array will be returned.
  190. *
  191. * If ajax is set the form will never be redirected.
  192. */
  193. function ctools_modal_form_wrapper($form_id, &$form_state) {
  194. // Since this will run again on form rebuild while still in the modal, prevent
  195. // form IDs from being incremented.
  196. // @todo https://drupal.org/node/1305882
  197. if (!empty($form_state['reset_html_ids']) && !empty($_POST['ajax_html_ids'])) {
  198. unset($_POST['ajax_html_ids']);
  199. }
  200. // This won't override settings already in.
  201. $form_state += array(
  202. 're_render' => FALSE,
  203. 'no_redirect' => !empty($form_state['ajax']),
  204. );
  205. $output = drupal_build_form($form_id, $form_state);
  206. if (!empty($form_state['ajax']) && (!$form_state['executed'] || $form_state['rebuild'])) {
  207. return ctools_modal_form_render($form_state, $output);
  208. }
  209. return $output;
  210. }
  211. /**
  212. * Render a form into an AJAX display.
  213. */
  214. function ctools_modal_form_render($form_state, $output) {
  215. if (is_array($output)) {
  216. $output = drupal_render($output);
  217. }
  218. $title = empty($form_state['title']) ? drupal_get_title() : $form_state['title'];
  219. // If there are messages for the form, render them.
  220. if ($messages = theme('status_messages')) {
  221. $output = $messages . $output;
  222. }
  223. $commands = array();
  224. // If the form has not yet been rendered, render it.
  225. $commands[] = ctools_modal_command_display($title, $output);
  226. return $commands;
  227. }
  228. /**
  229. * Perform a simple modal render and immediately exit.
  230. *
  231. * This is primarily used for error displays, since usually modals will
  232. * contain forms.
  233. */
  234. function ctools_modal_render($title, $output) {
  235. $commands = array();
  236. $commands[] = ctools_modal_command_display($title, $output);
  237. print ajax_render($commands);
  238. }