Skip to content
views.module 67.7 KiB
Newer Older
  // 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;
  }
  // 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");
// ---------------------------------------------------------------------------
 * 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) {
  global $locale;
Earl Miles's avatar
Earl Miles committed

    $data = cache_get("views_arguments:$locale");
    $cache = unserialize($data->data);
    if (is_array($cache)) {
      $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);
      }
      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']);
          }
        }
        $views_arguments['base'][$name] = $arg['name'];
        $views_arguments['title'][$name] = $arg;
      cache_set("views_arguments:$locale", serialize($cache));
Earl Miles's avatar
Earl Miles committed
    }
  }
  return ($titles ? $views_arguments['base'] : $views_arguments['title']);
Earl Miles's avatar
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) {
  global $locale;
Earl Miles's avatar
Earl Miles committed

    $data = cache_get("views_tables:$locale");
    $cache = unserialize($data->data);
Earl Miles's avatar
Earl Miles committed

Earl Miles's avatar
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);
      }

      foreach ($table_data as $name => $table) {
        if (is_array($table['filters'])) {
          foreach ($table['filters'] as $filter => $data) {
            // 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);
            }
            $views_tables['filters']['titles']["$name.$filter"] = $data['name'];
            $views_tables['filters']['base']["$name.$filter"] = $data;
          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']);
              }
            }
            $views_tables['fields']['titles']["$name.$field"] = $data['name'];
            $views_tables['fields']['base']["$name.$field"] = $data;
          foreach ($table['sorts'] 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']);
              }
            }
            $views_tables['sorts']['titles']["$name.$field"] = $data['name'];
            $views_tables['sorts']['base']["$name.$field"] = $data;
      cache_set("views_tables:$locale", serialize($views_tables));
  return ($full ? $views_tables : $views_tables['tables']);
 * Gets the filter information; if it doesn't exist, call the function
 * that constructs all that.
 */
Earl Miles's avatar
Earl Miles committed
function _views_get_filters($titles = false) {
  $table_data = _views_get_tables(true);
  return ($titles ? $table_data['filters']['titles'] : $table_data['filters']['base']);
Earl Miles's avatar
Earl Miles committed
}

 * Gets the field information; if it doesn't exist, call the function
 * that constructs all that.
 */
Earl Miles's avatar
Earl Miles committed
function _views_get_fields($titles = false) {
  $table_data = _views_get_tables(true);
  return ($titles ? $table_data['fields']['titles'] : $table_data['fields']['base']);
Earl Miles's avatar
Earl Miles committed
}

 * Gets the sort information; if it doesn't exist, call the function
 * that constructs all that.
 */
Earl Miles's avatar
Earl Miles committed
function _views_get_sorts($titles = false) {
  $table_data = _views_get_tables(true);
  return ($titles ? $table_data['sorts']['titles'] : $table_data['sorts']['base']);
}
Earl Miles's avatar
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's avatar
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.
 */
