modal.inc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. drupal_add_library('system', 'ui');
  66. ctools_add_js('modal');
  67. ctools_add_css('modal');
  68. $done = TRUE;
  69. }
  70. /**
  71. * @todo this is deprecated
  72. */
  73. function ctools_modal_add_plugin_js($plugins) {
  74. $css = array();
  75. $js = array(drupal_get_path('module', 'ctools') . '/js/dependent.js' => TRUE);
  76. foreach ($plugins as $subtype) {
  77. if (isset($subtype['js'])) {
  78. foreach ($subtype['js'] as $file) {
  79. if (file_exists($file)) {
  80. $js[$file] = TRUE;
  81. }
  82. else if (file(exists($subtype['path'] . '/' . $file))) {
  83. $js[$subtype['path'] . '/' . $file] = TRUE;
  84. }
  85. }
  86. }
  87. if (isset($subtype['css'])) {
  88. foreach ($subtype['css'] as $file) {
  89. if (file_exists($file)) {
  90. $css[$file] = TRUE;
  91. }
  92. else if (file(exists($subtype['path'] . '/' . $file))) {
  93. $css[$subtype['path'] . '/' . $file] = TRUE;
  94. }
  95. }
  96. }
  97. }
  98. foreach (array_keys($js) as $file) {
  99. drupal_add_js($file);
  100. }
  101. foreach (array_keys($css) as $file) {
  102. drupal_add_css($file);
  103. }
  104. }
  105. /**
  106. * Place HTML within the modal.
  107. *
  108. * @param $title
  109. * The title of the modal.
  110. * @param $html
  111. * The html to place within the modal.
  112. */
  113. function ctools_modal_command_display($title, $html) {
  114. if (is_array($html)) {
  115. $html = drupal_render($html);
  116. }
  117. return array(
  118. 'command' => 'modal_display',
  119. 'title' => $title,
  120. 'output' => $html,
  121. );
  122. }
  123. /**
  124. * Dismiss the modal.
  125. */
  126. function ctools_modal_command_dismiss() {
  127. return array(
  128. 'command' => 'modal_dismiss',
  129. );
  130. }
  131. /**
  132. * Display loading screen in the modal
  133. */
  134. function ctools_modal_command_loading() {
  135. return array(
  136. 'command' => 'modal_loading',
  137. );
  138. }
  139. /**
  140. * Render an image as a button link. This will automatically apply an AJAX class
  141. * to the link and add the appropriate javascript to make this happen.
  142. *
  143. * @param $image
  144. * The path to an image to use that will be sent to theme('image') for rendering.
  145. * @param $dest
  146. * The destination of the link.
  147. * @param $alt
  148. * The alt text of the link.
  149. * @param $class
  150. * Any class to apply to the link. @todo this should be a options array.
  151. */
  152. function ctools_modal_image_button($image, $dest, $alt, $class = '') {
  153. return ctools_ajax_text_button(theme('image', array('path' => $image)), $dest, $alt, $class, 'ctools-use-modal');
  154. }
  155. /**
  156. * Render text as a link. This will automatically apply an AJAX class
  157. * to the link and add the appropriate javascript to make this happen.
  158. *
  159. * Note: 'html' => true so be sure any text is vetted! Chances are these kinds of buttons will
  160. * not use user input so this is a very minor concern.
  161. *
  162. * @param $text
  163. * The text that will be displayed as the link.
  164. * @param $dest
  165. * The destination of the link.
  166. * @param $alt
  167. * The alt text of the link.
  168. * @param $class
  169. * Any class to apply to the link. @todo this should be a options array.
  170. */
  171. function ctools_modal_text_button($text, $dest, $alt, $class = '') {
  172. return ctools_ajax_text_button($text, $dest, $alt, $class, 'ctools-use-modal');
  173. }
  174. /**
  175. * Wrap a form so that we can use it properly with AJAX. Essentially if the
  176. * form wishes to render, it automatically does that, otherwise it returns
  177. * the render array so we can see submission results.
  178. * @param array $form
  179. * An associative array containing the structure of the form.
  180. * @param array $form_state
  181. * An associative array containing the current state of the form.
  182. * If the 'reset_html_ids' key is set to TRUE, it will prevent HTML IDs in
  183. * forms from being incremented.
  184. *
  185. * @return mixed
  186. * The output of the form, if it was rendered. If $form_state['ajax']
  187. * is set, this will use ctools_modal_form_render so it will be
  188. * a $command object suitable for ajax_render already.
  189. *
  190. * If the form was not rendered, the raw render array will be returned.
  191. *
  192. * If ajax is set the form will never be redirected.
  193. */
  194. function ctools_modal_form_wrapper($form_id, &$form_state) {
  195. // Since this will run again on form rebuild while still in the modal, prevent
  196. // form IDs from being incremented.
  197. // @todo https://drupal.org/node/1305882
  198. if (!empty($form_state['reset_html_ids']) && !empty($_POST['ajax_html_ids'])) {
  199. unset($_POST['ajax_html_ids']);
  200. }
  201. // This won't override settings already in.
  202. $form_state += array(
  203. 're_render' => FALSE,
  204. 'no_redirect' => !empty($form_state['ajax']),
  205. );
  206. $output = drupal_build_form($form_id, $form_state);
  207. if (!empty($form_state['ajax']) && (!$form_state['executed'] || $form_state['rebuild'])) {
  208. return ctools_modal_form_render($form_state, $output);
  209. }
  210. return $output;
  211. }
  212. /**
  213. * Render a form into an AJAX display.
  214. */
  215. function ctools_modal_form_render($form_state, $output) {
  216. if (is_array($output)) {
  217. $output = drupal_render($output);
  218. }
  219. $title = empty($form_state['title']) ? drupal_get_title() : $form_state['title'];
  220. // If there are messages for the form, render them.
  221. if ($messages = theme('status_messages')) {
  222. $output = $messages . $output;
  223. }
  224. $commands = array();
  225. // If the form has not yet been rendered, render it.
  226. $commands[] = ctools_modal_command_display($title, $output);
  227. return $commands;
  228. }
  229. /**
  230. * Perform a simple modal render and immediately exit.
  231. *
  232. * This is primarily used for error displays, since usually modals will
  233. * contain forms.
  234. */
  235. function ctools_modal_render($title, $output) {
  236. $commands = array();
  237. $commands[] = ctools_modal_command_display($title, $output);
  238. print ajax_render($commands);
  239. }