Projet

Général

Profil

Paste
Télécharger (81,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / views.module @ ba09eb79

1
<?php
2

    
3
/**
4
 * @file
5
 * Primarily Drupal hooks and global API functions to manipulate views.
6
 *
7
 * This is the main module file for Views. The main entry points into
8
 * this module are views_page() and views_block(), where it handles
9
 * incoming page and block requests.
10
 */
11

    
12
/**
13
 * Advertise the current views api version
14
 */
15
function views_api_version() {
16
  return '3.0';
17
}
18

    
19
/**
20
 * Implements hook_forms().
21
 *
22
 * To provide distinct form IDs for Views forms, the View name and
23
 * specific display name are appended to the base ID,
24
 * views_form_views_form. When such a form is built or submitted, this
25
 * function will return the proper callback function to use for the given form.
26
 */
27
function views_forms($form_id, $args) {
28
  if (strpos($form_id, 'views_form_') === 0) {
29
    return array(
30
      $form_id => array(
31
        'callback' => 'views_form',
32
      ),
33
    );
34
  }
35
}
36

    
37
/**
38
 * Returns a form ID for a Views form using the name and display of the View.
39
 */
40
function views_form_id($view) {
41
  $parts = array(
42
    'views_form',
43
    $view->name,
44
    $view->current_display,
45
  );
46

    
47
  return implode('_', $parts);
48
}
49

    
50
/**
51
 * Views will not load plugins advertising a version older than this.
52
 */
53
function views_api_minimum_version() {
54
  return '2';
55
}
56

    
57
/**
58
 * Implement hook_theme(). Register views theming functions.
59
 */
60
function views_theme($existing, $type, $theme, $path) {
61
  $path = drupal_get_path('module', 'views');
62
  ctools_include('theme', 'views', 'theme');
63

    
64
  // Some quasi clever array merging here.
65
  $base = array(
66
    'file' => 'theme.inc',
67
    'path' => $path . '/theme',
68
  );
69

    
70
  // Our extra version of pager from pager.inc
71
  $hooks['views_mini_pager'] = $base + array(
72
    'variables' => array('tags' => array(), 'element' => 0, 'parameters' => array()),
73
    'pattern' => 'views_mini_pager__',
74
  );
75

    
76
  $variables = array(
77
    // For displays, we pass in a dummy array as the first parameter, since
78
    // $view is an object but the core contextual_preprocess() function only
79
    // attaches contextual links when the primary theme argument is an array.
80
    'display' => array('view_array' => array(), 'view' => NULL),
81
    'style' => array('view' => NULL, 'options' => NULL, 'rows' => NULL, 'title' => NULL),
82
    'row' => array('view' => NULL, 'options' => NULL, 'row' => NULL, 'field_alias' => NULL),
83
    'exposed_form' => array('view' => NULL, 'options' => NULL),
84
    'pager' => array(
85
      'view' => NULL, 'options' => NULL,
86
      'tags' => array(), 'quantity' => 10, 'element' => 0, 'parameters' => array()
87
    ),
88
  );
89

    
90
  // Default view themes
91
  $hooks['views_view_field'] = $base + array(
92
    'pattern' => 'views_view_field__',
93
    'variables' => array('view' => NULL, 'field' => NULL, 'row' => NULL),
94
  );
95
  $hooks['views_view_grouping'] = $base + array(
96
    'pattern' => 'views_view_grouping__',
97
    'variables' => array('view' => NULL, 'grouping' => NULL, 'grouping_level' => NULL, 'rows' => NULL, 'title' => NULL),
98
  );
99

    
100
  $plugins = views_fetch_plugin_data();
101

    
102
  // Register theme functions for all style plugins
103
  foreach ($plugins as $type => $info) {
104
    foreach ($info as $plugin => $def) {
105
      if (isset($def['theme']) && (!isset($def['register theme']) || !empty($def['register theme']))) {
106
        $hooks[$def['theme']] = array(
107
          'pattern' => $def['theme'] . '__',
108
          'file' => $def['theme file'],
109
          'path' => $def['theme path'],
110
          'variables' => $variables[$type],
111
        );
112

    
113
        $include = DRUPAL_ROOT . '/' . $def['theme path'] . '/' . $def['theme file'];
114
        if (file_exists($include)) {
115
          require_once $include;
116
        }
117

    
118
        if (!function_exists('theme_' . $def['theme'])) {
119
          $hooks[$def['theme']]['template'] = drupal_clean_css_identifier($def['theme']);
120
        }
121
      }
122
      if (isset($def['additional themes'])) {
123
        foreach ($def['additional themes'] as $theme => $theme_type) {
124
          if (empty($theme_type)) {
125
            $theme = $theme_type;
126
            $theme_type = $type;
127
          }
128

    
129
          $hooks[$theme] = array(
130
            'pattern' => $theme . '__',
131
            'file' => $def['theme file'],
132
            'path' => $def['theme path'],
133
            'variables' => $variables[$theme_type],
134
          );
135

    
136
          if (!function_exists('theme_' . $theme)) {
137
            $hooks[$theme]['template'] = drupal_clean_css_identifier($theme);
138
          }
139
        }
140
      }
141
    }
142
  }
143

    
144
  $hooks['views_form_views_form'] = $base + array(
145
    'render element' => 'form',
146
  );
147

    
148
  $hooks['views_exposed_form'] = $base + array(
149
    'template' => 'views-exposed-form',
150
    'pattern' => 'views_exposed_form__',
151
    'render element' => 'form',
152
  );
153

    
154
  $hooks['views_more'] = $base + array(
155
    'template' => 'views-more',
156
    'pattern' => 'views_more__',
157
    'variables' => array('more_url' => NULL, 'link_text' => 'more', 'view' => NULL),
158
  );
159

    
160
  // Add theme suggestions which are part of modules.
161
  foreach (views_get_module_apis() as $info) {
162
    if (isset($info['template path'])) {
163
      $hooks += _views_find_module_templates($hooks, $info['template path']);
164
    }
165
  }
166
  return $hooks;
167
}
168

    
169
/**
170
 * Scans a directory of a module for template files.
171
 *
172
 * @param $cache
173
 *   The existing cache of theme hooks to test against.
174
 * @param $path
175
 *   The path to search.
176
 *
177
 * @see drupal_find_theme_templates()
178
 */
179
function _views_find_module_templates($cache, $path) {
180
  $templates = array();
181
  $regex = '/' . '\.tpl\.php' . '$' . '/';
182

    
183
  // Because drupal_system_listing works the way it does, we check for real
184
  // templates separately from checking for patterns.
185
  $files = drupal_system_listing($regex, $path, 'name', 0);
186
  foreach ($files as $template => $file) {
187
    // Chop off the remaining extensions if there are any. $template already
188
    // has the rightmost extension removed, but there might still be more,
189
    // such as with .tpl.php, which still has .tpl in $template at this point.
190
    if (($pos = strpos($template, '.')) !== FALSE) {
191
      $template = substr($template, 0, $pos);
192
    }
193
    // Transform - in filenames to _ to match function naming scheme
194
    // for the purposes of searching.
195
    $hook = strtr($template, '-', '_');
196
    if (isset($cache[$hook])) {
197
      $templates[$hook] = array(
198
        'template' => $template,
199
        'path' => dirname($file->filename),
200
        'includes' => isset($cache[$hook]['includes']) ? $cache[$hook]['includes'] : NULL,
201
      );
202
    }
203
    // Ensure that the pattern is maintained from base themes to its sub-themes.
204
    // Each sub-theme will have their templates scanned so the pattern must be
205
    // held for subsequent runs.
206
    if (isset($cache[$hook]['pattern'])) {
207
      $templates[$hook]['pattern'] = $cache[$hook]['pattern'];
208
    }
209
  }
210

    
211
  $patterns = array_keys($files);
212

    
213
  foreach ($cache as $hook => $info) {
214
    if (!empty($info['pattern'])) {
215
      // Transform _ in pattern to - to match file naming scheme
216
      // for the purposes of searching.
217
      $pattern = strtr($info['pattern'], '_', '-');
218

    
219
      $matches = preg_grep('/^'. $pattern .'/', $patterns);
220
      if ($matches) {
221
        foreach ($matches as $match) {
222
          $file = substr($match, 0, strpos($match, '.'));
223
          // Put the underscores back in for the hook name and register this pattern.
224
          $templates[strtr($file, '-', '_')] = array(
225
            'template' => $file,
226
            'path' => dirname($files[$match]->uri),
227
            'variables' => isset($info['variables']) ? $info['variables'] : NULL,
228
            'render element' => isset($info['render element']) ? $info['render element'] : NULL,
229
            'base hook' => $hook,
230
            'includes' => isset($info['includes']) ? $info['includes'] : NULL,
231
          );
232
        }
233
      }
234
    }
235
  }
236

    
237
  return $templates;
238
}
239

    
240
/**
241
 * Returns a list of plugins and metadata about them.
242
 *
243
 * @return array
244
 *   An array keyed by PLUGIN_TYPE:PLUGIN_NAME, like 'display:page' or
245
 *   'pager:full', containing an array with the following keys:
246
 *   - title: The plugin's title.
247
 *   - type: The plugin type.
248
 *   - module: The module providing the plugin.
249
 *   - views: An array of enabled Views that are currently using this plugin,
250
 *     keyed by machine name.
251
 */
252
function views_plugin_list() {
253
  $plugin_data = views_fetch_plugin_data();
254
  $plugins = array();
255
  foreach (views_get_enabled_views() as $view) {
256
    foreach ($view->display as $display_id => $display) {
257
      foreach ($plugin_data as $type => $info) {
258
        if ($type == 'display' && isset($display->display_plugin)) {
259
          $name = $display->display_plugin;
260
        }
261
        elseif (isset($display->display_options["{$type}_plugin"])) {
262
          $name = $display->display_options["{$type}_plugin"];
263
        }
264
        elseif (isset($display->display_options[$type]['type'])) {
265
          $name = $display->display_options[$type]['type'];
266
        }
267
        else {
268
          continue;
269
        }
270

    
271
        // Key first by the plugin type, then the name.
272
        $key = $type . ':' . $name;
273
        // Add info for this plugin.
274
        if (!isset($plugins[$key])) {
275
          $plugins[$key] = array(
276
            'type' => $type,
277
            'title' => check_plain($info[$name]['title']),
278
            'module' => check_plain($info[$name]['module']),
279
            'views' => array(),
280
          );
281
        }
282

    
283
        // Add this view to the list for this plugin.
284
        $plugins[$key]['views'][$view->name] = $view->name;
285
      }
286
    }
287
  }
288
  return $plugins;
289
}
290

    
291
/**
292
 * A theme preprocess function to automatically allow view-based node
293
 * templates if called from a view.
294
 *
295
 * The 'modules/node.views.inc' file is a better place for this, but
296
 * we haven't got a chance to load that file before Drupal builds the
297
 * node portion of the theme registry.
298
 */
299
function views_preprocess_node(&$vars) {
300
  // The 'view' attribute of the node is added in views_preprocess_node()
301
  if (!empty($vars['node']->view) && !empty($vars['node']->view->name)) {
302
    $vars['view'] = $vars['node']->view;
303
    $vars['theme_hook_suggestions'][] = 'node__view__' . $vars['node']->view->name;
304
    if (!empty($vars['node']->view->current_display)) {
305
      $vars['theme_hook_suggestions'][] = 'node__view__' . $vars['node']->view->name . '__' . $vars['node']->view->current_display;
306

    
307
      // If a node is being rendered in a view, and the view does not have a path,
308
      // prevent drupal from accidentally setting the $page variable:
309
      if ($vars['page'] && $vars['view_mode'] == 'full' && !$vars['view']->display_handler->has_path()) {
310
        $vars['page'] = FALSE;
311
      }
312
    }
313
  }
314

    
315
  // Allow to alter comments and links based on the settings in the row plugin.
316
  if (!empty($vars['view']->style_plugin->row_plugin) && get_class($vars['view']->style_plugin->row_plugin) == 'views_plugin_row_node_view') {
317
    node_row_node_view_preprocess_node($vars);
318
  }
319
}
320

    
321
/**
322
 * A theme preprocess function to automatically allow view-based node
323
 * templates if called from a view.
324
 */
325
function views_preprocess_comment(&$vars) {
326
  // The 'view' attribute of the node is added in template_preprocess_views_view_row_comment()
327
  if (!empty($vars['node']->view) && !empty($vars['node']->view->name)) {
328
    $vars['view'] = &$vars['node']->view;
329
    $vars['theme_hook_suggestions'][] = 'comment__view__' . $vars['node']->view->name;
330
    if (!empty($vars['node']->view->current_display)) {
331
      $vars['theme_hook_suggestions'][] = 'comment__view__' . $vars['node']->view->name . '__' . $vars['node']->view->current_display;
332
    }
333
  }
334
}
335

    
336
/**
337
 * Implement hook_permission().
338
 */
339
function views_permission() {
340
  return array(
341
    'administer views' => array(
342
      'title' => t('Administer views'),
343
      'description' => t('Access the views administration pages.'),
344
      'restrict access' => TRUE,
345
    ),
346
    'access all views' => array(
347
      'title' => t('Bypass views access control'),
348
      'description' => t('Bypass access control when accessing views.'),
349
      'restrict access' => TRUE,
350
    ),
351
  );
352
}
353

    
354
/**
355
 * Implement hook_menu().
356
 */
357
function views_menu() {
358
  $items = array();
359
  $items['views/ajax'] = array(
360
    'title' => 'Views',
361
    'page callback' => 'views_ajax',
362
    'theme callback' => 'ajax_base_page_theme',
363
    'delivery callback' => 'ajax_deliver',
364
    'access callback' => TRUE,
365
    'description' => 'Ajax callback for view loading.',
366
    'type' => MENU_CALLBACK,
367
    'file' => 'includes/ajax.inc',
368
  );
369
  // Path is not admin/structure/views due to menu complications with the wildcards from
370
  // the generic ajax callback.
371
  $items['admin/views/ajax/autocomplete/user'] = array(
372
    'page callback' => 'views_ajax_autocomplete_user',
373
    'theme callback' => 'ajax_base_page_theme',
374
    'access callback' => 'user_access',
375
    'access arguments' => array('access user profiles'),
376
    'type' => MENU_CALLBACK,
377
    'file' => 'includes/ajax.inc',
378
  );
379
  // Define another taxonomy autocomplete because the default one of drupal
380
  // does not support a vid a argument anymore
381
  $items['admin/views/ajax/autocomplete/taxonomy'] = array(
382
    'page callback' => 'views_ajax_autocomplete_taxonomy',
383
    'theme callback' => 'ajax_base_page_theme',
384
    'access callback' => 'user_access',
385
    'access arguments' => array('access content'),
386
    'type' => MENU_CALLBACK,
387
    'file' => 'includes/ajax.inc',
388
  );
389
  return $items;
390
}
391

    
392
/**
393
 * Implement hook_menu_alter().
394
 */
395
function views_menu_alter(&$callbacks) {
396
  $our_paths = array();
397
  $views = views_get_applicable_views('uses hook menu');
398
  foreach ($views as $data) {
399
    list($view, $display_id) = $data;
400
    $result = $view->execute_hook_menu($display_id, $callbacks);
401
    if (is_array($result)) {
402
      // The menu system doesn't support having two otherwise
403
      // identical paths with different placeholders.  So we
404
      // want to remove the existing items from the menu whose
405
      // paths would conflict with ours.
406

    
407
      // First, we must find any existing menu items that may
408
      // conflict.  We use a regular expression because we don't
409
      // know what placeholders they might use.  Note that we
410
      // first construct the regex itself by replacing %views_arg
411
      // in the display path, then we use this constructed regex
412
      // (which will be something like '#^(foo/%[^/]*/bar)$#') to
413
      // search through the existing paths.
414
      $regex = '#^(' . preg_replace('#%views_arg#', '%[^/]*', implode('|', array_keys($result))) . ')$#';
415
      $matches = preg_grep($regex, array_keys($callbacks));
416

    
417
      // Remove any conflicting items that were found.
418
      foreach ($matches as $path) {
419
        // Don't remove the paths we just added!
420
        if (!isset($our_paths[$path])) {
421
          unset($callbacks[$path]);
422
        }
423
      }
424
      foreach ($result as $path => $item) {
425
        if (!isset($callbacks[$path])) {
426
          // Add a new item, possibly replacing (and thus effectively
427
          // overriding) one that we removed above.
428
          $callbacks[$path] = $item;
429
        }
430
        else {
431
          // This item already exists, so it must be one that we added.
432
          // We change the various callback arguments to pass an array
433
          // of possible display IDs instead of a single ID.
434
          $callbacks[$path]['page arguments'][1] = (array)$callbacks[$path]['page arguments'][1];
435
          $callbacks[$path]['page arguments'][1][] = $display_id;
436
          $callbacks[$path]['access arguments'][] = $item['access arguments'][0];
437
          $callbacks[$path]['load arguments'][1] = (array)$callbacks[$path]['load arguments'][1];
438
          $callbacks[$path]['load arguments'][1][] = $display_id;
439
        }
440
        $our_paths[$path] = TRUE;
441
      }
442
    }
443
  }
444

    
445
  // Save memory: Destroy those views.
446
  foreach ($views as $data) {
447
    list($view, $display_id) = $data;
448
    $view->destroy();
449
  }
450
}
451

    
452
/**
453
 * Helper function for menu loading. This will automatically be
454
 * called in order to 'load' a views argument; primarily it
455
 * will be used to perform validation.
456
 *
457
 * @param $value
458
 *   The actual value passed.
459
 * @param $name
460
 *   The name of the view. This needs to be specified in the 'load function'
461
 *   of the menu entry.
462
 * @param $display_id
463
 *   The display id that will be loaded for this menu item.
464
 * @param $index
465
 *   The menu argument index. This counts from 1.
466
 */
467
function views_arg_load($value, $name, $display_id, $index) {
468
 static $views = array();
469

    
470
  $display_ids = is_array($display_id) ? $display_id : array($display_id);
471
  $display_id = reset($display_ids);
472

    
473
  foreach ($display_ids as $id) {
474
    // Make sure we haven't already loaded this views argument for a similar
475
    // menu item elsewhere. Since access is always checked for the current user,
476
    // we are sure that the static cache contains valid entries.
477
    $key = $name . ':' . $id . ':' . $value . ':' . $index;
478
    if (isset($views[$key])) {
479
      return $views[$key];
480
    }
481
    // Lazy load the view object to avoid unnecessary work.
482
    if (!isset($view)) {
483
      $view = views_get_view($name);
484
    }
485
    // Pick the first display we have access to.
486
    if ($view && count($display_ids) > 1 && $view->access($id)) {
487
      $display_id = $id;
488
      break;
489
    }
490
  }
491

    
492
  if ($view) {
493
    $view->set_display($display_id);
494
    $view->init_handlers();
495

    
496
    $ids = array_keys($view->argument);
497

    
498
    $indexes = array();
499
    $path = explode('/', $view->get_path());
500

    
501
    foreach ($path as $id => $piece) {
502
      if ($piece == '%' && !empty($ids)) {
503
        $indexes[$id] = array_shift($ids);
504
      }
505
    }
506

    
507
    if (isset($indexes[$index])) {
508
      if (isset($view->argument[$indexes[$index]])) {
509
        $arg = $view->argument[$indexes[$index]]->validate_argument($value) ? $value : FALSE;
510
        $view->destroy();
511

    
512
        // Store the output in case we load this same menu item again.
513
        $views[$key] = $arg;
514
        return $arg;
515
      }
516
    }
517
    $view->destroy();
518
  }
519
}
520

    
521
/**
522
 * Page callback: Displays a page view, given a name and display id.
523
 *
524
 * @param $name
525
 *   The name of a view.
526
 * @param $display_id
527
 *   The display id of a view.
528
 *
529
 * @return
530
 *   Either the HTML of a fully-executed view, or MENU_NOT_FOUND.
531
 */
532
function views_page($name, $display_id) {
533
  $args = func_get_args();
534
  // Remove $name and $display_id from the arguments.
535
  array_shift($args);
536
  array_shift($args);
537

    
538
  // Load the view and render it.
539
  if ($view = views_get_view($name)) {
540
    return $view->execute_display($display_id, $args);
541
  }
542

    
543
  // Fallback; if we get here no view was found or handler was not valid.
544
  return MENU_NOT_FOUND;
545
}
546

    
547
/**
548
 * Implements hook_page_alter().
549
 */
550
function views_page_alter(&$page) {
551
  // If the main content of this page contains a view, attach its contextual
552
  // links to the overall page array. This allows them to be rendered directly
553
  // next to the page title.
554
  $view = views_get_page_view();
555
  if (!empty($view)) {
556
    // If a module is still putting in the display like we used to, catch that.
557
    if (is_subclass_of($view, 'views_plugin_display')) {
558
      $view = $view->view;
559
    }
560

    
561
    views_add_contextual_links($page, 'page', $view, $view->current_display);
562
  }
563
}
564

    
565
/**
566
 * Implements MODULE_preprocess_HOOK() for html.tpl.php.
567
 */
568
function views_preprocess_html(&$variables) {
569
  // If the page contains a view as its main content, contextual links may have
570
  // been attached to the page as a whole; for example, by views_page_alter().
571
  // This allows them to be associated with the page and rendered by default
572
  // next to the page title (which we want). However, it also causes the
573
  // Contextual Links module to treat the wrapper for the entire page (i.e.,
574
  // the <body> tag) as the HTML element that these contextual links are
575
  // associated with. This we don't want; for better visual highlighting, we
576
  // prefer a smaller region to be chosen. The region we prefer differs from
577
  // theme to theme and depends on the details of the theme's markup in
578
  // page.tpl.php, so we can only find it using JavaScript. We therefore remove
579
  // the "contextual-links-region" class from the <body> tag here and add
580
  // JavaScript that will insert it back in the correct place.
581
  if (!empty($variables['page']['#views_contextual_links_info'])) {
582
    $key = array_search('contextual-links-region', $variables['classes_array']);
583
    if ($key !== FALSE) {
584
      $variables['classes_array'] = array_diff($variables['classes_array'], array('contextual-links-region'));
585
      // Add the JavaScript, with a group and weight such that it will run
586
      // before modules/contextual/contextual.js.
587
      drupal_add_js(drupal_get_path('module', 'views') . '/js/views-contextual.js', array('group' => JS_LIBRARY, 'weight' => -1));
588
    }
589
  }
590
}
591

    
592
/**
593
* Implements hook_preprocess_HOOK() for page.tpl.php.
594
*/
595
function views_preprocess_page(&$variables) {
596
  // If the page contains a view as its main content, contextual links may have
597
  // been attached to the page as a whole; for example, by views_page_alter().
598
  // This allows them to be associated with the page and rendered by default
599
  // next to the page title (which we want). However, it also causes the
600
  // Contextual Links module to treat the wrapper for the entire page (i.e.,
601
  // the <body> tag) as the HTML element that these contextual links are
602
  // associated with. This we don't want; for better visual highlighting, we
603
  // prefer a smaller region to be chosen. The region we prefer differs from
604
  // theme to theme and depends on the details of the theme's markup in
605
  // page.tpl.php, so we can only find it using JavaScript. We therefore remove
606
  // the "contextual-links-region" class from the <body> tag here and add
607
  // JavaScript that will insert it back in the correct place.
608
  if (!empty($variables['page']['#views_contextual_links_info'])) {
609
    $variables['classes_array'] = array_diff($variables['classes_array'], array('contextual-links-region'));
610
  }
611
}
612

    
613
/**
614
 * Implements hook_contextual_links_view_alter().
615
 */
616
function views_contextual_links_view_alter(&$element, $items) {
617
  // If we are rendering views-related contextual links attached to the overall
618
  // page array, add a class to the list of contextual links. This will be used
619
  // by the JavaScript added in views_preprocess_html().
620
  if (!empty($element['#element']['#views_contextual_links_info']) && !empty($element['#element']['#type']) && $element['#element']['#type'] == 'page') {
621
    $element['#attributes']['class'][] = 'views-contextual-links-page';
622
  }
623
}
624

    
625
/**
626
 * Implement hook_block_info().
627
 */
628
function views_block_info() {
629
  // Try to avoid instantiating all the views just to get the blocks info.
630
  views_include('cache');
631
  $cache = views_cache_get('views_block_items', TRUE);
632
  if ($cache && is_array($cache->data)) {
633
    return $cache->data;
634
  }
635

    
636
  $items = array();
637
  $views = views_get_all_views();
638
  foreach ($views as $view) {
639
    // disabled views get nothing.
640
    if (!empty($view->disabled)) {
641
      continue;
642
    }
643

    
644
    $view->init_display();
645
    foreach ($view->display as $display_id => $display) {
646

    
647
      if (isset($display->handler) && !empty($display->handler->definition['uses hook block'])) {
648
        $result = $display->handler->execute_hook_block_list();
649
        if (is_array($result)) {
650
          $items = array_merge($items, $result);
651
        }
652
      }
653

    
654
      if (isset($display->handler) && $display->handler->get_option('exposed_block')) {
655
        $result = $display->handler->get_special_blocks();
656
        if (is_array($result)) {
657
          $items = array_merge($items, $result);
658
        }
659
      }
660
    }
661
  }
662

    
663
  // block.module has a delta length limit of 32, but our deltas can
664
  // unfortunately be longer because view names can be 32 and display IDs
665
  // can also be 32. So for very long deltas, change to md5 hashes.
666
  $hashes = array();
667

    
668
  // get the keys because we're modifying the array and we don't want to
669
  // confuse PHP too much.
670
  $keys = array_keys($items);
671
  foreach ($keys as $delta) {
672
    if (strlen($delta) >= 32) {
673
      $hash = md5($delta);
674
      $hashes[$hash] = $delta;
675
      $items[$hash] = $items[$delta];
676
      unset($items[$delta]);
677
    }
678
  }
679

    
680
  // Only save hashes if they have changed.
681
  $old_hashes = variable_get('views_block_hashes', array());
682
  if ($hashes != $old_hashes) {
683
    variable_set('views_block_hashes', $hashes);
684
  }
685
  // Save memory: Destroy those views.
686
  foreach ($views as $view) {
687
    $view->destroy();
688
  }
689

    
690
  views_cache_set('views_block_items', $items, TRUE);
691

    
692
  return $items;
693
}
694

    
695
/**
696
 * Implement hook_block_view().
697
 */
698
function views_block_view($delta) {
699
  $start = microtime(TRUE);
700
  // if this is 32, this should be an md5 hash.
701
  if (strlen($delta) == 32) {
702
    $hashes = variable_get('views_block_hashes', array());
703
    if (!empty($hashes[$delta])) {
704
      $delta = $hashes[$delta];
705
    }
706
  }
707

    
708
  // This indicates it's a special one.
709
  if (substr($delta, 0, 1) == '-') {
710
    list($nothing, $type, $name, $display_id) = explode('-', $delta);
711
    // Put the - back on.
712
    $type = '-' . $type;
713
    if ($view = views_get_view($name)) {
714
      if ($view->access($display_id)) {
715
        $view->set_display($display_id);
716
        if (isset($view->display_handler)) {
717
          $output = $view->display_handler->view_special_blocks($type);
718
          // Before returning the block output, convert it to a renderable
719
          // array with contextual links.
720
          views_add_block_contextual_links($output, $view, $display_id, 'special_block_' . $type);
721
          $view->destroy();
722
          return $output;
723
        }
724
      }
725
      $view->destroy();
726
    }
727
  }
728

    
729
  // If the delta doesn't contain valid data return nothing.
730
  $explode = explode('-', $delta);
731
  if (count($explode) != 2) {
732
    return;
733
  }
734
  list($name, $display_id) = $explode;
735
  // Load the view
736
  if ($view = views_get_view($name)) {
737
    if ($view->access($display_id)) {
738
      $output = $view->execute_display($display_id);
739
      // Before returning the block output, convert it to a renderable array
740
      // with contextual links.
741
      views_add_block_contextual_links($output, $view, $display_id);
742
      $view->destroy();
743
      return $output;
744
    }
745
    $view->destroy();
746
  }
747
}
748

    
749
/**
750
 * Converts Views block content to a renderable array with contextual links.
751
 *
752
 * @param $block
753
 *   An array representing the block, with the same structure as the return
754
 *   value of hook_block_view(). This will be modified so as to force
755
 *   $block['content'] to be a renderable array, containing the optional
756
 *   '#contextual_links' property (if there are any contextual links associated
757
 *   with the block).
758
 * @param $view
759
 *   The view that was used to generate the block content.
760
 * @param $display_id
761
 *   The ID of the display within the view that was used to generate the block
762
 *   content.
763
 * @param $block_type
764
 *   The type of the block. If it's block it's a regular views display,
765
 *   but 'special_block_-exp' exist as well.
766
 */
767
function views_add_block_contextual_links(&$block, $view, $display_id, $block_type = 'block') {
768
  // Do not add contextual links to an empty block.
769
  if (!empty($block['content'])) {
770
    // Contextual links only work on blocks whose content is a renderable
771
    // array, so if the block contains a string of already-rendered markup,
772
    // convert it to an array.
773
    if (is_string($block['content'])) {
774
      $block['content'] = array('#markup' => $block['content']);
775
    }
776
    // Add the contextual links.
777
    views_add_contextual_links($block['content'], $block_type, $view, $display_id);
778
  }
779
}
780

    
781
/**
782
 * Adds contextual links associated with a view display to a renderable array.
783
 *
784
 * This function should be called when a view is being rendered in a particular
785
 * location and you want to attach the appropriate contextual links (e.g.,
786
 * links for editing the view) to it.
787
 *
788
 * The function operates by checking the view's display plugin to see if it has
789
 * defined any contextual links that are intended to be displayed in the
790
 * requested location; if so, it attaches them. The contextual links intended
791
 * for a particular location are defined by the 'contextual links' and
792
 * 'contextual links locations' properties in hook_views_plugins() and
793
 * hook_views_plugins_alter(); as a result, these hook implementations have
794
 * full control over where and how contextual links are rendered for each
795
 * display.
796
 *
797
 * In addition to attaching the contextual links to the passed-in array (via
798
 * the standard #contextual_links property), this function also attaches
799
 * additional information via the #views_contextual_links_info property. This
800
 * stores an array whose keys are the names of each module that provided
801
 * views-related contextual links (same as the keys of the #contextual_links
802
 * array itself) and whose values are themselves arrays whose keys ('location',
803
 * 'view_name', and 'view_display_id') store the location, name of the view,
804
 * and display ID that were passed in to this function. This allows you to
805
 * access information about the contextual links and how they were generated in
806
 * a variety of contexts where you might be manipulating the renderable array
807
 * later on (for example, alter hooks which run later during the same page
808
 * request).
809
 *
810
 * @param $render_element
811
 *   The renderable array to which contextual links will be added. This array
812
 *   should be suitable for passing in to drupal_render() and will normally
813
 *   contain a representation of the view display whose contextual links are
814
 *   being requested.
815
 * @param $location
816
 *   The location in which the calling function intends to render the view and
817
 *   its contextual links. The core system supports three options for this
818
 *   parameter:
819
 *   - 'block': Used when rendering a block which contains a view. This
820
 *     retrieves any contextual links intended to be attached to the block
821
 *     itself.
822
 *   - 'page': Used when rendering the main content of a page which contains a
823
 *     view. This retrieves any contextual links intended to be attached to the
824
 *     page itself (for example, links which are displayed directly next to the
825
 *     page title).
826
 *   - 'view': Used when rendering the view itself, in any context. This
827
 *     retrieves any contextual links intended to be attached directly to the
828
 *     view.
829
 *   If you are rendering a view and its contextual links in another location,
830
 *   you can pass in a different value for this parameter. However, you will
831
 *   also need to use hook_views_plugins() or hook_views_plugins_alter() to
832
 *   declare, via the 'contextual links locations' array key, which view
833
 *   displays support having their contextual links rendered in the location
834
 *   you have defined.
835
 * @param $view
836
 *   The view whose contextual links will be added.
837
 * @param $display_id
838
 *   The ID of the display within $view whose contextual links will be added.
839
 *
840
 * @see hook_views_plugins()
841
 * @see views_block_view()
842
 * @see views_page_alter()
843
 * @see template_preprocess_views_view()
844
 */
845
function views_add_contextual_links(&$render_element, $location, $view, $display_id) {
846
  // Do not do anything if the view is configured to hide its administrative
847
  // links.
848
  if (empty($view->hide_admin_links)) {
849
    // Also do not do anything if the display plugin has not defined any
850
    // contextual links that are intended to be displayed in the requested
851
    // location.
852
    $plugin = views_fetch_plugin_data('display', $view->display[$display_id]->display_plugin);
853
    // If contextual links locations are not set, provide a sane default. (To
854
    // avoid displaying any contextual links at all, a display plugin can still
855
    // set 'contextual links locations' to, e.g., an empty array.)
856
    $plugin += array('contextual links locations' => array('view'));
857
    // On exposed_forms blocks contextual links should always be visible.
858
    $plugin['contextual links locations'][] = 'special_block_-exp';
859
    $has_links = !empty($plugin['contextual links']) && !empty($plugin['contextual links locations']);
860
    if ($has_links && in_array($location, $plugin['contextual links locations'])) {
861
      foreach ($plugin['contextual links'] as $module => $link) {
862
        $args = array();
863
        $valid = TRUE;
864
        if (!empty($link['argument properties'])) {
865
          foreach ($link['argument properties'] as $property) {
866
            // If the plugin is trying to create an invalid contextual link
867
            // (for example, "path/to/{$view->property}", where $view->property
868
            // does not exist), we cannot construct the link, so we skip it.
869
            if (!property_exists($view, $property)) {
870
              $valid = FALSE;
871
              break;
872
            }
873
            else {
874
              $args[] = $view->{$property};
875
            }
876
          }
877
        }
878
        // If the link was valid, attach information about it to the renderable
879
        // array.
880
        if ($valid) {
881
          $render_element['#contextual_links'][$module] = array($link['parent path'], $args);
882
          $render_element['#views_contextual_links_info'][$module] = array(
883
            'location' => $location,
884
            'view' => $view,
885
            'view_name' => $view->name,
886
            'view_display_id' => $display_id,
887
          );
888
        }
889
      }
890
    }
891
  }
892
}
893

    
894
/**
895
 * Returns an array of language names.
896
 *
897
 * This is a one to one copy of locale_language_list because we can't rely on enabled locale module.
898
 *
899
 * @param $field
900
 *   'name' => names in current language, localized
901
 *   'native' => native names
902
 * @param $all
903
 *   Boolean to return all languages or only enabled ones
904
 *
905
 * @see locale_language_list()
906
 */
907
function views_language_list($field = 'name', $all = FALSE) {
908
  if ($all) {
909
    $languages = language_list();
910
  }
911
  else {
912
    $languages = language_list('enabled');
913
    $languages = $languages[1];
914
  }
915
  $list = array();
916
  foreach ($languages as $language) {
917
    $list[$language->language] = ($field == 'name') ? t($language->name) : $language->$field;
918
  }
919
  return $list;
920
}
921

    
922
/**
923
 * Implements hook_flush_caches().
924
 */
925
function views_flush_caches() {
926
  return array('cache_views', 'cache_views_data');
927
}
928

    
929
/**
930
 * Implements hook_field_create_instance.
931
 */
932
function views_field_create_instance($instance) {
933
  cache_clear_all('*', 'cache_views', TRUE);
934
  cache_clear_all('*', 'cache_views_data', TRUE);
935
}
936

    
937
/**
938
 * Implements hook_field_update_instance.
939
 */
940
function views_field_update_instance($instance, $prior_instance) {
941
  cache_clear_all('*', 'cache_views', TRUE);
942
  cache_clear_all('*', 'cache_views_data', TRUE);
943
}
944

    
945
/**
946
 * Implements hook_field_delete_instance.
947
 */
948
function views_field_delete_instance($instance) {
949
  cache_clear_all('*', 'cache_views', TRUE);
950
  cache_clear_all('*', 'cache_views_data', TRUE);
951
}
952

    
953
/**
954
 * Invalidate the views cache, forcing a rebuild on the next grab of table data.
955
 */
956
function views_invalidate_cache() {
957
  // Clear the views cache.
958
  cache_clear_all('*', 'cache_views', TRUE);
959

    
960
  // Clear the page and block cache.
961
  cache_clear_all();
962

    
963
  // Set the menu as needed to be rebuilt.
964
  variable_set('menu_rebuild_needed', TRUE);
965

    
966
  // Allow modules to respond to the Views cache being cleared.
967
  module_invoke_all('views_invalidate_cache');
968
}
969

    
970
/**
971
 * Access callback to determine if the user can import Views.
972
 *
973
 * View imports require an additional access check because they are PHP
974
 * code and PHP is more locked down than administer views.
975
 */
976
function views_import_access() {
977
  return user_access('administer views') && user_access('use PHP for settings');
978
}
979

    
980
/**
981
 * Determine if the logged in user has access to a view.
982
 *
983
 * This function should only be called from a menu hook or some other
984
 * embedded source. Each argument is the result of a call to
985
 * views_plugin_access::get_access_callback() which is then used
986
 * to determine if that display is accessible. If *any* argument
987
 * is accessible, then the view is accessible.
988
 */
989
function views_access() {
990
  $args = func_get_args();
991
  foreach ($args as $arg) {
992
    if ($arg === TRUE) {
993
      return TRUE;
994
    }
995

    
996
    if (!is_array($arg)) {
997
      continue;
998
    }
999

    
1000
    list($callback, $arguments) = $arg;
1001
    $arguments = $arguments ? $arguments : array();
1002
    // Bring dynamic arguments to the access callback.
1003
    foreach ($arguments as $key => $value) {
1004
      if (is_int($value) && isset($args[$value])) {
1005
        $arguments[$key] = $args[$value];
1006
      }
1007
    }
1008
    if (function_exists($callback) && call_user_func_array($callback, $arguments)) {
1009
      return TRUE;
1010
    }
1011
  }
1012

    
1013
  return FALSE;
1014
}
1015

    
1016
/**
1017
 * Access callback for the views_plugin_access_perm access plugin.
1018
 *
1019
 * Determine if the specified user has access to a view on the basis of
1020
 * permissions. If the $account argument is omitted, the current user
1021
 * is used.
1022
 */
1023
function views_check_perm($perm, $account = NULL) {
1024
  return user_access($perm, $account) || user_access('access all views', $account);
1025
}
1026

    
1027
/**
1028
 * Access callback for the views_plugin_access_role access plugin.
1029

    
1030
 * Determine if the specified user has access to a view on the basis of any of
1031
 * the requested roles. If the $account argument is omitted, the current user
1032
 * is used.
1033
 */
1034
function views_check_roles($rids, $account = NULL) {
1035
  global $user;
1036
  $account = isset($account) ? $account : $user;
1037
  $roles = array_keys($account->roles);
1038
  $roles[] = $account->uid ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID;
1039
  return user_access('access all views', $account) || array_intersect(array_filter($rids), $roles);
1040
}
1041
// ------------------------------------------------------------------
1042
// Functions to help identify views that are running or ran
1043

    
1044
/**
1045
 * Set the current 'page view' that is being displayed so that it is easy
1046
 * for other modules or the theme to identify.
1047
 */
1048
function &views_set_page_view($view = NULL) {
1049
  static $cache = NULL;
1050
  if (isset($view)) {
1051
    $cache = $view;
1052
  }
1053

    
1054
  return $cache;
1055
}
1056

    
1057
/**
1058
 * Find out what, if any, page view is currently in use. Please note that
1059
 * this returns a reference, so be careful! You can unintentionally modify the
1060
 * $view object.
1061
 *
1062
 * @return view
1063
 *   A fully formed, empty $view object.
1064
 */
1065
function &views_get_page_view() {
1066
  return views_set_page_view();
1067
}
1068

    
1069
/**
1070
 * Set the current 'current view' that is being built/rendered so that it is
1071
 * easy for other modules or items in drupal_eval to identify
1072
 *
1073
 * @return view
1074
 */
1075
function &views_set_current_view($view = NULL) {
1076
  static $cache = NULL;
1077
  if (isset($view)) {
1078
    $cache = $view;
1079
  }
1080

    
1081
  return $cache;
1082
}
1083

    
1084
/**
1085
 * Find out what, if any, current view is currently in use. Please note that
1086
 * this returns a reference, so be careful! You can unintentionally modify the
1087
 * $view object.
1088
 *
1089
 * @return view
1090
 */
1091
function &views_get_current_view() {
1092
  return views_set_current_view();
1093
}
1094

    
1095
// ------------------------------------------------------------------
1096
// Include file helpers
1097

    
1098
/**
1099
 * Include views .inc files as necessary.
1100
 */
1101
function views_include($file) {
1102
  ctools_include($file, 'views');
1103
}
1104

    
1105
/**
1106
 * Load views files on behalf of modules.
1107
 */
1108
function views_module_include($api, $reset = FALSE) {
1109
  if ($reset) {
1110
    $cache = &drupal_static('ctools_plugin_api_info');
1111
    if (isset($cache['views']['views'])) {
1112
      unset($cache['views']['views']);
1113
    }
1114
  }
1115
  ctools_include('plugins');
1116
  return ctools_plugin_api_include('views', $api, views_api_minimum_version(), views_api_version());
1117
}
1118

    
1119
/**
1120
 * Get a list of modules that support the current views API.
1121
 */
1122
function views_get_module_apis($api = 'views', $reset = FALSE) {
1123
  if ($reset) {
1124
    $cache = &drupal_static('ctools_plugin_api_info');
1125
    if (isset($cache['views']['views'])) {
1126
      unset($cache['views']['views']);
1127
    }
1128
  }
1129
  ctools_include('plugins');
1130
  return ctools_plugin_api_info('views', $api, views_api_minimum_version(), views_api_version());
1131
}
1132

    
1133
/**
1134
 * Include views .css files.
1135
 */
1136
function views_add_css($file) {
1137
  // We set preprocess to FALSE because we are adding the files conditionally,
1138
  // and we don't want to generate duplicate cache files.
1139
  // TODO: at some point investigate adding some files unconditionally and
1140
  // allowing preprocess.
1141
  drupal_add_css(drupal_get_path('module', 'views') . "/css/$file.css", array('preprocess' => FALSE));
1142
}
1143

    
1144
/**
1145
 * Include views .js files.
1146
 */
1147
function views_add_js($file) {
1148
  // If javascript has been disabled by the user, never add js files.
1149
  if (variable_get('views_no_javascript', FALSE)) {
1150
    return;
1151
  }
1152
  static $base = TRUE, $ajax = TRUE;
1153
  if ($base) {
1154
    drupal_add_js(drupal_get_path('module', 'views') . "/js/base.js");
1155
    $base = FALSE;
1156
  }
1157
  if ($ajax && in_array($file, array('ajax', 'ajax_view'))) {
1158
    drupal_add_library('system', 'drupal.ajax');
1159
    drupal_add_library('system', 'jquery.form');
1160
    $ajax = FALSE;
1161
  }
1162
  ctools_add_js($file, 'views');
1163
}
1164

    
1165
/**
1166
 * Load views files on behalf of modules.
1167
 */
1168
function views_include_handlers($reset = FALSE) {
1169
  static $finished = FALSE;
1170
  // Ensure this only gets run once.
1171
  if ($finished && !$reset) {
1172
    return;
1173
  }
1174

    
1175
  views_include('base');
1176
  views_include('handlers');
1177
  views_include('cache');
1178
  views_include('plugins');
1179
  views_module_include('views', $reset);
1180
  $finished = TRUE;
1181
}
1182

    
1183
// -----------------------------------------------------------------------
1184
// Views handler functions
1185

    
1186
/**
1187
 * Fetch a handler from the data cache.
1188
 *
1189
 * @param $table
1190
 *   The name of the table this handler is from.
1191
 * @param $field
1192
 *   The name of the field this handler is from.
1193
 * @param $key
1194
 *   The type of handler. i.e, sort, field, argument, filter, relationship
1195
 * @param $override
1196
 *   Override the actual handler object with this class. Used for
1197
 *   aggregation when the handler is redirected to the aggregation
1198
 *   handler.
1199
 *
1200
 * @return views_handler
1201
 *   An instance of a handler object. May be views_handler_broken.
1202
 */
1203
function views_get_handler($table, $field, $key, $override = NULL) {
1204
  static $recursion_protection = array();
1205

    
1206
  $data = views_fetch_data($table, FALSE);
1207
  $handler = NULL;
1208
  views_include('handlers');
1209

    
1210
  // Support old views_data entries conversion.
1211

    
1212
  // Support conversion on table level.
1213
  if (isset($data['moved to'])) {
1214
    $moved = array($data['moved to'], $field);
1215
  }
1216
  // Support conversion on datafield level.
1217
  if (isset($data[$field]['moved to'])) {
1218
    $moved = $data[$field]['moved to'];
1219
  }
1220
  // Support conversion on handler level.
1221
  if (isset($data[$field][$key]['moved to'])) {
1222
    $moved = $data[$field][$key]['moved to'];
1223
  }
1224

    
1225
  if (isset($data[$field][$key]) || !empty($moved)) {
1226
    if (!empty($moved)) {
1227
      list($moved_table, $moved_field) = $moved;
1228
      if (!empty($recursion_protection[$moved_table][$moved_field])) {
1229
        // recursion detected!
1230
        return NULL;
1231
      }
1232

    
1233
      $recursion_protection[$moved_table][$moved_field] = TRUE;
1234
      $handler = views_get_handler($moved_table, $moved_field, $key, $override);
1235
      $recursion_protection = array();
1236
      if ($handler) {
1237
        // store these values so we know what we were originally called.
1238
        $handler->original_table = $table;
1239
        $handler->original_field = $field;
1240
        if (empty($handler->actual_table)) {
1241
          $handler->actual_table = $moved_table;
1242
          $handler->actual_field = $moved_field;
1243
        }
1244
      }
1245
      return $handler;
1246
    }
1247

    
1248
    // Set up a default handler:
1249
    if (empty($data[$field][$key]['handler'])) {
1250
      $data[$field][$key]['handler'] = 'views_handler_' . $key;
1251
    }
1252

    
1253
    if ($override) {
1254
      $data[$field][$key]['override handler'] = $override;
1255
    }
1256

    
1257
    $handler = _views_prepare_handler($data[$field][$key], $data, $field, $key);
1258
  }
1259

    
1260
  if ($handler) {
1261
    return $handler;
1262
  }
1263

    
1264
  // DEBUG -- identify missing handlers
1265
  vpr("Missing handler: @table @field @key", array('@table' => $table, '@field' => $field, '@key' => $key));
1266
  $broken = array(
1267
    'title' => t('Broken handler @table.@field', array('@table' => $table, '@field' => $field)),
1268
    'handler' => 'views_handler_' . $key . '_broken',
1269
    'table' => $table,
1270
    'field' => $field,
1271
  );
1272
  return _views_create_handler($broken, 'handler', $key);
1273
}
1274

    
1275
/**
1276
 * Fetch Views' data from the cache
1277
 */
1278
function views_fetch_data($table = NULL, $move = TRUE, $reset = FALSE) {
1279
  views_include('cache');
1280
  return _views_fetch_data($table, $move, $reset);
1281
}
1282

    
1283
// -----------------------------------------------------------------------
1284
// Views plugin functions
1285

    
1286
/**
1287
 * Fetch the plugin data from cache.
1288
 */
1289
function views_fetch_plugin_data($type = NULL, $plugin = NULL, $reset = FALSE) {
1290
  views_include('cache');
1291
  return _views_fetch_plugin_data($type, $plugin, $reset);
1292
}
1293

    
1294
/**
1295
 * Fetch a list of all base tables available
1296
 *
1297
 * @param $type
1298
 *   Either 'display', 'style' or 'row'
1299
 * @param $key
1300
 *   For style plugins, this is an optional type to restrict to. May be 'normal',
1301
 *   'summary', 'feed' or others based on the neds of the display.
1302
 * @param $base
1303
 *   An array of possible base tables.
1304
 *
1305
 * @return
1306
 *   A keyed array of in the form of 'base_table' => 'Description'.
1307
 */
1308
function views_fetch_plugin_names($type, $key = NULL, $base = array()) {
1309
  $data = views_fetch_plugin_data();
1310

    
1311
  $plugins[$type] = array();
1312

    
1313
  foreach ($data[$type] as $id => $plugin) {
1314
    // Skip plugins that don't conform to our key.
1315
    if ($key && (empty($plugin['type']) || $plugin['type'] != $key)) {
1316
      continue;
1317
    }
1318
    if (empty($plugin['no ui']) && (empty($base) || empty($plugin['base']) || array_intersect($base, $plugin['base']))) {
1319
      $plugins[$type][$id] = $plugin['title'];
1320
    }
1321
  }
1322

    
1323
  if (!empty($plugins[$type])) {
1324
    asort($plugins[$type]);
1325
    return $plugins[$type];
1326
  }
1327
  // fall-through
1328
  return array();
1329
}
1330

    
1331
/**
1332
 * Get a handler for a plugin
1333
 *
1334
 * @return views_plugin
1335
 *
1336
 * The created plugin object.
1337
 */
1338
function views_get_plugin($type, $plugin, $reset = FALSE) {
1339
  views_include('handlers');
1340
  $definition = views_fetch_plugin_data($type, $plugin, $reset);
1341
  if (!empty($definition)) {
1342
    return _views_create_handler($definition, $type);
1343
  }
1344
}
1345

    
1346
/**
1347
 * Load the current enabled localization plugin.
1348
 *
1349
 * @return The name of the localization plugin.
1350
 */
1351
function views_get_localization_plugin() {
1352
  $plugin = variable_get('views_localization_plugin', '');
1353
  // Provide sane default values for the localization plugin.
1354
  if (empty($plugin)) {
1355
    if (module_exists('locale')) {
1356
      $plugin = 'core';
1357
    }
1358
    else {
1359
      $plugin = 'none';
1360
    }
1361
  }
1362

    
1363
  return $plugin;
1364
}
1365

    
1366
// -----------------------------------------------------------------------
1367
// Views database functions
1368

    
1369
/**
1370
 * Get all view templates.
1371
 *
1372
 * Templates are special in-code views that are never active, but exist only
1373
 * to be cloned into real views as though they were templates.
1374
 */
1375
function views_get_all_templates() {
1376
  $templates = array();
1377
  $modules = views_module_include('views_template');
1378

    
1379
  foreach ($modules as $module => $info) {
1380
    $function = $module . '_views_templates';
1381
    if (function_exists($function)) {
1382
      $new = $function();
1383
      if ($new && is_array($new)) {
1384
        $templates = array_merge($new, $templates);
1385
      }
1386
    }
1387
  }
1388

    
1389
  return $templates;
1390
}
1391

    
1392
/**
1393
 * Create an empty view to work with.
1394
 *
1395
 * @return view
1396
 *   A fully formed, empty $view object. This object must be populated before
1397
 *   it can be successfully saved.
1398
 */
1399
function views_new_view() {
1400
  views_include('view');
1401
  $view = new view();
1402
  $view->vid = 'new';
1403
  $view->add_display('default');
1404

    
1405
  return $view;
1406
}
1407

    
1408
/**
1409
 * Return a list of all views and display IDs that have a particular
1410
 * setting in their display's plugin settings.
1411
 *
1412
 * @return
1413
 * @code
1414
 * array(
1415
 *   array($view, $display_id),
1416
 *   array($view, $display_id),
1417
 * );
1418
 * @endcode
1419
 */
1420
function views_get_applicable_views($type) {
1421
  // @todo: Use a smarter flagging system so that we don't have to
1422
  // load every view for this.
1423
  $result = array();
1424
  $views = views_get_all_views();
1425

    
1426
  foreach ($views as $view) {
1427
    // Skip disabled views.
1428
    if (!empty($view->disabled)) {
1429
      continue;
1430
    }
1431

    
1432
    if (empty($view->display)) {
1433
      // Skip this view as it is broken.
1434
      vsm(t("Skipping broken view @view", array('@view' => $view->name)));
1435
      continue;
1436
    }
1437

    
1438
    // Loop on array keys because something seems to muck with $view->display
1439
    // a bit in PHP4.
1440
    foreach (array_keys($view->display) as $id) {
1441
      $plugin = views_fetch_plugin_data('display', $view->display[$id]->display_plugin);
1442
      if (!empty($plugin[$type])) {
1443
        // This view uses hook menu. Clone it so that different handlers
1444
        // don't trip over each other, and add it to the list.
1445
        $v = $view->clone_view();
1446
        if ($v->set_display($id) && $v->display_handler->get_option('enabled')) {
1447
          $result[] = array($v, $id);
1448
        }
1449
        // In PHP 4.4.7 and presumably earlier, if we do not unset $v
1450
        // here, we will find that it actually overwrites references
1451
        // possibly due to shallow copying issues.
1452
        unset($v);
1453
      }
1454
    }
1455
  }
1456
  return $result;
1457
}
1458

    
1459
/**
1460
 * Return an array of all views as fully loaded $view objects.
1461
 *
1462
 * @param $reset
1463
 *   If TRUE, reset the static cache forcing views to be reloaded.
1464
 */
1465
function views_get_all_views($reset = FALSE) {
1466
  ctools_include('export');
1467
  return ctools_export_crud_load_all('views_view', $reset);
1468
}
1469

    
1470
/**
1471
 * Returns an array of all enabled views, as fully loaded $view objects.
1472
 */
1473
function views_get_enabled_views() {
1474
  $views = views_get_all_views();
1475
  return array_filter($views, 'views_view_is_enabled');
1476
}
1477

    
1478
/**
1479
 * Returns an array of all disabled views, as fully loaded $view objects.
1480
 */
1481
function views_get_disabled_views() {
1482
  $views = views_get_all_views();
1483
  return array_filter($views, 'views_view_is_disabled');
1484
}
1485

    
1486
/**
1487
 * Return an array of view as options array, that can be used by select,
1488
 * checkboxes and radios as #options.
1489
 *
1490
 * @param bool $views_only
1491
 *  If TRUE, only return views, not displays.
1492
 * @param string $filter
1493
 *  Filters the views on status. Can either be 'all' (default), 'enabled' or
1494
 *  'disabled'
1495
 * @param  mixed $exclude_view
1496
 *  view or current display to exclude
1497
 *  either a
1498
 *  - views object (containing $exclude_view->name and $exclude_view->current_display)
1499
 *  - views name as string:  e.g. my_view
1500
 *  - views name and display id (separated by ':'): e.g. my_view:default
1501
 * @param bool $optgroup
1502
 *  If TRUE, returns an array with optgroups for each view (will be ignored for
1503
 *  $views_only = TRUE). Can be used by select
1504
 * @param bool $sort
1505
 *  If TRUE, the list of views is sorted ascending.
1506
 *
1507
 * @return array
1508
 *  an associative array for use in select.
1509
 *  - key: view name and display id separated by ':', or the view name only
1510
 */
1511
function views_get_views_as_options($views_only = FALSE, $filter = 'all', $exclude_view = NULL, $optgroup = FALSE, $sort = FALSE) {
1512

    
1513
  // Filter the big views array.
1514
  switch ($filter) {
1515
    case 'all':
1516
    case 'disabled':
1517
    case 'enabled':
1518
      $func = "views_get_{$filter}_views";
1519
      $views = $func();
1520
      break;
1521
    default:
1522
      return array();
1523
  }
1524

    
1525
  // Prepare exclude view strings for comparison.
1526
  if (empty($exclude_view)) {
1527
    $exclude_view_name = '';
1528
    $exclude_view_display = '';
1529
  }
1530
  elseif (is_object($exclude_view)) {
1531
    $exclude_view_name = $exclude_view->name;
1532
    $exclude_view_display = $exclude_view->current_display;
1533
  }
1534
  else {
1535
    list($exclude_view_name, $exclude_view_display) = explode(':', $exclude_view);
1536
  }
1537

    
1538
  $options = array();
1539
  foreach ($views as $view) {
1540
    // Return only views.
1541
    if ($views_only && $view->name != $exclude_view_name) {
1542
      $options[$view->name] = $view->get_human_name();
1543
    }
1544
    // Return views with display ids.
1545
    else {
1546
      foreach ($view->display as $display_id => $display) {
1547
        if (!($view->name == $exclude_view_name && $display_id == $exclude_view_display)) {
1548
          if ($optgroup) {
1549
            $options[$view->name][$view->name . ':' . $display->id] = t('@view : @display', array('@view' => $view->name, '@display' => $display->id));
1550
          }
1551
          else {
1552
            $options[$view->name . ':' . $display->id] = t('View: @view - Display: @display', array('@view' => $view->name, '@display' => $display->id));
1553
          }
1554
        }
1555
      }
1556
    }
1557
  }
1558
  if ($sort) {
1559
    ksort($options);
1560
  }
1561
  return $options;
1562
}
1563

    
1564
/**
1565
 * Returns TRUE if a view is enabled, FALSE otherwise.
1566
 */
1567
function views_view_is_enabled($view) {
1568
  return empty($view->disabled);
1569
}
1570

    
1571
/**
1572
 * Returns TRUE if a view is disabled, FALSE otherwise.
1573
 */
1574
function views_view_is_disabled($view) {
1575
  return !empty($view->disabled);
1576
}
1577

    
1578
/**
1579
 * Get a view from the database or from default views.
1580
 *
1581
 * This function is just a static wrapper around views::load(). This function
1582
 * isn't called 'views_load()' primarily because it might get a view
1583
 * from the default views which aren't technically loaded from the database.
1584
 *
1585
 * @param $name
1586
 *   The name of the view.
1587
 * @param $reset
1588
 *   If TRUE, reset this entry in the load cache.
1589
 * @return view
1590
 *   A reference to the $view object. Use $reset if you're sure you want
1591
 *   a fresh one.
1592
 */
1593
function views_get_view($name, $reset = FALSE) {
1594
  if ($reset) {
1595
    $cache = &drupal_static('ctools_export_load_object');
1596
    if (isset($cache['views_view'][$name])) {
1597
      unset($cache['views_view'][$name]);
1598
    }
1599
  }
1600

    
1601
  ctools_include('export');
1602
  $view = ctools_export_crud_load('views_view', $name);
1603
  if ($view) {
1604
    $view->update();
1605
    return $view->clone_view();
1606
  }
1607
}
1608

    
1609
/**
1610
 * Find the real location of a table.
1611
 *
1612
 * If a table has moved, find the new name of the table so that we can
1613
 * change its name directly in options where necessary.
1614
 */
1615
function views_move_table($table) {
1616
  $data = views_fetch_data($table, FALSE);
1617
  if (isset($data['moved to'])) {
1618
    $table = $data['moved to'];
1619
  }
1620

    
1621
  return $table;
1622
}
1623

    
1624
/**
1625
 * Export callback to load the view subrecords, which are the displays.
1626
 */
1627
function views_load_display_records(&$views) {
1628
  // Get vids from the views.
1629
  $names = array();
1630
  foreach ($views as $view) {
1631
    if (empty($view->display)) {
1632
      $names[$view->vid] = $view->name;
1633
    }
1634
  }
1635

    
1636
  if (empty($names)) {
1637
    return;
1638
  }
1639

    
1640
  foreach (view::db_objects() as $key) {
1641
    $object_name = "views_$key";
1642
    $result = db_query("SELECT * FROM {{$object_name}} WHERE vid IN (:vids) ORDER BY vid, position",
1643
      array(':vids' => array_keys($names)));
1644

    
1645
    foreach ($result as $data) {
1646
      $object = new $object_name(FALSE);
1647
      $object->load_row($data);
1648

    
1649
      // Because it can get complicated with this much indirection,
1650
      // make a shortcut reference.
1651
      $location = &$views[$names[$object->vid]]->$key;
1652

    
1653
      // If we have a basic id field, load the item onto the view based on
1654
      // this ID, otherwise push it on.
1655
      if (!empty($object->id)) {
1656
        $location[$object->id] = $object;
1657
      }
1658
      else {
1659
        $location[] = $object;
1660
      }
1661
    }
1662
  }
1663
}
1664

    
1665
/**
1666
 * Export CRUD callback to save a view.
1667
 */
1668
function views_save_view(&$view) {
1669
  return $view->save();
1670
}
1671

    
1672
/**
1673
 * Export CRUD callback to delete a view.
1674
 */
1675
function views_delete_view(&$view) {
1676
  return $view->delete(TRUE);
1677
}
1678

    
1679
/**
1680
 * Export CRUD callback to export a view.
1681
 */
1682
function views_export_view(&$view, $indent = '') {
1683
  return $view->export($indent);
1684
}
1685

    
1686
/**
1687
 * Export callback to change view status.
1688
 */
1689
function views_export_status($view, $status) {
1690
  ctools_export_set_object_status($view, $status);
1691
  views_invalidate_cache();
1692
}
1693

    
1694
// ------------------------------------------------------------------
1695
// Views debug helper functions
1696

    
1697
/**
1698
 * Provide debug output for Views.
1699
 *
1700
 * This relies on devel.module
1701
 * or on the debug() function if you use a simpletest.
1702
 *
1703
 * @param $message
1704
 *   The message/variable which should be debugged.
1705
 *   This either could be
1706
 *     * an array/object which is converted to pretty output
1707
 *     * a translation source string which is used together with the parameter placeholders.
1708
 *
1709
 * @param $placeholder
1710
 *   The placeholders which are used for the translation source string.
1711
 */
1712
function views_debug($message, $placeholders = array()) {
1713
  if (!is_string($message)) {
1714
    $output = '<pre>' . var_export($message, TRUE) . '</pre>';
1715
  }
1716
  if (module_exists('devel') && variable_get('views_devel_output', FALSE) && user_access('access devel information')) {
1717
    $devel_region = variable_get('views_devel_region', 'footer');
1718
    if ($devel_region == 'watchdog') {
1719
      $output = $message;
1720
      watchdog('views_logging', $output, $placeholders);
1721
    }
1722
    else if ($devel_region == 'drupal_debug') {
1723
      $output = empty($output) ? t($message, $placeholders) : $output;
1724
      dd($output);
1725
    }
1726
    else {
1727
      $output = empty($output) ? t($message, $placeholders) : $output;
1728
      dpm($output);
1729
    }
1730
  }
1731
  elseif (isset($GLOBALS['drupal_test_info'])) {
1732
    $output = empty($output) ? t($message, $placeholders) : $output;
1733
    debug($output);
1734
  }
1735
}
1736

    
1737
/**
1738
 * Shortcut to views_debug()
1739
 */
1740
function vpr($message, $placeholders = array()) {
1741
  views_debug($message, $placeholders);
1742
}
1743

    
1744
/**
1745
 * Debug messages
1746
 */
1747
function vsm($message) {
1748
  if (module_exists('devel')) {
1749
    dpm($message);
1750
  }
1751
}
1752

    
1753
function views_trace() {
1754
  $message = '';
1755
  foreach (debug_backtrace() as $item) {
1756
    if (!empty($item['file']) && !in_array($item['function'], array('vsm_trace', 'vpr_trace', 'views_trace'))) {
1757
      $message .= basename($item['file']) . ": " . (empty($item['class']) ? '' : ($item['class'] . '->')) . "$item[function] line $item[line]" . "\n";
1758
    }
1759
  }
1760
  return $message;
1761
}
1762

    
1763
function vsm_trace() {
1764
  vsm(views_trace());
1765
}
1766

    
1767
function vpr_trace() {
1768
  dpr(views_trace());
1769
}
1770

    
1771
// ------------------------------------------------------------------
1772
// Views form (View with form elements)
1773

    
1774
/**
1775
 * Returns TRUE if the passed-in view contains handlers with views form
1776
 * implementations, FALSE otherwise.
1777
 */
1778
function views_view_has_form_elements($view) {
1779
  foreach ($view->field as $field) {
1780
    if (property_exists($field, 'views_form_callback') || method_exists($field, 'views_form')) {
1781
      return TRUE;
1782
    }
1783
  }
1784
  $area_handlers = array_merge(array_values($view->header), array_values($view->footer));
1785
  $empty = empty($view->result);
1786
  foreach ($area_handlers as $area) {
1787
    if (method_exists($area, 'views_form') && !$area->views_form_empty($empty)) {
1788
      return TRUE;
1789
    }
1790
  }
1791
  return FALSE;
1792
}
1793

    
1794
/**
1795
 * This is the entry function. Just gets the form for the current step.
1796
 * The form is always assumed to be multistep, even if it has only one
1797
 * step (the default 'views_form_views_form' step). That way it is actually
1798
 * possible for modules to have a multistep form if they need to.
1799
 */
1800
function views_form($form, &$form_state, $view, $output) {
1801
  $form_state['step'] = isset($form_state['step']) ? $form_state['step'] : 'views_form_views_form';
1802
  // Cache the built form to prevent it from being rebuilt prior to validation
1803
  // and submission, which could lead to data being processed incorrectly,
1804
  // because the views rows (and thus, the form elements as well) have changed
1805
  // in the meantime.
1806
  $form_state['cache'] = TRUE;
1807

    
1808
  $form = array();
1809
  $query = drupal_get_query_parameters($_GET, array('q'));
1810
  $form['#action'] = url($view->get_url(), array('query' => $query));
1811
  // Tell the preprocessor whether it should hide the header, footer, pager...
1812
  $form['show_view_elements'] = array(
1813
    '#type' => 'value',
1814
    '#value' => ($form_state['step'] == 'views_form_views_form') ? TRUE : FALSE,
1815
  );
1816

    
1817
  $form = $form_state['step']($form, $form_state, $view, $output);
1818
  return $form;
1819
}
1820

    
1821
/**
1822
 * Callback for the main step of a Views form.
1823
 * Invoked by views_form().
1824
 */
1825
function views_form_views_form($form, &$form_state, $view, $output) {
1826
  $form['#prefix'] = '<div class="views-form">';
1827
  $form['#suffix'] = '</div>';
1828
  $form['#theme'] = 'views_form_views_form';
1829
  $form['#validate'][] = 'views_form_views_form_validate';
1830
  $form['#submit'][] = 'views_form_views_form_submit';
1831

    
1832
  // Add the output markup to the form array so that it's included when the form
1833
  // array is passed to the theme function.
1834
  $form['output'] = array(
1835
    '#type' => 'markup',
1836
    '#markup' => $output,
1837
    // This way any additional form elements will go before the view
1838
    // (below the exposed widgets).
1839
    '#weight' => 50,
1840
  );
1841

    
1842
  $substitutions = array();
1843
  foreach ($view->field as $field_name => $field) {
1844
    $form_element_name = $field_name;
1845
    if (method_exists($field, 'form_element_name')) {
1846
      $form_element_name = $field->form_element_name();
1847
    }
1848
    $method_form_element_row_id_exists = FALSE;
1849
    if (method_exists($field, 'form_element_row_id')) {
1850
      $method_form_element_row_id_exists = TRUE;
1851
    }
1852

    
1853
    // If the field provides a views form, allow it to modify the $form array.
1854
    $has_form = FALSE;
1855
    if (property_exists($field, 'views_form_callback')) {
1856
      $callback = $field->views_form_callback;
1857
      $callback($view, $field, $form, $form_state);
1858
      $has_form = TRUE;
1859
    }
1860
    elseif (method_exists($field, 'views_form')) {
1861
      $field->views_form($form, $form_state);
1862
      $has_form = TRUE;
1863
    }
1864

    
1865
    // Build the substitutions array for use in the theme function.
1866
    if ($has_form) {
1867
      foreach ($view->result as $row_id => $row) {
1868
        if ($method_form_element_row_id_exists) {
1869
          $form_element_row_id = $field->form_element_row_id($row_id);
1870
        }
1871
        else {
1872
          $form_element_row_id = $row_id;
1873
        }
1874

    
1875
        $substitutions[] = array(
1876
          'placeholder' => '<!--form-item-' . $form_element_name . '--' . $form_element_row_id . '-->',
1877
          'field_name' => $form_element_name,
1878
          'row_id' => $form_element_row_id,
1879
        );
1880
      }
1881
    }
1882
  }
1883

    
1884
  // Give the area handlers a chance to extend the form.
1885
  $area_handlers = array_merge(array_values($view->header), array_values($view->footer));
1886
  $empty = empty($view->result);
1887
  foreach ($area_handlers as $area) {
1888
    if (method_exists($area, 'views_form') && !$area->views_form_empty($empty)) {
1889
      $area->views_form($form, $form_state);
1890
    }
1891
  }
1892

    
1893
  $form['#substitutions'] = array(
1894
    '#type' => 'value',
1895
    '#value' => $substitutions,
1896
  );
1897
  $form['actions'] = array(
1898
    '#type' => 'container',
1899
    '#attributes' => array('class' => array('form-actions')),
1900
    '#weight' => 100,
1901
  );
1902
  $form['actions']['submit'] = array(
1903
    '#type' => 'submit',
1904
    '#value' => t('Save'),
1905
  );
1906

    
1907
  return $form;
1908
}
1909

    
1910
/**
1911
 * Validate handler for the first step of the views form.
1912
 * Calls any existing views_form_validate functions located
1913
 * on the views fields.
1914
 */
1915
function views_form_views_form_validate($form, &$form_state) {
1916
  $view = $form_state['build_info']['args'][0];
1917

    
1918
  // Call the validation method on every field handler that has it.
1919
  foreach ($view->field as $field_name => $field) {
1920
    if (method_exists($field, 'views_form_validate')) {
1921
      $field->views_form_validate($form, $form_state);
1922
    }
1923
  }
1924

    
1925
  // Call the validate method on every area handler that has it.
1926
  foreach (array('header', 'footer') as $area) {
1927
    foreach ($view->{$area} as $area_name => $area_handler) {
1928
      if (method_exists($area_handler, 'views_form_validate')) {
1929
        $area_handler->views_form_validate($form, $form_state);
1930
      }
1931
    }
1932
  }
1933
}
1934

    
1935
/**
1936
 * Submit handler for the first step of the views form.
1937
 * Calls any existing views_form_submit functions located
1938
 * on the views fields.
1939
 */
1940
function views_form_views_form_submit($form, &$form_state) {
1941
  $view = $form_state['build_info']['args'][0];
1942

    
1943
  // Call the submit method on every field handler that has it.
1944
  foreach ($view->field as $field_name => $field) {
1945
    if (method_exists($field, 'views_form_submit')) {
1946
      $field->views_form_submit($form, $form_state);
1947
    }
1948
  }
1949

    
1950
  // Call the submit method on every area handler that has it.
1951
  foreach (array('header', 'footer') as $area) {
1952
    foreach ($view->{$area} as $area_name => $area_handler) {
1953
      if (method_exists($area_handler, 'views_form_submit')) {
1954
        $area_handler->views_form_submit($form, $form_state);
1955
      }
1956
    }
1957
  }
1958
}
1959

    
1960
// ------------------------------------------------------------------
1961
// Exposed widgets form
1962

    
1963
/**
1964
 * Form builder for the exposed widgets form.
1965
 *
1966
 * Be sure that $view and $display are references.
1967
 */
1968
function views_exposed_form($form, &$form_state) {
1969
  // Don't show the form when batch operations are in progress.
1970
  if ($batch = batch_get() && isset($batch['current_set'])) {
1971
    return array(
1972
      // Set the theme callback to be nothing to avoid errors in template_preprocess_views_exposed_form().
1973
      '#theme' => '',
1974
    );
1975
  }
1976

    
1977
  // Make sure that we validate because this form might be submitted
1978
  // multiple times per page.
1979
  $form_state['must_validate'] = TRUE;
1980
  $view = &$form_state['view'];
1981
  $display = &$form_state['display'];
1982

    
1983
  $form_state['input'] = $view->get_exposed_input();
1984

    
1985
  // Let form plugins know this is for exposed widgets.
1986
  $form_state['exposed'] = TRUE;
1987
  // Check if the form was already created
1988
  if ($cache = views_exposed_form_cache($view->name, $view->current_display)) {
1989
    return $cache;
1990
  }
1991

    
1992
  $form['#info'] = array();
1993

    
1994
  if (!variable_get('clean_url', FALSE)) {
1995
    $form['q'] = array(
1996
      '#type' => 'hidden',
1997
      '#value' => $view->get_url(),
1998
    );
1999
  }
2000

    
2001
  // Go through each handler and let it generate its exposed widget.
2002
  foreach ($view->display_handler->handlers as $type => $value) {
2003
    foreach ($view->$type as $id => $handler) {
2004
      if ($handler->can_expose() && $handler->is_exposed()) {
2005
        // Grouped exposed filters have their own forms.
2006
        // Instead of render the standard exposed form, a new Select or
2007
        // Radio form field is rendered with the available groups.
2008
        // When an user choose an option the selected value is split
2009
        // into the operator and value that the item represents.
2010
        if ($handler->is_a_group()) {
2011
          $handler->group_form($form, $form_state);
2012
          $id = $handler->options['group_info']['identifier'];
2013
        }
2014
        else {
2015
          $handler->exposed_form($form, $form_state);
2016
        }
2017
        if ($info = $handler->exposed_info()) {
2018
          $form['#info']["$type-$id"] = $info;
2019
        }
2020
      }
2021
    }
2022
  }
2023

    
2024
  $form['submit'] = array(
2025
    '#name' => '', // prevent from showing up in $_GET.
2026
    '#type' => 'submit',
2027
    '#value' => t('Apply'),
2028
    '#id' => drupal_html_id('edit-submit-' . $view->name),
2029
  );
2030

    
2031
  $form['#action'] = url($view->display_handler->get_url());
2032
  $form['#theme'] = views_theme_functions('views_exposed_form', $view, $display);
2033
  $form['#id'] = drupal_clean_css_identifier('views_exposed_form-' . check_plain($view->name) . '-' . check_plain($display->id));
2034
//  $form['#attributes']['class'] = array('views-exposed-form');
2035

    
2036
  // If using AJAX, we need the form plugin.
2037
  if ($view->use_ajax) {
2038
    drupal_add_library('system', 'jquery.form');
2039
  }
2040
  ctools_include('dependent');
2041

    
2042
  $exposed_form_plugin = $form_state['exposed_form_plugin'];
2043
  $exposed_form_plugin->exposed_form_alter($form, $form_state);
2044

    
2045
  // Save the form
2046
  views_exposed_form_cache($view->name, $view->current_display, $form);
2047

    
2048
  return $form;
2049
}
2050

    
2051
/**
2052
 * Implement hook_form_alter for the exposed form.
2053
 *
2054
 * Since the exposed form is a GET form, we don't want it to send a wide
2055
 * variety of information.
2056
 */
2057
function views_form_views_exposed_form_alter(&$form, &$form_state) {
2058
  $form['form_build_id']['#access'] = FALSE;
2059
  $form['form_token']['#access'] = FALSE;
2060
  $form['form_id']['#access'] = FALSE;
2061
}
2062

    
2063
/**
2064
 * Validate handler for exposed filters
2065
 */
2066
function views_exposed_form_validate(&$form, &$form_state) {
2067
  foreach (array('field', 'filter') as $type) {
2068
    $handlers = &$form_state['view']->$type;
2069
    foreach ($handlers as $key => $handler) {
2070
      $handlers[$key]->exposed_validate($form, $form_state);
2071
    }
2072
  }
2073
  $exposed_form_plugin = $form_state['exposed_form_plugin'];
2074
  $exposed_form_plugin->exposed_form_validate($form, $form_state);
2075
}
2076

    
2077
/**
2078
 * Submit handler for exposed filters
2079
 */
2080
function views_exposed_form_submit(&$form, &$form_state) {
2081
  foreach (array('field', 'filter') as $type) {
2082
    $handlers = &$form_state['view']->$type;
2083
    foreach ($handlers as $key => $info) {
2084
      $handlers[$key]->exposed_submit($form, $form_state);
2085
    }
2086
  }
2087
  $form_state['view']->exposed_data = $form_state['values'];
2088
  $form_state['view']->exposed_raw_input = array();
2089

    
2090

    
2091
  $exclude = array('q', 'submit', 'form_build_id', 'form_id', 'form_token', 'exposed_form_plugin', '', 'reset');
2092
  $exposed_form_plugin = $form_state['exposed_form_plugin'];
2093
  $exposed_form_plugin->exposed_form_submit($form, $form_state, $exclude);
2094

    
2095
  foreach ($form_state['values'] as $key => $value) {
2096
    if (!in_array($key, $exclude)) {
2097
      $form_state['view']->exposed_raw_input[$key] = $value;
2098
    }
2099
  }
2100
}
2101

    
2102
/**
2103
 * Save the Views exposed form for later use.
2104
 *
2105
 * @param $views_name
2106
 *   String. The views name.
2107
 * @param $display_name
2108
 *   String. The current view display name.
2109
 * @param $form_output
2110
 *   Array (optional). The form structure. Only needed when inserting the value.
2111
 * @return
2112
 *   Array. The form structure, if any. Otherwise, return FALSE.
2113
 */
2114
function views_exposed_form_cache($views_name, $display_name, $form_output = NULL) {
2115
  // When running tests for exposed filters, this cache should
2116
  // be cleared between each test.
2117
  $views_exposed = &drupal_static(__FUNCTION__);
2118

    
2119
  // Save the form output
2120
  if (!empty($form_output)) {
2121
    $views_exposed[$views_name][$display_name] = $form_output;
2122
    return;
2123
  }
2124

    
2125
  // Return the form output, if any
2126
  return empty($views_exposed[$views_name][$display_name]) ? FALSE : $views_exposed[$views_name][$display_name];
2127
}
2128

    
2129
// ------------------------------------------------------------------
2130
// Misc helpers
2131

    
2132
/**
2133
 * Build a list of theme function names for use most everywhere.
2134
 */
2135
function views_theme_functions($hook, $view, $display = NULL) {
2136
  require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'views') . "/theme/theme.inc";
2137
  return _views_theme_functions($hook, $view, $display);
2138
}
2139

    
2140
/**
2141
 * Substitute current time; this works with cached queries.
2142
 */
