annotate sites/all/modules/ctools/includes/modal.inc @ 11:b0ee71395280

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