modal.inc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. * so we can see submission results.
  177. *
  178. * @return
  179. * The output of the form, if it was rendered. If $form_state['ajax']
  180. * is set, this will use ctools_modal_form_render so it will be
  181. * a $command object suitable for ajax_render already.
  182. *
  183. * The return will be NULL if the form was successfully submitted unless
  184. * you specifically set re_render = TRUE. If ajax is set the
  185. * form will never be redirected.
  186. */
  187. function ctools_modal_form_wrapper($form_id, &$form_state) {
  188. // This won't override settings already in.
  189. $form_state += array(
  190. 're_render' => FALSE,
  191. 'no_redirect' => !empty($form_state['ajax']),
  192. );
  193. $output = drupal_build_form($form_id, $form_state);
  194. if (!empty($form_state['ajax']) && (!$form_state['executed'] || $form_state['rebuild'])) {
  195. return ctools_modal_form_render($form_state, $output);
  196. }
  197. return $output;
  198. }
  199. /**
  200. * Render a form into an AJAX display.
  201. */
  202. function ctools_modal_form_render($form_state, $output) {
  203. if (is_array($output)) {
  204. $output = drupal_render($output);
  205. }
  206. $title = empty($form_state['title']) ? drupal_get_title() : $form_state['title'];
  207. // If there are messages for the form, render them.
  208. if ($messages = theme('status_messages')) {
  209. $output = $messages . $output;
  210. }
  211. $commands = array();
  212. // If the form has not yet been rendered, render it.
  213. $commands[] = ctools_modal_command_display($title, $output);
  214. return $commands;
  215. }
  216. /**
  217. * Perform a simple modal render and immediately exit.
  218. *
  219. * This is primarily used for error displays, since usually modals will
  220. * contain forms.
  221. */
  222. function ctools_modal_render($title, $output) {
  223. $commands = array();
  224. $commands[] = ctools_modal_command_display($title, $output);
  225. print ajax_render($commands);
  226. }