2143
function views_views_query_substitutions($view) {
2144
  global $language_content;
2145
  return array(
2146
    '***CURRENT_VERSION***' => VERSION,
2147
    '***CURRENT_TIME***' => REQUEST_TIME,
2148
    '***CURRENT_LANGUAGE***' => $language_content->language,
2149
    '***DEFAULT_LANGUAGE***' => language_default('language'),
2150
  );
2151
}
2152

    
2153
/**
2154
 * Implements hook_query_TAG_alter().
2155
 *
2156
 * This is the hook_query_alter() for queries tagged by Views and is used to
2157
 * add in substitutions from hook_views_query_substitutions().
2158
 */
2159
function views_query_views_alter(QueryAlterableInterface $query) {
2160
  $substitutions = $query->getMetaData('views_substitutions');
2161
  $tables =& $query->getTables();
2162
  $where =& $query->conditions();
2163

    
2164
  // Replaces substitions in tables.
2165
  foreach ($tables as $table_name => $table_metadata) {
2166
    foreach ($table_metadata['arguments'] as $replacement_key => $value) {
2167
      if (isset($substitutions[$value])) {
2168
        $tables[$table_name]['arguments'][$replacement_key] = $substitutions[$value];
2169
      }
2170
    }
2171
  }
2172

    
2173
  // Replaces substitions in filter criterias.
2174
  _views_query_tag_alter_condition($query, $where, $substitutions);
2175
}
2176

    
2177
/**
2178
 * Replaces the substitutions recursive foreach condition.
2179
 */
