[177a560] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * @file |
---|
| 4 | * Primarily Drupal hooks and global API functions to manipulate views. |
---|
| 5 | * |
---|
| 6 | * This is the main module file for Views. The main entry points into |
---|
| 7 | * this module are views_page() and views_block(), where it handles |
---|
| 8 | * incoming page and block requests. |
---|
| 9 | */ |
---|
| 10 | |
---|
| 11 | /** |
---|
| 12 | * Advertise the current views api version |
---|
| 13 | */ |
---|
| 14 | function views_api_version() { |
---|
[59029b2] | 15 | return '3.0'; |
---|
| 16 | } |
---|
| 17 | |
---|
| 18 | /** |
---|
| 19 | * Implements hook_forms(). |
---|
| 20 | * |
---|
| 21 | * To provide distinct form IDs for Views forms, the View name and |
---|
| 22 | * specific display name are appended to the base ID, |
---|
| 23 | * views_form_views_form. When such a form is built or submitted, this |
---|
| 24 | * function will return the proper callback function to use for the given form. |
---|
| 25 | */ |
---|
| 26 | function views_forms($form_id, $args) { |
---|
| 27 | if (strpos($form_id, 'views_form_') === 0) { |
---|
| 28 | return array( |
---|
| 29 | $form_id => array( |
---|
| 30 | 'callback' => 'views_form', |
---|
| 31 | ), |
---|
| 32 | ); |
---|
| 33 | } |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | /** |
---|
| 37 | * Returns a form ID for a Views form using the name and display of the View. |
---|
| 38 | */ |
---|
| 39 | function views_form_id($view) { |
---|
| 40 | $parts = array( |
---|
| 41 | 'views_form', |
---|
| 42 | $view->name, |
---|
| 43 | $view->current_display, |
---|
| 44 | ); |
---|
| 45 | |
---|
| 46 | return implode('_', $parts); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | /** |
---|
| 50 | * Views will not load plugins advertising a version older than this. |
---|
| 51 | */ |
---|
| 52 | function views_api_minimum_version() { |
---|
| 53 | return '2'; |
---|
[177a560] | 54 | } |
---|
| 55 | |
---|
| 56 | /** |
---|
| 57 | * Implementation of hook_init(). |
---|
| 58 | */ |
---|
| 59 | function views_init() { |
---|
| 60 | drupal_add_css(drupal_get_path('module', 'views') . '/css/views.css'); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | * Implementation of hook_theme(). Register views theming functions. |
---|
| 65 | */ |
---|
[59029b2] | 66 | function views_theme($existing, $type, $theme, $path) { |
---|
[177a560] | 67 | $path = drupal_get_path('module', 'views'); |
---|
| 68 | require_once "./$path/theme/theme.inc"; |
---|
| 69 | |
---|
| 70 | // Some quasi clever array merging here. |
---|
| 71 | $base = array( |
---|
| 72 | 'file' => 'theme.inc', |
---|
| 73 | 'path' => "$path/theme", |
---|
| 74 | ); |
---|
| 75 | |
---|
| 76 | // Our extra version of pager from pager.inc |
---|
| 77 | $hooks['views_mini_pager'] = $base + array( |
---|
| 78 | 'arguments' => array('tags' => array(), 'limit' => 10, 'element' => 0, 'parameters' => array()), |
---|
| 79 | 'pattern' => 'views_mini_pager__', |
---|
| 80 | ); |
---|
| 81 | |
---|
| 82 | $arguments = array( |
---|
| 83 | 'display' => array('view' => NULL), |
---|
| 84 | 'style' => array('view' => NULL, 'options' => NULL, 'rows' => NULL, 'title' => NULL), |
---|
| 85 | 'row' => array('view' => NULL, 'options' => NULL, 'row' => NULL, 'field_alias' => NULL), |
---|
[59029b2] | 86 | 'exposed_form' => array('view' => NULL, 'options' => NULL), |
---|
| 87 | 'pager' => array( |
---|
| 88 | 'view' => NULL, 'options' => NULL, |
---|
| 89 | 'tags' => array(), 'quantity' => 10, 'element' => 0, 'parameters' => array() |
---|
| 90 | ), |
---|
[177a560] | 91 | ); |
---|
| 92 | |
---|
| 93 | // Default view themes |
---|
| 94 | $hooks['views_view_field'] = $base + array( |
---|
| 95 | 'pattern' => 'views_view_field__', |
---|
| 96 | 'arguments' => array('view' => NULL, 'field' => NULL, 'row' => NULL), |
---|
| 97 | ); |
---|
| 98 | |
---|
| 99 | $plugins = views_fetch_plugin_data(); |
---|
| 100 | |
---|
| 101 | // Register theme functions for all style plugins |
---|
| 102 | foreach ($plugins as $type => $info) { |
---|
| 103 | foreach ($info as $plugin => $def) { |
---|
| 104 | if (isset($def['theme'])) { |
---|
| 105 | $hooks[$def['theme']] = array( |
---|
| 106 | 'pattern' => $def['theme'] . '__', |
---|
| 107 | 'file' => $def['theme file'], |
---|
| 108 | 'path' => $def['theme path'], |
---|
| 109 | 'arguments' => $arguments[$type], |
---|
| 110 | ); |
---|
| 111 | |
---|
| 112 | $include = './' . $def['theme path'] . '/' . $def['theme file']; |
---|
| 113 | if (file_exists($include)) { |
---|
| 114 | require_once $include; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | if (!function_exists('theme_' . $def['theme'])) { |
---|
| 118 | $hooks[$def['theme']]['template'] = views_css_safe($def['theme']); |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | if (isset($def['additional themes'])) { |
---|
| 122 | foreach ($def['additional themes'] as $theme => $theme_type) { |
---|
| 123 | if (empty($theme_type)) { |
---|
| 124 | $theme = $theme_type; |
---|
| 125 | $theme_type = $type; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | $hooks[$theme] = array( |
---|
| 129 | 'pattern' => $theme . '__', |
---|
| 130 | 'file' => $def['theme file'], |
---|
| 131 | 'path' => $def['theme path'], |
---|
| 132 | 'arguments' => $arguments[$theme_type], |
---|
| 133 | ); |
---|
| 134 | |
---|
| 135 | if (!function_exists('theme_' . $theme)) { |
---|
| 136 | $hooks[$theme]['template'] = views_css_safe($theme); |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | |
---|
[59029b2] | 143 | $hooks['views_form_views_form'] = $base; |
---|
| 144 | |
---|
[177a560] | 145 | $hooks['views_exposed_form'] = $base + array( |
---|
| 146 | 'template' => 'views-exposed-form', |
---|
| 147 | 'pattern' => 'views_exposed_form__', |
---|
| 148 | 'arguments' => array('form' => NULL), |
---|
| 149 | ); |
---|
| 150 | |
---|
| 151 | $hooks['views_more'] = $base + array( |
---|
| 152 | 'template' => 'views-more', |
---|
| 153 | 'pattern' => 'views_more__', |
---|
| 154 | 'arguments' => array('more_url' => NULL, 'link_text' => 'more'), |
---|
| 155 | ); |
---|
[59029b2] | 156 | |
---|
| 157 | // Add theme suggestions which are part of modules. |
---|
| 158 | foreach (views_get_module_apis() as $info) { |
---|
| 159 | if (isset($info['template path'])) { |
---|
| 160 | $hooks += _views_find_module_templates($hooks, $info['template path']); |
---|
| 161 | } |
---|
| 162 | } |
---|
[177a560] | 163 | return $hooks; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | /** |
---|
[59029b2] | 167 | * Scans a directory of a module for template files. |
---|
| 168 | * |
---|
| 169 | * @param $cache |
---|
| 170 | * The existing cache of theme hooks to test against. |
---|
| 171 | * @param $path |
---|
| 172 | * The path to search. |
---|
| 173 | * |
---|
| 174 | * @see drupal_find_theme_templates |
---|
| 175 | */ |
---|
| 176 | function _views_find_module_templates($cache, $path) { |
---|
| 177 | $templates = array(); |
---|
| 178 | $regex = '\.tpl\.php' . '$'; |
---|
| 179 | |
---|
| 180 | // Because drupal_system_listing works the way it does, we check for real |
---|
| 181 | // templates separately from checking for patterns. |
---|
| 182 | $files = drupal_system_listing($regex, $path, 'name', 0); |
---|
| 183 | foreach ($files as $template => $file) { |
---|
| 184 | // Chop off the remaining extensions if there are any. $template already |
---|
| 185 | // has the rightmost extension removed, but there might still be more, |
---|
| 186 | // such as with .tpl.php, which still has .tpl in $template at this point. |
---|
| 187 | if (($pos = strpos($template, '.')) !== FALSE) { |
---|
| 188 | $template = substr($template, 0, $pos); |
---|
| 189 | } |
---|
| 190 | // Transform - in filenames to _ to match function naming scheme |
---|
| 191 | // for the purposes of searching. |
---|
| 192 | $hook = strtr($template, '-', '_'); |
---|
| 193 | if (isset($cache[$hook])) { |
---|
| 194 | $templates[$hook] = array( |
---|
| 195 | 'template' => $template, |
---|
| 196 | 'path' => dirname($file->filename), |
---|
| 197 | 'include files' => $cache[$hook]['include files'], |
---|
| 198 | ); |
---|
| 199 | } |
---|
| 200 | // Ensure that the pattern is maintained from base themes to its sub-themes. |
---|
| 201 | // Each sub-theme will have their templates scanned so the pattern must be |
---|
| 202 | // held for subsequent runs. |
---|
| 203 | if (isset($cache[$hook]['pattern'])) { |
---|
| 204 | $templates[$hook]['pattern'] = $cache[$hook]['pattern']; |
---|
| 205 | } |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | $patterns = array_keys($files); |
---|
| 209 | |
---|
| 210 | foreach ($cache as $hook => $info) { |
---|
| 211 | if (!empty($info['pattern'])) { |
---|
| 212 | // Transform _ in pattern to - to match file naming scheme |
---|
| 213 | // for the purposes of searching. |
---|
| 214 | $pattern = strtr($info['pattern'], '_', '-'); |
---|
| 215 | |
---|
| 216 | $matches = preg_grep('/^'. $pattern .'/', $patterns); |
---|
| 217 | if ($matches) { |
---|
| 218 | foreach ($matches as $match) { |
---|
| 219 | $file = substr($match, 0, strpos($match, '.')); |
---|
| 220 | // Put the underscores back in for the hook name and register this pattern. |
---|
| 221 | $templates[strtr($file, '-', '_')] = array( |
---|
| 222 | 'template' => $file, |
---|
| 223 | 'path' => dirname($files[$match]->filename), |
---|
| 224 | 'arguments' => $info['arguments'], |
---|
| 225 | 'original hook' => $hook, |
---|
| 226 | 'include files' => $info['include files'], |
---|
| 227 | ); |
---|
| 228 | } |
---|
| 229 | } |
---|
| 230 | } |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | return $templates; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | /** |
---|
[177a560] | 237 | * A theme preprocess function to automatically allow view-based node |
---|
| 238 | * templates if called from a view. |
---|
| 239 | * |
---|
| 240 | * The 'modules/node.views.inc' file is a better place for this, but |
---|
| 241 | * we haven't got a chance to load that file before Drupal builds the |
---|
| 242 | * node portion of the theme registry. |
---|
| 243 | */ |
---|
| 244 | function views_preprocess_node(&$vars) { |
---|
| 245 | // The 'view' attribute of the node is added in template_preprocess_views_view_row_node() |
---|
| 246 | if (!empty($vars['node']->view) && !empty($vars['node']->view->name)) { |
---|
| 247 | $vars['view'] = &$vars['node']->view; |
---|
| 248 | $vars['template_files'][] = 'node-view-' . $vars['node']->view->name; |
---|
| 249 | if(!empty($vars['node']->view->current_display)) { |
---|
| 250 | $vars['template_files'][] = 'node-view-' . $vars['node']->view->name . '-' . $vars['node']->view->current_display; |
---|
| 251 | } |
---|
| 252 | } |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | /** |
---|
| 256 | * A theme preprocess function to automatically allow view-based node |
---|
| 257 | * templates if called from a view. |
---|
| 258 | */ |
---|
| 259 | function views_preprocess_comment(&$vars) { |
---|
| 260 | // The 'view' attribute of the node is added in template_preprocess_views_view_row_comment() |
---|
| 261 | if (!empty($vars['node']->view) && !empty($vars['node']->view->name)) { |
---|
| 262 | $vars['view'] = &$vars['node']->view; |
---|
| 263 | $vars['template_files'][] = 'comment-view-' . $vars['node']->view->name; |
---|
| 264 | if(!empty($vars['node']->view->current_display)) { |
---|
| 265 | $vars['template_files'][] = 'comment-view-' . $vars['node']->view->name . '-' . $vars['node']->view->current_display; |
---|
| 266 | } |
---|
| 267 | } |
---|
| 268 | } |
---|
| 269 | |
---|
[59029b2] | 270 | /** |
---|
| 271 | * A theme preprocess function to automatically allow blocks with view-based |
---|
| 272 | * block templates if called from a view. |
---|
| 273 | */ |
---|
| 274 | function views_preprocess_block($vars) { |
---|
| 275 | if (!empty($vars['block']->view)) { |
---|
| 276 | $vars['view'] = &$vars['block']->view; |
---|
| 277 | $vars['template_files'][] = 'block-view-' . $vars['view']->name; |
---|
| 278 | if(!empty($vars['view']->current_display)) { |
---|
| 279 | $vars['template_files'][] = 'block-view-' . $vars['view']->name . '-' . $vars['view']->current_display; |
---|
| 280 | } |
---|
| 281 | } |
---|
| 282 | } |
---|
| 283 | |
---|
[177a560] | 284 | /* |
---|
| 285 | * Implementation of hook_perm() |
---|
| 286 | */ |
---|
| 287 | function views_perm() { |
---|
| 288 | return array('access all views', 'administer views'); |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | /** |
---|
| 292 | * Implementation of hook_menu(). |
---|
| 293 | */ |
---|
| 294 | function views_menu() { |
---|
| 295 | // Any event which causes a menu_rebuild could potentially mean that the |
---|
| 296 | // Views data is updated -- module changes, profile changes, etc. |
---|
| 297 | views_invalidate_cache(); |
---|
| 298 | $items = array(); |
---|
| 299 | $items['views/ajax'] = array( |
---|
| 300 | 'title' => 'Views', |
---|
| 301 | 'page callback' => 'views_ajax', |
---|
| 302 | 'access callback' => TRUE, |
---|
| 303 | 'description' => 'Ajax callback for view loading.', |
---|
| 304 | 'file' => 'includes/ajax.inc', |
---|
| 305 | 'type' => MENU_CALLBACK, |
---|
| 306 | ); |
---|
| 307 | // Path is not admin/build/views due to menu complications with the wildcards from |
---|
| 308 | // the generic ajax callback. |
---|
| 309 | $items['admin/views/ajax/autocomplete/user'] = array( |
---|
| 310 | 'page callback' => 'views_ajax_autocomplete_user', |
---|
| 311 | 'access callback' => 'user_access', |
---|
| 312 | 'access arguments' => array('access content'), |
---|
| 313 | 'file' => 'includes/ajax.inc', |
---|
| 314 | 'type' => MENU_CALLBACK, |
---|
| 315 | ); |
---|
| 316 | return $items; |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | /** |
---|
| 320 | * Implementation of hook_menu_alter(). |
---|
| 321 | */ |
---|
| 322 | function views_menu_alter(&$callbacks) { |
---|
| 323 | $our_paths = array(); |
---|
| 324 | $views = views_get_applicable_views('uses hook menu'); |
---|
| 325 | foreach ($views as $data) { |
---|
| 326 | list($view, $display_id) = $data; |
---|
| 327 | $result = $view->execute_hook_menu($display_id, $callbacks); |
---|
| 328 | if (is_array($result)) { |
---|
| 329 | // The menu system doesn't support having two otherwise |
---|
| 330 | // identical paths with different placeholders. So we |
---|
| 331 | // want to remove the existing items from the menu whose |
---|
| 332 | // paths would conflict with ours. |
---|
| 333 | |
---|
| 334 | // First, we must find any existing menu items that may |
---|
| 335 | // conflict. We use a regular expression because we don't |
---|
| 336 | // know what placeholders they might use. Note that we |
---|
| 337 | // first construct the regex itself by replacing %views_arg |
---|
| 338 | // in the display path, then we use this constructed regex |
---|
| 339 | // (which will be something like '#^(foo/%[^/]*/bar)$#') to |
---|
| 340 | // search through the existing paths. |
---|
| 341 | $regex = '#^(' . preg_replace('#%views_arg#', '%[^/]*', implode('|', array_keys($result))) . ')$#'; |
---|
| 342 | $matches = preg_grep($regex, array_keys($callbacks)); |
---|
| 343 | |
---|
| 344 | // Remove any conflicting items that were found. |
---|
| 345 | foreach ($matches as $path) { |
---|
| 346 | // Don't remove the paths we just added! |
---|
| 347 | if (!isset($our_paths[$path])) { |
---|
| 348 | unset($callbacks[$path]); |
---|
| 349 | } |
---|
| 350 | } |
---|
| 351 | foreach ($result as $path => $item) { |
---|
| 352 | if (!isset($callbacks[$path])) { |
---|
| 353 | // Add a new item, possibly replacing (and thus effectively |
---|
| 354 | // overriding) one that we removed above. |
---|
| 355 | $callbacks[$path] = $item; |
---|
| 356 | } |
---|
| 357 | else { |
---|
| 358 | // This item already exists, so it must be one that we added. |
---|
| 359 | // We change the various callback arguments to pass an array |
---|
| 360 | // of possible display IDs instead of a single ID. |
---|
| 361 | $callbacks[$path]['page arguments'][1] = (array)$callbacks[$path]['page arguments'][1]; |
---|
| 362 | $callbacks[$path]['page arguments'][1][] = $display_id; |
---|
| 363 | $callbacks[$path]['access arguments'][] = $item['access arguments'][0]; |
---|
| 364 | $callbacks[$path]['load arguments'][1] = (array)$callbacks[$path]['load arguments'][1]; |
---|
| 365 | $callbacks[$path]['load arguments'][1][] = $display_id; |
---|
| 366 | } |
---|
| 367 | $our_paths[$path] = TRUE; |
---|
| 368 | } |
---|
| 369 | } |
---|
| 370 | } |
---|
| 371 | |
---|
| 372 | // Save memory: Destroy those views. |
---|
| 373 | foreach ($views as $data) { |
---|
| 374 | list($view, $display_id) = $data; |
---|
| 375 | $view->destroy(); |
---|
| 376 | } |
---|
| 377 | } |
---|
| 378 | |
---|
| 379 | /** |
---|
| 380 | * Helper function for menu loading. This will automatically be |
---|
| 381 | * called in order to 'load' a views argument; primarily it |
---|
| 382 | * will be used to perform validation. |
---|
| 383 | * |
---|
| 384 | * @param $value |
---|
| 385 | * The actual value passed. |
---|
| 386 | * @param $name |
---|
| 387 | * The name of the view. This needs to be specified in the 'load function' |
---|
| 388 | * of the menu entry. |
---|
| 389 | * @param $display_id |
---|
| 390 | * The display id that will be loaded for this menu item. |
---|
| 391 | * @param $index |
---|
| 392 | * The menu argument index. This counts from 1. |
---|
| 393 | */ |
---|
| 394 | function views_arg_load($value, $name, $display_id, $index) { |
---|
| 395 | static $views = array(); |
---|
| 396 | |
---|
| 397 | // Make sure we haven't already loaded this views argument for a similar menu |
---|
| 398 | // item elsewhere. |
---|
| 399 | $key = $name . ':' . $display_id . ':' . $value . ':' . $index; |
---|
| 400 | if (isset($views[$key])) { |
---|
| 401 | return $views[$key]; |
---|
| 402 | } |
---|
| 403 | |
---|
| 404 | if ($view = views_get_view($name)) { |
---|
| 405 | $view->set_display($display_id); |
---|
| 406 | $view->init_handlers(); |
---|
| 407 | |
---|
| 408 | $ids = array_keys($view->argument); |
---|
| 409 | |
---|
| 410 | $indexes = array(); |
---|
| 411 | $path = explode('/', $view->get_path()); |
---|
| 412 | |
---|
| 413 | foreach ($path as $id => $piece) { |
---|
| 414 | if ($piece == '%' && !empty($ids)) { |
---|
| 415 | $indexes[$id] = array_shift($ids); |
---|
| 416 | } |
---|
| 417 | } |
---|
| 418 | |
---|
| 419 | if (isset($indexes[$index])) { |
---|
| 420 | if (isset($view->argument[$indexes[$index]])) { |
---|
| 421 | $arg = $view->argument[$indexes[$index]]->validate_argument($value) ? $value : FALSE; |
---|
| 422 | $view->destroy(); |
---|
| 423 | |
---|
| 424 | // Store the output in case we load this same menu item again. |
---|
| 425 | $views[$key] = $arg; |
---|
| 426 | return $arg; |
---|
| 427 | } |
---|
| 428 | } |
---|
| 429 | $view->destroy(); |
---|
| 430 | } |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | /** |
---|
| 434 | * Page callback entry point; requires a view and a display id, then |
---|
| 435 | * passes control to the display handler. |
---|
| 436 | */ |
---|
| 437 | function views_page() { |
---|
| 438 | $args = func_get_args(); |
---|
| 439 | $name = array_shift($args); |
---|
| 440 | $display_id = array_shift($args); |
---|
| 441 | |
---|
| 442 | // Load the view |
---|
| 443 | if ($view = views_get_view($name)) { |
---|
| 444 | return $view->execute_display($display_id, $args); |
---|
| 445 | } |
---|
| 446 | |
---|
| 447 | // Fallback; if we get here no view was found or handler was not valid. |
---|
| 448 | return drupal_not_found(); |
---|
| 449 | } |
---|
| 450 | |
---|
| 451 | /** |
---|
| 452 | * Implementation of hook_block |
---|
| 453 | */ |
---|
| 454 | function views_block($op = 'list', $delta = 0, $edit = array()) { |
---|
| 455 | switch ($op) { |
---|
| 456 | case 'list': |
---|
| 457 | // Try to avoid instantiating all the views just to get the blocks info. |
---|
| 458 | views_include('cache'); |
---|
| 459 | $cache = views_cache_get('views_block_items', TRUE); |
---|
| 460 | if ($cache && is_array($cache->data)) { |
---|
| 461 | return $cache->data; |
---|
| 462 | } |
---|
| 463 | |
---|
| 464 | $items = array(); |
---|
| 465 | $views = views_get_all_views(); |
---|
| 466 | foreach ($views as $view) { |
---|
| 467 | // disabled views get nothing. |
---|
| 468 | if (!empty($view->disabled)) { |
---|
| 469 | continue; |
---|
| 470 | } |
---|
| 471 | |
---|
| 472 | $view->init_display(); |
---|
| 473 | foreach ($view->display as $display_id => $display) { |
---|
| 474 | |
---|
| 475 | if (isset($display->handler) && !empty($display->handler->definition['uses hook block'])) { |
---|
| 476 | $result = $display->handler->execute_hook_block(); |
---|
| 477 | if (is_array($result)) { |
---|
| 478 | $items = array_merge($items, $result); |
---|
| 479 | } |
---|
| 480 | } |
---|
| 481 | |
---|
| 482 | if (isset($display->handler) && $display->handler->get_option('exposed_block')) { |
---|
| 483 | $result = $display->handler->get_special_blocks(); |
---|
| 484 | if (is_array($result)) { |
---|
| 485 | $items = array_merge($items, $result); |
---|
| 486 | } |
---|
| 487 | } |
---|
| 488 | } |
---|
| 489 | } |
---|
| 490 | |
---|
| 491 | // block.module has a delta length limit of 32, but our deltas can |
---|
| 492 | // unfortunately be longer because view names can be 32 and display IDs |
---|
| 493 | // can also be 32. So for very long deltas, change to md5 hashes. |
---|
| 494 | $hashes = array(); |
---|
| 495 | |
---|
| 496 | // get the keys because we're modifying the array and we don't want to |
---|
| 497 | // confuse PHP too much. |
---|
| 498 | $keys = array_keys($items); |
---|
| 499 | foreach ($keys as $delta) { |
---|
| 500 | if (strlen($delta) >= 32) { |
---|
| 501 | $hash = md5($delta); |
---|
| 502 | $hashes[$hash] = $delta; |
---|
| 503 | $items[$hash] = $items[$delta]; |
---|
| 504 | unset($items[$delta]); |
---|
| 505 | } |
---|
| 506 | } |
---|
| 507 | |
---|
| 508 | // Only save hashes if they have changed. |
---|
| 509 | $old_hashes = variable_get('views_block_hashes', array()); |
---|
| 510 | if ($hashes != $old_hashes) { |
---|
| 511 | variable_set('views_block_hashes', $hashes); |
---|
| 512 | } |
---|
| 513 | // Save memory: Destroy those views. |
---|
| 514 | foreach ($views as $view) { |
---|
| 515 | $view->destroy(); |
---|
| 516 | } |
---|
| 517 | |
---|
| 518 | views_cache_set('views_block_items', $items, TRUE); |
---|
| 519 | |
---|
| 520 | return $items; |
---|
| 521 | case 'view': |
---|
| 522 | // if this is 32, this should be an md5 hash. |
---|
| 523 | if (strlen($delta) == 32) { |
---|
| 524 | $hashes = variable_get('views_block_hashes', array()); |
---|
| 525 | if (!empty($hashes[$delta])) { |
---|
| 526 | $delta = $hashes[$delta]; |
---|
| 527 | } |
---|
| 528 | } |
---|
| 529 | |
---|
| 530 | // This indicates it's a special one. |
---|
| 531 | if (substr($delta, 0, 1) == '-') { |
---|
| 532 | list($nothing, $type, $name, $display_id) = explode('-', $delta); |
---|
| 533 | // Put the - back on. |
---|
| 534 | $type = '-' . $type; |
---|
| 535 | if ($view = views_get_view($name)) { |
---|
| 536 | if ($view->access($display_id)) { |
---|
| 537 | $view->set_display($display_id); |
---|
| 538 | if (isset($view->display_handler)) { |
---|
| 539 | $output = $view->display_handler->view_special_blocks($type); |
---|
| 540 | $view->destroy(); |
---|
| 541 | return $output; |
---|
| 542 | } |
---|
| 543 | } |
---|
| 544 | $view->destroy(); |
---|
| 545 | } |
---|
| 546 | } |
---|
| 547 | |
---|
| 548 | list($name, $display_id) = explode('-', $delta); |
---|
| 549 | // Load the view |
---|
| 550 | if ($view = views_get_view($name)) { |
---|
| 551 | if ($view->access($display_id)) { |
---|
| 552 | $output = $view->execute_display($display_id); |
---|
| 553 | $view->destroy(); |
---|
| 554 | return $output; |
---|
| 555 | } |
---|
| 556 | $view->destroy(); |
---|
| 557 | } |
---|
| 558 | break; |
---|
| 559 | } |
---|
| 560 | } |
---|
| 561 | |
---|
[59029b2] | 562 | /** |
---|
| 563 | + * Returns an array of language names. |
---|
| 564 | + * |
---|
| 565 | + * This is a one to one copy of locale_language_list because we can't rely on enabled locale module. |
---|
| 566 | + * |
---|
| 567 | + * @param $field |
---|
| 568 | + * 'name' => names in current language, localized |
---|
| 569 | + * 'native' => native names |
---|
| 570 | + * @param $all |
---|
| 571 | + * Boolean to return all languages or only enabled ones |
---|
| 572 | + * |
---|
| 573 | + * @see locale_language_list |
---|
| 574 | + */ |
---|
| 575 | function views_language_list($field = 'name', $all = FALSE) { |
---|
| 576 | if ($all) { |
---|
| 577 | $languages = language_list(); |
---|
| 578 | } |
---|
| 579 | else { |
---|
| 580 | $languages = language_list('enabled'); |
---|
| 581 | $languages = $languages[1]; |
---|
| 582 | } |
---|
| 583 | $list = array(); |
---|
| 584 | foreach ($languages as $language) { |
---|
| 585 | $list[$language->language] = ($field == 'name') ? t($language->name) : $language->$field; |
---|
| 586 | } |
---|
| 587 | return $list; |
---|
| 588 | } |
---|
| 589 | |
---|
| 590 | |
---|
[177a560] | 591 | /** |
---|
| 592 | * Implementation of hook_flush_caches(). |
---|
| 593 | */ |
---|
| 594 | function views_flush_caches() { |
---|
| 595 | return array('cache_views', 'cache_views_data'); |
---|
| 596 | } |
---|
| 597 | |
---|
| 598 | /** |
---|
| 599 | * Invalidate the views cache, forcing a rebuild on the next grab of table data. |
---|
| 600 | */ |
---|
| 601 | function views_invalidate_cache() { |
---|
| 602 | cache_clear_all('*', 'cache_views', true); |
---|
| 603 | } |
---|
| 604 | |
---|
| 605 | /** |
---|
| 606 | * Access callback to determine if the user can import Views. |
---|
| 607 | * |
---|
| 608 | * View imports require an additional access check because they are PHP |
---|
| 609 | * code and PHP is more locked down than administer views. |
---|
| 610 | */ |
---|
| 611 | function views_import_access() { |
---|
| 612 | return user_access('administer views') && user_access('use PHP for block visibility'); |
---|
| 613 | } |
---|
| 614 | |
---|
| 615 | /** |
---|
| 616 | * Determine if the logged in user has access to a view. |
---|
| 617 | * |
---|
| 618 | * This function should only be called from a menu hook or some other |
---|
| 619 | * embedded source. Each argument is the result of a call to |
---|
| 620 | * views_plugin_access::get_access_callback() which is then used |
---|
| 621 | * to determine if that display is accessible. If *any* argument |
---|
| 622 | * is accessible, then the view is accessible. |
---|
| 623 | */ |
---|
| 624 | function views_access() { |
---|
| 625 | $args = func_get_args(); |
---|
| 626 | foreach ($args as $arg) { |
---|
| 627 | if ($arg === TRUE) { |
---|
| 628 | return TRUE; |
---|
| 629 | } |
---|
| 630 | |
---|
| 631 | if (!is_array($arg)) { |
---|
| 632 | continue; |
---|
| 633 | } |
---|
| 634 | |
---|
| 635 | list($callback, $arguments) = $arg; |
---|
| 636 | $arguments = $arguments ? $arguments : array(); |
---|
[59029b2] | 637 | // Bring dynamic arguments to the access callback. |
---|
| 638 | foreach ($arguments as $key => $value) { |
---|
| 639 | if (is_int($value) && isset($args[$value])) { |
---|
| 640 | $arguments[$key] = $args[$value]; |
---|
| 641 | } |
---|
| 642 | } |
---|
[177a560] | 643 | if (function_exists($callback) && call_user_func_array($callback, $arguments)) { |
---|
| 644 | return TRUE; |
---|
| 645 | } |
---|
| 646 | } |
---|
| 647 | |
---|
| 648 | return FALSE; |
---|
| 649 | } |
---|
| 650 | |
---|
| 651 | /** |
---|
| 652 | * Access callback for the views_plugin_access_perm access plugin. |
---|
| 653 | * |
---|
| 654 | * Determine if the specified user has access to a view on the basis of |
---|
| 655 | * permissions. If the $account argument is omitted, the current user |
---|
| 656 | * is used. |
---|
| 657 | */ |
---|
| 658 | function views_check_perm($perm, $account = NULL) { |
---|
| 659 | return user_access($perm, $account) || user_access('access all views', $account); |
---|
| 660 | } |
---|
| 661 | |
---|
| 662 | /** |
---|
| 663 | * Access callback for the views_plugin_access_role access plugin. |
---|
| 664 | |
---|
| 665 | * Determine if the specified user has access to a view on the basis of any of |
---|
| 666 | * the requested roles. If the $account argument is omitted, the current user |
---|
| 667 | * is used. |
---|
| 668 | */ |
---|
| 669 | function views_check_roles($rids, $account = NULL) { |
---|
| 670 | global $user; |
---|
| 671 | $account = isset($account) ? $account : $user; |
---|
| 672 | $roles = array_keys($account->roles); |
---|
| 673 | $roles[] = $account->uid ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID; |
---|
| 674 | return user_access('access all views', $account) || array_intersect(array_filter($rids), $roles); |
---|
| 675 | } |
---|
| 676 | // ------------------------------------------------------------------ |
---|
| 677 | // Functions to help identify views that are running or ran |
---|
| 678 | |
---|
| 679 | /** |
---|
| 680 | * Set the current 'page view' that is being displayed so that it is easy |
---|
| 681 | * for other modules or the theme to identify. |
---|
| 682 | */ |
---|
| 683 | function &views_set_page_view($view = NULL) { |
---|
| 684 | static $cache = NULL; |
---|
| 685 | if (isset($view)) { |
---|
| 686 | $cache = $view; |
---|
| 687 | } |
---|
| 688 | |
---|
| 689 | return $cache; |
---|
| 690 | } |
---|
| 691 | |
---|
| 692 | /** |
---|
| 693 | * Find out what, if any, page view is currently in use. Please note that |
---|
| 694 | * this returns a reference, so be careful! You can unintentionally modify the |
---|
| 695 | * $view object. |
---|
| 696 | */ |
---|
| 697 | function &views_get_page_view() { |
---|
| 698 | return views_set_page_view(); |
---|
| 699 | } |
---|
| 700 | |
---|
| 701 | /** |
---|
| 702 | * Set the current 'current view' that is being built/rendered so that it is |
---|
| 703 | * easy for other modules or items in drupal_eval to identify |
---|
[59029b2] | 704 | * |
---|
| 705 | * @return view |
---|
[177a560] | 706 | */ |
---|
| 707 | function &views_set_current_view($view = NULL) { |
---|
| 708 | static $cache = NULL; |
---|
| 709 | if (isset($view)) { |
---|
| 710 | $cache = $view; |
---|
| 711 | } |
---|
| 712 | |
---|
| 713 | return $cache; |
---|
| 714 | } |
---|
| 715 | |
---|
| 716 | /** |
---|
| 717 | * Find out what, if any, current view is currently in use. Please note that |
---|
| 718 | * this returns a reference, so be careful! You can unintentionally modify the |
---|
| 719 | * $view object. |
---|
[59029b2] | 720 | * |
---|
| 721 | * @return view |
---|
[177a560] | 722 | */ |
---|
| 723 | function &views_get_current_view() { |
---|
| 724 | return views_set_current_view(); |
---|
| 725 | } |
---|
| 726 | |
---|
| 727 | // ------------------------------------------------------------------ |
---|
| 728 | // Include file helpers |
---|
| 729 | |
---|
| 730 | /** |
---|
| 731 | * Include views .inc files as necessary. |
---|
| 732 | */ |
---|
| 733 | function views_include($file) { |
---|
| 734 | static $used = array(); |
---|
| 735 | if (!isset($used[$file])) { |
---|
| 736 | require_once './' . drupal_get_path('module', 'views') . "/includes/$file.inc"; |
---|
| 737 | } |
---|
| 738 | |
---|
| 739 | $used[$file] = TRUE; |
---|
| 740 | } |
---|
| 741 | |
---|
| 742 | /** |
---|
| 743 | * Load views files on behalf of modules. |
---|
| 744 | */ |
---|
[59029b2] | 745 | function views_module_include($file, $reset = FALSE) { |
---|
| 746 | foreach (views_get_module_apis($reset) as $module => $info) { |
---|
[177a560] | 747 | if (file_exists("./$info[path]/$module.$file")) { |
---|
| 748 | require_once "./$info[path]/$module.$file"; |
---|
| 749 | } |
---|
| 750 | } |
---|
| 751 | } |
---|
| 752 | |
---|
| 753 | /** |
---|
| 754 | * Get a list of modules that support the current views API. |
---|
| 755 | */ |
---|
[59029b2] | 756 | function views_get_module_apis($reset = FALSE) { |
---|
[177a560] | 757 | static $cache = NULL; |
---|
[59029b2] | 758 | if (!isset($cache) || $reset) { |
---|
[177a560] | 759 | $cache = array(); |
---|
| 760 | foreach (module_implements('views_api') as $module) { |
---|
| 761 | $function = $module . '_views_api'; |
---|
| 762 | $info = $function(); |
---|
[59029b2] | 763 | if (version_compare($info['api'], views_api_minimum_version(), '>=') && |
---|
| 764 | version_compare($info['api'], views_api_version(), '<=')) { |
---|
[177a560] | 765 | if (!isset($info['path'])) { |
---|
| 766 | $info['path'] = drupal_get_path('module', $module); |
---|
| 767 | } |
---|
| 768 | $cache[$module] = $info; |
---|
| 769 | } |
---|
| 770 | } |
---|
| 771 | } |
---|
| 772 | |
---|
| 773 | return $cache; |
---|
| 774 | } |
---|
| 775 | |
---|
| 776 | /** |
---|
| 777 | * Include views .css files. |
---|
| 778 | */ |
---|
| 779 | function views_add_css($file) { |
---|
| 780 | // We set preprocess to FALSE because we are adding the files conditionally, |
---|
| 781 | // and we don't want to generate duplicate cache files. |
---|
| 782 | // TODO: at some point investigate adding some files unconditionally and |
---|
| 783 | // allowing preprocess. |
---|
| 784 | drupal_add_css(drupal_get_path('module', 'views') . "/css/$file.css", 'module', 'all', FALSE); |
---|
| 785 | } |
---|
| 786 | |
---|
| 787 | /** |
---|
| 788 | * Include views .js files. |
---|
| 789 | */ |
---|
| 790 | function views_add_js($file) { |
---|
| 791 | // If javascript has been disabled by the user, never add js files. |
---|
| 792 | if (variable_get('views_no_javascript', FALSE)) { |
---|
| 793 | return; |
---|
| 794 | } |
---|
| 795 | |
---|
| 796 | static $base = TRUE; |
---|
| 797 | if ($base) { |
---|
| 798 | drupal_add_js(drupal_get_path('module', 'views') . "/js/base.js"); |
---|
| 799 | $base = FALSE; |
---|
| 800 | } |
---|
| 801 | drupal_add_js(drupal_get_path('module', 'views') . "/js/$file.js"); |
---|
| 802 | } |
---|
| 803 | |
---|
| 804 | /** |
---|
| 805 | * Load views files on behalf of modules. |
---|
| 806 | */ |
---|
[59029b2] | 807 | function views_include_handlers($reset = FALSE) { |
---|
[177a560] | 808 | static $finished = FALSE; |
---|
| 809 | // Ensure this only gets run once. |
---|
[59029b2] | 810 | if ($finished && !$reset) { |
---|
[177a560] | 811 | return; |
---|
| 812 | } |
---|
| 813 | |
---|
| 814 | views_include('base'); |
---|
| 815 | views_include('handlers'); |
---|
| 816 | views_include('cache'); |
---|
| 817 | views_include('plugins'); |
---|
[59029b2] | 818 | _views_include_handlers($reset); |
---|
[177a560] | 819 | $finished = TRUE; |
---|
| 820 | } |
---|
| 821 | |
---|
| 822 | /** |
---|
| 823 | * Load default views files on behalf of modules. |
---|
| 824 | */ |
---|
[59029b2] | 825 | function views_include_default_views($reset = FALSE) { |
---|
[177a560] | 826 | static $finished = FALSE; |
---|
| 827 | // Ensure this only gets run once. |
---|
[59029b2] | 828 | if ($finished && !$reset) { |
---|
[177a560] | 829 | return; |
---|
| 830 | } |
---|
| 831 | |
---|
| 832 | // Default views hooks may be in the normal handler file, |
---|
| 833 | // or in a separate views_default file at the discretion of |
---|
| 834 | // the module author. |
---|
[59029b2] | 835 | views_include_handlers($reset); |
---|
[177a560] | 836 | |
---|
[59029b2] | 837 | _views_include_default_views($reset); |
---|
[177a560] | 838 | $finished = TRUE; |
---|
| 839 | } |
---|
| 840 | |
---|
| 841 | // ----------------------------------------------------------------------- |
---|
| 842 | // Views handler functions |
---|
| 843 | |
---|
| 844 | /** |
---|
| 845 | * Fetch a handler from the data cache. |
---|
| 846 | * |
---|
| 847 | * @param $table |
---|
| 848 | * The name of the table this handler is from. |
---|
| 849 | * @param $field |
---|
| 850 | * The name of the field this handler is from. |
---|
| 851 | * @param $key |
---|
| 852 | * The type of handler. i.e, sort, field, argument, filter, relationship |
---|
[59029b2] | 853 | * @param $override |
---|
| 854 | * Override the actual handler object with this class. Used for |
---|
| 855 | * aggregation when the handler is redirected to the aggregation |
---|
| 856 | * handler. |
---|
[177a560] | 857 | * |
---|
| 858 | * @return views_handler |
---|
| 859 | * An instance of a handler object. May be views_handler_broken. |
---|
| 860 | */ |
---|
[59029b2] | 861 | function views_get_handler($table, $field, $key, $override = NULL) { |
---|
[177a560] | 862 | $data = views_fetch_data($table); |
---|
[59029b2] | 863 | $handler = NULL; |
---|
| 864 | views_include('handlers'); |
---|
| 865 | |
---|
[177a560] | 866 | if (isset($data[$field][$key])) { |
---|
| 867 | // Set up a default handler: |
---|
| 868 | if (empty($data[$field][$key]['handler'])) { |
---|
| 869 | $data[$field][$key]['handler'] = 'views_handler_' . $key; |
---|
| 870 | } |
---|
[59029b2] | 871 | |
---|
| 872 | if ($override) { |
---|
| 873 | $data[$field][$key]['override handler'] = $override; |
---|
| 874 | } |
---|
| 875 | |
---|
| 876 | $handler = _views_prepare_handler($data[$field][$key], $data, $field, $key); |
---|
| 877 | } |
---|
| 878 | |
---|
| 879 | if ($handler) { |
---|
| 880 | return $handler; |
---|
[177a560] | 881 | } |
---|
[59029b2] | 882 | |
---|
[177a560] | 883 | vpr("Missing handler: $table $field $key"); |
---|
| 884 | $broken = array( |
---|
| 885 | 'title' => t('Broken handler @table.@field', array('@table' => $table, '@field' => $field)), |
---|
| 886 | 'handler' => 'views_handler_' . $key . '_broken', |
---|
| 887 | 'table' => $table, |
---|
| 888 | 'field' => $field, |
---|
| 889 | ); |
---|
[59029b2] | 890 | return _views_create_handler($broken, 'handler', $key); |
---|
[177a560] | 891 | } |
---|
| 892 | |
---|
| 893 | /** |
---|
| 894 | * Fetch Views' data from the cache |
---|
| 895 | */ |
---|
[59029b2] | 896 | function views_fetch_data($table = NULL, $reset = FALSE) { |
---|
[177a560] | 897 | views_include('cache'); |
---|
[59029b2] | 898 | return _views_fetch_data($table, $reset); |
---|
[177a560] | 899 | } |
---|
| 900 | |
---|
| 901 | // ----------------------------------------------------------------------- |
---|
| 902 | // Views plugin functions |
---|
| 903 | |
---|
| 904 | /** |
---|
| 905 | * Fetch the plugin data from cache. |
---|
| 906 | */ |
---|
[59029b2] | 907 | function views_fetch_plugin_data($type = NULL, $plugin = NULL, $reset = FALSE) { |
---|
[177a560] | 908 | views_include('cache'); |
---|
[59029b2] | 909 | return _views_fetch_plugin_data($type, $plugin, $reset); |
---|
[177a560] | 910 | } |
---|
| 911 | |
---|
| 912 | /** |
---|
| 913 | * Get a handler for a plugin |
---|
| 914 | * |
---|
| 915 | * @return views_plugin |
---|
| 916 | * |
---|
| 917 | * The created plugin object. |
---|
| 918 | */ |
---|
[59029b2] | 919 | function views_get_plugin($type, $plugin, $reset = FALSE) { |
---|
| 920 | $definition = views_fetch_plugin_data($type, $plugin, $reset); |
---|
[177a560] | 921 | if (!empty($definition)) { |
---|
| 922 | return _views_create_handler($definition, $type); |
---|
| 923 | } |
---|
| 924 | } |
---|
| 925 | |
---|
[59029b2] | 926 | /** |
---|
| 927 | * Load the current enabled localization plugin. |
---|
| 928 | * |
---|
| 929 | * @return The name of the localization plugin. |
---|
| 930 | */ |
---|
| 931 | function views_get_localization_plugin() { |
---|
| 932 | $plugin = variable_get('views_localization_plugin', ''); |
---|
| 933 | // Provide sane default values for the localization plugin. |
---|
| 934 | if (empty($plugin)) { |
---|
| 935 | if (module_exists('locale')) { |
---|
| 936 | $plugin = 'core'; |
---|
| 937 | } |
---|
| 938 | else { |
---|
| 939 | $plugin = 'none'; |
---|
| 940 | } |
---|
| 941 | } |
---|
| 942 | |
---|
| 943 | return $plugin; |
---|
| 944 | } |
---|
| 945 | |
---|
[177a560] | 946 | // ----------------------------------------------------------------------- |
---|
| 947 | // Views database functions |
---|
| 948 | |
---|
| 949 | /** |
---|
| 950 | * Get a view from the default views defined by modules. |
---|
| 951 | * |
---|
| 952 | * Default views are cached per-language. This function will rescan the |
---|
| 953 | * default_views hook if necessary. |
---|
| 954 | * |
---|
| 955 | * @param $view_name |
---|
| 956 | * The name of the view to load. |
---|
| 957 | * @return |
---|
| 958 | * A view object or NULL if it is not available. |
---|
| 959 | */ |
---|
[59029b2] | 960 | function &views_get_default_view($view_name, $reset = FALSE) { |
---|
[177a560] | 961 | $null = NULL; |
---|
| 962 | |
---|
| 963 | // Attempt to load individually cached view from cache. |
---|
| 964 | views_include('cache'); |
---|
[59029b2] | 965 | if (!$reset) { |
---|
| 966 | $data = views_cache_get("views_default:{$view_name}", TRUE); |
---|
| 967 | if (isset($data->data) && is_object($data->data)) { |
---|
| 968 | return $data->data; |
---|
| 969 | } |
---|
[177a560] | 970 | } |
---|
| 971 | |
---|
| 972 | // Otherwise, allow entire cache to be rebuilt. |
---|
[59029b2] | 973 | $cache = views_discover_default_views($reset); |
---|
[177a560] | 974 | if (isset($cache[$view_name])) { |
---|
| 975 | return $cache[$view_name]; |
---|
| 976 | } |
---|
| 977 | return $null; |
---|
| 978 | } |
---|
| 979 | |
---|
| 980 | /** |
---|
| 981 | * Create an empty view to work with. |
---|
| 982 | * |
---|
| 983 | * @return view |
---|
| 984 | * A fully formed, empty $view object. This object must be populated before |
---|
| 985 | * it can be successfully saved. |
---|
| 986 | */ |
---|
| 987 | function views_new_view() { |
---|
| 988 | views_include('view'); |
---|
| 989 | $view = new view(); |
---|
| 990 | $view->vid = 'new'; |
---|
| 991 | $view->add_display('default'); |
---|
| 992 | |
---|
| 993 | return $view; |
---|
| 994 | } |
---|
| 995 | |
---|
| 996 | /** |
---|
| 997 | * Scan all modules for default views and rebuild the default views cache. |
---|
| 998 | * |
---|
| 999 | * @return An associative array of all known default views. |
---|
| 1000 | */ |
---|
[59029b2] | 1001 | function views_discover_default_views($reset = FALSE) { |
---|
[177a560] | 1002 | static $cache = array(); |
---|
| 1003 | |
---|
[59029b2] | 1004 | if (empty($cache) || $reset) { |
---|
[177a560] | 1005 | views_include('cache'); |
---|
[59029b2] | 1006 | $cache = _views_discover_default_views($reset); |
---|
[177a560] | 1007 | } |
---|
| 1008 | return $cache; |
---|
| 1009 | } |
---|
| 1010 | |
---|
| 1011 | /** |
---|
| 1012 | * Return a list of all views and display IDs that have a particular |
---|
| 1013 | * setting in their display's plugin settings. |
---|
| 1014 | * |
---|
| 1015 | * @return |
---|
| 1016 | * @code |
---|
| 1017 | * array( |
---|
| 1018 | * array($view, $display_id), |
---|
| 1019 | * array($view, $display_id), |
---|
| 1020 | * ); |
---|
| 1021 | * @endcode |
---|
| 1022 | */ |
---|
| 1023 | function views_get_applicable_views($type) { |
---|
| 1024 | // @todo: Use a smarter flagging system so that we don't have to |
---|
| 1025 | // load every view for this. |
---|
| 1026 | $result = array(); |
---|
| 1027 | $views = views_get_all_views(); |
---|
| 1028 | |
---|
| 1029 | foreach ($views as $view) { |
---|
| 1030 | // Skip disabled views. |
---|
| 1031 | if (!empty($view->disabled)) { |
---|
| 1032 | continue; |
---|
| 1033 | } |
---|
| 1034 | |
---|
| 1035 | if (empty($view->display)) { |
---|
| 1036 | // Skip this view as it is broken. |
---|
| 1037 | vsm(t("Skipping broken view @view", array('@view' => $view->name))); |
---|
| 1038 | continue; |
---|
| 1039 | } |
---|
| 1040 | |
---|
| 1041 | // Loop on array keys because something seems to muck with $view->display |
---|
| 1042 | // a bit in PHP4. |
---|
| 1043 | foreach (array_keys($view->display) as $id) { |
---|
| 1044 | $plugin = views_fetch_plugin_data('display', $view->display[$id]->display_plugin); |
---|
| 1045 | if (!empty($plugin[$type])) { |
---|
| 1046 | // This view uses hook menu. Clone it so that different handlers |
---|
| 1047 | // don't trip over each other, and add it to the list. |
---|
| 1048 | $v = $view->clone_view(); |
---|
[59029b2] | 1049 | if ($v->set_display($id) && $v->display_handler->get_option('enabled')) { |
---|
[177a560] | 1050 | $result[] = array($v, $id); |
---|
| 1051 | } |
---|
| 1052 | // In PHP 4.4.7 and presumably earlier, if we do not unset $v |
---|
| 1053 | // here, we will find that it actually overwrites references |
---|
| 1054 | // possibly due to shallow copying issues. |
---|
| 1055 | unset($v); |
---|
| 1056 | } |
---|
| 1057 | } |
---|
| 1058 | } |
---|
| 1059 | return $result; |
---|
| 1060 | } |
---|
| 1061 | |
---|
| 1062 | /** |
---|
| 1063 | * Return an array of all views as fully loaded $view objects. |
---|
| 1064 | * |
---|
| 1065 | * @param $reset |
---|
| 1066 | * If TRUE, reset the static cache forcing views to be reloaded. |
---|
| 1067 | */ |
---|
| 1068 | function views_get_all_views($reset = FALSE) { |
---|
| 1069 | static $views = array(); |
---|
| 1070 | |
---|
| 1071 | if (empty($views) || $reset) { |
---|
| 1072 | $views = array(); |
---|
| 1073 | |
---|
| 1074 | // First, get all applicable views. |
---|
| 1075 | views_include('view'); |
---|
| 1076 | $views = view::load_views(); |
---|
| 1077 | |
---|
| 1078 | // Get all default views. |
---|
| 1079 | $status = variable_get('views_defaults', array()); |
---|
| 1080 | |
---|
[59029b2] | 1081 | foreach (views_discover_default_views($reset) as $view) { |
---|
[177a560] | 1082 | // Determine if default view is enabled or disabled. |
---|
| 1083 | if (isset($status[$view->name])) { |
---|
| 1084 | $view->disabled = $status[$view->name]; |
---|
| 1085 | } |
---|
| 1086 | |
---|
| 1087 | // If overridden, also say so. |
---|
| 1088 | if (!empty($views[$view->name])) { |
---|
| 1089 | $views[$view->name]->type = t('Overridden'); |
---|
| 1090 | } |
---|
| 1091 | else { |
---|
| 1092 | $view->type = t('Default'); |
---|
| 1093 | $views[$view->name] = $view; |
---|
| 1094 | } |
---|
| 1095 | } |
---|
| 1096 | |
---|
| 1097 | } |
---|
| 1098 | return $views; |
---|
| 1099 | } |
---|
| 1100 | |
---|
| 1101 | /** |
---|
| 1102 | * Get a view from the database or from default views. |
---|
| 1103 | * |
---|
| 1104 | * This function is just a static wrapper around views::load(). This function |
---|
| 1105 | * isn't called 'views_load()' primarily because it might get a view |
---|
| 1106 | * from the default views which aren't technically loaded from the database. |
---|
| 1107 | * |
---|
| 1108 | * @param $name |
---|
| 1109 | * The name of the view. |
---|
| 1110 | * @param $reset |
---|
| 1111 | * If TRUE, reset this entry in the load cache. |
---|
| 1112 | * @return view |
---|
| 1113 | * A reference to the $view object. Use $reset if you're sure you want |
---|
| 1114 | * a fresh one. |
---|
| 1115 | */ |
---|
| 1116 | function views_get_view($name, $reset = FALSE) { |
---|
| 1117 | views_include('view'); |
---|
| 1118 | $view = view::load($name, $reset); |
---|
[59029b2] | 1119 | $default_view = views_get_default_view($name, $reset); |
---|
[177a560] | 1120 | |
---|
| 1121 | // The view does not exist. |
---|
| 1122 | if (empty($view) && empty($default_view)) { |
---|
| 1123 | return; |
---|
| 1124 | } |
---|
| 1125 | // The view is defined in code. |
---|
| 1126 | elseif (empty($view) && !empty($default_view)) { |
---|
| 1127 | $status = variable_get('views_defaults', array()); |
---|
| 1128 | if (isset($status[$default_view->name])) { |
---|
| 1129 | $default_view->disabled = $status[$default_view->name]; |
---|
| 1130 | } |
---|
| 1131 | $default_view->type = t('Default'); |
---|
| 1132 | return $default_view->clone_view(); |
---|
| 1133 | } |
---|
| 1134 | // The view is overriden/defined in the database. |
---|
| 1135 | elseif (!empty($view) && !empty($default_view)) { |
---|
| 1136 | $view->type = t('Overridden'); |
---|
| 1137 | } |
---|
| 1138 | |
---|
| 1139 | return $view->clone_view(); |
---|
| 1140 | } |
---|
| 1141 | |
---|
| 1142 | // ------------------------------------------------------------------ |
---|
| 1143 | // Views debug helper functions |
---|
| 1144 | |
---|
| 1145 | /** |
---|
| 1146 | * Provide debug output for Views. This relies on devel.module |
---|
| 1147 | */ |
---|
| 1148 | function views_debug($message) { |
---|
| 1149 | if (module_exists('devel') && variable_get('views_devel_output', FALSE) && user_access('access devel information')) { |
---|
| 1150 | if (is_string($message)) { |
---|
| 1151 | $output = $message; |
---|
| 1152 | } |
---|
| 1153 | else { |
---|
| 1154 | $output = var_export($message, TRUE); |
---|
| 1155 | } |
---|
| 1156 | if (variable_get('views_devel_region', 'footer') != 'watchdog') { |
---|
| 1157 | drupal_set_content(variable_get('views_devel_region', 'footer'), '<pre>' . $output . '</pre>'); |
---|
| 1158 | } |
---|
| 1159 | else { |
---|
| 1160 | watchdog('views_logging', '<pre>' . $output . '</pre>'); |
---|
| 1161 | } |
---|
| 1162 | } |
---|
| 1163 | } |
---|
| 1164 | |
---|
| 1165 | /** |
---|
| 1166 | * Shortcut to views_debug() |
---|
| 1167 | */ |
---|
| 1168 | function vpr($message) { |
---|
| 1169 | views_debug($message); |
---|
| 1170 | } |
---|
| 1171 | |
---|
| 1172 | /** |
---|
| 1173 | * Debug messages |
---|
| 1174 | */ |
---|
| 1175 | function vsm($message) { |
---|
| 1176 | if (module_exists('devel')) { |
---|
| 1177 | dsm($message); |
---|
| 1178 | } |
---|
| 1179 | } |
---|
| 1180 | |
---|
| 1181 | function views_trace() { |
---|
| 1182 | $message = ''; |
---|
| 1183 | foreach (debug_backtrace() as $item) { |
---|
| 1184 | if (!empty($item['file']) && !in_array($item['function'], array('vsm_trace', 'vpr_trace', 'views_trace'))) { |
---|
| 1185 | $message .= basename($item['file']) . ": " . (empty($item['class']) ? '' : ($item['class'] . '->')) . "$item[function] line $item[line]" . "\n"; |
---|
| 1186 | } |
---|
| 1187 | } |
---|
| 1188 | return $message; |
---|
| 1189 | } |
---|
| 1190 | |
---|
| 1191 | function vsm_trace() { |
---|
| 1192 | vsm(views_trace()); |
---|
| 1193 | } |
---|
| 1194 | |
---|
| 1195 | function vpr_trace() { |
---|
| 1196 | dpr(views_trace()); |
---|
| 1197 | } |
---|
| 1198 | |
---|
| 1199 | // ------------------------------------------------------------------ |
---|
[59029b2] | 1200 | // Views form (View with form elements) |
---|
| 1201 | |
---|
| 1202 | /** |
---|
| 1203 | * Returns TRUE if the passed-in view contains handlers with views form |
---|
| 1204 | * implementations, FALSE otherwise. |
---|
| 1205 | */ |
---|
| 1206 | function views_view_has_form_elements($view) { |
---|
| 1207 | foreach ($view->field as $field) { |
---|
| 1208 | if (property_exists($field, 'views_form_callback') || method_exists($field, 'views_form')) { |
---|
| 1209 | return TRUE; |
---|
| 1210 | } |
---|
| 1211 | } |
---|
| 1212 | $area_handlers = array_merge(array_values($view->header), array_values($view->footer)); |
---|
| 1213 | $empty = empty($view->result); |
---|
| 1214 | foreach ($area_handlers as $area) { |
---|
| 1215 | if (method_exists($area, 'views_form') && !$area->views_form_empty($empty)) { |
---|
| 1216 | return TRUE; |
---|
| 1217 | } |
---|
| 1218 | } |
---|
| 1219 | return FALSE; |
---|
| 1220 | } |
---|
| 1221 | |
---|
| 1222 | /** |
---|
| 1223 | * This is the entry function. Just gets the form for the current step. |
---|
| 1224 | * The form is always assumed to be multistep, even if it has only one |
---|
| 1225 | * step (the default 'views_form_views_form' step). That way it is actually |
---|
| 1226 | * possible for modules to have a multistep form if they need to. |
---|
| 1227 | */ |
---|
| 1228 | function views_form(&$form_state, $view, $output) { |
---|
| 1229 | $form_state['storage']['step'] = isset($form_state['storage']['step']) ? $form_state['storage']['step'] : 'views_form_views_form'; |
---|
| 1230 | |
---|
| 1231 | $form = array(); |
---|
| 1232 | $form['#validate'] = array('views_form_validate'); |
---|
| 1233 | $form['#submit'] = array('views_form_submit'); |
---|
| 1234 | // Tell the preprocessor whether it should hide the header, footer, pager... |
---|
| 1235 | $view->show_view_elements = ($form_state['storage']['step'] == 'views_form_views_form') ? TRUE : FALSE; |
---|
| 1236 | |
---|
| 1237 | $form = $form_state['storage']['step']($form, $form_state, $view, $output); |
---|
| 1238 | return $form; |
---|
| 1239 | } |
---|
| 1240 | |
---|
| 1241 | /** |
---|
| 1242 | * The basic form validate handler. |
---|
| 1243 | * Fires the hook_views_form_validate() function. |
---|
| 1244 | */ |
---|
| 1245 | function views_form_validate($form, &$form_state) { |
---|
| 1246 | // Fire the hook. Doesn't use module_invoke_all() because $form_state needs |
---|
| 1247 | // to be passed by reference. |
---|
| 1248 | foreach (module_implements('views_form_validate') as $module) { |
---|
| 1249 | $function = $module . '_views_form_validate'; |
---|
| 1250 | $function($form, $form_state); |
---|
| 1251 | } |
---|
| 1252 | } |
---|
| 1253 | |
---|
| 1254 | /** |
---|
| 1255 | * The basic form submit handler. |
---|
| 1256 | * Fires the hook_views_form_submit() function. |
---|
| 1257 | */ |
---|
| 1258 | function views_form_submit($form, &$form_state) { |
---|
| 1259 | // Fire the hook. Doesn't use module_invoke_all() because $form_state needs |
---|
| 1260 | // to be passed by reference. |
---|
| 1261 | foreach (module_implements('views_form_submit') as $module) { |
---|
| 1262 | $function = $module . '_views_form_submit'; |
---|
| 1263 | $function($form, $form_state); |
---|
| 1264 | } |
---|
| 1265 | } |
---|
| 1266 | |
---|
| 1267 | /** |
---|
| 1268 | * Callback for the main step of a Views form. |
---|
| 1269 | * Invoked by views_form(). |
---|
| 1270 | */ |
---|
| 1271 | function views_form_views_form($form, &$form_state, $view, $output) { |
---|
| 1272 | $form['#prefix'] = '<div class="views-form">'; |
---|
| 1273 | $form['#suffix'] = '</div>'; |
---|
| 1274 | $form['#theme'] = 'views_form_views_form'; |
---|
| 1275 | $form['#validate'][] = 'views_form_views_form_validate'; |
---|
| 1276 | $form['#submit'][] = 'views_form_views_form_submit'; |
---|
| 1277 | |
---|
| 1278 | // Add the output markup to the form array so that it's included when the form |
---|
| 1279 | // array is passed to the theme function. |
---|
| 1280 | $form['output'] = array( |
---|
| 1281 | '#value' => $output, |
---|
| 1282 | // This way any additional form elements will go before the view |
---|
| 1283 | // (below the exposed widgets). |
---|
| 1284 | '#weight' => 50, |
---|
| 1285 | ); |
---|
| 1286 | |
---|
| 1287 | $substitutions = array(); |
---|
| 1288 | foreach ($view->field as $field_name => $field) { |
---|
| 1289 | $has_form = FALSE; |
---|
| 1290 | // If the field provides a views form, allow it to modify the $form array. |
---|
| 1291 | if (property_exists($field, 'views_form_callback')) { |
---|
| 1292 | $callback = $field->views_form_callback; |
---|
| 1293 | $callback($view, $field, $form, $form_state); |
---|
| 1294 | $has_form = TRUE; |
---|
| 1295 | } |
---|
| 1296 | elseif (method_exists($field, 'views_form')) { |
---|
| 1297 | $field->views_form($form, $form_state); |
---|
| 1298 | $has_form = TRUE; |
---|
| 1299 | } |
---|
| 1300 | |
---|
| 1301 | // Build the substitutions array for use in the theme function. |
---|
| 1302 | if ($has_form) { |
---|
| 1303 | foreach ($view->result as $row_id => $row) { |
---|
| 1304 | $substitutions[] = array( |
---|
| 1305 | 'placeholder' => '<!--form-item-' . $field_name . '--' . $row_id . '-->', |
---|
| 1306 | 'field_name' => $field_name, |
---|
| 1307 | 'row_id' => $row_id, |
---|
| 1308 | ); |
---|
| 1309 | } |
---|
| 1310 | } |
---|
| 1311 | } |
---|
| 1312 | |
---|
| 1313 | // Give the area handlers a chance to extend the form. |
---|
| 1314 | $area_handlers = array_merge(array_values($view->header), array_values($view->footer)); |
---|
| 1315 | $empty = empty($view->result); |
---|
| 1316 | foreach ($area_handlers as $area) { |
---|
| 1317 | if (method_exists($area, 'views_form') && !$area->views_form_empty($empty)) { |
---|
| 1318 | $area->views_form($form, $form_state); |
---|
| 1319 | } |
---|
| 1320 | } |
---|
| 1321 | |
---|
| 1322 | $form['#substitutions'] = array( |
---|
| 1323 | '#type' => 'value', |
---|
| 1324 | '#value' => $substitutions, |
---|
| 1325 | ); |
---|
| 1326 | $form['submit'] = array( |
---|
| 1327 | '#type' => 'submit', |
---|
| 1328 | '#value' => t('Save'), |
---|
| 1329 | '#weight' => 100, |
---|
| 1330 | ); |
---|
| 1331 | |
---|
| 1332 | return $form; |
---|
| 1333 | } |
---|
| 1334 | |
---|
| 1335 | /** |
---|
| 1336 | * Validate handler for the first step of the views form. |
---|
| 1337 | * Calls any existing views_form_validate functions located |
---|
| 1338 | * on the views fields. |
---|
| 1339 | */ |
---|
| 1340 | function views_form_views_form_validate($form, &$form_state) { |
---|
| 1341 | $view = $form['#parameters'][2]; |
---|
| 1342 | |
---|
| 1343 | // Call the validation method on every field handler that has it. |
---|
| 1344 | foreach ($view->field as $field_name => $field) { |
---|
| 1345 | if (method_exists($field, 'views_form_validate')) { |
---|
| 1346 | $field->views_form_validate($form, $form_state); |
---|
| 1347 | } |
---|
| 1348 | } |
---|
| 1349 | |
---|
| 1350 | // Call the validate method on every area handler that has it. |
---|
| 1351 | foreach (array('header', 'footer') as $area) { |
---|
| 1352 | foreach ($view->{$area} as $area_name => $area_handler) { |
---|
| 1353 | if (method_exists($area_handler, 'views_form_validate')) { |
---|
| 1354 | $area_handler->views_form_validate($form, $form_state); |
---|
| 1355 | } |
---|
| 1356 | } |
---|
| 1357 | } |
---|
| 1358 | } |
---|
| 1359 | |
---|
| 1360 | /** |
---|
| 1361 | * Submit handler for the first step of the views form. |
---|
| 1362 | * Calls any existing views_form_submit functions located |
---|
| 1363 | * on the views fields. |
---|
| 1364 | */ |
---|
| 1365 | function views_form_views_form_submit($form, &$form_state) { |
---|
| 1366 | $view = $form['#parameters'][2]; |
---|
| 1367 | |
---|
| 1368 | // Call the submit method on every field handler that has it. |
---|
| 1369 | foreach ($view->field as $field_name => $field) { |
---|
| 1370 | if (method_exists($field, 'views_form_submit')) { |
---|
| 1371 | $field->views_form_submit($form, $form_state); |
---|
| 1372 | } |
---|
| 1373 | } |
---|
| 1374 | |
---|
| 1375 | // Call the submit method on every area handler that has it. |
---|
| 1376 | foreach (array('header', 'footer') as $area) { |
---|
| 1377 | foreach ($view->{$area} as $area_name => $area_handler) { |
---|
| 1378 | if (method_exists($area_handler, 'views_form_submit')) { |
---|
| 1379 | $area_handler->views_form_submit($form, $form_state); |
---|
| 1380 | } |
---|
| 1381 | } |
---|
| 1382 | } |
---|
| 1383 | } |
---|
| 1384 | |
---|
| 1385 | // ------------------------------------------------------------------ |
---|
[177a560] | 1386 | // Exposed widgets form |
---|
| 1387 | |
---|
| 1388 | /** |
---|
| 1389 | * Form builder for the exposed widgets form. |
---|
| 1390 | * |
---|
| 1391 | * Be sure that $view and $display are references. |
---|
| 1392 | */ |
---|
| 1393 | function views_exposed_form(&$form_state) { |
---|
| 1394 | // Don't show the form when batch operations are in progress. |
---|
| 1395 | if ($batch = batch_get() && isset($batch['current_set'])) { |
---|
| 1396 | return array( |
---|
| 1397 | // Set the theme callback to be nothing to avoid errors in template_preprocess_views_exposed_form(). |
---|
| 1398 | '#theme' => '', |
---|
| 1399 | ); |
---|
| 1400 | } |
---|
| 1401 | |
---|
| 1402 | // Make sure that we validate because this form might be submitted |
---|
| 1403 | // multiple times per page. |
---|
| 1404 | $form_state['must_validate'] = TRUE; |
---|
| 1405 | $view = &$form_state['view']; |
---|
| 1406 | $display = &$form_state['display']; |
---|
| 1407 | |
---|
| 1408 | $form_state['input'] = $view->get_exposed_input(); |
---|
| 1409 | |
---|
| 1410 | // Let form plugins know this is for exposed widgets. |
---|
| 1411 | $form_state['exposed'] = TRUE; |
---|
| 1412 | // Check if the form was already created |
---|
| 1413 | if ($cache = views_exposed_form_cache($view->name, $view->current_display)) { |
---|
| 1414 | return $cache; |
---|
| 1415 | } |
---|
| 1416 | |
---|
| 1417 | $form['#info'] = array(); |
---|
| 1418 | |
---|
| 1419 | if (!variable_get('clean_url', FALSE)) { |
---|
| 1420 | $form['q'] = array( |
---|
| 1421 | '#type' => 'hidden', |
---|
| 1422 | '#value' => $view->get_url(), |
---|
| 1423 | ); |
---|
| 1424 | } |
---|
| 1425 | |
---|
[59029b2] | 1426 | // Go through each handler and let it generate its exposed widget. |
---|
| 1427 | foreach ($view->display_handler->handlers as $type => $value) { |
---|
| 1428 | foreach ($view->$type as $id => $handler) { |
---|
| 1429 | if ($handler->can_expose() && $handler->is_exposed()) { |
---|
| 1430 | $handler->exposed_form($form, $form_state); |
---|
| 1431 | if ($info = $handler->exposed_info()) { |
---|
| 1432 | $form['#info']["$type-$id"] = $info; |
---|
| 1433 | } |
---|
| 1434 | } |
---|
[177a560] | 1435 | } |
---|
| 1436 | } |
---|
| 1437 | |
---|
| 1438 | $form['submit'] = array( |
---|
| 1439 | '#name' => '', // prevent from showing up in $_GET. |
---|
| 1440 | '#type' => 'submit', |
---|
| 1441 | '#value' => t('Apply'), |
---|
| 1442 | '#id' => form_clean_id('edit-submit-' . $view->name), |
---|
| 1443 | ); |
---|
| 1444 | |
---|
| 1445 | $form['#action'] = url($view->get_url()); |
---|
| 1446 | $form['#theme'] = views_theme_functions('views_exposed_form', $view, $display); |
---|
| 1447 | $form['#id'] = views_css_safe('views_exposed_form-' . check_plain($view->name) . '-' . check_plain($display->id)); |
---|
| 1448 | // $form['#attributes']['class'] = array('views-exposed-form'); |
---|
| 1449 | |
---|
| 1450 | // If using AJAX, we need the form plugin. |
---|
| 1451 | if ($view->use_ajax) { |
---|
| 1452 | drupal_add_js('misc/jquery.form.js'); |
---|
| 1453 | } |
---|
| 1454 | views_add_js('dependent'); |
---|
| 1455 | |
---|
[59029b2] | 1456 | $exposed_form_plugin = $form_state['exposed_form_plugin']; |
---|
| 1457 | $exposed_form_plugin->exposed_form_alter($form, $form_state); |
---|
| 1458 | |
---|
[177a560] | 1459 | // Save the form |
---|
| 1460 | views_exposed_form_cache($view->name, $view->current_display, $form); |
---|
| 1461 | |
---|
| 1462 | return $form; |
---|
| 1463 | } |
---|
| 1464 | |
---|
| 1465 | /** |
---|
| 1466 | * Validate handler for exposed filters |
---|
| 1467 | */ |
---|
| 1468 | function views_exposed_form_validate(&$form, &$form_state) { |
---|
| 1469 | foreach (array('field', 'filter') as $type) { |
---|
| 1470 | $handlers = &$form_state['view']->$type; |
---|
| 1471 | foreach ($handlers as $key => $handler) { |
---|
| 1472 | $handlers[$key]->exposed_validate($form, $form_state); |
---|
| 1473 | } |
---|
| 1474 | } |
---|
[59029b2] | 1475 | $exposed_form_plugin = $form_state['exposed_form_plugin']; |
---|
| 1476 | $exposed_form_plugin->exposed_form_validate($form, $form_state); |
---|
[177a560] | 1477 | } |
---|
| 1478 | |
---|
| 1479 | /** |
---|
| 1480 | * Submit handler for exposed filters |
---|
| 1481 | */ |
---|
| 1482 | function views_exposed_form_submit(&$form, &$form_state) { |
---|
| 1483 | foreach (array('field', 'filter') as $type) { |
---|
| 1484 | $handlers = &$form_state['view']->$type; |
---|
| 1485 | foreach ($handlers as $key => $info) { |
---|
| 1486 | $handlers[$key]->exposed_submit($form, $form_state); |
---|
| 1487 | } |
---|
| 1488 | } |
---|
| 1489 | $form_state['view']->exposed_data = $form_state['values']; |
---|
| 1490 | $form_state['view']->exposed_raw_input = array(); |
---|
| 1491 | |
---|
[59029b2] | 1492 | $exclude = array('q', 'submit', 'form_build_id', 'form_id', 'form_token', 'exposed_form_plugin', ''); |
---|
| 1493 | $exposed_form_plugin = $form_state['exposed_form_plugin']; |
---|
| 1494 | $exposed_form_plugin->exposed_form_submit($form, $form_state, $exclude); |
---|
| 1495 | |
---|
[177a560] | 1496 | foreach ($form_state['values'] as $key => $value) { |
---|
[59029b2] | 1497 | if (!in_array($key, $exclude)) { |
---|
[177a560] | 1498 | $form_state['view']->exposed_raw_input[$key] = $value; |
---|
| 1499 | } |
---|
| 1500 | } |
---|
| 1501 | } |
---|
| 1502 | |
---|
| 1503 | /** |
---|
| 1504 | * Save the Views exposed form for later use. |
---|
| 1505 | * |
---|
| 1506 | * @param $views_name |
---|
| 1507 | * String. The views name. |
---|
| 1508 | * @param $display_name |
---|
| 1509 | * String. The current view display name. |
---|
| 1510 | * @param $form_output |
---|
| 1511 | * Array (optional). The form structure. Only needed when inserting the value. |
---|
| 1512 | * @return |
---|
| 1513 | * Array. The form structure, if any. Otherwise, return FALSE. |
---|
| 1514 | */ |
---|
| 1515 | function views_exposed_form_cache($views_name, $display_name, $form_output = NULL) { |
---|
| 1516 | static $views_exposed; |
---|
| 1517 | |
---|
| 1518 | // Save the form output |
---|
| 1519 | if (!empty($form_output)) { |
---|
| 1520 | $views_exposed[$views_name][$display_name] = $form_output; |
---|
| 1521 | return; |
---|
| 1522 | } |
---|
| 1523 | |
---|
| 1524 | // Return the form output, if any |
---|
| 1525 | return empty($views_exposed[$views_name][$display_name]) ? FALSE : $views_exposed[$views_name][$display_name]; |
---|
| 1526 | } |
---|
| 1527 | |
---|
| 1528 | // ------------------------------------------------------------------ |
---|
| 1529 | // Misc helpers |
---|
| 1530 | |
---|
| 1531 | /** |
---|
| 1532 | * Build a list of theme function names for use most everywhere. |
---|
| 1533 | */ |
---|
| 1534 | function views_theme_functions($hook, $view, $display = NULL) { |
---|
| 1535 | require_once './' . drupal_get_path('module', 'views') . "/theme/theme.inc"; |
---|
| 1536 | return _views_theme_functions($hook, $view, $display); |
---|
| 1537 | } |
---|
| 1538 | |
---|
| 1539 | /** |
---|
| 1540 | * Views' replacement for drupal_get_form so that we can do more with |
---|
| 1541 | * less. |
---|
| 1542 | * |
---|
| 1543 | * Items that can be set on the form_state include: |
---|
| 1544 | * - input: The source of input. If unset this will be $_POST. |
---|
| 1545 | * - no_redirect: Absolutely do not redirect the form even if instructed |
---|
| 1546 | * to do so. |
---|
| 1547 | * - rerender: If no_redirect is set and the form was successfully submitted, |
---|
| 1548 | * rerender the form. Otherwise it will just return. |
---|
| 1549 | * |
---|
| 1550 | */ |
---|
| 1551 | function drupal_build_form($form_id, &$form_state) { |
---|
| 1552 | views_include('form'); |
---|
| 1553 | return _drupal_build_form($form_id, $form_state); |
---|
| 1554 | } |
---|
| 1555 | |
---|
| 1556 | /** |
---|
| 1557 | * Substitute current time; this works with cached queries. |
---|
| 1558 | */ |
---|
| 1559 | function views_views_query_substitutions($view) { |
---|
| 1560 | global $language; |
---|
| 1561 | return array( |
---|
| 1562 | '***CURRENT_VERSION***' => VERSION, |
---|
| 1563 | '***CURRENT_TIME***' => time(), |
---|
| 1564 | '***CURRENT_LANGUAGE***' => $language->language, |
---|
| 1565 | '***DEFAULT_LANGUAGE***' => language_default('language'), |
---|
| 1566 | '***NO_LANGUAGE***' => '', |
---|
| 1567 | ); |
---|
| 1568 | } |
---|
| 1569 | |
---|
| 1570 | /** |
---|
| 1571 | * Embed a view using a PHP snippet. |
---|
| 1572 | * |
---|
| 1573 | * This function is meant to be called from PHP snippets, should one wish to |
---|
| 1574 | * embed a view in a node or something. It's meant to provide the simplest |
---|
| 1575 | * solution and doesn't really offer a lot of options, but breaking the function |
---|
| 1576 | * apart is pretty easy, and this provides a worthwhile guide to doing so. |
---|
| 1577 | * |
---|
| 1578 | * Note that this function does NOT display the title of the view. If you want |
---|
| 1579 | * to do that, you will need to do what this function does manually, by |
---|
| 1580 | * loading the view, getting the preview and then getting $view->get_title(). |
---|
| 1581 | * |
---|
| 1582 | * @param $name |
---|
| 1583 | * The name of the view to embed. |
---|
| 1584 | * @param $display_id |
---|
| 1585 | * The display id to embed. If unsure, use 'default', as it will always be |
---|
| 1586 | * valid. But things like 'page' or 'block' should work here. |
---|
| 1587 | * @param ... |
---|
| 1588 | * Any additional parameters will be passed as arguments. |
---|
| 1589 | */ |
---|
| 1590 | function views_embed_view($name, $display_id = 'default') { |
---|
| 1591 | $args = func_get_args(); |
---|
| 1592 | array_shift($args); // remove $name |
---|
| 1593 | if (count($args)) { |
---|
| 1594 | array_shift($args); // remove $display_id |
---|
| 1595 | } |
---|
| 1596 | |
---|
| 1597 | $view = views_get_view($name); |
---|
| 1598 | if (!$view || !$view->access($display_id)) { |
---|
| 1599 | return; |
---|
| 1600 | } |
---|
| 1601 | |
---|
| 1602 | return $view->preview($display_id, $args); |
---|
| 1603 | } |
---|
| 1604 | |
---|
| 1605 | /** |
---|
| 1606 | * Get the result of a view. |
---|
| 1607 | * |
---|
| 1608 | * @param string $name |
---|
| 1609 | * The name of the view to retrieve the data from. |
---|
| 1610 | * @param string $display_id |
---|
| 1611 | * The display id. On the edit page for the view in question, you'll find |
---|
| 1612 | * a list of displays at the left side of the control area. "Defaults" |
---|
| 1613 | * will be at the top of that list. Hover your cursor over the name of the |
---|
| 1614 | * display you want to use. An URL will appear in the status bar of your |
---|
| 1615 | * browser. This is usually at the bottom of the window, in the chrome. |
---|
| 1616 | * Everything after #views-tab- is the display ID, e.g. page_1. |
---|
| 1617 | * @param ... |
---|
| 1618 | * Any additional parameters will be passed as arguments. |
---|
| 1619 | * @return |
---|
| 1620 | * array |
---|
| 1621 | * An array containing an object for each view item. |
---|
| 1622 | */ |
---|
| 1623 | function views_get_view_result($name, $display_id = NULL) { |
---|
| 1624 | $args = func_get_args(); |
---|
| 1625 | array_shift($args); // remove $name |
---|
| 1626 | if (count($args)) { |
---|
| 1627 | array_shift($args); // remove $display_id |
---|
| 1628 | } |
---|
| 1629 | |
---|
| 1630 | $view = views_get_view($name); |
---|
| 1631 | if (is_object($view)) { |
---|
| 1632 | if (is_array($args)) { |
---|
| 1633 | $view->set_arguments($args); |
---|
| 1634 | } |
---|
| 1635 | if (is_string($display_id)) { |
---|
| 1636 | $view->set_display($display_id); |
---|
| 1637 | } |
---|
| 1638 | else { |
---|
| 1639 | $view->init_display(); |
---|
| 1640 | } |
---|
| 1641 | $view->pre_execute(); |
---|
| 1642 | $view->execute(); |
---|
| 1643 | return $view->result; |
---|
| 1644 | } |
---|
| 1645 | else { |
---|
| 1646 | return array(); |
---|
| 1647 | } |
---|
| 1648 | } |
---|
| 1649 | |
---|
| 1650 | /** |
---|
| 1651 | * Export a field. |
---|
| 1652 | */ |
---|
| 1653 | function views_var_export($var, $prefix = '', $init = TRUE) { |
---|
| 1654 | if (is_array($var)) { |
---|
| 1655 | if (empty($var)) { |
---|
| 1656 | $output = 'array()'; |
---|
| 1657 | } |
---|
| 1658 | else { |
---|
| 1659 | $output = "array(\n"; |
---|
| 1660 | foreach ($var as $key => $value) { |
---|
| 1661 | $output .= " " . views_var_export($key, '', FALSE) . " => " . views_var_export($value, ' ', FALSE) . ",\n"; |
---|
| 1662 | } |
---|
| 1663 | $output .= ')'; |
---|
| 1664 | } |
---|
| 1665 | } |
---|
| 1666 | else if (is_bool($var)) { |
---|
| 1667 | $output = $var ? 'TRUE' : 'FALSE'; |
---|
| 1668 | } |
---|
| 1669 | else if (is_string($var) && strpos($var, "\n") !== FALSE) { |
---|
| 1670 | // Replace line breaks in strings with a token for replacement |
---|
| 1671 | // at the very end. This protects multi-line strings from |
---|
| 1672 | // unintentional indentation. |
---|
| 1673 | $var = str_replace("\n", "***BREAK***", $var); |
---|
| 1674 | $output = var_export($var, TRUE); |
---|
| 1675 | } |
---|
| 1676 | else { |
---|
| 1677 | $output = var_export($var, TRUE); |
---|
| 1678 | } |
---|
| 1679 | |
---|
| 1680 | if ($prefix) { |
---|
| 1681 | $output = str_replace("\n", "\n$prefix", $output); |
---|
| 1682 | } |
---|
| 1683 | |
---|
| 1684 | if ($init) { |
---|
| 1685 | $output = str_replace("***BREAK***", "\n", $output); |
---|
| 1686 | } |
---|
| 1687 | |
---|
| 1688 | return $output; |
---|
| 1689 | } |
---|
| 1690 | |
---|
| 1691 | /** |
---|
| 1692 | * Prepare the specified string for use as a CSS identifier. |
---|
| 1693 | */ |
---|
| 1694 | function views_css_safe($string) { |
---|
| 1695 | return str_replace('_', '-', $string); |
---|
| 1696 | } |
---|
| 1697 | |
---|
| 1698 | /** |
---|
| 1699 | * Implementation of hook_views_exportables(). |
---|
| 1700 | */ |
---|
| 1701 | function views_views_exportables($op = 'list', $views = NULL, $name = 'foo') { |
---|
| 1702 | $all_views = views_get_all_views(); |
---|
| 1703 | if ($op == 'list') { |
---|
| 1704 | |
---|
| 1705 | foreach ($all_views as $name => $view) { |
---|
| 1706 | // in list, $views is a list of tags. |
---|
| 1707 | if (empty($views) || in_array($view->tag, $views)) { |
---|
| 1708 | $return[$name] = array( |
---|
| 1709 | 'name' => check_plain($name), |
---|
| 1710 | 'desc' => check_plain($view->description), |
---|
| 1711 | 'tag' => check_plain($view->tag) |
---|
| 1712 | ); |
---|
| 1713 | } |
---|
| 1714 | } |
---|
| 1715 | return $return; |
---|
| 1716 | } |
---|
| 1717 | |
---|
| 1718 | if ($op == 'export') { |
---|
| 1719 | $code = "/**\n"; |
---|
| 1720 | $code .= " * Implementation of hook_views_default_views().\n"; |
---|
| 1721 | $code .= " */\n"; |
---|
| 1722 | $code .= "function " . $name . "_views_default_views() {\n"; |
---|
| 1723 | foreach ($views as $view => $truth) { |
---|
| 1724 | $code .= " /*\n"; |
---|
| 1725 | $code .= " * View ". var_export($all_views[$view]->name, TRUE) ."\n"; |
---|
| 1726 | $code .= " */\n"; |
---|
| 1727 | $code .= $all_views[$view]->export(' '); |
---|
| 1728 | $code .= ' $views[$view->name] = $view;' . "\n\n"; |
---|
| 1729 | } |
---|
| 1730 | $code .= " return \$views;\n"; |
---|
| 1731 | $code .= "}\n"; |
---|
| 1732 | |
---|
| 1733 | return $code; |
---|
| 1734 | } |
---|
| 1735 | } |
---|
| 1736 | |
---|
| 1737 | /** |
---|
| 1738 | * Microtime helper function to return a float time value (php4 & php5 safe). |
---|
| 1739 | */ |
---|
| 1740 | function views_microtime() { |
---|
| 1741 | list($usec, $sec) = explode(' ', microtime()); |
---|
| 1742 | return (float)$sec + (float)$usec; |
---|
| 1743 | } |
---|
| 1744 | |
---|
| 1745 | /** |
---|
| 1746 | * Trim the field down to the specified length. |
---|
| 1747 | * |
---|
| 1748 | * @param $alter |
---|
| 1749 | * - max_length: Maximum lenght of the string, the rest gets truncated. |
---|
| 1750 | * - word_boundary: Trim only on a word boundary. |
---|
[59029b2] | 1751 | * - ellipsis: Show an ellipsis (...) at the end of the trimmed string. |
---|
[177a560] | 1752 | * - html: Take sure that the html is correct. |
---|
| 1753 | */ |
---|
| 1754 | function views_trim_text($alter, $value) { |
---|
| 1755 | if (drupal_strlen($value) > $alter['max_length']) { |
---|
| 1756 | $value = drupal_substr($value, 0, $alter['max_length']); |
---|
| 1757 | // TODO: replace this with cleanstring of ctools |
---|
| 1758 | if (!empty($alter['word_boundary'])) { |
---|
| 1759 | $regex = "(.*)\b.+"; |
---|
| 1760 | if (function_exists('mb_ereg')) { |
---|
| 1761 | mb_regex_encoding('UTF-8'); |
---|
| 1762 | $found = mb_ereg($regex, $value, $matches); |
---|
| 1763 | } |
---|
| 1764 | else { |
---|
| 1765 | $found = preg_match("/$regex/us", $value, $matches); |
---|
| 1766 | } |
---|
| 1767 | if ($found) { |
---|
| 1768 | $value = $matches[1]; |
---|
| 1769 | } |
---|
| 1770 | } |
---|
| 1771 | // Remove scraps of HTML entities from the end of a strings |
---|
| 1772 | $value = rtrim(preg_replace('/(?:<(?!.+>)|&(?!.+;)).*$/us', '', $value)); |
---|
| 1773 | |
---|
| 1774 | if (!empty($alter['ellipsis'])) { |
---|
| 1775 | $value .= '...'; |
---|
| 1776 | } |
---|
| 1777 | } |
---|
| 1778 | if (!empty($alter['html'])) { |
---|
| 1779 | $value = _filter_htmlcorrector($value); |
---|
| 1780 | } |
---|
| 1781 | |
---|
| 1782 | return $value; |
---|
| 1783 | } |
---|
[59029b2] | 1784 | |
---|
| 1785 | /** |
---|
| 1786 | * Adds one to each key of the array. |
---|
| 1787 | * |
---|
| 1788 | * For example array(0 => 'foo') would be array(1 => 'foo'). |
---|
| 1789 | */ |
---|
| 1790 | function views_array_key_plus($array) { |
---|
| 1791 | $keys = array_keys($array); |
---|
| 1792 | rsort($keys); |
---|
| 1793 | foreach ($keys as $key) { |
---|
| 1794 | $array[$key + 1] = $array[$key]; |
---|
| 1795 | unset($array[$key]); |
---|
| 1796 | } |
---|
| 1797 | asort($array); |
---|
| 1798 | return $array; |
---|
| 1799 | } |
---|