Projet

Général

Profil

Paste
Télécharger (11,3 ko) Statistiques
| Branche: | Révision:

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

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('/', htmlspecialchars_decode($_REQUEST['view_args'], ENT_QUOTES)) : array();
22
    $path = isset($_REQUEST['view_path']) ? htmlspecialchars_decode($_REQUEST['view_path'], ENT_QUOTES) : 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
29
    // tablesort URLs; do not modify $_POST itself but make a new "clean"
30
    // copy to merge it with $_GET later.
31
    $cleaned_post = $_POST;
32
    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) {
33
      if (isset($_GET[$key])) {
34
        unset($_GET[$key]);
35
      }
36
      if (isset($_REQUEST[$key])) {
37
        unset($_REQUEST[$key]);
38
      }
39
      if (isset($cleaned_post[$key])) {
40
        unset($cleaned_post[$key]);
41
      }
42
    }
43

    
44
    // Load the view.
45
    $view = views_get_view($name);
46
    if ($view && $view->access($display_id) && $view->set_display($display_id) && $view->display_handler->use_ajax()) {
47
      // Fix 'q' for paging.
48
      if (!empty($path)) {
49
        $_GET['q'] = $path;
50
      }
51

    
52
      // If page parameter is in the $_POST exclude it from $_GET, otherwise
53
      // support views_ajax requests using $_GET.
54
      $exclude = isset($_POST['page']) ? array('page') : array();
55
      // Add all $_POST data to $_GET as many things, such as tablesorts,
56
      // exposed filters and paging assume $_GET.
57
      $_GET = $cleaned_post + drupal_get_query_parameters($_GET, $exclude);
58

    
59
      // Overwrite the destination.
60
      // @see drupal_get_destination()
61
      $origin_destination = $path;
62
      $query = drupal_http_build_query(drupal_get_query_parameters());
63
      if ($query != '') {
64
        $origin_destination .= '?' . $query;
65
      }
66
      $destination = &drupal_static('drupal_get_destination');
67
      $destination = array('destination' => $origin_destination);
68

    
69
      // Override the display's pager_element with the one actually used.
70
      if (isset($pager_element)) {
71
        $commands[] = views_ajax_command_scroll_top('.view-dom-id-' . $dom_id);
72
        $view->display[$display_id]->handler->set_option('pager_element', $pager_element);
73
      }
74
      // Reuse the same DOM id so it matches that in Drupal.settings.
75
      $view->dom_id = $dom_id;
76

    
77
      // Always return HTML with the same DOM ID that was sent by the browser.
78
      $commands[] = ajax_command_replace('.view-dom-id-' . $dom_id, preg_replace('/view-dom-id-[a-zA-Z0-9_-]+/', 'view-dom-id-' . $view->dom_id, $view->preview($display_id, $args), 1));
79
    }
80
    drupal_alter('views_ajax_data', $commands, $view);
81
    return array('#type' => 'ajax', '#commands' => $commands);
82
  }
83
}
84

    
85
/**
86
 * Creates a Drupal AJAX 'viewsSetForm' command.
87
 *
88
 * @param string $output
89
 *   The form to display in the modal.
90
 * @param string $title
91
 *   The title.
92
 * @param string $url
93
 *   An optional URL.
94
 *
95
 * @return array
96
 *   An array suitable for use with the ajax_render() function.
97
 */
98
function views_ajax_command_set_form($output, $title, $url = NULL) {
99
  $command = array(
100
    'command' => 'viewsSetForm',
101
    'output' => $output,
102
    'title' => $title,
103
  );
104
  if (isset($url)) {
105
    $command['url'] = $url;
106
  }
107
  return $command;
108
}
109

    
110
/**
111
 * Creates a Drupal AJAX 'viewsDismissForm' command.
112
 *
113
 * @return array
114
 *   An array suitable for use with the ajax_render() function.
115
 */
116
function views_ajax_command_dismiss_form() {
117
  $command = array(
118
    'command' => 'viewsDismissForm',
119
  );
120
  return $command;
121
}
122

    
123
/**
124
 * Creates a Drupal AJAX 'viewsHilite' command.
125
 *
126
 * @param string $selector
127
 *   The selector to highlight.
128
 *
129
 * @return
130
 *   An array suitable for use with the ajax_render() function.
131
 */
