Project

General

Profile

Paste
Download (10.4 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / views / includes / ajax.inc @ d719f12f

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(drupal_get_query_parameters());
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
 * @param bool $changed
173
 *   Whether of not the view has changed.
174
 *
175
 * @return
176
 *   An array suitable for use with the ajax_render() function.
177
 */
178
function views_ajax_command_show_buttons($changed) {
179
  $command = array(
180
    'command' => 'viewsShowButtons',
181
    'changed' => (bool) $changed,
182
  );
183
  return $command;
184
}
185

    
186
/**
187
 * Trigger the Views live preview.
188
 *
189
 * @return
190
 *   An array suitable for use with the ajax_render() function.
191
 */
192
function views_ajax_command_trigger_preview() {
193
  $command = array(
194
    'command' => 'viewsTriggerPreview',
195
  );
196
  return $command;
197
}
198

    
199
/**
200
 * Replace the page title.
201
 *
202
 * @return
203
 *   An array suitable for use with the ajax_render() function.
204
 */
205
function views_ajax_command_replace_title($title) {
206
  $command = array(
207
    'command' => 'viewsReplaceTitle',
208
    'title' => $title,
209
    'siteName' => variable_get('site_name', 'Drupal'),
210
  );
211
  return $command;
212
}
213

    
214
/**
215
 * Return an AJAX error.
216
 */
217
function views_ajax_error($message) {
218
  $commands = array();
219
  $commands[] = views_ajax_command_set_form($message, t('Error'));
220
  return $commands;
221
}
222

    
223
/**
224
 * Wrapper around drupal_build_form to handle some AJAX stuff automatically.
225
 * This makes some assumptions about the client.
226
 */
227
function views_ajax_form_wrapper($form_id, &$form_state) {
228
  ctools_include('dependent');
229

    
230
  // This won't override settings already in.
231
  $form_state += array(
232
    'rerender' => FALSE,
233
    'no_redirect' => !empty($form_state['ajax']),
234
    'no_cache' => TRUE,
235
    'build_info' => array(
236
      'args' => array(),
237
    ),
238
  );
239

    
240
  $form = drupal_build_form($form_id, $form_state);
241
  $output = drupal_render($form);
242

    
243
  // These forms have the title built in, so set the title here:
244
  if (empty($form_state['ajax']) && !empty($form_state['title'])) {
245
    drupal_set_title($form_state['title']);
246
    drupal_add_css(drupal_get_path('module', 'views_ui') . '/css/views-admin.css');
247
  }
248

    
249
  if (!empty($form_state['ajax']) && (empty($form_state['executed']) || !empty($form_state['rerender']))) {
250
    // If the form didn't execute and we're using ajax, build up a
251
    // Ajax command list to execute.
252
    $commands = array();
253

    
254
    $display = '';
255
    if ($messages = theme('status_messages')) {
256
      $display = '<div class="views-messages">' . $messages . '</div>';
257
    }
258
    $display .= $output;
259

    
260
    $title = empty($form_state['title']) ? '' : $form_state['title'];
261
    if (!empty($form_state['help_topic'])) {
262
      $module = !empty($form_state['help_module']) ? $form_state['help_module'] : 'views';
263
      if (module_exists('advanced_help')) {
264
        $title = theme('advanced_help_topic', array('module' => $module, 'topic' => $form_state['help_topic'])) . $title;
265
      }
266
    }
267

    
268
    $url = empty($form_state['url']) ? url($_GET['q'], array('absolute' => TRUE)) : $form_state['url'];
269

    
270
    $commands[] = views_ajax_command_set_form($display, $title, $url);
271

    
272
    if (!empty($form_state['#section'])) {
273
      $commands[] = views_ajax_command_hilite('.' . drupal_clean_css_identifier($form_state['#section']));
274
    }
275

    
276
    return $commands;
277
  }
278

    
279
  // These forms have the title built in, so set the title here:
280
  if (empty($form_state['ajax']) && !empty($form_state['title'])) {
281
    drupal_set_title($form_state['title']);
282
  }
283

    
284
  return $output;
285
}
286

    
287

    
288
/**
289
 * Page callback for views user autocomplete
290
 */
291
function views_ajax_autocomplete_user($string = '') {
292
  // The user enters a comma-separated list of user name. We only autocomplete the last name.
293
  $array = drupal_explode_tags($string);
294

    
295
  // Fetch last name
296
  $last_string = trim(array_pop($array));
297
  $matches = array();
298
  if ($last_string != '') {
299
    $prefix = count($array) ? implode(', ', $array) . ', ' : '';
300

    
301
    if (strpos('anonymous', strtolower($last_string)) !== FALSE) {
302
      $matches[$prefix . 'Anonymous'] = 'Anonymous';
303
    }
304

    
305
    $result = db_select('users', 'u')
306
      ->fields('u', array('uid', 'name'))
307
      ->condition('u.name', db_like($last_string) . '%', 'LIKE')
308
      ->range(0, 10)
309
      ->execute()
310
      ->fetchAllKeyed();
311

    
312
    foreach ($result as $account) {
313
      $n = $account;
314
      // Commas and quotes in terms are special cases, so encode 'em.
315
      if (strpos($account, ',') !== FALSE || strpos($account, '"') !== FALSE) {
316
        $n = '"' . str_replace('"', '""', $account) . '"';
317
      }
318
      $matches[$prefix . $n] = check_plain($account);
319
    }
320
  }
321

    
322
  drupal_json_output($matches);
323
}
324

    
325
/**
326
 * Page callback for views taxonomy autocomplete.
327
 *
328
 * @param $vid
329
 *   The vocabulary id of the tags which should be returned.
330
 *
331
 * @param $tags_typed
332
 *   The typed string of the user.
333
 *
334
 * @see taxonomy_autocomplete()
335
 */
336
function views_ajax_autocomplete_taxonomy($vid, $tags_typed = '') {
337
  // The user enters a comma-separated list of tags. We only autocomplete the last tag.
338
  $tags_typed = drupal_explode_tags($tags_typed);
339
  $tag_last = drupal_strtolower(array_pop($tags_typed));
340

    
341
  $matches = array();
342
  if ($tag_last != '') {
343

    
344
    $query = db_select('taxonomy_term_data', 't');
345
    $query->addTag('translatable');
346
    $query->addTag('term_access');
347

    
348
    // Do not select already entered terms.
349
    if (!empty($tags_typed)) {
350
      $query->condition('t.name', $tags_typed, 'NOT IN');
351
    }
352
    // Select rows that match by term name.
353
    $tags_return = $query
354
      ->fields('t', array('tid', 'name'))
355
      ->condition('t.vid', $vid)
356
      ->condition('t.name', '%' . db_like($tag_last) . '%', 'LIKE')
357
      ->range(0, 10)
358
      ->execute()
359
      ->fetchAllKeyed();
360

    
361
    $prefix = count($tags_typed) ? drupal_implode_tags($tags_typed) . ', ' : '';
362

    
363
    $term_matches = array();
364
    foreach ($tags_return as $tid => $name) {
365
      $n = $name;
366
      // Term names containing commas or quotes must be wrapped in quotes.
367
      if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
368
        $n = '"' . str_replace('"', '""', $name) . '"';
369
      }
370
      // Add term name to list of matches.
371
      $term_matches[$prefix . $n] = check_plain($name);
372
    }
373
  }
374

    
375
  drupal_json_output($term_matches);
376
}
377

    
378
/**
379
 * @}
380
 */