2180
function _views_query_tag_alter_condition(QueryAlterableInterface $query, &$conditions, $substitutions) {
2181
  foreach ($conditions as $condition_id => &$condition) {
2182
    if (is_numeric($condition_id)) {
2183
      if (is_string($condition['field'])) {
2184
        $condition['field'] = str_replace(array_keys($substitutions), array_values($substitutions), $condition['field']);
2185
      }
2186
      elseif (is_object($condition['field'])) {
2187
        $sub_conditions =& $condition['field']->conditions();
2188
        _views_query_tag_alter_condition($query, $sub_conditions, $substitutions);
2189
      }
2190
      // $condition['value'] is a subquery so alter the subquery recursive.
2191
      // Therefore take sure to get the metadata of the main query.
2192
      if (is_object($condition['value'])) {
2193
        $subquery = $condition['value'];
2194
        $subquery->addMetaData('views_substitutions', $query->getMetaData('views_substitutions'));
2195
        views_query_views_alter($condition['value']);
2196
      }
2197
      elseif (isset($condition['value'])) {
2198
        $condition['value'] = str_replace(array_keys($substitutions), array_values($substitutions), $condition['value']);
2199
      }
2200
    }
2201
  }
2202
}
2203

    
2204
/**
2205
 * Embed a view using a PHP snippet.
2206
 *
2207
 * This function is meant to be called from PHP snippets, should one wish to
2208
 * embed a view in a node or something. It's meant to provide the simplest
2209
 * solution and doesn't really offer a lot of options, but breaking the function
2210
 * apart is pretty easy, and this provides a worthwhile guide to doing so.
2211
 *
2212
 * Note that this function does NOT display the title of the view. If you want
2213
 * to do that, you will need to do what this function does manually, by
2214
 * loading the view, getting the preview and then getting $view->get_title().
2215
 *
2216
 * @param $name
2217
 *   The name of the view to embed.
2218
 * @param $display_id
2219
 *   The display id to embed. If unsure, use 'default', as it will always be
2220
 *   valid. But things like 'page' or 'block' should work here.
2221
 * @param ...
2222
 *   Any additional parameters will be passed as arguments.
2223
 */