Earl Miles's avatar
Earl Miles committed
function views_sanitize_view(&$view) {
  _views_check_arrays($view); // so reference works.
  foreach ($view->field as $i => $field) {
Earl Miles's avatar
Earl Miles committed
    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]";
    }

  foreach ($view->filter as $i => $filter) {
Earl Miles's avatar
Earl Miles committed
    if (!isset($view->filter[$i]['id'])) {
      $view->filter[$i]['id'] = $view->filter[$i]['field'] = "$filter[tablename].$filter[field]";
    }

  foreach ($view->exposed_filter as $i => $exposed_filter) {
Earl Miles's avatar
Earl Miles committed
    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's avatar
Earl Miles committed
    if (!isset($view->sort[$i]['id'])) {
      $view->sort[$i]['id'] = $view->sort[$i]['field'] = "$sort[tablename].$sort[field]";
    }
  }

  foreach ($view->argument as $i => $argument) {
Earl Miles's avatar
Earl Miles committed
    if (!isset($view->argument[$i]['id'])) {
      $view->argument[$i]['id'] = $view->argument[$i]['type'];
    }
 * Build default view information from all modules and cache it.
 */
Earl Miles's avatar
Earl Miles committed
function _views_get_default_views() {
  global $locale;
Earl Miles's avatar
Earl Miles committed

    $data = cache_get("views_default_views:$locale");
    $cache = unserialize($data->data);

    if (is_array($cache)) {
      // We have to make sure table data is built in order to be sure about providers.
      $tables = array_keys(_views_get_tables());
      $views = module_invoke_all('views_default_views');
      foreach ($views as $i => $view) {
        if (!is_array($view->requires) || !array_diff($view->requires, $tables)) {
Earl Miles's avatar
Earl Miles committed
          views_sanitize_view($view);
      cache_set("views_default_views:$locale", serialize($views_default_views));
/**
 * sort callback
 */
function _views_sort_arrays($a, $b) {
  $a = (array) $a; // safety -- something send objects.
  $b = (array) $b;
  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;
}

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;
}

/**
 * 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');
      uasort($arguments, '_views_sort_arrays');
      foreach ($arguments as $name => $arg) {
        if (!isset($arg['summary_theme'])) {
          $arg['summary_theme'] = 'views_summary';
        }
        $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

 * 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.');
 * Implementation of hook_menu()
 */
function views_menu($may_cache) {
  $items = array();
  global $locale;
    // 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') {
    $result = db_query("SELECT * FROM {view_view} WHERE page = 1");
    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());

      // This happens before the next check; even if it's put off for later
      // it is still used.
      $used[$view->name] = true;
      _views_create_menu_item($items, $view, $view->url);
    $default_views = _views_get_default_views();
    $views_status = variable_get('views_defaults', array());

    foreach ($default_views as $name => $view) {
Earl Miles's avatar
Earl Miles committed
      if ($view->page && !$used[$name] &&
       ($views_status[$name] == 'enabled' || (!$view->disabled && $views_status[$name] != 'disabled'))) {
        if (strrpos($view->url, '$arg') !== FALSE) {
        _views_create_menu_item($items, $view, $view->url);
    $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");
    }

    $views = unserialize($data->data);
    if (is_array($views)) {
      foreach ($views as $view) {
        // Do substitution on args.
        foreach (explode('/', $view->url) as $num => $element) {
          if ($element == '$arg') {
            $menu_path[] = arg($num);
            $view_args[] = arg($num);
            $view->args[] = arg($num);
        _views_create_menu_item($items, $view, $path, MENU_CALLBACK, $view_args);
/**
 * 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));
}

/**
 * Helper function to add a menu item for a view.
 */
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'));
  if ($type == MENU_LOCAL_TASK || $type == MENU_DEFAULT_LOCAL_TASK) {
    $weight = $view->menu_tab_weight;
  }
Earl Miles's avatar
Earl Miles committed
  $access = views_access($view);
  $items[] = _views_menu_item($path, $title, $view, $args, $access, $type, $weight);
  if ($type == MENU_DEFAULT_LOCAL_TASK) {
    $items[] = _views_menu_item(dirname($path), $title, $view, $args, $access, $local_task_type, $weight);
Earl Miles's avatar
Earl Miles committed
/**
 * 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]);
}

/**
 * Helper function to create a menu item for a view.
 */
function _views_menu_item($path, $title, $view, $args, $access, $type, $weight = NULL) {
  array_unshift($args, $view->name);
  $retval = array('path' => $path,
    'title' => $title,
    'callback' => 'views_view_page',
    'callback arguments' => $args,
    'access' => user_access('access content') && $access,
  if ($weight !== NULL) {
    $retval['weight'] = $weight;
  }
  return $retval;
/**
 * 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;
}
 * 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'));
    $default_views = _views_get_default_views();
    $views_status = variable_get('views_defaults', array());

    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

 * Ensure that all the arrays in a view exist so we don't run into array
 * operations on a non-array error.
 */
Earl Miles's avatar
Earl Miles committed
function _views_check_arrays(&$view) {
Earl Miles's avatar
Earl Miles committed
  $fields = array('field', 'sort', 'argument', 'filter', 'exposed_filter', 'access');

  foreach($fields as $field) {
    if (!is_array($view->$field)) {
      $view->$field = array();
    }
 * This function loads a view by name or vid; if not found in db, it looks
 * for a default view by that name.
 */
function views_get_view($view_name) {
  $view = _views_load_view($view_name);
Earl Miles's avatar
Earl Miles committed
    return $view;
Earl Miles's avatar
Earl Miles committed

  if (is_int($view_name)) {
    return; // don't bother looking if view_name is an int!
Earl Miles's avatar
Earl Miles committed

  $default_views = _views_get_default_views();
Earl Miles's avatar
Earl Miles committed

    $view = $default_views[$view_name];
    $view->is_cacheable = _views_is_cacheable($view);
    return $view;
Earl Miles's avatar
Earl Miles committed
}

 * This views a view by page, and should only be used as a callback.
 *
 * @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();
  $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) {
  return $output;
 * This views a view by block. Can be used as a callback or programmatically.
 */
function views_view_block($vid) {
Earl Miles's avatar
Earl Miles committed
  if (!views_access($view)) {
  $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'));
 * 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.
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;
Earl Miles's avatar
Earl Miles committed

  $view->build_type = $type;
  $view->type = ($type == 'block' ? $view->block_type : $view->page_type);
    $result = eval($view->view_args_php);
    if (is_array($result)) {
      $args = $result;
    }
  // 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);
  }

  $info = _views_get_query($view, $args);
  
  if ($info['fail']) {
    return FALSE;
  
  if ($type == 'queries') {
    return $info;
  }
  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';
  $view->real_url = views_get_url($view, $args);

  $view->pager_limit = $limit;
  $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';
    $output .= $function($view, $items, $output);
Earl Miles's avatar
Earl Miles committed
  return $output;
// ---------------------------------------------------------------------------
// Utility

/**
 * Load the query sub-module
 */
function views_load_query() {
  $path = drupal_get_path('module', 'views');
  require_once("./$path/views_query.inc");
}

Earl Miles's avatar
Earl Miles committed
/**
Earl Miles's avatar
Earl Miles committed
 * @param $function
 *   The name of the function to call.
 * @param $view
 *   The view being themed.
 */
function views_theme() {
  $args = func_get_args();
  $function = array_shift($args);
  $view = $args[0];

  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);
  }
}

 * 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;
    }
  }


/**
 * 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.
        $url = substr_replace($url, $arg, $where, 4);
      }
      else {
        $url .= "/$arg";
      }
 * 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)
  if ($context == 'block-info') {
    return $view->description ? $view->description : $view->name;
  }

  // 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;
      }
    }
  if (!$title && ($context == 'menu' || $context == 'page' || $context == 'admin')) {
    $title = $view->page_title;
  if (!$title && ($context == 'block' || $context == 'admin')) {
    $title = $view->block_title;
  $arginfo = _views_get_arguments();
  foreach ($view->argument as $i => $arg) {
    if ($arg['wildcard'] == $args[$i] && $arg['wildcard_substitution'] != '') {
      $title = str_replace("%" . ($i + 1), $arg['wildcard_substitution'], $title);
    }
    else if (function_exists($arginfo[$argtype]['handler'])) {
      $rep = $arginfo[$argtype]['handler']('title', $args[$i], $argtype);
      $title = str_replace("%" . ($i + 1), $rep, $title);
    }
  }
  return $title;
}

 * 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.
 */
Earl Miles's avatar
Earl Miles committed
function _views_is_cacheable(&$view) {
  // views with arguments are immediately not cacheable.
  if (!empty($view->argument) || !empty($view->exposed_filter) || !empty($view->no_cache)) {
Earl Miles's avatar
Earl Miles committed
    return false;
Earl Miles's avatar
Earl Miles committed

  $filters = _views_get_filters();

  foreach ($view->filter as $i => $filter) {
    if ($filters[$filter['field']]['cacheable'] == 'no')  {
Earl Miles's avatar
Earl Miles committed
      return false;