*/
/**
+ * Advertise the current views api version
+ */
+function views_api_version() {
+ return 2.0;
+}
+
+/**
* Implementation of hook_theme(). Register views theming functions.
*/
function views_theme() {
$path = drupal_get_path('module', 'views');
require_once "./$path/theme/theme.inc";
- // Some quasi clever array merging here.
+ // Some quasi clever array merging here.
$base = array(
'file' => 'theme.inc',
'path' => "$path/theme",
// Our extra version of pager from pager.inc
$hooks['views_mini_pager'] = $base + array(
'arguments' => array('tags' => array(), 'limit' => 10, 'element' => 0, 'parameters' => array()),
+ 'pattern' => 'views_mini_pager__',
);
$arguments = array(
'display' => array('view' => NULL),
'style' => array('view' => NULL, 'options' => NULL, 'rows' => NULL, 'title' => NULL),
- 'row' => array('view' => NULL, 'options' => NULL, 'row' => NULL),
+ 'row' => array('view' => NULL, 'options' => NULL, 'row' => NULL, 'field_alias' => NULL),
);
// Default view themes
if (isset($def['theme'])) {
$hooks[$def['theme']] = array(
'pattern' => $def['theme'] . '__',
- 'file' => $def['file'],
- 'path' => $def['path'],
+ 'file' => $def['theme file'],
+ 'path' => $def['theme path'],
'arguments' => $arguments[$type],
);
+
+ $include = './' . $def['theme path'] . '/' . $def['theme file'];
+ if (file_exists($include)) {
+ require_once $include;
+ }
+
if (!function_exists('theme_' . $def['theme'])) {
$hooks[$def['theme']]['template'] = views_css_safe($def['theme']);
}
$hooks[$theme] = array(
'pattern' => $theme . '__',
- 'file' => $def['file'],
- 'path' => $def['path'],
+ 'file' => $def['theme file'],
+ 'path' => $def['theme path'],
'arguments' => $arguments[$theme_type],
);
+
if (!function_exists('theme_' . $theme)) {
$hooks[$theme]['template'] = views_css_safe($theme);
}
$hooks['views_more'] = $base + array(
'template' => 'views-more',
'pattern' => 'views_more__',
- 'arguments' => array('more_url' => NULL),
+ 'arguments' => array('more_url' => NULL, 'link_text' => 'more'),
);
return $hooks;
}
+/**
+ * A theme preprocess function to automatically allow view-based node
+ * templates if called from a view.
+ *
+ * The 'modules/node.views.inc' file is a better place for this, but
+ * we haven't got a chance to load that file before Drupal builds the
+ * node portion of the theme registry.
+ */
+function views_preprocess_node(&$vars) {
+ // The 'view' attribute of the node is added in template_preprocess_views_view_row_node()
+ if (!empty($vars['node']->view) && !empty($vars['node']->view->name)) {
+ $vars['view'] = &$vars['node']->view;
+ $vars['template_files'][] = 'node-view-' . $vars['node']->view->name;
+ if(!empty($vars['node']->view->current_display)) {
+ $vars['template_files'][] = 'node-view-' . $vars['node']->view->name . '-' . $vars['node']->view->current_display;
+ }
+ }
+}
+
+/**
+ * A theme preprocess function to automatically allow view-based node
+ * templates if called from a view.
+ */
+function views_preprocess_comment(&$vars) {
+ // The 'view' attribute of the node is added in template_preprocess_views_view_row_comment()
+ if (!empty($vars['node']->view) && !empty($vars['node']->view->name)) {
+ $vars['view'] = &$vars['node']->view;
+ $vars['template_files'][] = 'comment-view-' . $vars['node']->view->name;
+ if(!empty($vars['node']->view->current_display)) {
+ $vars['template_files'][] = 'comment-view-' . $vars['node']->view->name . '-' . $vars['node']->view->current_display;
+ }
+ }
+}
+
/*
* Implementation of hook_perm()
*/
-function views_ui_perm() {
- return array('access all views');
+function views_perm() {
+ return array('access all views', 'administer views');
}
/**
$items['views/ajax'] = array(
'title' => 'Views',
'page callback' => 'views_ajax',
+ 'access callback' => TRUE,
+ 'description' => 'Ajax callback for view loading.',
'file' => 'includes/ajax.inc',
+ 'type' => MENU_CALLBACK,
+ );
+ // Path is not admin/build/views due to menu complications with the wildcards from
+ // the generic ajax callback.
+ $items['admin/views/ajax/autocomplete/user'] = array(
+ 'page callback' => 'views_ajax_autocomplete_user',
'access callback' => 'user_access',
'access arguments' => array('access content'),
- 'description' => 'Ajax callback for view loading.',
+ 'file' => 'includes/ajax.inc',
'type' => MENU_CALLBACK,
);
return $items;
// in the display path, then we use this constructed regex
// (which will be something like '#^(foo/%[^/]*/bar)$#') to
// search through the existing paths.
- $regex = '#^('. preg_replace('#%views_arg#', '%[^/]*', implode('|', array_keys($result))) .')$#';
+ $regex = '#^(' . preg_replace('#%views_arg#', '%[^/]*', implode('|', array_keys($result))) . ')$#';
$matches = preg_grep($regex, array_keys($callbacks));
// Remove any conflicting items that were found.
// of possible display IDs instead of a single ID.
$callbacks[$path]['page arguments'][1] = (array)$callbacks[$path]['page arguments'][1];
$callbacks[$path]['page arguments'][1][] = $display_id;
- $callbacks[$path]['access arguments'][0][1] = (array)$callbacks[$path]['access arguments'][0][1];
- $callbacks[$path]['access arguments'][0][1][] = $display_id;
+ $callbacks[$path]['access arguments'][] = $item['access arguments'][0];
$callbacks[$path]['load arguments'][1] = (array)$callbacks[$path]['load arguments'][1];
$callbacks[$path]['load arguments'][1][] = $display_id;
}
$ids = array_keys($view->argument);
$indexes = array();
- $path = explode('/', $view->get_url());
+ $path = explode('/', $view->get_path());
+
foreach ($path as $id => $piece) {
if ($piece == '%' && !empty($ids)) {
$indexes[$id] = array_shift($ids);
switch ($op) {
case 'list':
$items = array();
- $views = views_get_applicable_views('uses hook block');
- foreach ($views as $data) {
- list($view, $display_id) = $data;
- $result = $view->execute_hook_block($display_id);
- if (is_array($result)) {
- $items = array_merge($items, $result);
+ $views = views_get_all_views();
+ foreach ($views as $view) {
+ // disabled views get nothing.
+ if (!empty($view->disabled)) {
+ continue;
+ }
+
+ $view->init_display();
+ foreach ($view->display as $display_id => $display) {
+
+ if (isset($display->handler) && !empty($display->handler->definition['uses hook block'])) {
+ $result = $display->handler->execute_hook_block();
+ if (is_array($result)) {
+ $items = array_merge($items, $result);
+ }
+ }
+
+ if (isset($display->handler) && $display->handler->get_option('exposed_block')) {
+ $result = $display->handler->get_special_blocks();
+ if (is_array($result)) {
+ $items = array_merge($items, $result);
+ }
+ }
}
}
variable_set('views_block_hashes', $hashes);
// Save memory: Destroy those views.
- foreach ($views as $data) {
- list($view, $display_id) = $data;
+ foreach ($views as $view) {
$view->destroy();
}
return $items;
case 'view':
- $start = microtime();
+ $start = views_microtime();
// if this is 32, this should be an md5 hash.
if (strlen($delta) == 32) {
$hashes = variable_get('views_block_hashes', array());
}
}
+ // This indicates it's a special one.
+ if (substr($delta, 0, 1) == '-') {
+ list($nothing, $type, $name, $display_id) = explode('-', $delta);
+ // Put the - back on.
+ $type = '-' . $type;
+ if ($view = views_get_view($name)) {
+ if ($view->access($display_id)) {
+ $view->set_display($display_id);
+ if (isset($view->display_handler)) {
+ $output = $view->display_handler->view_special_blocks($type);
+ $view->destroy();
+ return $output;
+ }
+ }
+ $view->destroy();
+ }
+ }
+
list($name, $display_id) = explode('-', $delta);
// Load the view
if ($view = views_get_view($name)) {
if ($view->access($display_id)) {
$output = $view->execute_display($display_id);
- vpr("Block $view->name execute time: " . (microtime() - $start) * 1000 . "ms");
+ vpr("Block $view->name execute time: " . (views_microtime() - $start) * 1000 . "ms");
$view->destroy();
return $output;
}
+ $view->destroy();
}
break;
}
* Implementation of hook_flush_caches().
*/
function views_flush_caches() {
- return array('cache_views');
+ return array('cache_views', 'cache_views_data');
}
/**
}
/**
- * Determine if the given user has access to the view + display.
+ * Determine if the logged in user has access to a view.
*
- * @param $view
- * May be a view object, or an array with the view name and the display ID,
- * or a string to use as the view name.
- * @param $account
- * An optional account to use; if left off, the current user will be used.
- */
-function views_access($view, $account = NULL) {
- if (is_array($view)) {
- list($name, $display_id) = $view;
- $view = views_get_view($name);
- if (!$view) {
- return FALSE;
- }
+ * This function should only be called from a menu hook or some other
+ * embedded source. Each argument is the result of a call to
+ * views_plugin_access::get_access_callback() which is then used
+ * to determine if that display is accessible. If *any* argument
+ * is accessible, then the view is accessible.
+ */
+function views_access() {
+ if (user_access('access all views')) {
+ return TRUE;
}
- elseif (is_string($view)) {
- $view = views_get_view($view);
- if (!$view) {
- return FALSE;
+
+ $args = func_get_args();
+ foreach ($args as $arg) {
+ if ($arg === TRUE) {
+ return TRUE;
+ }
+
+ if (!is_array($arg)) {
+ continue;
+ }
+
+ list($callback, $arguments) = $arg;
+ if (function_exists($callback) && call_user_func_array($callback, $arguments)) {
+ return TRUE;
}
- $display_id = 'default';
- }
- else {
- // Clone the view to prevent problems.
- $view = $view->clone_view();
- $display_id = isset($view->current_display) ? $view->current_display : 'default';
}
- $access = $view->access($display_id, $account);
- $view->destroy();
- return $access;
+ return FALSE;
}
+/**
+ * Access callback to determine if the logged in user has any of the
+ * requested roles.
+ *
+ * This must be in views.module as it is called by menu access callback
+ * and can be called often.
+ */
+function views_check_roles($rids) {
+ global $user;
+ $roles = array_keys($user->roles);
+ $roles[] = $user->uid ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID;
+ return array_intersect(array_filter($rids), $roles);
+}
// ------------------------------------------------------------------
// Functions to help identify views that are running or ran
* Load views files on behalf of modules.
*/
function views_module_include($file) {
- $views_path = drupal_get_path('module', 'views') . '/modules';
- foreach (module_list() as $module) {
- $module_path = drupal_get_path('module', $module);
- if (file_exists("$module_path/$module.$file")) {
- require_once "./$module_path/$module.$file";
+ foreach (views_get_module_apis() as $module => $info) {
+ if (file_exists("./$info[path]/$module.$file")) {
+ require_once "./$info[path]/$module.$file";
}
- else if (file_exists("$module_path/includes/$module.$file")) {
- require_once "./$module_path/includes/$module.$file";
- }
- else if (file_exists("$views_path/$module.$file")) {
- require_once "./$views_path/$module.$file";
+ }
+}
+
+/**
+ * Get a list of modules that support the current views API.
+ */
+function views_get_module_apis() {
+ static $cache = NULL;
+ if (!isset($cache)) {
+ $cache = array();
+ foreach (module_implements('views_api') as $module) {
+ $function = $module . '_views_api';
+ $info = $function();
+ if (isset($info['api']) && $info['api'] == 2.000) {
+ if (!isset($info['path'])) {
+ $info['path'] = drupal_get_path('module', $module);
+ }
+ $cache[$module] = $info;
+ }
}
}
+
+ return $cache;
}
/**
* Include views .css files.
*/
function views_add_css($file) {
- drupal_add_css(drupal_get_path('module', 'views') . "/css/$file.css");
+ // We set preprocess to FALSE because we are adding the files conditionally,
+ // and we don't want to generate duplicate cache files.
+ // TODO: at some point investigate adding some files unconditionally and
+ // allowing preprocess.
+ drupal_add_css(drupal_get_path('module', 'views') . "/css/$file.css", 'module', 'all', FALSE);
}
/**
* Include views .js files.
*/
function views_add_js($file) {
+ // If javascript has been disabled by the user, never add js files.
+ if (variable_get('views_no_javascript', FALSE)) {
+ return;
+ }
+
static $base = TRUE;
if ($base) {
drupal_add_js(drupal_get_path('module', 'views') . "/js/base.js");
+ $base = FALSE;
}
drupal_add_js(drupal_get_path('module', 'views') . "/js/$file.js");
}
views_include('base');
views_include('handlers');
- views_include('join.handlers');
- views_include('relationship.handlers');
- views_include('field.handlers');
- views_include('sort.handlers');
- views_include('filter.handlers');
- views_include('argument.handlers');
views_include('cache');
views_include('plugins');
_views_include_handlers();
/**
* Fetch a handler from the data cache.
+ *
+ * @param $table
+ * The name of the table this handler is from.
+ * @param $field
+ * The name of the field this handler is from.
+ * @param $key
+ * The type of handler. i.e, sort, field, argument, filter, relationship
+ *
+ * @return
+ * An instance of a handler object. May be views_handler_broken.
*/
function views_get_handler($table, $field, $key) {
$data = views_fetch_data($table);
if (isset($data[$field][$key])) {
+ // Set up a default handler:
+ if (empty($data[$field][$key]['handler'])) {
+ $data[$field][$key]['handler'] = 'views_handler_' . $key;
+ }
return _views_prepare_handler($data[$field][$key], $data, $field);
}
// DEBUG -- identify missing handlers
vpr("Missing handler: $table $field $key");
- return new views_handler_broken;
+ $broken = array(
+ 'title' => t('Broken handler @table.@field', array('@table' => $table, '@field' => $field)),
+ 'handler' => 'views_handler_' . $key . '_broken',
+ 'table' => $table,
+ 'field' => $field,
+ );
+ return _views_create_handler($broken);
}
/**
function views_get_plugin($type, $plugin) {
$definition = views_fetch_plugin_data($type, $plugin);
if (!empty($definition)) {
- return _views_create_handler($definition);
+ return _views_create_handler($definition, $type);
}
}
/**
* Return an array of all views as fully loaded $view objects.
+ *
+ * @param $reset
+ * If TRUE, reset the static cache forcing views to be reloaded.
*/
-function views_get_all_views() {
+function views_get_all_views($reset = FALSE) {
static $views = array();
- if (empty($views)) {
+ if (empty($views) || $reset) {
+ $views = array();
+
// First, get all applicable views.
views_include('view');
$views = view::load_views();
* Provide debug output for Views. This relies on devel.module
*/
function views_debug($message) {
- if (module_exists('devel') && variable_get('views_devel_output', FALSE)) {
+ if (module_exists('devel') && variable_get('views_devel_output', FALSE) && user_access('access devel information')) {
if (is_string($message)) {
$output = $message;
}
else {
$output = var_export($message, TRUE);
}
- drupal_set_content(variable_get('views_devel_region', 'footer'), '<pre>' . $output . '</pre>');
+ if (variable_get('views_devel_region', 'footer') != 'watchdog') {
+ drupal_set_content(variable_get('views_devel_region', 'footer'), '<pre>' . $output . '</pre>');
+ }
+ else {
+ watchdog('views_logging', '<pre>' . $output . '</pre>');
+ }
}
}
* Be sure that $view and $display are references.
*/
function views_exposed_form(&$form_state) {
+ // Don't show the form when batch operations are in progress.
+ $batch =& batch_get();
+ if (!empty($batch)) {
+ return array(
+ // Set the theme callback to be nothing to avoid errors in template_preprocess_views_exposed_form().
+ '#theme' => '',
+ );
+ }
+
+ // Make sure that we validate because this form might be submitted
+ // multiple times per page.
+ $form_state['must_validate'] = TRUE;
$view = &$form_state['view'];
$display = &$form_state['display'];
- // Fill our input either from $_GET or from something previously set on the
- // view.
- if (empty($view->exposed_input)) {
- $input = $_GET;
- // unset items that are definitely not our input:
- unset($input['page']);
- unset($input['q']);
- // If we have no input at all, check for remembered input via session.
- if (empty($input) && !empty($_SESSION['views'][$view->name][$view->current_display])) {
- $input = $_SESSION['views'][$view->name][$view->current_display];
- }
- $form['#post'] = $input;
- }
- else {
- $form['#post'] = $view->exposed_input;
- }
+
+ $form_state['input'] = $view->get_exposed_input();
// Let form plugins know this is for exposed widgets.
$form_state['exposed'] = TRUE;
+ // Check if the form was already created
+ if ($cache = views_exposed_form_cache($view->name, $view->current_display)) {
+ return $cache;
+ }
$form['#info'] = array();
'#name' => '', // prevent from showing up in $_GET.
'#type' => 'submit',
'#value' => t('Apply'),
+ '#id' => form_clean_id('edit-submit-' . $view->name),
);
$form['#action'] = url($view->get_url());
$form['#theme'] = views_theme_functions('views_exposed_form', $view, $display);
+ $form['#id'] = views_css_safe('views_exposed_form-' . check_plain($view->name) . '-' . check_plain($display->id));
+// $form['#attributes']['class'] = array('views-exposed-form');
// If using AJAX, we need the form plugin.
if ($view->use_ajax) {
drupal_add_js('misc/jquery.form.js');
}
views_add_js('dependent');
+
+ // Save the form
+ views_exposed_form_cache($view->name, $view->current_display, $form);
+
return $form;
}
$form_state['view']->exposed_raw_input = array();
foreach ($form_state['values'] as $key => $value) {
- if (!in_array($key, array('q', 'submit', 'form_build_id', 'form_id', 'form_token'))) {
+ if (!in_array($key, array('q', 'submit', 'form_build_id', 'form_id', 'form_token', ''))) {
$form_state['view']->exposed_raw_input[$key] = $value;
}
}
}
+/**
+ * Save the Views exposed form for later use.
+ *
+ * @param $views_name
+ * String. The views name.
+ * @param $display_name
+ * String. The current view display name.
+ * @param $form_output
+ * Array (optional). The form structure. Only needed when inserting the value.
+ * @return
+ * Array. The form structure, if any. Otherwise, return FALSE.
+ */
+function views_exposed_form_cache($views_name, $display_name, $form_output = NULL) {
+ static $views_exposed;
+
+ // Save the form output
+ if (!empty($form_output)) {
+ $views_exposed[$views_name][$display_name] = $form_output;
+ return;
+ }
+
+ // Return the form output, if any
+ return empty($views_exposed[$views_name][$display_name]) ? FALSE : $views_exposed[$views_name][$display_name];
+}
+
// ------------------------------------------------------------------
// Misc helpers
function views_views_query_substitutions($view) {
global $language;
return array(
+ '***CURRENT_VERSION***' => VERSION,
'***CURRENT_TIME***' => time(),
'***CURRENT_LANGUAGE***' => $language->language,
+ '***DEFAULT_LANGUAGE***' => language_default('language'),
'***NO_LANGUAGE***' => '',
);
}
* solution and doesn't really offer a lot of options, but breaking the function
* apart is pretty easy, and this provides a worthwhile guide to doing so.
*
+ * Note that this function does NOT display the title of the view. If you want
+ * to do that, you will need to do what this function does manually, by
+ * loading the view, getting the preview and then getting $view->get_title().
+ *
* @param $name
* The name of the view to embed.
* @param $display_id
}
$view = views_get_view($name);
- if (!$view) {
+ if (!$view || !$view->access($display_id)) {
return;
}
/**
* Export a field.
*/
-function views_var_export($var, $prefix = '') {
+function views_var_export($var, $prefix = '', $init = TRUE) {
if (is_array($var)) {
if (empty($var)) {
$output = 'array()';
else {
$output = "array(\n";
foreach ($var as $key => $value) {
- $output .= " '$key' => " . views_var_export($value, ' ') . ",\n";
+ $output .= " '$key' => " . views_var_export($value, ' ', FALSE) . ",\n";
}
$output .= ')';
}
else if (is_bool($var)) {
$output = $var ? 'TRUE' : 'FALSE';
}
+ else if (is_string($var) && strpos($var, "\n") !== FALSE) {
+ // Replace line breaks in strings with a token for replacement
+ // at the very end. This protects multi-line strings from
+ // unintentional indentation.
+ $var = str_replace("\n", "***BREAK***", $var);
+ $output = var_export($var, TRUE);
+ }
else {
$output = var_export($var, TRUE);
}
$output = str_replace("\n", "\n$prefix", $output);
}
+ if ($init) {
+ $output = str_replace("***BREAK***", "\n", $output);
+ }
+
return $output;
}
function views_css_safe($string) {
return str_replace('_', '-', $string);
}
+
+/**
+ * Implementation of hook_views_exportables().
+ */
+function views_views_exportables($op = 'list', $views = NULL, $name = 'foo') {
+ $all_views = views_get_all_views();
+ if ($op == 'list') {
+
+ foreach ($all_views as $name => $view) {
+ // in list, $views is a list of tags.
+ if (empty($views) || in_array($view->tag, $views)) {
+ $return[$name] = array(
+ 'name' => check_plain($name),
+ 'desc' => check_plain($view->description),
+ 'tag' => check_plain($view->tag)
+ );
+ }
+ }
+ return $return;
+ }
+
+ if ($op == 'export') {
+ $code = "/**\n";
+ $code .= " * Implementation of hook_views_default_views().\n";
+ $code .= " */\n";
+ $code .= "function " . $name . "_views_default_views() {\n";
+ foreach ($views as $view => $truth) {
+ $code .= " /*\n";
+ $code .= " * View ". var_export($all_views[$view]->name, TRUE) ."\n";
+ $code .= " */\n";
+ $code .= $all_views[$view]->export(' ');
+ $code .= ' $views[$view->name] = $view;' . "\n\n";
+ }
+ $code .= " return \$views;\n";
+ $code .= "}\n";
+
+ return $code;
+ }
+}
+
+/**
+ * Microtime helper function to return a float time value (php4 & php5 safe).
+ */
+function views_microtime() {
+ list($usec, $sec) = explode(' ', microtime());
+ return (float)$sec + (float)$usec;
+}