2224
function views_embed_view($name, $display_id = 'default') {
2225
  $args = func_get_args();
2226
  array_shift($args); // remove $name
2227
  if (count($args)) {
2228
    array_shift($args); // remove $display_id
2229
  }
2230

    
2231
  $view = views_get_view($name);
2232
  if (!$view || !$view->access($display_id)) {
2233
    return;
2234
  }
2235

    
2236
  return $view->preview($display_id, $args);
2237
}
2238

    
2239
/**
2240
 * Get the result of a view.
2241
 *
2242
 * @param string $name
2243
 *   The name of the view to retrieve the data from.
2244
 * @param string $display_id
2245
 *   The display id. On the edit page for the view in question, you'll find
2246
 *   a list of displays at the left side of the control area. "Master"
2247
 *   will be at the top of that list. Hover your cursor over the name of the
2248
 *   display you want to use. An URL will appear in the status bar of your
2249
 *   browser. This is usually at the bottom of the window, in the chrome.
2250
 *   Everything after #views-tab- is the display ID, e.g. page_1.
2251
 * @param ...
2252
 *   Any additional parameters will be passed as arguments.
2253
 * @return array
2254
 *   An array containing an object for each view item.
2255
 */
2256
function views_get_view_result($name, $display_id = NULL) {
2257
  $args = func_get_args();
2258
  array_shift($args); // remove $name
2259
  if (count($args)) {
2260
    array_shift($args); // remove $display_id
2261
  }
2262

    
2263
  $view = views_get_view($name);
2264
  if (is_object($view)) {
2265
    if (is_array($args)) {
2266
      $view->set_arguments($args);
2267
    }
2268
    if (is_string($display_id)) {
2269
      $view->set_display($display_id);
2270
    }
2271
    else {
2272
      $view->init_display();
2273
    }
2274
    $view->pre_execute();
2275
    $view->execute();
2276
    return $view->result;
2277
  }
2278
  else {
2279
    return array();
2280
  }
2281
}
2282

    
2283
/**
2284
 * Export a field.
2285
 */
