Projet

Général

Profil

Paste
Télécharger (10,7 ko) Statistiques
| Branche: | Révision:

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

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) && $view->set_display($display_id) && $view->display_handler->use_ajax()) {
44
      // Fix 'q' for paging.
45
      if (!empty($path)) {
46
        $_GET['q'] = $path;
47
      }
48

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

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

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

    
74
      $commands[] = ajax_command_replace('.view-dom-id-' . $dom_id, $view->preview($display_id, $args));
75
    }
76
    drupal_alter('views_ajax_data', $commands, $view);
77
    return array('#type' => 'ajax', '#commands' => $commands);
78
  }
79
}
80

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

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

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

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

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

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

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

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

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

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

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

    
243
  $form = drupal_build_form($form_id, $form_state);
244
  $output = drupal_render($form);
245

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

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

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

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

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

    
273
    $commands[] = views_ajax_command_set_form($display, $title, $url);
274

    
275
    if (!empty($form_state['#section'])) {
276
      $commands[] = views_ajax_command_hilite('.' . drupal_clean_css_identifier($form_state['#section']));
277
    }
278

    
279
    return $commands;
280
  }
281

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

    
287
  return $output;
288
}
289

    
290

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

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

    
304
    if (strpos('anonymous', strtolower($last_string)) !== FALSE) {
305
      $matches[$prefix . 'Anonymous'] = 'Anonymous';
306
    }
307

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

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

    
325
  drupal_json_output($matches);
326
}
327

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

    
344
  $matches = array();
345
  if ($tag_last != '') {
346

    
347
    $query = db_select('taxonomy_term_data', 't');
348
    $query->addTag('translatable');
349
    $query->addTag('taxonomy_term_access');
350

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

    
364
    $prefix = count($tags_typed) ? drupal_implode_tags($tags_typed) . ', ' : '';
365

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

    
378
  drupal_json_output($term_matches);
379
}
380

    
381
/**
382
 * @}
383
 */