132
function views_ajax_command_hilite($selector) {
133
  return array(
134
    'command' => 'viewsHilite',
135
    'selector' => $selector,
136
  );
137
}
138

    
139
/**
140
 * Creates a Drupal AJAX 'addTab' command.
141
 *
142
 * @param string $id
143
 *   The DOM ID.
144
 * @param string $title
145
 *   The title.
146
 * @param string $body
147
 *   The body.
148
 *
149
 * @return array
150
 *   An array suitable for use with the ajax_render() function.
151
 */
152
function views_ajax_command_add_tab($id, $title, $body) {
153
  $command = array(
154
    'command' => 'viewsAddTab',
155
    'id' => $id,
156
    'title' => $title,
157
    'body' => $body,
158
  );
159
  return $command;
160
}
161

    
162
/**
163
 * Scroll to top of the current view.
164
 *
165
 * @return array
166
 *   An array suitable for use with the ajax_render() function.
167
 */
168
function views_ajax_command_scroll_top($selector) {
169
  $command = array(
170
    'command' => 'viewsScrollTop',
171
    'selector' => $selector,
172
  );
173
  return $command;
174
}
175

    
176
/**
177
 * Shows Save and Cancel buttons.
178
 *
179
 * @param bool $changed
180
 *   Whether of not the view has changed.
181
 *
182
 * @return array
183
 *   An array suitable for use with the ajax_render() function.
184
 */
185
function views_ajax_command_show_buttons($changed) {
186
  $command = array(
187
    'command' => 'viewsShowButtons',
188
    'changed' => (bool) $changed,
189
  );
190
  return $command;
191
}
192

    
193
/**
194
 * Trigger the Views live preview.
195
 *
196
 * @return array
197
 *   An array suitable for use with the ajax_render() function.
198
 */
199
function views_ajax_command_trigger_preview() {
200
  $command = array(
201
    'command' => 'viewsTriggerPreview',
202
  );
203
  return $command;
204
}
205

    
206
/**
207
 * Replace the page title.
208
 *
209
 * @return array
210
 *   An array suitable for use with the ajax_render() function.
211
 */
212
function views_ajax_command_replace_title($title) {
213
  $command = array(
214
    'command' => 'viewsReplaceTitle',
215
    'title' => $title,
216
    'siteName' => variable_get('site_name', 'Drupal'),
217
  );
218
  return $command;
219
}
220

    
221
/**
222
 * Return an AJAX error.
223
 *
224
 * @param string $message
225
 *   The message to display.
226
 *
227
 * @return array
228
 *   An array suitable for use with the ajax_render() function.
229
 */
230
function views_ajax_error($message) {
231
  $commands = array();
232
  $commands[] = views_ajax_command_set_form($message, t('Error'));
233
  return $commands;
234
}
235

    
236
/**
237
 * Wrapper around drupal_build_form to handle some AJAX stuff automatically.
238
 *
239
 * This makes some assumptions about the client.
240
 */
241
function views_ajax_form_wrapper($form_id, &$form_state) {
242
  ctools_include('dependent');
243

    
244
  // This won't override settings already in.
245
  $form_state += array(
246
    'rerender' => FALSE,
247
    'no_redirect' => !empty($form_state['ajax']),
248
    'no_cache' => TRUE,
249
    'build_info' => array(
250
      'args' => array(),
251
    ),
252
  );
253

    
254
  $form = drupal_build_form($form_id, $form_state);
255
  $output = drupal_render($form);
256

    
257
  // These forms have the title built in, so set the title here.
258
  if (empty($form_state['ajax']) && !empty($form_state['title'])) {
259
    drupal_set_title($form_state['title']);
260
    drupal_add_css(drupal_get_path('module', 'views_ui') . '/css/views-admin.css');
261
  }
262

    
263
  if (!empty($form_state['ajax']) && (empty($form_state['executed']) || !empty($form_state['rerender']))) {
264
    // If the form didn't execute and we're using ajax, build up a AJAX command
265
    // list to execute.
266
    $commands = array();
267

    
268
    $display = '';
269
    if ($messages = theme('status_messages')) {
270
      $display = '<div class="views-messages">' . $messages . '</div>';
271
    }
272
    $display .= $output;
273

    
274
    $title = empty($form_state['title']) ? '' : $form_state['title'];
275
    if (!empty($form_state['help_topic'])) {
276
      $module = !empty($form_state['help_module']) ? $form_state['help_module'] : 'views';
277
      if (module_exists('advanced_help')) {
278
        $title = theme('advanced_help_topic', array('module' => $module, 'topic' => $form_state['help_topic'])) . $title;
279
      }
280
    }
281

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

    
284
    $commands[] = views_ajax_command_set_form($display, $title, $url);
285

    
286
    if (!empty($form_state['#section'])) {
287
      $commands[] = views_ajax_command_hilite('.' . drupal_clean_css_identifier($form_state['#section']));
288
    }
289

    
290
    return $commands;
291
  }
292

    
293
  // These forms have the title built in, so set the title here.
294
  if (empty($form_state['ajax']) && !empty($form_state['title'])) {
295
    drupal_set_title($form_state['title']);
296
  }
297

    
298
  return $output;
299
}
300

    
301

    
302
/**
303
 * Page callback for views user autocomplete.
304
 */