2286
function views_var_export($var, $prefix = '', $init = TRUE) {
2287
  if (is_array($var)) {
2288
    if (empty($var)) {
2289
      $output = 'array()';
2290
    }
2291
    else {
2292
      $output = "array(\n";
2293
      foreach ($var as $key => $value) {
2294
        $output .= "  " . views_var_export($key, '', FALSE) . " => " . views_var_export($value, '  ', FALSE) . ",\n";
2295
      }
2296
      $output .= ')';
2297
    }
2298
  }
2299
  elseif (is_bool($var)) {
2300
    $output = $var ? 'TRUE' : 'FALSE';
2301
  }
2302
  elseif (is_string($var) && strpos($var, "\n") !== FALSE) {
2303
    // Replace line breaks in strings with a token for replacement
2304
    // at the very end. This protects multi-line strings from
2305
    // unintentional indentation.
2306
    $var = str_replace("\n", "***BREAK***", $var);
2307
    $output = var_export($var, TRUE);
2308
  }
2309
  else {
2310
    $output = var_export($var, TRUE);
2311
  }
2312

    
2313
  if ($prefix) {
2314
    $output = str_replace("\n", "\n$prefix", $output);
2315
  }
2316

    
2317
  if ($init) {
2318
    $output = str_replace("***BREAK***", "\n", $output);
2319
  }
2320

    
2321
  return $output;
2322
}
2323

    
2324
/**
2325
 * Prepare a string for use as a valid CSS identifier (element, class or ID name).
2326
 * This function is similar to a core version but with more sane filter values.
2327
 *
2328
 * http://www.w3.org/TR/CSS21/syndata.html#characters shows the syntax for valid
2329
 * CSS identifiers (including element names, classes, and IDs in selectors.)
2330
 *
2331
 * @param $identifier
2332
 *   The identifier to clean.
2333
 * @param $filter
2334
 *   An array of string replacements to use on the identifier.
2335
 * @return
2336
 *   The cleaned identifier.
2337
 *
2338
 * @see drupal_clean_css_identifier()
2339
 */
