| Commit | Line | Data |
|---|---|---|
| 31dd0540 TGGM |
1 | <?php |
| 2 | // $Id$ | |
| 3 | /** | |
| 4 | * @file | |
| 5 | * Primarily Drupal hooks and global API functions to manipulate views. | |
| 6 | * | |
| 7 | * This is the main module file for Views. The main entry points into | |
| 8 | * this module are views_page() and views_block(), where it handles | |
| 9 | * incoming page and block requests. | |
| 10 | */ | |
| 11 | ||
| 12 | /** | |
| 13 | * Advertise the current views api version | |
| 14 | */ | |
| 15 | function views_api_version() { | |
| a8f0683e DW |
16 | return '3.0-alpha1'; |
| 17 | } | |
| 18 | ||
| 19 | /** | |
| 20 | * Views will not load plugins advertising a version older than this. | |
| 21 | */ | |
| 22 | function views_api_minimum_version() { | |
| 23 | return '2'; | |
| 31dd0540 TGGM |
24 | } |
| 25 | ||
| 26 | /** | |
| 80e25196 DW |
27 | * Implement hook_init(). |
| 28 | */ | |
| 29 | function views_init() { | |
| 30 | drupal_add_css(drupal_get_path('module', 'views') .'/css/views.css'); | |
| 31 | } | |
| 32 | ||
| 33 | /** | |
| 2dd76fe0 | 34 | * Implement hook_theme(). Register views theming functions. |
| 31dd0540 TGGM |
35 | */ |
| 36 | function views_theme() { | |
| 37 | $path = drupal_get_path('module', 'views'); | |
| 2dd76fe0 | 38 | include_once $path . '/theme/theme.inc'; |
| 31dd0540 TGGM |
39 | |
| 40 | // Some quasi clever array merging here. | |
| 41 | $base = array( | |
| 42 | 'file' => 'theme.inc', | |
| 2dd76fe0 | 43 | 'path' => $path . '/theme', |
| 31dd0540 TGGM |
44 | ); |
| 45 | ||
| 46 | // Our extra version of pager from pager.inc | |
| 47 | $hooks['views_mini_pager'] = $base + array( | |
| ac9dea05 | 48 | 'variables' => array('tags' => array(), 'quantity' => 10, 'element' => 0, 'parameters' => array()), |
| 31dd0540 TGGM |
49 | 'pattern' => 'views_mini_pager__', |
| 50 | ); | |
| 51 | ||
| 52 | $arguments = array( | |
| 53 | 'display' => array('view' => NULL), | |
| 54 | 'style' => array('view' => NULL, 'options' => NULL, 'rows' => NULL, 'title' => NULL), | |
| 55 | 'row' => array('view' => NULL, 'options' => NULL, 'row' => NULL, 'field_alias' => NULL), | |
| 56 | ); | |
| 57 | ||
| 58 | // Default view themes | |
| 59 | $hooks['views_view_field'] = $base + array( | |
| 60 | 'pattern' => 'views_view_field__', | |
| 3c45ad76 | 61 | 'variables' => array('view' => NULL, 'field' => NULL, 'row' => NULL), |
| 31dd0540 TGGM |
62 | ); |
| 63 | ||
| 64 | $plugins = views_fetch_plugin_data(); | |
| 65 | ||
| 66 | // Register theme functions for all style plugins | |
| 67 | foreach ($plugins as $type => $info) { | |
| 68 | foreach ($info as $plugin => $def) { | |
| 69 | if (isset($def['theme'])) { | |
| 70 | $hooks[$def['theme']] = array( | |
| 71 | 'pattern' => $def['theme'] . '__', | |
| 72 | 'file' => $def['theme file'], | |
| 73 | 'path' => $def['theme path'], | |
| 3c45ad76 | 74 | 'variables' => $arguments[$type], |
| 31dd0540 TGGM |
75 | ); |
| 76 | ||
| 2dd76fe0 | 77 | $include = DRUPAL_ROOT . '/' . $def['theme path'] . '/' . $def['theme file']; |
| 31dd0540 TGGM |
78 | if (file_exists($include)) { |
| 79 | require_once $include; | |
| 80 | } | |
| 81 | ||
| 82 | if (!function_exists('theme_' . $def['theme'])) { | |
| 83 | $hooks[$def['theme']]['template'] = views_css_safe($def['theme']); | |
| 84 | } | |
| 85 | } | |
| 86 | if (isset($def['additional themes'])) { | |
| 87 | foreach ($def['additional themes'] as $theme => $theme_type) { | |
| 88 | if (empty($theme_type)) { | |
| 89 | $theme = $theme_type; | |
| 90 | $theme_type = $type; | |
| 91 | } | |
| 92 | ||
| 93 | $hooks[$theme] = array( | |
| 94 | 'pattern' => $theme . '__', | |
| 95 | 'file' => $def['theme file'], | |
| 96 | 'path' => $def['theme path'], | |
| 3c45ad76 | 97 | 'variables' => $arguments[$theme_type], |
| 31dd0540 TGGM |
98 | ); |
| 99 | ||
| 100 | if (!function_exists('theme_' . $theme)) { | |
| 101 | $hooks[$theme]['template'] = views_css_safe($theme); | |
| 102 | } | |
| 103 | } | |
| 104 | } | |
| 105 | } | |
| 106 | } | |
| 107 | ||
| 108 | $hooks['views_exposed_form'] = $base + array( | |
| 109 | 'template' => 'views-exposed-form', | |
| 110 | 'pattern' => 'views_exposed_form__', | |
| 3c45ad76 | 111 | 'render element' => 'form', |
| 31dd0540 TGGM |
112 | ); |
| 113 | ||
| 114 | $hooks['views_more'] = $base + array( | |
| 115 | 'template' => 'views-more', | |
| 116 | 'pattern' => 'views_more__', | |
| 3c45ad76 | 117 | 'variables' => array('more_url' => NULL, 'link_text' => 'more'), |
| 31dd0540 TGGM |
118 | ); |
| 119 | return $hooks; | |
| 120 | } | |
| 121 | ||
| 122 | /** | |
| 123 | * A theme preprocess function to automatically allow view-based node | |
| 124 | * templates if called from a view. | |
| 125 | * | |
| 126 | * The 'modules/node.views.inc' file is a better place for this, but | |
| 127 | * we haven't got a chance to load that file before Drupal builds the | |
| 128 | * node portion of the theme registry. | |
| 129 | */ | |
| 130 | function views_preprocess_node(&$vars) { | |
| 131 | // The 'view' attribute of the node is added in template_preprocess_views_view_row_node() | |
| 132 | if (!empty($vars['node']->view) && !empty($vars['node']->view->name)) { | |
| 133 | $vars['view'] = &$vars['node']->view; | |
| 134 | $vars['template_files'][] = 'node-view-' . $vars['node']->view->name; | |
| 2dd76fe0 | 135 | if (!empty($vars['node']->view->current_display)) { |
| 31dd0540 TGGM |
136 | $vars['template_files'][] = 'node-view-' . $vars['node']->view->name . '-' . $vars['node']->view->current_display; |
| 137 | } | |
| 138 | } | |
| 139 | } | |
| 140 | ||
| 141 | /** | |
| 142 | * A theme preprocess function to automatically allow view-based node | |
| 143 | * templates if called from a view. | |
| 144 | */ | |
| 145 | function views_preprocess_comment(&$vars) { | |
| 146 | // The 'view' attribute of the node is added in template_preprocess_views_view_row_comment() | |
| 147 | if (!empty($vars['node']->view) && !empty($vars['node']->view->name)) { | |
| 148 | $vars['view'] = &$vars['node']->view; | |
| 149 | $vars['template_files'][] = 'comment-view-' . $vars['node']->view->name; | |
| 2dd76fe0 | 150 | if (!empty($vars['node']->view->current_display)) { |
| 31dd0540 TGGM |
151 | $vars['template_files'][] = 'comment-view-' . $vars['node']->view->name . '-' . $vars['node']->view->current_display; |
| 152 | } | |
| 153 | } | |
| 154 | } | |
| 155 | ||
| 156 | /* | |
| 2dd76fe0 | 157 | * Implement hook_permission(). |
| 31dd0540 | 158 | */ |
| 2dd76fe0 EM |
159 | function views_permission() { |
| 160 | return array( | |
| 161 | 'administer views' => array( | |
| 162 | 'title' => t('Administer views'), | |
| 163 | 'description' => t('Access the views administration pages.'), | |
| 164 | ), | |
| 165 | 'access all views' => array( | |
| 166 | 'title' => t('Access all views'), | |
| 167 | 'description' => t('Bypass access control when accessing views.'), | |
| 168 | ), | |
| 169 | ); | |
| 31dd0540 TGGM |
170 | } |
| 171 | ||
| 172 | /** | |
| 2dd76fe0 | 173 | * Implement hook_menu(). |
| 31dd0540 TGGM |
174 | */ |
| 175 | function views_menu() { | |
| 176 | // Any event which causes a menu_rebuild could potentially mean that the | |
| 177 | // Views data is updated -- module changes, profile changes, etc. | |
| 178 | views_invalidate_cache(); | |
| 179 | $items = array(); | |
| 180 | $items['views/ajax'] = array( | |
| 181 | 'title' => 'Views', | |
| 182 | 'page callback' => 'views_ajax', | |
| 183 | 'access callback' => TRUE, | |
| 184 | 'description' => 'Ajax callback for view loading.', | |
| 31dd0540 TGGM |
185 | 'type' => MENU_CALLBACK, |
| 186 | ); | |
| 2dd76fe0 | 187 | // Path is not admin/structure/views due to menu complications with the wildcards from |
| 31dd0540 TGGM |
188 | // the generic ajax callback. |
| 189 | $items['admin/views/ajax/autocomplete/user'] = array( | |
| 190 | 'page callback' => 'views_ajax_autocomplete_user', | |
| 191 | 'access callback' => 'user_access', | |
| 192 | 'access arguments' => array('access content'), | |
| 31dd0540 | 193 | 'type' => MENU_CALLBACK, |
| 2dd76fe0 | 194 | 'file' => 'includes/ajax.inc', |
| 31dd0540 TGGM |
195 | ); |
| 196 | return $items; | |
| 197 | } | |
| 198 | ||
| 199 | /** | |
| 2dd76fe0 | 200 | * Implement hook_menu_alter(). |
| 31dd0540 TGGM |
201 | */ |
| 202 | function views_menu_alter(&$callbacks) { | |
| 203 | $our_paths = array(); | |
| 204 | $views = views_get_applicable_views('uses hook menu'); | |
| 205 | foreach ($views as $data) { | |
| 206 | list($view, $display_id) = $data; | |
| 58f55929 | 207 | $result = $view->execute_hook_menu($display_id, $callbacks); |
| 31dd0540 TGGM |
208 | if (is_array($result)) { |
| 209 | // The menu system doesn't support having two otherwise | |
| 210 | // identical paths with different placeholders. So we | |
| 211 | // want to remove the existing items from the menu whose | |
| 212 | // paths would conflict with ours. | |
| 213 | ||
| 214 | // First, we must find any existing menu items that may | |
| 215 | // conflict. We use a regular expression because we don't | |
| 216 | // know what placeholders they might use. Note that we | |
| 217 | // first construct the regex itself by replacing %views_arg | |
| 218 | // in the display path, then we use this constructed regex | |
| 219 | // (which will be something like '#^(foo/%[^/]*/bar)$#') to | |
| 220 | // search through the existing paths. | |
| 221 | $regex = '#^(' . preg_replace('#%views_arg#', '%[^/]*', implode('|', array_keys($result))) . ')$#'; | |
| 222 | $matches = preg_grep($regex, array_keys($callbacks)); | |
| 223 | ||
| 224 | // Remove any conflicting items that were found. | |
| 225 | foreach ($matches as $path) { | |
| 226 | // Don't remove the paths we just added! | |
| 227 | if (!isset($our_paths[$path])) { | |
| 228 | unset($callbacks[$path]); | |
| 229 | } | |
| 230 | } | |
| 231 | foreach ($result as $path => $item) { | |
| 232 | if (!isset($callbacks[$path])) { | |
| 233 | // Add a new item, possibly replacing (and thus effectively | |
| 234 | // overriding) one that we removed above. | |
| 235 | $callbacks[$path] = $item; | |
| 236 | } | |
| 237 | else { | |
| 238 | // This item already exists, so it must be one that we added. | |
| 239 | // We change the various callback arguments to pass an array | |
| 240 | // of possible display IDs instead of a single ID. | |
| 241 | $callbacks[$path]['page arguments'][1] = (array)$callbacks[$path]['page arguments'][1]; | |
| 242 | $callbacks[$path]['page arguments'][1][] = $display_id; | |
| 243 | $callbacks[$path]['access arguments'][] = $item['access arguments'][0]; | |
| 244 | $callbacks[$path]['load arguments'][1] = (array)$callbacks[$path]['load arguments'][1]; | |
| 245 | $callbacks[$path]['load arguments'][1][] = $display_id; | |
| 246 | } | |
| 247 | $our_paths[$path] = TRUE; | |
| 248 | } | |
| 249 | } | |
| 250 | } | |
| 251 | ||
| 252 | // Save memory: Destroy those views. | |
| 253 | foreach ($views as $data) { | |
| 254 | list($view, $display_id) = $data; | |
| 255 | $view->destroy(); | |
| 256 | } | |
| 257 | } | |
| 258 | ||
| 259 | /** | |
| 260 | * Helper function for menu loading. This will automatically be | |
| 261 | * called in order to 'load' a views argument; primarily it | |
| 262 | * will be used to perform validation. | |
| 263 | * | |
| 264 | * @param $value | |
| 265 | * The actual value passed. | |
| 266 | * @param $name | |
| 267 | * The name of the view. This needs to be specified in the 'load function' | |
| 268 | * of the menu entry. | |
| 269 | * @param $index | |
| 270 | * The menu argument index. This counts from 1. | |
| 271 | */ | |
| 272 | function views_arg_load($value, $name, $display_id, $index) { | |
| 273 | if ($view = views_get_view($name)) { | |
| 274 | $view->set_display($display_id); | |
| 275 | $view->init_handlers(); | |
| 276 | ||
| 277 | $ids = array_keys($view->argument); | |
| 278 | ||
| 279 | $indexes = array(); | |
| 280 | $path = explode('/', $view->get_path()); | |
| 281 | ||
| 282 | foreach ($path as $id => $piece) { | |
| 283 | if ($piece == '%' && !empty($ids)) { | |
| 284 | $indexes[$id] = array_shift($ids); | |
| 285 | } | |
| 286 | } | |
| 287 | ||
| 288 | if (isset($indexes[$index])) { | |
| 289 | if (isset($view->argument[$indexes[$index]])) { | |
| 290 | $arg = $view->argument[$indexes[$index]]->validate_argument($value) ? $value : FALSE; | |
| 291 | $view->destroy(); | |
| 292 | return $arg; | |
| 293 | } | |
| 294 | } | |
| 295 | $view->destroy(); | |
| 296 | } | |
| 297 | } | |
| 298 | ||
| 299 | /** | |
| 300 | * Page callback entry point; requires a view and a display id, then | |
| 301 | * passes control to the display handler. | |
| 302 | */ | |
| 303 | function views_page() { | |
| 304 | $args = func_get_args(); | |
| 305 | $name = array_shift($args); | |
| 306 | $display_id = array_shift($args); | |
| 307 | ||
| 308 | // Load the view | |
| 309 | if ($view = views_get_view($name)) { | |
| 310 | return $view->execute_display($display_id, $args); | |
| 311 | } | |
| 312 | ||
| 313 | // Fallback; if we get here no view was found or handler was not valid. | |
| 314 | return drupal_not_found(); | |
| 315 | } | |
| 316 | ||
| 317 | /** | |
| ea9088df | 318 | * Implement hook_block_info(). |
| 31dd0540 | 319 | */ |
| ea9088df | 320 | function views_block_info() { |
| 2dd76fe0 EM |
321 | $items = array(); |
| 322 | $views = views_get_all_views(); | |
| 323 | foreach ($views as $view) { | |
| 324 | // disabled views get nothing. | |
| 325 | if (!empty($view->disabled)) { | |
| 326 | continue; | |
| 327 | } | |
| 31dd0540 | 328 | |
| 2dd76fe0 EM |
329 | $view->init_display(); |
| 330 | foreach ($view->display as $display_id => $display) { | |
| 31dd0540 | 331 | |
| 2dd76fe0 EM |
332 | if (isset($display->handler) && !empty($display->handler->definition['uses hook block'])) { |
| 333 | $result = $display->handler->execute_hook_block_list(); | |
| 334 | if (is_array($result)) { | |
| 335 | $items = array_merge($items, $result); | |
| 31dd0540 TGGM |
336 | } |
| 337 | } | |
| 338 | ||
| 2dd76fe0 EM |
339 | if (isset($display->handler) && $display->handler->get_option('exposed_block')) { |
| 340 | $result = $display->handler->get_special_blocks(); | |
| 341 | if (is_array($result)) { | |
| 342 | $items = array_merge($items, $result); | |
| 31dd0540 TGGM |
343 | } |
| 344 | } | |
| 2dd76fe0 EM |
345 | } |
| 346 | } | |
| 31dd0540 | 347 | |
| 2dd76fe0 EM |
348 | // block.module has a delta length limit of 32, but our deltas can |
| 349 | // unfortunately be longer because view names can be 32 and display IDs | |
| 350 | // can also be 32. So for very long deltas, change to md5 hashes. | |
| 351 | $hashes = array(); | |
| 352 | ||
| 353 | // get the keys because we're modifying the array and we don't want to | |
| 354 | // confuse PHP too much. | |
| 355 | $keys = array_keys($items); | |
| 356 | foreach ($keys as $delta) { | |
| 357 | if (strlen($delta) >= 32) { | |
| 358 | $hash = md5($delta); | |
| 359 | $hashes[$hash] = $delta; | |
| 360 | $items[$hash] = $items[$delta]; | |
| 361 | unset($items[$delta]); | |
| 362 | } | |
| 363 | } | |
| 31dd0540 | 364 | |
| 593e6ae6 DW |
365 | // Only save hashes if they have changed. |
| 366 | $old_hashes = variable_get('views_block_hashes', array()); | |
| 367 | if ($hashes != $old_hashes) { | |
| 368 | variable_set('views_block_hashes', $hashes); | |
| 369 | } | |
| 2dd76fe0 EM |
370 | // Save memory: Destroy those views. |
| 371 | foreach ($views as $view) { | |
| 372 | $view->destroy(); | |
| 373 | } | |
| 31dd0540 | 374 | |
| 2dd76fe0 EM |
375 | return $items; |
| 376 | } | |
| 377 | ||
| 378 | /** | |
| 379 | * Implement hook_block_view(). | |
| 380 | */ | |
| 381 | function views_block_view($delta) { | |
| 382 | $start = microtime(TRUE); | |
| 383 | // if this is 32, this should be an md5 hash. | |
| 384 | if (strlen($delta) == 32) { | |
| 385 | $hashes = variable_get('views_block_hashes', array()); | |
| 386 | if (!empty($hashes[$delta])) { | |
| 387 | $delta = $hashes[$delta]; | |
| 388 | } | |
| 389 | } | |
| 31dd0540 | 390 | |
| 2dd76fe0 EM |
391 | // This indicates it's a special one. |
| 392 | if (substr($delta, 0, 1) == '-') { | |
| 393 | list($nothing, $type, $name, $display_id) = explode('-', $delta); | |
| 394 | // Put the - back on. | |
| 395 | $type = '-' . $type; | |
| 396 | if ($view = views_get_view($name)) { | |
| 397 | if ($view->access($display_id)) { | |
| 398 | $view->set_display($display_id); | |
| 399 | if (isset($view->display_handler)) { | |
| 400 | $output = $view->display_handler->view_special_blocks($type); | |
| 31dd0540 TGGM |
401 | $view->destroy(); |
| 402 | return $output; | |
| 403 | } | |
| 31dd0540 | 404 | } |
| 2dd76fe0 EM |
405 | $view->destroy(); |
| 406 | } | |
| 407 | } | |
| 408 | ||
| 409 | list($name, $display_id) = explode('-', $delta); | |
| 410 | // Load the view | |
| 411 | if ($view = views_get_view($name)) { | |
| 412 | if ($view->access($display_id)) { | |
| 413 | $output = $view->execute_display($display_id); | |
| 2dd76fe0 EM |
414 | $view->destroy(); |
| 415 | return $output; | |
| 416 | } | |
| 417 | $view->destroy(); | |
| 31dd0540 TGGM |
418 | } |
| 419 | } | |
| 420 | ||
| 421 | /** | |
| 2dd76fe0 | 422 | * Implement hook_flush_caches(). |
| 31dd0540 TGGM |
423 | */ |
| 424 | function views_flush_caches() { | |
| 425 | return array('cache_views', 'cache_views_data'); | |
| 426 | } | |
| 427 | ||
| 428 | /** | |
| 429 | * Invalidate the views cache, forcing a rebuild on the next grab of table data. | |
| 430 | */ | |
| 431 | function views_invalidate_cache() { | |
| 2dd76fe0 | 432 | cache_clear_all('*', 'cache_views', TRUE); |
| 31dd0540 TGGM |
433 | } |
| 434 | ||
| 435 | /** | |
| 436 | * Determine if the logged in user has access to a view. | |
| 437 | * | |
| 438 | * This function should only be called from a menu hook or some other | |
| 439 | * embedded source. Each argument is the result of a call to | |
| 440 | * views_plugin_access::get_access_callback() which is then used | |
| 441 | * to determine if that display is accessible. If *any* argument | |
| 442 | * is accessible, then the view is accessible. | |
| 443 | */ | |
| 444 | function views_access() { | |
| 31dd0540 TGGM |
445 | $args = func_get_args(); |
| 446 | foreach ($args as $arg) { | |
| 447 | if ($arg === TRUE) { | |
| 448 | return TRUE; | |
| 449 | } | |
| 450 | ||
| 451 | if (!is_array($arg)) { | |
| 452 | continue; | |
| 453 | } | |
| 454 | ||
| 455 | list($callback, $arguments) = $arg; | |
| 456 | if (function_exists($callback) && call_user_func_array($callback, $arguments)) { | |
| 457 | return TRUE; | |
| 458 | } | |
| 459 | } | |
| 460 | ||
| 461 | return FALSE; | |
| 462 | } | |
| 463 | ||
| 464 | /** | |
| 5eb98d56 | 465 | * Access callback for the views_plugin_access_perm access plugin. |
| 31dd0540 | 466 | * |
| 5eb98d56 EM |
467 | * Determine if the specified user has access to a view on the basis of |
| 468 | * permissions. If the $account argument is omitted, the current user | |
| 469 | * is used. | |
| 470 | */ | |
| 471 | function views_check_perm($perm, $account = NULL) { | |
| 472 | return user_access($perm, $account) || user_access('access all views', $account); | |
| 473 | } | |
| 474 | ||
| 475 | /** | |
| 476 | * Access callback for the views_plugin_access_role access plugin. | |
| 477 | ||
| 478 | * Determine if the specified user has access to a view on the basis of any of | |
| 479 | * the requested roles. If the $account argument is omitted, the current user | |
| 480 | * is used. | |
| 31dd0540 | 481 | */ |
| 5eb98d56 | 482 | function views_check_roles($rids, $account = NULL) { |
| 31dd0540 | 483 | global $user; |
| 5eb98d56 EM |
484 | $account = isset($account) ? $account : $user; |
| 485 | $roles = array_keys($account->roles); | |
| 486 | $roles[] = $account->uid ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID; | |
| 487 | return user_access('access all views', $account) || array_intersect(array_filter($rids), $roles); | |
| 31dd0540 TGGM |
488 | } |
| 489 | // ------------------------------------------------------------------ | |
| 490 | // Functions to help identify views that are running or ran | |
| 491 | ||
| 492 | /** | |
| 493 | * Set the current 'page view' that is being displayed so that it is easy | |
| 494 | * for other modules or the theme to identify. | |
| 495 | */ | |
| 496 | function &views_set_page_view($view = NULL) { | |
| 497 | static $cache = NULL; | |
| 498 | if (isset($view)) { | |
| 499 | $cache = $view; | |
| 500 | } | |
| 501 | ||
| 502 | return $cache; | |
| 503 | } | |
| 504 | ||
| 505 | /** | |
| 506 | * Find out what, if any, page view is currently in use. Please note that | |
| 507 | * this returns a reference, so be careful! You can unintentionally modify the | |
| 508 | * $view object. | |
| 509 | */ | |
| 510 | function &views_get_page_view() { | |
| 511 | return views_set_page_view(); | |
| 512 | } | |
| 513 | ||
| 514 | /** | |
| 515 | * Set the current 'current view' that is being built/rendered so that it is | |
| 516 | * easy for other modules or items in drupal_eval to identify | |
| 517 | */ | |
| 518 | function &views_set_current_view($view = NULL) { | |
| 519 | static $cache = NULL; | |
| 520 | if (isset($view)) { | |
| 521 | $cache = $view; | |
| 522 | } | |
| 523 | ||
| 524 | return $cache; | |
| 525 | } | |
| 526 | ||
| 527 | /** | |
| 528 | * Find out what, if any, current view is currently in use. Please note that | |
| 529 | * this returns a reference, so be careful! You can unintentionally modify the | |
| 530 | * $view object. | |
| 531 | */ | |
| 532 | function &views_get_current_view() { | |
| 533 | return views_set_current_view(); | |
| 534 | } | |
| 535 | ||
| 536 | // ------------------------------------------------------------------ | |
| 537 | // Include file helpers | |
| 538 | ||
| 539 | /** | |
| 540 | * Include views .inc files as necessary. | |
| 541 | */ | |
| 542 | function views_include($file) { | |
| 2dd76fe0 | 543 | module_load_include('inc', 'views', "includes/$file"); |
| 31dd0540 TGGM |
544 | } |
| 545 | ||
| 546 | /** | |
| 547 | * Load views files on behalf of modules. | |
| 548 | */ | |
| 549 | function views_module_include($file) { | |
| 550 | foreach (views_get_module_apis() as $module => $info) { | |
| 2dd76fe0 EM |
551 | if (file_exists(DRUPAL_ROOT . "/$info[path]/$module.$file")) { |
| 552 | require_once DRUPAL_ROOT . "/$info[path]/$module.$file"; | |
| 31dd0540 TGGM |
553 | } |
| 554 | } | |
| 555 | } | |
| 556 | ||
| 557 | /** | |
| 558 | * Get a list of modules that support the current views API. | |
| 559 | */ | |
| 560 | function views_get_module_apis() { | |
| 561 | static $cache = NULL; | |
| 562 | if (!isset($cache)) { | |
| 563 | $cache = array(); | |
| 564 | foreach (module_implements('views_api') as $module) { | |
| 2dd76fe0 | 565 | $info = module_invoke($module, 'views_api'); |
| a8f0683e DW |
566 | if (version_compare($info['api'], views_api_minimum_version(), '>=') && |
| 567 | version_compare($info['api'], views_api_version(), '<=')) { | |
| a89254f5 DW |
568 | if (!isset($info['path'])) { |
| 569 | $info['path'] = drupal_get_path('module', $module); | |
| 570 | } | |
| a8f0683e | 571 | $cache[$module] = $info; |
| 31dd0540 TGGM |
572 | } |
| 573 | } | |
| 574 | } | |
| 575 | ||
| 576 | return $cache; | |
| 577 | } | |
| 578 | ||
| 579 | /** | |
| 580 | * Include views .css files. | |
| 581 | */ | |
| 582 | function views_add_css($file) { | |
| 583 | // We set preprocess to FALSE because we are adding the files conditionally, | |
| 584 | // and we don't want to generate duplicate cache files. | |
| 585 | // TODO: at some point investigate adding some files unconditionally and | |
| 586 | // allowing preprocess. | |
| 2dd76fe0 | 587 | drupal_add_css(drupal_get_path('module', 'views') . "/css/$file.css", array('preprocess' => FALSE)); |
| 31dd0540 TGGM |
588 | } |
| 589 | ||
| 590 | /** | |
| 591 | * Include views .js files. | |
| 592 | */ | |
| 593 | function views_add_js($file) { | |
| 594 | // If javascript has been disabled by the user, never add js files. | |
| 595 | if (variable_get('views_no_javascript', FALSE)) { | |
| 596 | return; | |
| 597 | } | |
| 598 | ||
| 2dd76fe0 | 599 | static $base = TRUE, $ajax = TRUE; |
| 31dd0540 TGGM |
600 | if ($base) { |
| 601 | drupal_add_js(drupal_get_path('module', 'views') . "/js/base.js"); | |
| 602 | $base = FALSE; | |
| 603 | } | |
| 2dd76fe0 EM |
604 | if ($ajax && $file == 'ajax') { |
| 605 | drupal_add_js('misc/ajax.js', array('weight' => -1)); | |
| 606 | $ajax = TRUE; | |
| 607 | } | |
| 31dd0540 TGGM |
608 | drupal_add_js(drupal_get_path('module', 'views') . "/js/$file.js"); |
| 609 | } | |
| 610 | ||
| 611 | /** | |
| 612 | * Load views files on behalf of modules. | |
| 613 | */ | |
| 614 | function views_include_handlers() { | |
| 615 | static $finished = FALSE; | |
| 616 | // Ensure this only gets run once. | |
| 617 | if ($finished) { | |
| 618 | return; | |
| 619 | } | |
| 620 | ||
| 621 | views_include('base'); | |
| 622 | views_include('handlers'); | |
| 623 | views_include('cache'); | |
| 624 | views_include('plugins'); | |
| 625 | _views_include_handlers(); | |
| 626 | $finished = TRUE; | |
| 627 | } | |
| 628 | ||
| 629 | /** | |
| 630 | * Load default views files on behalf of modules. | |
| 631 | */ | |
| 632 | function views_include_default_views() { | |
| 633 | static $finished = FALSE; | |
| 634 | // Ensure this only gets run once. | |
| 635 | if ($finished) { | |
| 636 | return; | |
| 637 | } | |
| 638 | ||
| 639 | // Default views hooks may be in the normal handler file, | |
| 640 | // or in a separate views_default file at the discretion of | |
| 641 | // the module author. | |
| 642 | views_include_handlers(); | |
| 643 | ||
| 644 | _views_include_default_views(); | |
| 645 | $finished = TRUE; | |
| 646 | } | |
| 647 | ||
| 648 | // ----------------------------------------------------------------------- | |
| 649 | // Views handler functions | |
| 650 | ||
| 651 | /** | |
| 652 | * Fetch a handler from the data cache. | |
| 653 | * | |
| 654 | * @param $table | |
| 655 | * The name of the table this handler is from. | |
| 656 | * @param $field | |
| 657 | * The name of the field this handler is from. | |
| 658 | * @param $key | |
| 659 | * The type of handler. i.e, sort, field, argument, filter, relationship | |
| a8f0683e DW |
660 | * @param $override |
| 661 | * Override the actual handler object with this class. Used for | |
| 662 | * aggregation when the handler is redirected to the aggregation | |
| 663 | * handler. | |
| 31dd0540 TGGM |
664 | * |
| 665 | * @return | |
| 666 | * An instance of a handler object. May be views_handler_broken. | |
| 667 | */ | |
| a8f0683e | 668 | function views_get_handler($table, $field, $key, $override = NULL) { |
| 31dd0540 | 669 | $data = views_fetch_data($table); |
| a8f0683e DW |
670 | $handler = NULL; |
| 671 | ||
| 31dd0540 TGGM |
672 | if (isset($data[$field][$key])) { |
| 673 | // Set up a default handler: | |
| 674 | if (empty($data[$field][$key]['handler'])) { | |
| 675 | $data[$field][$key]['handler'] = 'views_handler_' . $key; | |
| 676 | } | |
| a8f0683e DW |
677 | |
| 678 | if ($override) { | |
| 679 | $data[$field][$key]['override handler'] = $override; | |
| 680 | } | |
| 681 | ||
| 682 | $handler = _views_prepare_handler($data[$field][$key], $data, $field); | |
| 31dd0540 | 683 | } |
| a8f0683e DW |
684 | |
| 685 | if ($handler) { | |
| 686 | return $handler; | |
| 687 | } | |
| 688 | ||
| 31dd0540 | 689 | // DEBUG -- identify missing handlers |
| 2dd76fe0 | 690 | debug("Missing handler: $table $field $key"); |
| 31dd0540 TGGM |
691 | $broken = array( |
| 692 | 'title' => t('Broken handler @table.@field', array('@table' => $table, '@field' => $field)), | |
| 693 | 'handler' => 'views_handler_' . $key . '_broken', | |
| 694 | 'table' => $table, | |
| 695 | 'field' => $field, | |
| 696 | ); | |
| 697 | return _views_create_handler($broken); | |
| 698 | } | |
| 699 | ||
| 700 | /** | |
| 701 | * Fetch Views' data from the cache | |
| 702 | */ | |
| 703 | function views_fetch_data($table = NULL) { | |
| 704 | views_include('cache'); | |
| 705 | return _views_fetch_data($table); | |
| 706 | } | |
| 707 | ||
| 708 | // ----------------------------------------------------------------------- | |
| 709 | // Views plugin functions | |
| 710 | ||
| 711 | /** | |
| 712 | * Fetch the plugin data from cache. | |
| 713 | */ | |
| 714 | function views_fetch_plugin_data($type = NULL, $plugin = NULL) { | |
| 715 | views_include('cache'); | |
| 716 | return _views_fetch_plugin_data($type, $plugin); | |
| 717 | } | |
| 718 | ||
| 719 | /** | |
| 720 | * Get a handler for a plugin | |
| 721 | */ | |
| 722 | function views_get_plugin($type, $plugin) { | |
| 2dd76fe0 | 723 | views_include('handlers'); |
| 31dd0540 TGGM |
724 | $definition = views_fetch_plugin_data($type, $plugin); |
| 725 | if (!empty($definition)) { | |
| 726 | return _views_create_handler($definition, $type); | |
| 727 | } | |
| 728 | } | |
| 729 | ||
| 730 | // ----------------------------------------------------------------------- | |
| 731 | // Views database functions | |
| 732 | ||
| 733 | /** | |
| 734 | * Get a view from the default views defined by modules. | |
| 735 | * | |
| 736 | * Default views are cached per-language. This function will rescan the | |
| 737 | * default_views hook if necessary. | |
| 738 | * | |
| 739 | * @param $view_name | |
| 740 | * The name of the view to load. | |
| 741 | * @return | |
| 742 | * A view object or NULL if it is not available. | |
| 743 | */ | |
| 0dc1ea54 | 744 | function &views_get_default_view($view_name, $reset = FALSE) { |
| 31dd0540 | 745 | $null = NULL; |
| 31dd0540 | 746 | |
| 7ba86678 EM |
747 | // Attempt to load individually cached view from cache. |
| 748 | views_include('cache'); | |
| 0dc1ea54 EM |
749 | if (!$reset) { |
| 750 | $data = views_cache_get("views_default:{$view_name}", TRUE); | |
| 751 | if (isset($data->data) && is_object($data->data)) { | |
| 752 | return $data->data; | |
| 753 | } | |
| 7ba86678 EM |
754 | } |
| 755 | ||
| 756 | // Otherwise, allow entire cache to be rebuilt. | |
| 0dc1ea54 | 757 | $cache = views_discover_default_views($reset); |
| 31dd0540 TGGM |
758 | if (isset($cache[$view_name])) { |
| 759 | return $cache[$view_name]; | |
| 760 | } | |
| 761 | return $null; | |
| 762 | } | |
| 763 | ||
| 764 | /** | |
| 765 | * Create an empty view to work with. | |
| 766 | * | |
| 767 | * @return | |
| 768 | * A fully formed, empty $view object. This object must be populated before | |
| 769 | * it can be successfully saved. | |
| 770 | */ | |
| 771 | function views_new_view() { | |
| 772 | views_include('view'); | |
| 773 | $view = new view(); | |
| 774 | $view->vid = 'new'; | |
| 775 | $view->add_display('default'); | |
| 776 | ||
| 777 | return $view; | |
| 778 | } | |
| 779 | ||
| 780 | /** | |
| 781 | * Scan all modules for default views and rebuild the default views cache. | |
| 782 | * | |
| 783 | * @return An associative array of all known default views. | |
| 784 | */ | |
| 0dc1ea54 | 785 | function views_discover_default_views($reset = FALSE) { |
| 31dd0540 TGGM |
786 | static $cache = array(); |
| 787 | ||
| 0dc1ea54 | 788 | if (empty($cache) || $reset) { |
| 31dd0540 | 789 | views_include('cache'); |
| 0dc1ea54 | 790 | $cache = _views_discover_default_views($reset); |
| 31dd0540 TGGM |
791 | } |
| 792 | return $cache; | |
| 793 | } | |
| 794 | ||
| 795 | /** | |
| 796 | * Return a list of all views and display IDs that have a particular | |
| 797 | * setting in their display's plugin settings. | |
| 798 | * | |
| 799 | * @return | |
| 800 | * @code | |
| 801 | * array( | |
| 802 | * array($view, $display_id), | |
| 803 | * array($view, $display_id), | |
| 804 | * ); | |
| 805 | * @endcode | |
| 806 | */ | |
| 807 | function views_get_applicable_views($type) { | |
| 808 | // @todo: Use a smarter flagging system so that we don't have to | |
| 809 | // load every view for this. | |
| 810 | $result = array(); | |
| 811 | $views = views_get_all_views(); | |
| 812 | ||
| 813 | foreach ($views as $view) { | |
| 814 | // Skip disabled views. | |
| 815 | if (!empty($view->disabled)) { | |
| 816 | continue; | |
| 817 | } | |
| 818 | ||
| 819 | if (empty($view->display)) { | |
| 820 | // Skip this view as it is broken. | |
| 821 | vsm(t("Skipping broken view @view", array('@view' => $view->name))); | |
| 822 | continue; | |
| 823 | } | |
| 824 | ||
| 825 | // Loop on array keys because something seems to muck with $view->display | |
| 826 | // a bit in PHP4. | |
| 827 | foreach (array_keys($view->display) as $id) { | |
| 828 | $plugin = views_fetch_plugin_data('display', $view->display[$id]->display_plugin); | |
| 829 | if (!empty($plugin[$type])) { | |
| 830 | // This view uses hook menu. Clone it so that different handlers | |
| 831 | // don't trip over each other, and add it to the list. | |
| 832 | $v = $view->clone_view(); | |
| 833 | if ($v->set_display($id)) { | |
| 834 | $result[] = array($v, $id); | |
| 835 | } | |
| 836 | // In PHP 4.4.7 and presumably earlier, if we do not unset $v | |
| 837 | // here, we will find that it actually overwrites references | |
| 838 | // possibly due to shallow copying issues. | |
| 839 | unset($v); | |
| 840 | } | |
| 841 | } | |
| 842 | } | |
| 843 | return $result; | |
| 844 | } | |
| 845 | ||
| 846 | /** | |
| 847 | * Return an array of all views as fully loaded $view objects. | |
| 848 | * | |
| 849 | * @param $reset | |
| 850 | * If TRUE, reset the static cache forcing views to be reloaded. | |
| 851 | */ | |
| 852 | function views_get_all_views($reset = FALSE) { | |
| 853 | static $views = array(); | |
| 854 | ||
| 855 | if (empty($views) || $reset) { | |
| 856 | $views = array(); | |
| 857 | ||
| 858 | // First, get all applicable views. | |
| 859 | views_include('view'); | |
| 860 | $views = view::load_views(); | |
| 861 | ||
| 862 | // Get all default views. | |
| 863 | $status = variable_get('views_defaults', array()); | |
| 864 | ||
| 0dc1ea54 | 865 | foreach (views_discover_default_views($reset) as $view) { |
| 31dd0540 TGGM |
866 | // Determine if default view is enabled or disabled. |
| 867 | if (isset($status[$view->name])) { | |
| 868 | $view->disabled = $status[$view->name]; | |
| 869 | } | |
| 870 | ||
| 871 | // If overridden, also say so. | |
| 872 | if (!empty($views[$view->name])) { | |
| 873 | $views[$view->name]->type = t('Overridden'); | |
| 874 | } | |
| 875 | else { | |
| 876 | $view->type = t('Default'); | |
| 877 | $views[$view->name] = $view; | |
| 878 | } | |
| 879 | } | |
| 880 | ||
| 881 | } | |
| 882 | return $views; | |
| 883 | } | |
| 884 | ||
| 885 | /** | |
| 886 | * Get a view from the database or from default views. | |
| 887 | * | |
| 888 | * This function is just a static wrapper around views::load(). This function | |
| 889 | * isn't called 'views_load()' primarily because it might get a view | |
| 890 | * from the default views which aren't technically loaded from the database. | |
| 891 | * | |
| 892 | * @param $name | |
| 893 | * The name of the view. | |
| 894 | * @param $reset | |
| 895 | * If TRUE, reset this entry in the load cache. | |
| 896 | * @return $view | |
| 897 | * A reference to the $view object. Use $reset if you're sure you want | |
| 898 | * a fresh one. | |
| 899 | */ | |
| 900 | function views_get_view($name, $reset = FALSE) { | |
| 901 | views_include('view'); | |
| 902 | $view = view::load($name, $reset); | |
| 0dc1ea54 | 903 | $default_view = views_get_default_view($name, $reset); |
| 31dd0540 TGGM |
904 | |
| 905 | if (empty($view) && empty($default_view)) { | |
| 906 | return; | |
| 907 | } | |
| 908 | elseif (empty($view) && !empty($default_view)) { | |
| aaef8fac EM |
909 | $status = variable_get('views_defaults', array()); |
| 910 | if (isset($status[$default_view->name])) { | |
| 911 | $default_view->disabled = $status[$default_view->name]; | |
| 912 | } | |
| 31dd0540 TGGM |
913 | $default_view->type = t('Default'); |
| 914 | return $default_view->clone_view(); | |
| 915 | } | |
| 916 | elseif (!empty($view) && !empty($default_view)) { | |
| 917 | $view->type = t('Overridden'); | |
| 918 | } | |
| 919 | ||
| 920 | return $view->clone_view(); | |
| 921 | } | |
| 922 | ||
| 31dd0540 TGGM |
923 | // ------------------------------------------------------------------ |
| 924 | // Views debug helper functions | |
| 925 | ||
| 926 | /** | |
| 31dd0540 TGGM |
927 | * Debug messages |
| 928 | */ | |
| 929 | function vsm($message) { | |
| 930 | if (module_exists('devel')) { | |
| 931 | dsm($message); | |
| 932 | } | |
| 933 | } | |
| 934 | ||
| 935 | function views_trace() { | |
| 936 | $message = ''; | |
| 937 | foreach (debug_backtrace() as $item) { | |
| 938 | if (!empty($item['file']) && !in_array($item['function'], array('vsm_trace', 'vpr_trace', 'views_trace'))) { | |
| 939 | $message .= basename($item['file']) . ": " . (empty($item['class']) ? '' : ($item['class'] . '->')) . "$item[function] line $item[line]" . "\n"; | |
| 940 | } | |
| 941 | } | |
| 942 | return $message; | |
| 943 | } | |
| 944 | ||
| 945 | function vsm_trace() { | |
| 946 | vsm(views_trace()); | |
| 947 | } | |
| 948 | ||
| 949 | function vpr_trace() { | |
| 950 | dpr(views_trace()); | |
| 951 | } | |
| 952 | ||
| 953 | // ------------------------------------------------------------------ | |
| 954 | // Exposed widgets form | |
| 955 | ||
| 956 | /** | |
| 957 | * Form builder for the exposed widgets form. | |
| 958 | * | |
| 959 | * Be sure that $view and $display are references. | |
| 960 | */ | |
| 2dd76fe0 | 961 | function views_exposed_form($form, &$form_state) { |
| 31dd0540 TGGM |
962 | // Don't show the form when batch operations are in progress. |
| 963 | $batch =& batch_get(); | |
| 964 | if (!empty($batch)) { | |
| 965 | return array( | |
| 966 | // Set the theme callback to be nothing to avoid errors in template_preprocess_views_exposed_form(). | |
| 967 | '#theme' => '', | |
| 968 | ); | |
| 969 | } | |
| 970 | ||
| 971 | // Make sure that we validate because this form might be submitted | |
| 972 | // multiple times per page. | |
| 973 | $form_state['must_validate'] = TRUE; | |
| 974 | $view = &$form_state['view']; | |
| 975 | $display = &$form_state['display']; | |
| 976 | ||
| 977 | $form_state['input'] = $view->get_exposed_input(); | |
| 978 | ||
| 979 | // Let form plugins know this is for exposed widgets. | |
| 980 | $form_state['exposed'] = TRUE; | |
| 981 | // Check if the form was already created | |
| 982 | if ($cache = views_exposed_form_cache($view->name, $view->current_display)) { | |
| 983 | return $cache; | |
| 984 | } | |
| 985 | ||
| 986 | $form['#info'] = array(); | |
| 987 | ||
| 988 | if (!variable_get('clean_url', FALSE)) { | |
| 989 | $form['q'] = array( | |
| 990 | '#type' => 'hidden', | |
| 991 | '#value' => $view->get_url(), | |
| 992 | ); | |
| 993 | } | |
| 994 | ||
| 2dd76fe0 EM |
995 | // Go through each handler and let it generate its exposed widget. |
| 996 | foreach ($view->display_handler->handlers as $type => $value) { | |
| 997 | foreach ($view->$type as $id => $handler) { | |
| 998 | if ($handler->can_expose() && $handler->is_exposed()) { | |
| 999 | $handler->exposed_form($form, $form_state); | |
| 1000 | if ($info = $handler->exposed_info()) { | |
| 1001 | $form['#info']["$type-$id"] = $info; | |
| 1002 | } | |
| 1003 | } | |
| 31dd0540 TGGM |
1004 | } |
| 1005 | } | |
| 1006 | ||
| 31dd0540 TGGM |
1007 | $form['submit'] = array( |
| 1008 | '#name' => '', // prevent from showing up in $_GET. | |
| 1009 | '#type' => 'submit', | |
| 9d4cb3d4 | 1010 | '#value' => t('Apply'), |
| 2dd76fe0 | 1011 | '#id' => drupal_html_id('edit-submit-' . $view->name), |
| 31dd0540 TGGM |
1012 | ); |
| 1013 | ||
| 1014 | $form['#action'] = url($view->get_url()); | |
| 1015 | $form['#theme'] = views_theme_functions('views_exposed_form', $view, $display); | |
| 1016 | $form['#id'] = views_css_safe('views_exposed_form-' . check_plain($view->name) . '-' . check_plain($display->id)); | |
| 1017 | // $form['#attributes']['class'] = array('views-exposed-form'); | |
| 1018 | ||
| 1019 | // If using AJAX, we need the form plugin. | |
| 1020 | if ($view->use_ajax) { | |
| 1021 | drupal_add_js('misc/jquery.form.js'); | |
| 1022 | } | |
| 1023 | views_add_js('dependent'); | |
| 1024 | ||
| 652ea18e DW |
1025 | $exposed_form_plugin = $form_state['exposed_form_plugin']; |
| 1026 | $exposed_form_plugin->exposed_form_alter($form, $form_state); | |
| 1027 | ||
| 31dd0540 TGGM |
1028 | // Save the form |
| 1029 | views_exposed_form_cache($view->name, $view->current_display, $form); | |
| 1030 | ||
| 1031 | return $form; | |
| 1032 | } | |
| 1033 | ||
| 1034 | /** | |
| 1035 | * Validate handler for exposed filters | |
| 1036 | */ | |
| 1037 | function views_exposed_form_validate(&$form, &$form_state) { | |
| 1038 | foreach (array('field', 'filter') as $type) { | |
| 1039 | $handlers = &$form_state['view']->$type; | |
| 1040 | foreach ($handlers as $key => $handler) { | |
| 1041 | $handlers[$key]->exposed_validate($form, $form_state); | |
| 1042 | } | |
| 1043 | } | |
| 652ea18e DW |
1044 | $exposed_form_plugin = $form_state['exposed_form_plugin']; |
| 1045 | $exposed_form_plugin->exposed_form_validate($form, $form_state); | |
| 31dd0540 TGGM |
1046 | } |
| 1047 | ||
| 1048 | /** | |
| 1049 | * Submit handler for exposed filters | |
| 1050 | */ | |
| 1051 | function views_exposed_form_submit(&$form, &$form_state) { | |
| 1052 | foreach (array('field', 'filter') as $type) { | |
| 1053 | $handlers = &$form_state['view']->$type; | |
| 1054 | foreach ($handlers as $key => $info) { | |
| 1055 | $handlers[$key]->exposed_submit($form, $form_state); | |
| 1056 | } | |
| 1057 | } | |
| 1058 | $form_state['view']->exposed_data = $form_state['values']; | |
| 1059 | $form_state['view']->exposed_raw_input = array(); | |
| 1060 | ||
| 652ea18e | 1061 | |
| 7df3edc3 | 1062 | $exclude = array('q', 'submit', 'form_build_id', 'form_id', 'form_token', 'exposed_form_plugin', ''); |
| 652ea18e DW |
1063 | $exposed_form_plugin = $form_state['exposed_form_plugin']; |
| 1064 | $exposed_form_plugin->exposed_form_submit($form, $form_state, $exclude); | |
| 1065 | ||
| 31dd0540 | 1066 | foreach ($form_state['values'] as $key => $value) { |
| 652ea18e | 1067 | if (!in_array($key, $exclude)) { |
| 31dd0540 TGGM |
1068 | $form_state['view']->exposed_raw_input[$key] = $value; |
| 1069 | } | |
| 1070 | } | |
| 1071 | } | |
| 1072 | ||
| 1073 | /** | |
| 1074 | * Save the Views exposed form for later use. | |
| 1075 | * | |
| 1076 | * @param $views_name | |
| 1077 | * String. The views name. | |
| 1078 | * @param $display_name | |
| 1079 | * String. The current view display name. | |
| 1080 | * @param $form_output | |
| 1081 | * Array (optional). The form structure. Only needed when inserting the value. | |
| 1082 | * @return | |
| 1083 | * Array. The form structure, if any. Otherwise, return FALSE. | |
| 1084 | */ | |
| 1085 | function views_exposed_form_cache($views_name, $display_name, $form_output = NULL) { | |
| 1086 | static $views_exposed; | |
| 1087 | ||
| 1088 | // Save the form output | |
| 1089 | if (!empty($form_output)) { | |
| 1090 | $views_exposed[$views_name][$display_name] = $form_output; | |
| 1091 | return; | |
| 1092 | } | |
| 1093 | ||
| 1094 | // Return the form output, if any | |
| 1095 | return empty($views_exposed[$views_name][$display_name]) ? FALSE : $views_exposed[$views_name][$display_name]; | |
| 1096 | } | |
| 1097 | ||
| 1098 | // ------------------------------------------------------------------ | |
| 1099 | // Misc helpers | |
| 1100 | ||
| 1101 | /** | |
| 1102 | * Build a list of theme function names for use most everywhere. | |
| 1103 | */ | |
| 1104 | function views_theme_functions($hook, $view, $display = NULL) { | |
| 2dd76fe0 | 1105 | require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'views') . "/theme/theme.inc"; |
| 31dd0540 TGGM |
1106 | return _views_theme_functions($hook, $view, $display); |
| 1107 | } | |
| 1108 | ||
| 1109 | /** | |
| 31dd0540 TGGM |
1110 | * Substitute current time; this works with cached queries. |
| 1111 | */ | |
| 1112 | function views_views_query_substitutions($view) { | |
| 1113 | global $language; | |
| 1114 | return array( | |
| 2dd76fe0 EM |
1115 | ':views-current-version' => VERSION, |
| 1116 | ':views-current-time' => REQUEST_TIME, | |
| 1117 | ':views-current-language' => $language->language, | |
| 1118 | ':views-default-language' => language_default('language'), | |
| 1119 | ':views-no-language' => '', | |
| 31dd0540 TGGM |
1120 | ); |
| 1121 | } | |
| 1122 | ||
| 1123 | /** | |
| 1124 | * Embed a view using a PHP snippet. | |
| 1125 | * | |
| 1126 | * This function is meant to be called from PHP snippets, should one wish to | |
| 1127 | * embed a view in a node or something. It's meant to provide the simplest | |
| 1128 | * solution and doesn't really offer a lot of options, but breaking the function | |
| 1129 | * apart is pretty easy, and this provides a worthwhile guide to doing so. | |
| 1130 | * | |
| 1131 | * Note that this function does NOT display the title of the view. If you want | |
| 1132 | * to do that, you will need to do what this function does manually, by | |
| 1133 | * loading the view, getting the preview and then getting $view->get_title(). | |
| 1134 | * | |
| 1135 | * @param $name | |
| 1136 | * The name of the view to embed. | |
| 1137 | * @param $display_id | |
| 1138 | * The display id to embed. If unsure, use 'default', as it will always be | |
| 1139 | * valid. But things like 'page' or 'block' should work here. | |
| 1140 | * @param ... | |
| 1141 | * Any additional parameters will be passed as arguments. | |
| 1142 | */ | |
| 1143 | function views_embed_view($name, $display_id = 'default') { | |
| 1144 | $args = func_get_args(); | |
| 1145 | array_shift($args); // remove $name | |
| 1146 | if (count($args)) { | |
| 1147 | array_shift($args); // remove $display_id | |
| 1148 | } | |
| 1149 | ||
| 1150 | $view = views_get_view($name); | |
| 1151 | if (!$view || !$view->access($display_id)) { | |
| 1152 | return; | |
| 1153 | } | |
| 1154 | ||
| 1155 | return $view->preview($display_id, $args); | |
| 1156 | } | |
| 1157 | ||
| 1158 | /** | |
| 5ee9b0c7 EM |
1159 | * Get the result of a view. |
| 1160 | * | |
| 1161 | * @param string $name | |
| 1162 | * The name of the view to retrieve the data from. | |
| 1163 | * @param string $display_id | |
| 1164 | * The display id. On the edit page for the view in question, you'll find | |
| 1165 | * a list of displays at the left side of the control area. "Defaults" | |
| 1166 | * will be at the top of that list. Hover your cursor over the name of the | |
| 1167 | * display you want to use. An URL will appear in the status bar of your | |
| 1168 | * browser. This is usually at the bottom of the window, in the chrome. | |
| 1169 | * Everything after #views-tab- is the display ID, e.g. page_1. | |
| 1170 | * @param ... | |
| 1171 | * Any additional parameters will be passed as arguments. | |
| 1172 | * @return | |
| 1173 | * array | |
| 1174 | * An array containing an object for each view item. | |
| 1175 | */ | |
| 1176 | function views_get_view_result($name, $display_id = NULL) { | |
| 1177 | $args = func_get_args(); | |
| 1178 | array_shift($args); // remove $name | |
| 1179 | if (count($args)) { | |
| 1180 | array_shift($args); // remove $display_id | |
| 1181 | } | |
| 1182 | ||
| 1183 | $view = views_get_view($name); | |
| 1184 | if (is_object($view)) { | |
| 1185 | if (is_array($args)) { | |
| 1186 | $view->set_arguments($args); | |
| 1187 | } | |
| 1188 | if (is_string($display_id)) { | |
| 1189 | $view->set_display($display_id); | |
| 1190 | } | |
| 1191 | else { | |
| 1192 | $view->init_display(); | |
| 1193 | } | |
| 1194 | $view->pre_execute(); | |
| 1195 | $view->execute(); | |
| 1196 | return $view->result; | |
| 1197 | } | |
| 1198 | else { | |
| 1199 | return array(); | |
| 1200 | } | |
| 1201 | } | |
| 1202 | ||
| 1203 | /** | |
| 31dd0540 TGGM |
1204 | * Export a field. |
| 1205 | */ | |
| 1206 | function views_var_export($var, $prefix = '', $init = TRUE) { | |
| 1207 | if (is_array($var)) { | |
| 1208 | if (empty($var)) { | |
| 1209 | $output = 'array()'; | |
| 1210 | } | |
| 1211 | else { | |
| 1212 | $output = "array(\n"; | |
| 1213 | foreach ($var as $key => $value) { | |
| 1214 | $output .= " '$key' => " . views_var_export($value, ' ', FALSE) . ",\n"; | |
| 1215 | } | |
| 1216 | $output .= ')'; | |
| 1217 | } | |
| 1218 | } | |
| 2dd76fe0 | 1219 | elseif (is_bool($var)) { |
| 31dd0540 TGGM |
1220 | $output = $var ? 'TRUE' : 'FALSE'; |
| 1221 | } | |
| 2dd76fe0 | 1222 | elseif (is_string($var) && strpos($var, "\n") !== FALSE) { |
| 31dd0540 TGGM |
1223 | // Replace line breaks in strings with a token for replacement |
| 1224 | // at the very end. This protects multi-line strings from | |
| 1225 | // unintentional indentation. | |
| 1226 | $var = str_replace("\n", "***BREAK***", $var); | |
| 1227 | $output = var_export($var, TRUE); | |
| 1228 | } | |
| 1229 | else { | |
| 1230 | $output = var_export($var, TRUE); | |
| 1231 | } | |
| 1232 | ||
| 1233 | if ($prefix) { | |
| 1234 | $output = str_replace("\n", "\n$prefix", $output); | |
| 1235 | } | |
| 1236 | ||
| 1237 | if ($init) { | |
| 1238 | $output = str_replace("***BREAK***", "\n", $output); | |
| 1239 | } | |
| 1240 | ||
| 1241 | return $output; | |
| 1242 | } | |
| 1243 | ||
| 1244 | /** | |
| 1245 | * Prepare the specified string for use as a CSS identifier. | |
| 1246 | */ | |
| 1247 | function views_css_safe($string) { | |
| 1248 | return str_replace('_', '-', $string); | |
| 1249 | } | |
| 1250 | ||
| 1251 | /** | |
| 2dd76fe0 | 1252 | * Implement hook_views_exportables(). |
| 31dd0540 TGGM |
1253 | */ |
| 1254 | function views_views_exportables($op = 'list', $views = NULL, $name = 'foo') { | |
| 1255 | $all_views = views_get_all_views(); | |
| 1256 | if ($op == 'list') { | |
| 1257 | ||
| 1258 | foreach ($all_views as $name => $view) { | |
| 1259 | // in list, $views is a list of tags. | |
| 1260 | if (empty($views) || in_array($view->tag, $views)) { | |
| 1261 | $return[$name] = array( | |
| 1262 | 'name' => check_plain($name), | |
| 1263 | 'desc' => check_plain($view->description), | |
| 1264 | 'tag' => check_plain($view->tag) | |
| 1265 | ); | |
| 1266 | } | |
| 1267 | } | |
| 1268 | return $return; | |
| 1269 | } | |
| 1270 | ||
| 1271 | if ($op == 'export') { | |
| 1272 | $code = "/**\n"; | |
| 2dd76fe0 | 1273 | $code .= " * Implement hook_views_default_views().\n"; |
| 31dd0540 TGGM |
1274 | $code .= " */\n"; |
| 1275 | $code .= "function " . $name . "_views_default_views() {\n"; | |
| 1276 | foreach ($views as $view => $truth) { | |
| 1277 | $code .= " /*\n"; | |
| 2dd76fe0 | 1278 | $code .= " * View " . var_export($all_views[$view]->name, TRUE) . "\n"; |
| 31dd0540 TGGM |
1279 | $code .= " */\n"; |
| 1280 | $code .= $all_views[$view]->export(' '); | |
| 1281 | $code .= ' $views[$view->name] = $view;' . "\n\n"; | |
| 1282 | } | |
| 1283 | $code .= " return \$views;\n"; | |
| 1284 | $code .= "}\n"; | |
| 1285 | ||
| 1286 | return $code; | |
| 1287 | } | |
| 1288 | } | |
| 1289 | ||
| 1290 | /** | |
| 2dd76fe0 EM |
1291 | * Process callback to add dependency to form items. |
| 1292 | * | |
| 1293 | * Usage: | |
| 1294 | * | |
| 1295 | * On any form item, add | |
| 1296 | * - @code '#process' => 'views_process_dependency' @endcode | |
| 1297 | * - @code '#dependency' => array('id-of-form-without-the-#' => array(list, of, values, that, make, this, gadget, visible)); @endcode | |
| 31dd0540 | 1298 | */ |
| 2dd76fe0 EM |
1299 | function views_process_dependency($element) { |
| 1300 | if (isset($element['#dependency'])) { | |
| 1301 | if (!isset($element['#dependency_count'])) { | |
| 1302 | $element['#dependency_count'] = 1; | |
| 1303 | } | |
| 1304 | views_add_js('dependent'); | |
| 1305 | $options['viewsAjax']['formRelationships'][$element['#id']] = array('num' => $element['#dependency_count'], 'values' => $element['#dependency']); | |
| 1306 | drupal_add_js($options, 'setting'); | |
| 1307 | } | |
| 1308 | ||
| 1309 | return $element; | |
| 31dd0540 | 1310 | } |
| 2dd76fe0 | 1311 | |
| 28244380 DW |
1312 | /** |
| 1313 | * #process callback to see if we need to check_plain() the options. | |
| 1314 | * | |
| 1315 | * Since FAPI is inconsistent, the #options are sanitized for you in all cases | |
| 1316 | * _except_ checkboxes. We have form elements that are sometimes 'select' and | |
| 1317 | * sometimes 'checkboxes', so we need decide late in the form rendering cycle | |
| 1318 | * if the options need to be sanitized before they're rendered. This callback | |
| 1319 | * inspects the type, and if it's still 'checkboxes', does the sanitation. | |
| 1320 | */ | |
| 0f2586f9 | 1321 | function views_process_check_options($element, &$form_state) { |
| 28244380 DW |
1322 | if ($element['#type'] == 'checkboxes' || $element['#type'] == 'checkbox') { |
| 1323 | $element['#options'] = array_map('check_plain', $element['#options']); | |
| 1324 | } | |
| 1325 | return $element; | |
| 1326 | } | |
| 2f5e3034 EM |
1327 | |
| 1328 | /** | |
| 1329 | * Trim the field down to the specified length. | |
| 1330 | * | |
| 1331 | * @param $alter | |
| 1332 | * - max_length: Maximum lenght of the string, the rest gets truncated. | |
| 1333 | * - word_boundary: Trim only on a word boundary. | |
| 1334 | * - ellipsis: Trim only on a word boundary. | |
| 1335 | * - html: Take sure that the html is correct. | |
| 1336 | * | |
| 1337 | * @param $value | |
| 1338 | * The string which should be trimmed. | |
| 1339 | */ | |
| 1340 | function drupal_trim_text($alter, $value) { | |
| 1341 | if (drupal_strlen($value) > $alter['max_length']) { | |
| 1342 | $value = drupal_substr($value, 0, $alter['max_length']); | |
| 1343 | if (!empty($alter['word_boundary'])) { | |
| 1344 | if (preg_match("/(.*)\b.+/us", $value, $matches)) { | |
| 1345 | $value = $matches[1]; | |
| 1346 | } | |
| 1347 | } | |
| 1348 | // Remove scraps of HTML entities from the end of a strings | |
| 1349 | $value = rtrim(preg_replace('/(?:<(?!.+>)|&(?!.+;)).*$/us', '', $value)); | |
| 1350 | ||
| 1351 | if (!empty($alter['ellipsis'])) { | |
| 1352 | $value .= '...'; | |
| 1353 | } | |
| 1354 | } | |
| 1355 | if (!empty($alter['html'])) { | |
| 1356 | $value = _filter_htmlcorrector($value); | |
| 1357 | } | |
| 1358 | ||
| 1359 | return $value; | |
| 1360 | } |