305
function views_ajax_autocomplete_user($string = '') {
306
  // The user enters a comma-separated list of user name. We only autocomplete
307
  // the last name.
308
  $array = drupal_explode_tags($string);
309

    
310
  // Fetch last name.
311
  $last_string = trim(array_pop($array));
312
  $matches = array();
313
  if ($last_string != '') {
314
    $prefix = count($array) ? implode(', ', $array) . ', ' : '';
315

    
316
    if (strpos('anonymous', strtolower($last_string)) !== FALSE) {
317
      $matches[$prefix . 'Anonymous'] = 'Anonymous';
318
    }
319

    
320
    $result = db_select('users', 'u')
321
      ->fields('u', array('uid', 'name'))
322
      ->condition('u.name', db_like($last_string) . '%', 'LIKE')
323
      ->range(0, 10)
324
      ->execute()
325
      ->fetchAllKeyed();
326

    
327
    foreach ($result as $account) {
328
      $n = $account;
329
      // Commas and quotes in terms are special cases, so encode 'em.
330
      if (strpos($account, ',') !== FALSE || strpos($account, '"') !== FALSE) {
331
        $n = '"' . str_replace('"', '""', $account) . '"';
332
      }
333
      $matches[$prefix . $n] = check_plain($account);
334
    }
335
  }
336

    
337
  drupal_json_output($matches);
338
}
339

    
340
/**
341
 * Page callback for views taxonomy autocomplete.
342
 *
343
 * @param int $vid
344
 *   The vocabulary id of the tags which should be returned.
345
 * @param string $tags_typed
346
 *   The typed string of the user.
347
 *
348
 * @see taxonomy_autocomplete()
349
 */
350
function views_ajax_autocomplete_taxonomy($vid, $tags_typed = '') {
351
  // The user enters a comma-separated list of tags. We only autocomplete the
352
  // last tag.
353
  $tags_typed = drupal_explode_tags($tags_typed);
354
  $tag_last = drupal_strtolower(array_pop($tags_typed));
355

    
356
  $matches = array();
357
  if ($tag_last != '') {
358

    
359
    $query = db_select('taxonomy_term_data', 't');
360
    $query->addTag('translatable');
361
    $query->addTag('taxonomy_term_access');
362

    
363
    // Do not select already entered terms.
364
    if (!empty($tags_typed)) {
365
      $query->condition('t.name', $tags_typed, 'NOT IN');
366
    }
367
    // Select rows that match by term name.
368
    $tags_return = $query
369
      ->fields('t', array('tid', 'name'))
370
      ->condition('t.vid', $vid)
371
      ->condition('t.name', '%' . db_like($tag_last) . '%', 'LIKE')
372
      ->range(0, 10)
373
      ->execute()
374
      ->fetchAllKeyed();
375

    
376
    $prefix = count($tags_typed) ? drupal_implode_tags($tags_typed) . ', ' : '';
377

    
378
    $term_matches = array();
379
    foreach ($tags_return as $tid => $name) {
380
      $n = $name;
381
      // Term names containing commas or quotes must be wrapped in quotes.
382
      if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
383
        $n = '"' . str_replace('"', '""', $name) . '"';
384
      }
385
      // Add term name to list of matches.
386
      $term_matches[$prefix . $n] = check_plain($name);
387
    }
388
  }
389

    
390
  drupal_json_output($term_matches);
391
}
392

    
393
/**
394
 * @}
395
 */