2340
function views_clean_css_identifier($identifier, $filter = array(' ' => '-', '/' => '-', '[' => '-', ']' => '')) {
2341
  // By default, we filter using Drupal's coding standards.
2342
  $identifier = strtr($identifier, $filter);
2343

    
2344
  // Valid characters in a CSS identifier are:
2345
  // - the hyphen (U+002D)
2346
  // - a-z (U+0030 - U+0039)
2347
  // - A-Z (U+0041 - U+005A)
2348
  // - the underscore (U+005F)
2349
  // - 0-9 (U+0061 - U+007A)
2350
  // - ISO 10646 characters U+00A1 and higher
2351
  // We strip out any character not in the above list.
2352
  $identifier = preg_replace('/[^\x{002D}\x{0030}-\x{0039}\x{0041}-\x{005A}\x{005F}\x{0061}-\x{007A}\x{00A1}-\x{FFFF}]/u', '', $identifier);
2353

    
2354
  return $identifier;
2355
}
2356

    
2357
/**
2358
 * Implement hook_views_exportables().
2359
 */
2360
function views_views_exportables($op = 'list', $views = NULL, $name = 'foo') {
2361
  $all_views = views_get_all_views();
2362
  if ($op == 'list') {
2363

    
2364
    foreach ($all_views as $name => $view) {
2365
      // in list, $views is a list of tags.
2366
      if (empty($views) || in_array($view->tag, $views)) {
2367
        $return[$name] = array(
2368
          'name' => check_plain($name),
2369
          'desc' => check_plain($view->description),
2370
          'tag' => check_plain($view->tag)
2371
        );
2372
      }
2373
    }
2374
    return $return;
2375
  }
2376

    
2377
  if ($op == 'export') {
2378
    $code = "/**\n";
2379
    $code .= " * Implement hook_views_default_views().\n";
2380
    $code .= " */\n";
2381
    $code .= "function " . $name . "_views_default_views() {\n";
2382
    foreach ($views as $view => $truth) {
2383
      $code .= "  /*\n";
2384
      $code .= "   * View " . var_export($all_views[$view]->name, TRUE) . "\n";
2385
      $code .= "   */\n";
2386
      $code .= $all_views[$view]->export('  ');
2387
      $code .= '  $views[$view->name] = $view;' . "\n\n";
2388
    }
2389
    $code .= "  return \$views;\n";
2390
    $code .= "}\n";
2391

    
2392
    return $code;
2393
  }
2394
}
2395

    
2396
/**
2397
 * #process callback to see if we need to check_plain() the options.
2398
 *
2399
 * Since FAPI is inconsistent, the #options are sanitized for you in all cases
2400
 * _except_ checkboxes. We have form elements that are sometimes 'select' and
2401
 * sometimes 'checkboxes', so we need decide late in the form rendering cycle
2402
 * if the options need to be sanitized before they're rendered. This callback
2403
 * inspects the type, and if it's still 'checkboxes', does the sanitation.
2404
 */
