views_cache.inc 11.5 KB
Newer Older
1 2 3 4 5 6
<?php
// $Id$

// Load all our module 'on behalfs'.
// Whenever we decide we need this, load our module 'on behalfs'.
$path = drupal_get_path('module', 'views') . '/modules';
7
$files = drupal_system_listing('views_.*\.inc$', $path, 'name', 0);
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

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_exists($module)) {
    require_once("./$file->filename");
  }
}

// ---------------------------------------------------------------------------
// Acquire Views Data

/**
 * 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) {
  static $views_arguments;
  global $locale;

  if (!$views_arguments) {
30
    $data = cache_get("views_arguments:$locale", 'cache_views');
31 32 33 34 35 36
    $cache = unserialize($data->data);
    if (is_array($cache)) {
      $views_arguments = $cache;
    }
    else {
      $arguments = module_invoke_all('views_arguments');
37 38 39 40 41 42
      // allow modules to alter the definitions supplied others
      foreach (module_implements('views_arguments_alter') as $module) {
        $function = $module . '_views_arguments_alter';
        $function($arguments);
      }

merlinofchaos's avatar
merlinofchaos committed
43
      uasort($arguments, '_views_sort_arrays');
44 45 46 47 48 49 50 51 52 53 54 55 56
      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 = $views_arguments;
57
      cache_set("views_arguments:$locale", 'cache_views', serialize($cache));
58 59 60 61 62 63 64 65 66 67 68 69 70 71
    }
  }
  return ($titles ? $views_arguments['base'] : $views_arguments['title']);
}

/**
 * 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) {
  static $views_tables;
  global $locale;

  if (!$views_tables) {
72
    $data = cache_get("views_tables:$locale", 'cache_views');
73 74 75 76 77 78 79 80
    $cache = unserialize($data->data);

    if (is_array($cache)) {
      $views_tables = $cache;
    }
    else {
      $table_data = module_invoke_all('views_tables');
      $views_tables['tables'] = $table_data;
81 82 83 84 85
      // allow modules to alter the definitions supplied others
      foreach (module_implements('views_tables_alter') as $module) {
        $function = $module . '_views_tables_alter';
        $function($table_data);
      }
86 87 88 89

      foreach ($table_data as $name => $table) {
        if (is_array($table['filters'])) {
          foreach ($table['filters'] as $filter => $data) {
90
            $data['table'] = $name;
91 92 93 94 95 96 97 98 99 100
            // 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']) {
101
              $data['value'] = array('#type' => 'select', '#options' => $data['list'], '#validate' => array('views_filter_validate_array' => array($data['name'])));
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
              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;
          }
        }
        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;
            $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']);
              }
            }
            $views_tables['sorts']['titles']["$name.$field"] = $data['name'];
            $views_tables['sorts']['base']["$name.$field"] = $data;
          }
        }
      }
144
      cache_set("views_tables:$locale", 'cache_views', serialize($views_tables));
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    }
  }
  return ($full ? $views_tables : $views_tables['tables']);
}

/**
 * Gets the filter information; if it doesn't exist, call the function
 * that constructs all that.
 */
function _views_get_filters($titles = false) {
  $table_data = _views_get_tables(true);
  return ($titles ? $table_data['filters']['titles'] : $table_data['filters']['base']);
}

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

/**
 * Gets the sort information; if it doesn't exist, call the function
 * that constructs all that.
 */
function _views_get_sorts($titles = false) {
  $table_data = _views_get_tables(true);
  return ($titles ? $table_data['sorts']['titles'] : $table_data['sorts']['base']);
}

/**
 * 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.
 */
function views_sanitize_view(&$view) {
  _views_check_arrays($view); // so reference works.
  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]";
    }
  }

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

  foreach ($view->sort as $i => $sort) {
    if (!isset($view->sort[$i]['id'])) {
      $view->sort[$i]['id'] = $view->sort[$i]['field'] = "$sort[tablename].$sort[field]";
    }
  }

  foreach ($view->argument as $i => $argument) {
    if (!isset($view->argument[$i]['id'])) {
      $view->argument[$i]['id'] = $view->argument[$i]['type'];
    }
  }
}

/**
 * Build default view information from all modules and cache it.
 */
function _views_get_default_views() {
  static $views_default_views;
  global $locale;

  if (!$views_default_views) {
224
    $data = cache_get("views_default_views:$locale", 'cache_views');
225 226 227 228 229 230 231 232 233 234
    $cache = unserialize($data->data);

    if (is_array($cache)) {
      $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());

      $views = module_invoke_all('views_default_views');
merlinofchaos's avatar
merlinofchaos committed
235
      uasort($views, '_views_sort_arrays');
236 237 238 239 240 241 242
      $views_default_views = array();
      foreach ($views as $i => $view) {
        if (!is_array($view->requires) || !array_diff($view->requires, $tables)) {
          views_sanitize_view($view);
          $views_default_views[$i] = $view;
        }
      }
243
      cache_set("views_default_views:$locale", 'cache_views', serialize($views_default_views));
244 245 246 247 248
    }
  }
  return $views_default_views;
}

merlinofchaos's avatar
merlinofchaos committed
249 250 251 252
/**
 * sort callback
 */
function _views_sort_arrays($a, $b) {
253 254
  $a = (array) $a; // safety -- something send objects.
  $b = (array) $b;
merlinofchaos's avatar
merlinofchaos committed
255 256 257 258 259 260 261 262 263
  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;
}

264
function _views_get_query(&$view, $args, $filters) {
265
  if ($view->is_cacheable && ($cached = cache_get('views_query:' . $view->name, 'cache_views'))) {
266 267 268 269 270 271 272 273 274
    $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();
275
    $info = _views_build_query($view, $args, $filters);
276 277 278 279 280
    if ($view->is_cacheable) {
      $data = array(
        'query' => _views_replace_args($info['query'], $info['args']),
        'countquery' => _views_replace_args($info['countquery'], $info['args']),
      );
281
      cache_set('views_query:' . $view->name, 'cache_views', serialize($data));
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
    }
  }

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

301 302 303 304 305 306 307 308 309 310
/**
 * 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) {
311
    $data = cache_get("views_style_plugins:$locale", 'cache_views');
312 313 314 315 316 317
    $cache = unserialize($data->data);
    if (is_array($cache)) {
      $views_style_plugins = $cache;
    }
    else {
      $arguments = module_invoke_all('views_style_plugins');
merlinofchaos's avatar
merlinofchaos committed
318
      uasort($arguments, '_views_sort_arrays');
319 320 321 322 323 324 325 326
      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;
327
      cache_set("views_style_plugins:$locale", 'cache_views', serialize($cache));
328 329 330 331 332
    }
  }
  return ($titles ? $views_style_plugins['title'] : $views_style_plugins['base']);
}

333 334 335 336
// An implementation of hook_devel_caches() from devel.module
function views_devel_caches() {
  return array('cache_views');
}