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