2405
function views_process_check_options($element, &$form_state) {
2406
  if ($element['#type'] == 'checkboxes' || $element['#type'] == 'checkbox') {
2407
    $element['#options'] = array_map('check_plain', $element['#options']);
2408
  }
2409
  return $element;
2410
}
2411

    
2412
/**
2413
 * Trim the field down to the specified length.
2414
 *
2415
 * @param $alter
2416
 *   - max_length: Maximum lenght of the string, the rest gets truncated.
2417
 *   - word_boundary: Trim only on a word boundary.
2418
 *   - ellipsis: Show an ellipsis (...) at the end of the trimmed string.
2419
 *   - html: Take sure that the html is correct.
2420
 *
2421
 * @param $value
2422
 *   The string which should be trimmed.
2423
 */
2424
function views_trim_text($alter, $value) {
2425
  if (drupal_strlen($value) > $alter['max_length']) {
2426
    $value = drupal_substr($value, 0, $alter['max_length']);
2427
    // TODO: replace this with cleanstring of ctools
2428
    if (!empty($alter['word_boundary'])) {
2429
      $regex = "(.*)\b.+";
2430
      if (function_exists('mb_ereg')) {
2431
        mb_regex_encoding('UTF-8');
2432
        $found = mb_ereg($regex, $value, $matches);
2433
      }
2434
      else {
2435
        $found = preg_match("/$regex/us", $value, $matches);
2436
      }
2437
      if ($found) {
2438
        $value = $matches[1];
2439
      }
2440
    }
2441
    // Remove scraps of HTML entities from the end of a strings
2442
    $value = rtrim(preg_replace('/(?:<(?!.+>)|&(?!.+;)).*$/us', '', $value));
2443

    
2444
    if (!empty($alter['ellipsis'])) {
2445
      $value .= t('...');
2446
    }
2447
  }
2448
  if (!empty($alter['html'])) {
2449
    $value = _filter_htmlcorrector($value);
2450
  }
2451

    
2452
  return $value;
2453
}
2454

    
2455
/**
2456
 * Adds one to each key of the array.
2457
 *
2458
 * For example array(0 => 'foo') would be array(1 => 'foo').
2459
 */
