Newer
Older
Earl Miles
committed
function views_init() {
// hook init is called even on cached pages, but we don't want to
// actually do anything in that case.
if (!function_exists('drupal_get_path')) {
return;
}
Earl Miles
committed
// Load all our module 'on behalfs'.
$path = drupal_get_path('module', 'views') . '/modules';
$files = system_listing('views_.*\.inc$', $path, 'name', 0);
foreach($files as $file) {
// The filename format is very specific. It must be views_MODULENAME.inc
$module = substr_replace($file->name, '', 0, 6);
if (module_exist($module)) {
require_once("./$file->filename");
Earl Miles
committed
}
}
}
// ---------------------------------------------------------------------------
// Acquire Views Data
Earl Miles
committed
/**
* Return the arguments array; construct one if we haven't already. The
* array is cached in a global, safely named variable so that arguments
* are only constructed once per run.
*/
function _views_get_arguments($titles = false) {
Earl Miles
committed
static $views_arguments;
Earl Miles
committed
if (!$views_arguments) {
$data = cache_get("views_arguments:$locale");
$cache = unserialize($data->data);
if (is_array($cache)) {
Earl Miles
committed
$views_arguments = $cache;
}
else {
Earl Miles
committed
$arguments = module_invoke_all('views_arguments');
// allow modules to alter the definitions supplied others
foreach (module_implements('views_arguments_alter') as $module) {
$function = $module . '_views_arguments_alter';
$function($arguments);
}
Earl Miles
committed
uasort($arguments, '_views_sort_arrays');
foreach ($arguments as $name => $arg) {
if ($arg['option'] && !is_array($arg['option'])) {
if ($arg['option'] == 'string' || $arg['option'] == 'integer') {
$arg['option'] = array('#type' => 'textfield', '#size' => 10, '#maxlength' => 255);
}
else {
$arg['option'] = array('#type' => 'select', '#options' => $arg['option']);
}
}
Earl Miles
committed
$views_arguments['base'][$name] = $arg['name'];
$views_arguments['title'][$name] = $arg;
Earl Miles
committed
$cache = $views_arguments;
cache_set("views_arguments:$locale", serialize($cache));
Earl Miles
committed
return ($titles ? $views_arguments['base'] : $views_arguments['title']);
Earl Miles
committed
/**
* Constructs the full table information array. Caches it into a global array
* so that it will only be called once per run.
*/
function _views_get_tables($full = false) {
Earl Miles
committed
static $views_tables;
Earl Miles
committed
if (!$views_tables) {
$cache = unserialize($data->data);
if (is_array($cache)) {
Earl Miles
committed
$views_tables = $cache;
else {
Earl Miles
committed
$table_data = module_invoke_all('views_tables');
// allow modules to alter the definitions supplied others
foreach (module_implements('views_tables_alter') as $module) {
$function = $module . '_views_tables_alter';
$function($table_data);
}
Earl Miles
committed
$views_tables['tables'] = $table_data;
foreach ($table_data as $name => $table) {
if (is_array($table['filters'])) {
foreach ($table['filters'] as $filter => $data) {
$data['table'] = $name;
// translate for deprecated APIs...
if ($data['option'] && !is_array($data['option'])) {
if ($data['option'] == 'string' || $data['option'] == 'integer') {
$data['option'] = array('#type' => 'textfield', '#size' => 10, '#maxlength' => 255);
}
else {
$data['option'] = array('#type' => 'select', '#options' => $data['option']);
}
}
if ($data['list']) {
$data['value'] = array('#type' => 'select', '#options' => $data['list'], '#validate' => array('views_filter_validate_array' => array($data['name'])));
if ($data['list-type'] != 'select') {
$data['value']['#multiple'] = TRUE;
}
}
else if (!$data['value']) {
$data['value'] = array('#type' => 'textfield', '#size' => 10, '#maxlength' => 255);
}
Earl Miles
committed
$views_tables['filters']['titles']["$name.$filter"] = $data['name'];
$views_tables['filters']['base']["$name.$filter"] = $data;
}
}
if (is_array($table['fields'])) {
foreach ($table['fields'] as $field => $data) {
if ($data['option'] && !is_array($data['option'])) {
if ($data['option'] == 'string' || $data['option'] == 'integer') {
$data['option'] = array('#type' => 'textfield', '#size' => 10, '#maxlength' => 255);
}
else {
$data['option'] = array('#type' => 'select', '#options' => $data['option']);
}
}
$data['table'] = $name;
Earl Miles
committed
$views_tables['fields']['titles']["$name.$field"] = $data['name'];
$views_tables['fields']['base']["$name.$field"] = $data;
}
}
if (is_array($table['sorts'])) {
foreach ($table['sorts'] as $field => $data) {
$data['table'] = $name;
if ($data['option'] && !is_array($data['option'])) {
if ($data['option'] == 'string' || $data['option'] == 'integer') {
$data['option'] = array('#type' => 'textfield', '#size' => 10, '#maxlength' => 255);
}
else {
$data['option'] = array('#type' => 'select', '#options' => $data['option']);
}
}
Earl Miles
committed
$views_tables['sorts']['titles']["$name.$field"] = $data['name'];
$views_tables['sorts']['base']["$name.$field"] = $data;
}
}
cache_set("views_tables:$locale", serialize($views_tables));
Earl Miles
committed
return ($full ? $views_tables : $views_tables['tables']);
Earl Miles
committed
/**
* Gets the filter information; if it doesn't exist, call the function
* that constructs all that.
*/
$table_data = _views_get_tables(true);
return ($titles ? $table_data['filters']['titles'] : $table_data['filters']['base']);
Earl Miles
committed
/**
* Gets the field information; if it doesn't exist, call the function
* that constructs all that.
*/
$table_data = _views_get_tables(true);
return ($titles ? $table_data['fields']['titles'] : $table_data['fields']['base']);
Earl Miles
committed
/**
* Gets the sort information; if it doesn't exist, call the function
* that constructs all that.
*/
$table_data = _views_get_tables(true);
return ($titles ? $table_data['sorts']['titles'] : $table_data['sorts']['base']);
}
Earl Miles
committed
/**
* Invalidate the views cache, forcing a rebuild on the next grab of table data.
*/
function views_invalidate_cache() {
cache_clear_all('views_', true);
Earl Miles
committed
/**
* Ensures that views have legitimate information; a bit more is stored on
* the $view object than is in the database, and this isn't necessarily
* set when a view is constructed externally.
*/
_views_check_arrays($view); // so reference works.
Earl Miles
committed
foreach ($view->field as $i => $field) {
if (!isset($view->field[$i]['id'])) {
$view->field[$i]['id'] = $view->field[$i]['fullname'] = "$field[tablename].$field[field]";
$view->field[$i]['queryname'] = "$field[tablename]_$field[field]";
}
Earl Miles
committed
foreach ($view->filter as $i => $filter) {
if (!isset($view->filter[$i]['id'])) {
$view->filter[$i]['id'] = $view->filter[$i]['field'] = "$filter[tablename].$filter[field]";
}
Earl Miles
committed
foreach ($view->exposed_filter as $i => $exposed_filter) {
if (!isset($view->exposed_filter[$i]['id'])) {
$view->exposed_filter[$i]['id'] = $view->exposed_filter[$i]['field'] = "$exposed_filter[tablename].$exposed_filter[field]";
}
Earl Miles
committed
foreach ($view->sort as $i => $sort) {
if (!isset($view->sort[$i]['id'])) {
$view->sort[$i]['id'] = $view->sort[$i]['field'] = "$sort[tablename].$sort[field]";
}
Earl Miles
committed
}
foreach ($view->argument as $i => $argument) {
if (!isset($view->argument[$i]['id'])) {
$view->argument[$i]['id'] = $view->argument[$i]['type'];
}
Earl Miles
committed
}
Earl Miles
committed
/**
* Build default view information from all modules and cache it.
*/
Earl Miles
committed
static $views_default_views;
Earl Miles
committed
if (!$views_default_views) {
$data = cache_get("views_default_views:$locale");
$cache = unserialize($data->data);
if (is_array($cache)) {
Earl Miles
committed
$views_default_views = $cache;
}
else {
// We have to make sure table data is built in order to be sure about providers.
$tables = array_keys(_views_get_tables());
Earl Miles
committed
$views = module_invoke_all('views_default_views');
Earl Miles
committed
uasort($views, '_views_sort_arrays');
Earl Miles
committed
$views_default_views = array();
foreach ($views as $i => $view) {
if (!is_array($view->requires) || !array_diff($view->requires, $tables)) {
Earl Miles
committed
$views_default_views[$i] = $view;
cache_set("views_default_views:$locale", serialize($views_default_views));
Earl Miles
committed
return $views_default_views;
Earl Miles
committed
/**
* sort callback
*/
function _views_sort_arrays($a, $b) {
$a = (array) $a; // safety -- something send objects.
$b = (array) $b;
Earl Miles
committed
if ($a['weight'] == $b['weight']) {
if ($a['name'] == $b['name']) {
return 0;
}
return ($a['name'] < $b['name']) ? -1 : 1;
}
return ($a['weight'] > $b['weight']) ? -1 : 1;
}
Earl Miles
committed
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
function _views_get_query(&$view, $args) {
if ($view->is_cacheable && ($cached = cache_get('views_query:' . $view->name))) {
$info = unserialize($cached->data);
$plugins = _views_get_style_plugins();
if ($plugins[$view->type]['needs_table_header']) {
$view->table_header = _views_construct_header($view, _views_get_fields());
}
}
else {
views_load_query();
$info = _views_build_query($view, $args);
if ($view->is_cacheable) {
$data = array(
'query' => _views_replace_args($info['query'], $info['args']),
'countquery' => _views_replace_args($info['countquery'], $info['args']),
);
cache_set('views_query:' . $view->name, serialize($data));
}
}
// Run-time replacement so we can do cacheing
$replacements = module_invoke_all('views_query_substitutions', $view);
foreach ($replacements as $src => $dest) {
$info['query'] = str_replace($src, $dest, $info['query']);
$info['countquery'] = str_replace($src, $dest, $info['countquery']);
if (is_array($info['args'])) {
foreach ($info['args'] as $id => $arg) {
$info['args'][$id] = str_replace($src, $dest, $arg);
}
}
}
return $info;
}
Earl Miles
committed
/**
* Return the style plugins; construct one if we haven't already. The
* array is cached in a static variable so that arguments
* are only constructed once per run.
*/
function _views_get_style_plugins($titles = false) {
static $views_style_plugins;
global $locale;
if (!$views_style_plugins) {
$data = cache_get("views_style_plugins:$locale");
$cache = unserialize($data->data);
if (is_array($cache)) {
$views_style_plugins = $cache;
}
else {
$arguments = module_invoke_all('views_style_plugins');
Earl Miles
committed
uasort($arguments, '_views_sort_arrays');
Earl Miles
committed
foreach ($arguments as $name => $arg) {
if (!isset($arg['summary_theme'])) {
$arg['summary_theme'] = 'views_summary';
}
Earl Miles
committed
$views_style_plugins['title'][$name] = $arg['name'];
$views_style_plugins['base'][$name] = $arg;
}
$cache = $views_style_plugins;
cache_set("views_style_plugins:$locale", serialize($cache));
}
}
return ($titles ? $views_style_plugins['title'] : $views_style_plugins['base']);
}
// ---------------------------------------------------------------------------
// Drupal Hooks
Earl Miles
committed
/**
* Implementation of hook_help()
*/
function views_help($section) {
switch ($section) {
case 'admin/help#views':
case 'admin/modules#description':
return t('The views module creates customized views of node lists.');
Earl Miles
committed
/**
* Implementation of hook_menu()
*/
function views_menu($may_cache) {
$items = array();
if ($may_cache) {
// Invalidate the views cache to ensure that views data gets rebuilt.
// This is the best way to tell that module configuration has changed.
if (arg(0) == 'admin' && arg(1) == 'modules') {
views_invalidate_cache();
}
Earl Miles
committed
$result = db_query("SELECT * FROM {view_view} WHERE page = 1");
Jeff Eaton
committed
while ($view = db_fetch_object($result)) {
// Skip views with arguments.
if (strrpos($view->url, '$arg') !== FALSE) {
continue;
}
// unpack the array
$view->access = ($view->access ? explode(', ', $view->access) : array());
Jeff Eaton
committed
// This happens before the next check; even if it's put off for later
// it is still used.
$used[$view->name] = true;
Earl Miles
committed
_views_create_menu_item($items, $view, $view->url);
Earl Miles
committed
$default_views = _views_get_default_views();
$views_status = variable_get('views_defaults', array());
Earl Miles
committed
foreach ($default_views as $name => $view) {
Jeff Eaton
committed
($views_status[$name] == 'enabled' || (!$view->disabled && $views_status[$name] != 'disabled'))) {
if (strrpos($view->url, '$arg') !== FALSE) {
continue;
}
Jeff Eaton
committed
Earl Miles
committed
_views_create_menu_item($items, $view, $view->url);
}
}
}
else {
$data = cache_get("views_with_inline_args:$locale");
if ($data == 0) {
// There's no cache for our language, regenerate it.
views_reset_inline_args_cache($locale);
$data = cache_get("views_with_inline_args:$locale");
}
Jeff Eaton
committed
$views = unserialize($data->data);
if (is_array($views)) {
foreach ($views as $view) {
// Do substitution on args.
Earl Miles
committed
$view_args = array();
$menu_path = array();
Jeff Eaton
committed
foreach (explode('/', $view->url) as $num => $element) {
if ($element == '$arg') {
$menu_path[] = arg($num);
$view_args[] = arg($num);
}
else {
Jeff Eaton
committed
$menu_path[] = $element;
}
}
$path = implode('/', $menu_path);
Earl Miles
committed
_views_create_menu_item($items, $view, $path, MENU_CALLBACK, $view_args);
}
return $items;
}
/**
* Reset the views with inline arguments cache for a locale.
*/
function views_reset_inline_args_cache($locale = 'en') {
$result = db_query("SELECT * FROM {view_view} WHERE page = 1");
$views_with_inline_args = array();
while ($view = db_fetch_object($result)) {
// Skip over any non-argument views
if (strrpos($view->url, '$arg') === FALSE) {
continue;
}
// unpack the array
$view->access = ($view->access ? explode(', ', $view->access) : array());
// This happens before the next check; even if it's put off for later
// it is still used.
$used[$view->name] = true;
$arg_result = db_query("SELECT * FROM {view_argument} WHERE vid = %d", $view->vid);
while ($arg = db_fetch_array($arg_result)) {
$view->argument[] = $arg;
}
$views_with_inline_args[$view->name] = $view;
}
$default_views = _views_get_default_views();
$views_status = variable_get('views_defaults', array());
foreach ($default_views as $name => $view) {
if ($view->page && !$used[$name] && ($views_status[$name] == 'enabled' || (!$view->disabled && $views_status[$name] != 'disabled'))) {
if (strrpos($view->url, '$arg') !== FALSE) {
$views_with_inline_args[$view->name] = $view;
}
}
}
cache_set("views_with_inline_args:$locale", serialize($views_with_inline_args));
}
Earl Miles
committed
/**
* Helper function to add a menu item for a view.
*/
Earl Miles
committed
function _views_create_menu_item(&$items, $view, $path, $local_task_type = MENU_NORMAL_ITEM, $args = array()) {
$title = filter_xss_admin(views_get_title($view, 'menu'));
Jeff Eaton
committed
$type = _views_menu_type($view);
if ($type == MENU_LOCAL_TASK || $type == MENU_DEFAULT_LOCAL_TASK) {
$weight = $view->menu_tab_weight;
}
Earl Miles
committed
$items[] = _views_menu_item($path, $title, $view, $args, $access, $type, $weight);
Jeff Eaton
committed
if ($type == MENU_DEFAULT_LOCAL_TASK) {
Earl Miles
committed
$items[] = _views_menu_item(dirname($path), $title, $view, $args, $access, $local_task_type, $weight);
Jeff Eaton
committed
}
}
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/**
* Implementation of hook_perm
*/
function views_perm() {
return array('access all views');
}
/**
* Determine if the specified user has access to a view.
*/
function views_access($view, $account = NULL) {
if (!$account) {
global $user;
$account = $user;
}
// Administrator privileges
if (user_access('access all views', $account)) {
return TRUE;
}
// All views with an empty access setting are available to all roles.
if (!$view->access) {
return TRUE;
}
// Otherwise, check roles
static $roles = array();
if (!isset($roles[$account->uid])) {
$roles[$account->uid] = array_keys($account->roles);
}
return array_intersect($view->access, $roles[$account->uid]);
}
Earl Miles
committed
/**
* Helper function to create a menu item for a view.
*/
Earl Miles
committed
function _views_menu_item($path, $title, $view, $args, $access, $type, $weight = NULL) {
array_unshift($args, $view->name);
$retval = array('path' => $path,
Jeff Eaton
committed
'title' => $title,
'callback' => 'views_view_page',
'callback arguments' => $args,
Earl Miles
committed
'access' => user_access('access content') && $access,
Jeff Eaton
committed
'type' => $type,
);
if ($weight !== NULL) {
$retval['weight'] = $weight;
}
return $retval;
Jeff Eaton
committed
}
Earl Miles
committed
/**
* Determine what menu type a view needs to use.
*/
function _views_menu_type($view) {
if ($view->menu) {
if ($view->menu_tab_default) {
$type = MENU_DEFAULT_LOCAL_TASK;
}
else if ($view->menu_tab) {
$type = MENU_LOCAL_TASK;
}
else {
$type = MENU_NORMAL_ITEM;
}
}
else {
$type = MENU_CALLBACK;
}
return $type;
}
Earl Miles
committed
/**
* Implementation of hook_block()
*/
function views_block($op = 'list', $delta = 0) {
$block = array();
if ($op == 'list') {
// Grab views from the database and provide them as blocks.
$result = db_query("SELECT vid, block_title, page_title, name FROM {view_view} WHERE block = 1");
while ($view = db_fetch_object($result)) {
$block[$view->name]['info'] = filter_xss_admin(views_get_title($view, 'block-info'));
Earl Miles
committed
$default_views = _views_get_default_views();
$views_status = variable_get('views_defaults', array());
Earl Miles
committed
foreach ($default_views as $name => $view) {
if (!isset($block[$name]) && $view->block &&
($views_status[$name] == 'enabled' || (!$view->disabled && $views_status[$name] != 'disabled'))) {
$title = filter_xss_admin(views_get_title($view, 'block'));
$block[$name]['info'] = empty($title) ? $name : $title;
}
return $block;
}
else if ($op == 'view') {
return views_view_block($delta);
}
}
// ---------------------------------------------------------------------------
// View Construction
Earl Miles
committed
/**
* Ensure that all the arrays in a view exist so we don't run into array
* operations on a non-array error.
*/
$fields = array('field', 'sort', 'argument', 'filter', 'exposed_filter', 'access');
Earl Miles
committed
foreach($fields as $field) {
if (!is_array($view->$field)) {
$view->$field = array();
}
}
return $view;
Earl Miles
committed
/**
* This function loads a view by name or vid; if not found in db, it looks
* for a default view by that name.
*/
Earl Miles
committed
function views_get_view($view_name) {
$view = _views_load_view($view_name);
if ($view) {
Earl Miles
committed
if (is_int($view_name)) {
return; // don't bother looking if view_name is an int!
Earl Miles
committed
$default_views = _views_get_default_views();
Earl Miles
committed
if (isset($default_views[$view_name])) {
Earl Miles
committed
$view = $default_views[$view_name];
$view->is_cacheable = _views_is_cacheable($view);
return $view;
Earl Miles
committed
/**
* This views a view by page, and should only be used as a callback.
Earl Miles
committed
*
* @param $view_name
* The name or id of the view to load
* @param $args
* The arguments from the end of the url. Usually passed from the menu system.
*
* @return
* The view page.
function views_view_page() {
$args = func_get_args();
Earl Miles
committed
$view_name = array_shift($args);
$view = views_get_view($view_name);
if (!$view) {
drupal_not_found();
exit;
}
$output = views_build_view('page', $view, $args, $view->use_pager, $view->nodes_per_page);
if ($output === FALSE) {
drupal_not_found();
exit;
}
Earl Miles
committed
/**
* This views a view by block. Can be used as a callback or programmatically.
*/
function views_view_block($vid) {
$view = views_get_view($vid);
if (!$view || !$view->block) {
return NULL;
return NULL;
}
$content = views_build_view('block', $view, array(), false, $view->nodes_per_block);
if ($content) {
$block['content'] = $content;
$block['subject'] = filter_xss_admin(views_get_title($view, 'block'));
return $block;
}
else {
return NULL;
}
Earl Miles
committed
/**
* This builds the basic view.
* @param $type
* 'page' -- Produce output as a page, sent through theme.
* The only real difference between this and block is that
* a page uses drupal_set_title to change the page title.
* 'block' -- Produce output as a block, sent through theme.
* 'embed' -- Use this if you want to embed a view onto another page,
* and don't want any block or page specific things to happen to it.
* 'result' -- return an $info array. The array contains:
* query: The actual query ran.
* countquery: The count query that would be run if limiting was required.
* summary: True if an argument was missing and a summary was generated.
* level: What level the missing argument was at.
* result: Database object you can use db_fetch_object on.
* 'items' -- return info array as above, except instead of result,
* items: An array of objects containing the results of the query.
* 'queries' -- returns an array, summarizing the queries, but does not
* run them.
* @param $view
* The actual view object. Use views_get_view() if you only have the name or
* vid.
* @param $args
* args taken from the URL. Not relevant for many views. Can be null.
* @param $use_pager
* If set, use a pager. Set this to the pager id you want it
* to use if you plan on using multiple pagers on a page. To go with the
* default setting, set to $view->use_pager. Note that the pager element
* id will be decremented in order to have the IDs start at 0.
* @param $limit
* Required if $use_pager is set; if $limit is set and $use_pager is
* not, this will be the maximum number of records returned. This is ignored
* if using a view set to return a random result. To go with the default
* setting set to $view->nodes_per_page or $view->nodes_per_block. If
* $use_pager is set and this field is not, you'll get a SQL error. Don't
* do that!
* @param $page
* $use_pager is false, and $limit is !0, $page tells it what page to start
* on, in case for some reason a particular section of view is needed,
* without paging on.
Earl Miles
committed
function views_build_view($type, &$view, $args = array(), $use_pager = false, $limit = 0, $page = 0, $offset = 0) {
// Fix a number of annoying whines when NULL is passed in..
if ($args == NULL) {
$args = array();
}
$GLOBALS['current_view'] = &$view;
$view->build_type = $type;
$view->type = ($type == 'block' ? $view->block_type : $view->page_type);
if ($view->view_args_php) {
ob_start();
Earl Miles
committed
$result = eval($view->view_args_php);
if (is_array($result)) {
$args = $result;
}
ob_end_clean();
}
Earl Miles
committed
// Call a hook that'll let modules modify the view query before it is created
foreach (module_implements('views_pre_query') as $module) {
$function = $module .'_views_pre_query';
$output .= $function($view);
}
Earl Miles
committed
$info = _views_get_query($view, $args);
if ($info['fail']) {
return FALSE;
if ($type == 'queries') {
return $info;
}
Earl Miles
committed
$query = db_rewrite_sql($info['query'], 'node');
$items = array();
Earl Miles
committed
if ($query) {
if ($use_pager) {
$cquery = db_rewrite_sql($info['countquery'], 'node', 'nid', $info['rewrite_args']);
$result = pager_query($query, $limit, $use_pager - 1, $cquery, $info['args']);
$view->total_rows = $GLOBALS['pager_total_items'][$use_pager - 1];
}
else {
$result = ($limit ? db_query_range($query, $info['args'], $page * $limit + $offset, $limit) : db_query($query, $info['args']));
}
$view->num_rows = db_num_rows($result);
if ($type == 'result') {
$info['result'] = $result;
return $info;
}
while ($item = db_fetch_object($result)) {
$items[] = $item;
}
if ($type == 'items') {
$info['items'] = $items;
return $info;
}
// Call a hook that'll let modules modify the view just before it is displayed.
foreach (module_implements('views_pre_view') as $module) {
$function = $module .'_views_pre_view';
Earl Miles
committed
$output .= $function($view, $items);
$view->real_url = views_get_url($view, $args);
Earl Miles
committed
$view->use_pager = $use_pager;
Earl Miles
committed
$output .= views_theme('views_view', $view, $type, $items, $info['level'], $args);
// Call a hook that'll let modules modify the view just after it is displayed.
foreach (module_implements('views_post_view') as $module) {
$function = $module .'_views_post_view';
Earl Miles
committed
$output .= $function($view, $items, $output);
// ---------------------------------------------------------------------------
// Utility
Earl Miles
committed
/**
* Load the query sub-module
*/
function views_load_query() {
$path = drupal_get_path('module', 'views');
require_once("./$path/views_query.inc");
}
Earl Miles
committed
* Easily theme any item to a view.
Earl Miles
committed
* The name of the function to call.
* @param $view
* The view being themed.
*/
function views_theme() {
$args = func_get_args();
$function = array_shift($args);
Earl Miles
committed
if (!($func = theme_get_function($function . "_" . $view->name))) {
$func = theme_get_function($function);
}
if ($func) {
return call_user_func_array($func, $args);
}
}
/**
* Easily theme any item to a field name.
* field name will be in the format of TABLENAME_FIELDNAME
* You have to understand a bit about the views data to utilize this.
*
* @param $function
* The name of the function to call.
* @param $field_name
* The field being themed.
*/
function views_theme_field() {
$args = func_get_args();
$function = array_shift($args);
$field_name = array_shift($args);
$view = array_pop($args);
if (!($func = theme_get_function($function . '_' . $view->name . '_' . $field_name))) {
if (!($func = theme_get_function($function . '_' . $field_name))) {
$func = theme_get_function($function);
}
}
if ($func) {
return call_user_func_array($func, $args);
}
}
Earl Miles
committed
/**
* Figure out what timezone we're in; needed for some date manipulations.
function _views_get_timezone() {
global $user;
if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
$timezone = $user->timezone;
}
else {
$timezone = variable_get('date_default_timezone', 0);
// set up the database timezone
if (in_array($GLOBALS['db_type'], array('mysql', 'mysqli'))) {
static $already_set = false;
if (!$already_set) {
if ($GLOBALS['db_type'] == 'mysqli' || version_compare(mysql_get_server_info(), '4.1.3', '>=')) {
db_query("SET @@session.time_zone = '+00:00'");
}
$already_set = true;
}
}
return $timezone;
/**
* Figure out what the URL of the view we're currently looking at is.
*/
function views_get_url($view, $args) {
$url = $view->url;
if (!empty($url)) {
$where = 1;
foreach ($args as $arg) {
// This odd construct prevents us from strposing once there is no
// longer an $arg to replace.
Earl Miles
committed
if ($where && $where = strpos($url, '$arg')) {
$url = substr_replace($url, $arg, $where, 4);
}
else {
$url .= "/$arg";
}
}
}
return $url;
}
Earl Miles
committed
/**
* Figure out what the title of a view should be.
*/
function views_get_title($view, $context = 'menu', $args = NULL) {
if (($context == 'menu' || $context == 'admin' )&& $view->menu_title)
return $view->menu_title;
if ($context == 'block-info') {
return $view->description ? $view->description : $view->name;
}
Earl Miles
committed
if ($args === NULL) {
$args = $view->args;
Earl Miles
committed
}
Earl Miles
committed
// Grab the title from the highest argument we got. If there is no such
// title, track back until we find a title.
if (is_array($args)) {
$rargs = array_reverse(array_keys($args));
foreach ($rargs as $arg_id) {
if ($title = $view->argument[$arg_id]['title']) {
break;
}
}
Earl Miles
committed
if (!$title && ($context == 'menu' || $context == 'page' || $context == 'admin')) {
$title = $view->page_title;
}
if (!$title && ($context == 'block' || $context == 'admin')) {
$title = $view->block_title;
Earl Miles
committed
}
if (!$view->argument) {
return $title;
}
$arginfo = _views_get_arguments();
foreach ($view->argument as $i => $arg) {
if (!isset($args[$i])) {
break;
}
$argtype = $arg['type'];
Earl Miles
committed
if ($arg['wildcard'] == $args[$i] && $arg['wildcard_substitution'] != '') {
$title = str_replace("%" . ($i + 1), $arg['wildcard_substitution'], $title);
}
else if (function_exists($arginfo[$argtype]['handler'])) {
// call the handler
$rep = $arginfo[$argtype]['handler']('title', $args[$i], $argtype);
$title = str_replace("%" . ($i + 1), $rep, $title);
}
}
return $title;
}
Earl Miles
committed
/**
* Determine whether or not a view is cacheable. A view is not cacheable if
* there is some kind of user input or data required. For example, views
* that need to restrict to the 'current' user, or any views that require
* arguments or allow click-sorting are not cacheable.
*/
function _views_is_cacheable(&$view) {
// views with arguments are immediately not cacheable.
if (!empty($view->argument) || !empty($view->exposed_filter) || !empty($view->no_cache)) {
$filters = _views_get_filters();
foreach ($view->filter as $i => $filter) {
if ($filters[$filter['field']]['cacheable'] == 'no') {