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