1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Handles the server side AJAX interactions of Views.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* @defgroup ajax Views AJAX library
|
10
|
* @{
|
11
|
* Handles the server side AJAX interactions of Views.
|
12
|
*/
|
13
|
|
14
|
/**
|
15
|
* Menu callback to load a view via AJAX.
|
16
|
*/
|
17
|
function views_ajax() {
|
18
|
if (isset($_REQUEST['view_name']) && isset($_REQUEST['view_display_id'])) {
|
19
|
$name = $_REQUEST['view_name'];
|
20
|
$display_id = $_REQUEST['view_display_id'];
|
21
|
$args = isset($_REQUEST['view_args']) && $_REQUEST['view_args'] !== '' ? explode('/', $_REQUEST['view_args']) : array();
|
22
|
$path = isset($_REQUEST['view_path']) ? rawurldecode($_REQUEST['view_path']) : NULL;
|
23
|
$dom_id = isset($_REQUEST['view_dom_id']) ? preg_replace('/[^a-zA-Z0-9_-]+/', '-', $_REQUEST['view_dom_id']) : NULL;
|
24
|
$pager_element = isset($_REQUEST['pager_element']) ? intval($_REQUEST['pager_element']) : NULL;
|
25
|
|
26
|
$commands = array();
|
27
|
|
28
|
// Remove all of this stuff from $_GET so it doesn't end up in pagers and tablesort URLs.
|
29
|
foreach (array('view_name', 'view_display_id', 'view_args', 'view_path', 'view_dom_id', 'pager_element', 'view_base_path', 'ajax_html_ids', 'ajax_page_state') as $key) {
|
30
|
if (isset($_GET[$key])) {
|
31
|
unset($_GET[$key]);
|
32
|
}
|
33
|
if (isset($_REQUEST[$key])) {
|
34
|
unset($_REQUEST[$key]);
|
35
|
}
|
36
|
if (isset($_POST[$key])) {
|
37
|
unset($_POST[$key]);
|
38
|
}
|
39
|
}
|
40
|
|
41
|
// Load the view.
|
42
|
$view = views_get_view($name);
|
43
|
if ($view && $view->access($display_id)) {
|
44
|
// Fix 'q' for paging.
|
45
|
if (!empty($path)) {
|
46
|
$_GET['q'] = $path;
|
47
|
}
|
48
|
|
49
|
// Add all $_POST data, because AJAX is always a post and many things,
|
50
|
// such as tablesorts, exposed filters and paging assume $_GET.
|
51
|
$_GET = $_POST + $_GET;
|
52
|
|
53
|
// Overwrite the destination.
|
54
|
// @see drupal_get_destination()
|
55
|
$origin_destination = $path;
|
56
|
$query = drupal_http_build_query($_REQUEST);
|
57
|
if ($query != '') {
|
58
|
$origin_destination .= '?' . $query;
|
59
|
}
|
60
|
$destination = &drupal_static('drupal_get_destination');
|
61
|
$destination = array('destination' => $origin_destination);
|
62
|
|
63
|
// Override the display's pager_element with the one actually used.
|
64
|
if (isset($pager_element)) {
|
65
|
$commands[] = views_ajax_command_scroll_top('.view-dom-id-' . $dom_id);
|
66
|
$view->display[$display_id]->handler->set_option('pager_element', $pager_element);
|
67
|
}
|
68
|
// Reuse the same DOM id so it matches that in Drupal.settings.
|
69
|
$view->dom_id = $dom_id;
|
70
|
|
71
|
$commands[] = ajax_command_replace('.view-dom-id-' . $dom_id, $view->preview($display_id, $args));
|
72
|
}
|
73
|
drupal_alter('views_ajax_data', $commands, $view);
|
74
|
return array('#type' => 'ajax', '#commands' => $commands);
|
75
|
}
|
76
|
}
|
77
|
|
78
|
/**
|
79
|
* Creates a Drupal AJAX 'viewsSetForm' command.
|
80
|
*
|
81
|
* @param $output
|
82
|
* The form to display in the modal.
|
83
|
* @param $title
|
84
|
* The title.
|
85
|
* @param $url
|
86
|
* An optional URL.
|
87
|
*
|
88
|
* @return
|
89
|
* An array suitable for use with the ajax_render() function.
|
90
|
*/
|
91
|
function views_ajax_command_set_form($output, $title, $url = NULL) {
|
92
|
$command = array(
|
93
|
'command' => 'viewsSetForm',
|
94
|
'output' => $output,
|
95
|
'title' => $title,
|
96
|
);
|
97
|
if (isset($url)) {
|
98
|
$command['url'] = $url;
|
99
|
}
|
100
|
return $command;
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* Creates a Drupal AJAX 'viewsDismissForm' command.
|
105
|
*
|
106
|
* @return
|
107
|
* An array suitable for use with the ajax_render() function.
|
108
|
*/
|
109
|
function views_ajax_command_dismiss_form() {
|
110
|
$command = array(
|
111
|
'command' => 'viewsDismissForm',
|
112
|
);
|
113
|
return $command;
|
114
|
}
|
115
|
|
116
|
/**
|
117
|
* Creates a Drupal AJAX 'viewsHilite' command.
|
118
|
*
|
119
|
* @param $selector
|
120
|
* The selector to highlight
|
121
|
*
|
122
|
* @return
|
123
|
* An array suitable for use with the ajax_render() function.
|
124
|
*/
|
125
|
function views_ajax_command_hilite($selector) {
|
126
|
return array(
|
127
|
'command' => 'viewsHilite',
|
128
|
'selector' => $selector,
|
129
|
);
|
130
|
}
|
131
|
|
132
|
/**
|
133
|
* Creates a Drupal AJAX 'addTab' command.
|
134
|
*
|
135
|
* @param $id
|
136
|
* The DOM ID.
|
137
|
* @param $title
|
138
|
* The title.
|
139
|
* @param $body
|
140
|
* The body.
|
141
|
*
|
142
|
* @return
|
143
|
* An array suitable for use with the ajax_render() function.
|
144
|
*/
|
145
|
function views_ajax_command_add_tab($id, $title, $body) {
|
146
|
$command = array(
|
147
|
'command' => 'viewsAddTab',
|
148
|
'id' => $id,
|
149
|
'title' => $title,
|
150
|
'body' => $body,
|
151
|
);
|
152
|
return $command;
|
153
|
}
|
154
|
|
155
|
/**
|
156
|
* Scroll to top of the current view.
|
157
|
*
|
158
|
* @return
|
159
|
* An array suitable for use with the ajax_render() function.
|
160
|
*/
|
161
|
function views_ajax_command_scroll_top($selector) {
|
162
|
$command = array(
|
163
|
'command' => 'viewsScrollTop',
|
164
|
'selector' => $selector,
|
165
|
);
|
166
|
return $command;
|
167
|
}
|
168
|
|
169
|
/**
|
170
|
* Shows Save and Cancel buttons.
|
171
|
*
|
172
|
* @return
|
173
|
* An array suitable for use with the ajax_render() function.
|
174
|
*/
|
175
|
function views_ajax_command_show_buttons() {
|
176
|
$command = array(
|
177
|
'command' => 'viewsShowButtons',
|
178
|
);
|
179
|
return $command;
|
180
|
}
|
181
|
|
182
|
/**
|
183
|
* Trigger the Views live preview.
|
184
|
*
|
185
|
* @return
|
186
|
* An array suitable for use with the ajax_render() function.
|
187
|
*/
|
188
|
function views_ajax_command_trigger_preview() {
|
189
|
$command = array(
|
190
|
'command' => 'viewsTriggerPreview',
|
191
|
);
|
192
|
return $command;
|
193
|
}
|
194
|
|
195
|
/**
|
196
|
* Replace the page title.
|
197
|
*
|
198
|
* @return
|
199
|
* An array suitable for use with the ajax_render() function.
|
200
|
*/
|
201
|
function views_ajax_command_replace_title($title) {
|
202
|
$command = array(
|
203
|
'command' => 'viewsReplaceTitle',
|
204
|
'title' => $title,
|
205
|
'siteName' => variable_get('site_name', 'Drupal'),
|
206
|
);
|
207
|
return $command;
|
208
|
}
|
209
|
|
210
|
/**
|
211
|
* Return an AJAX error.
|
212
|
*/
|
213
|
function views_ajax_error($message) {
|
214
|
$commands = array();
|
215
|
$commands[] = views_ajax_command_set_form($message, t('Error'));
|
216
|
return $commands;
|
217
|
}
|
218
|
|
219
|
/**
|
220
|
* Wrapper around drupal_build_form to handle some AJAX stuff automatically.
|
221
|
* This makes some assumptions about the client.
|
222
|
*/
|
223
|
function views_ajax_form_wrapper($form_id, &$form_state) {
|
224
|
ctools_include('dependent');
|
225
|
|
226
|
// This won't override settings already in.
|
227
|
$form_state += array(
|
228
|
'rerender' => FALSE,
|
229
|
'no_redirect' => !empty($form_state['ajax']),
|
230
|
'no_cache' => TRUE,
|
231
|
'build_info' => array(
|
232
|
'args' => array(),
|
233
|
),
|
234
|
);
|
235
|
|
236
|
$form = drupal_build_form($form_id, $form_state);
|
237
|
$output = drupal_render($form);
|
238
|
|
239
|
// These forms have the title built in, so set the title here:
|
240
|
if (empty($form_state['ajax']) && !empty($form_state['title'])) {
|
241
|
drupal_set_title($form_state['title']);
|
242
|
drupal_add_css(drupal_get_path('module', 'views_ui') . '/css/views-admin.css');
|
243
|
}
|
244
|
|
245
|
if (!empty($form_state['ajax']) && (empty($form_state['executed']) || !empty($form_state['rerender']))) {
|
246
|
// If the form didn't execute and we're using ajax, build up a
|
247
|
// Ajax command list to execute.
|
248
|
$commands = array();
|
249
|
|
250
|
$display = '';
|
251
|
if ($messages = theme('status_messages')) {
|
252
|
$display = '<div class="views-messages">' . $messages . '</div>';
|
253
|
}
|
254
|
$display .= $output;
|
255
|
|
256
|
$title = empty($form_state['title']) ? '' : $form_state['title'];
|
257
|
if (!empty($form_state['help_topic'])) {
|
258
|
$module = !empty($form_state['help_module']) ? $form_state['help_module'] : 'views';
|
259
|
if (module_exists('advanced_help')) {
|
260
|
$title = theme('advanced_help_topic', array('module' => $module, 'topic' => $form_state['help_topic'])) . $title;
|
261
|
}
|
262
|
}
|
263
|
|
264
|
$url = empty($form_state['url']) ? url($_GET['q'], array('absolute' => TRUE)) : $form_state['url'];
|
265
|
|
266
|
$commands[] = views_ajax_command_set_form($display, $title, $url);
|
267
|
|
268
|
if (!empty($form_state['#section'])) {
|
269
|
$commands[] = views_ajax_command_hilite('.' . drupal_clean_css_identifier($form_state['#section']));
|
270
|
}
|
271
|
|
272
|
return $commands;
|
273
|
}
|
274
|
|
275
|
// These forms have the title built in, so set the title here:
|
276
|
if (empty($form_state['ajax']) && !empty($form_state['title'])) {
|
277
|
drupal_set_title($form_state['title']);
|
278
|
}
|
279
|
|
280
|
return $output;
|
281
|
}
|
282
|
|
283
|
|
284
|
/**
|
285
|
* Page callback for views user autocomplete
|
286
|
*/
|
287
|
function views_ajax_autocomplete_user($string = '') {
|
288
|
// The user enters a comma-separated list of user name. We only autocomplete the last name.
|
289
|
$array = drupal_explode_tags($string);
|
290
|
|
291
|
// Fetch last name
|
292
|
$last_string = trim(array_pop($array));
|
293
|
$matches = array();
|
294
|
if ($last_string != '') {
|
295
|
$prefix = count($array) ? implode(', ', $array) . ', ' : '';
|
296
|
|
297
|
if (strpos('anonymous', strtolower($last_string)) !== FALSE) {
|
298
|
$matches[$prefix . 'Anonymous'] = 'Anonymous';
|
299
|
}
|
300
|
|
301
|
$result = db_select('users', 'u')
|
302
|
->fields('u', array('uid', 'name'))
|
303
|
->condition('u.name', db_like($last_string) . '%', 'LIKE')
|
304
|
->range(0, 10)
|
305
|
->execute()
|
306
|
->fetchAllKeyed();
|
307
|
|
308
|
foreach ($result as $account) {
|
309
|
$n = $account;
|
310
|
// Commas and quotes in terms are special cases, so encode 'em.
|
311
|
if (strpos($account, ',') !== FALSE || strpos($account, '"') !== FALSE) {
|
312
|
$n = '"' . str_replace('"', '""', $account) . '"';
|
313
|
}
|
314
|
$matches[$prefix . $n] = check_plain($account);
|
315
|
}
|
316
|
}
|
317
|
|
318
|
drupal_json_output($matches);
|
319
|
}
|
320
|
|
321
|
/**
|
322
|
* Page callback for views taxonomy autocomplete.
|
323
|
*
|
324
|
* @param $vid
|
325
|
* The vocabulary id of the tags which should be returned.
|
326
|
*
|
327
|
* @param $tags_typed
|
328
|
* The typed string of the user.
|
329
|
*
|
330
|
* @see taxonomy_autocomplete()
|
331
|
*/
|
332
|
function views_ajax_autocomplete_taxonomy($vid, $tags_typed = '') {
|
333
|
// The user enters a comma-separated list of tags. We only autocomplete the last tag.
|
334
|
$tags_typed = drupal_explode_tags($tags_typed);
|
335
|
$tag_last = drupal_strtolower(array_pop($tags_typed));
|
336
|
|
337
|
$matches = array();
|
338
|
if ($tag_last != '') {
|
339
|
|
340
|
$query = db_select('taxonomy_term_data', 't');
|
341
|
$query->addTag('translatable');
|
342
|
$query->addTag('term_access');
|
343
|
|
344
|
// Do not select already entered terms.
|
345
|
if (!empty($tags_typed)) {
|
346
|
$query->condition('t.name', $tags_typed, 'NOT IN');
|
347
|
}
|
348
|
// Select rows that match by term name.
|
349
|
$tags_return = $query
|
350
|
->fields('t', array('tid', 'name'))
|
351
|
->condition('t.vid', $vid)
|
352
|
->condition('t.name', '%' . db_like($tag_last) . '%', 'LIKE')
|
353
|
->range(0, 10)
|
354
|
->execute()
|
355
|
->fetchAllKeyed();
|
356
|
|
357
|
$prefix = count($tags_typed) ? drupal_implode_tags($tags_typed) . ', ' : '';
|
358
|
|
359
|
$term_matches = array();
|
360
|
foreach ($tags_return as $tid => $name) {
|
361
|
$n = $name;
|
362
|
// Term names containing commas or quotes must be wrapped in quotes.
|
363
|
if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
|
364
|
$n = '"' . str_replace('"', '""', $name) . '"';
|
365
|
}
|
366
|
// Add term name to list of matches.
|
367
|
$term_matches[$prefix . $n] = check_plain($name);
|
368
|
}
|
369
|
}
|
370
|
|
371
|
drupal_json_output($term_matches);
|
372
|
}
|
373
|
|
374
|
/**
|
375
|
* @}
|
376
|
*/
|