2460
function views_array_key_plus($array) {
2461
  $keys = array_keys($array);
2462
  rsort($keys);
2463
  foreach ($keys as $key) {
2464
    $array[$key+1] = $array[$key];
2465
    unset($array[$key]);
2466
  }
2467
  asort($array);
2468
  return $array;
2469
}
2470

    
2471
/**
2472
 * Report to CTools that we use hook_views_api instead of hook_ctools_plugin_api()
2473
 */
2474
function views_ctools_plugin_api_hook_name() {
2475
  return 'views_api';
2476
}
2477

    
2478
// Declare API compatibility on behalf of core modules:
2479

    
2480
/**
2481
 * Implements hook_views_api().
2482
 *
2483
 * This one is used as the base to reduce errors when updating.
2484
 */
2485
function views_views_api() {
2486
  return array(
2487
    // in your modules do *not* use views_api_version()!!!
2488
    'api' => views_api_version(),
2489
    'path' => drupal_get_path('module', 'views') . '/modules',
2490
  );
2491
}
2492

    
2493
if (!function_exists('aggregator_views_api')) {
2494
  function aggregator_views_api() { return views_views_api(); }
2495
}
2496

    
2497
if (!function_exists('book_views_api')) {
2498
  function book_views_api() { return views_views_api(); }
2499
}
2500

    
2501
if (!function_exists('comment_views_api')) {
2502
  function comment_views_api() { return views_views_api(); }
2503
}
2504

    
2505
if (!function_exists('field_views_api')) {
2506
  function field_views_api() { return views_views_api(); }
2507
}
2508

    
2509
if (!function_exists('file_views_api')) {
2510
  function file_views_api() { return views_views_api(); }
2511
}
2512

    
2513
if (!function_exists('filter_views_api')) {
2514
  function filter_views_api() { return views_views_api(); }
2515
}
2516

    
2517
if (!function_exists('image_views_api')) {
2518
  function image_views_api() { return views_views_api(); }
2519
}
2520

    
2521
if (!function_exists('locale_views_api')) {
2522
  function locale_views_api() { return views_views_api(); }
2523
}
2524

    
2525
if (!function_exists('node_views_api')) {
2526
  function node_views_api() { return views_views_api(); }
2527
}
2528

    
2529
if (!function_exists('poll_views_api')) {
2530
  function poll_views_api() { return views_views_api(); }
2531
}
2532

    
2533
if (!function_exists('profile_views_api')) {
2534
  function profile_views_api() { return views_views_api(); }
2535
}
2536

    
2537
if (!function_exists('search_views_api')) {
2538
  function search_views_api() { return views_views_api(); }
2539
}
2540

    
2541
if (!function_exists('statistics_views_api')) {
2542
  function statistics_views_api() { return views_views_api(); }
2543
}
2544

    
2545
if (!function_exists('system_views_api')) {
2546
  function system_views_api() { return views_views_api(); }
2547
}
2548

    
2549
if (!function_exists('tracker_views_api')) {
2550
  function tracker_views_api() { return views_views_api(); }
2551
}
2552

    
2553
if (!function_exists('taxonomy_views_api')) {
2554
  function taxonomy_views_api() { return views_views_api(); }
2555
}
2556

    
2557
if (!function_exists('translation_views_api')) {
2558
  function translation_views_api() { return views_views_api(); }
2559
}
2560

    
2561
if (!function_exists('user_views_api')) {
2562
  function user_views_api() { return views_views_api(); }
2563
}
2564

    
2565
if (!function_exists('contact_views_api')) {
2566
  function contact_views_api() { return views_views_api(); }
2567
}