| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: block.module,v 1.299.2.4 2009/10/06 12:13:01 goba Exp $ 3 4 /** 5 * @file 6 * Controls the boxes that are displayed around the main content. 7 */ 8 9 /** 10 * Denotes that a block is not enabled in any region and should not 11 * be shown. 12 */ 13 define('BLOCK_REGION_NONE', -1); 14 15 /** 16 * Constants defining cache granularity for blocks. 17 * 18 * Modules specify the caching patterns for their blocks using binary 19 * combinations of these constants in their hook_block(op 'list'): 20 * $block[delta]['cache'] = BLOCK_CACHE_PER_ROLE | BLOCK_CACHE_PER_PAGE; 21 * BLOCK_CACHE_PER_ROLE is used as a default when no caching pattern is 22 * specified. 23 * 24 * The block cache is cleared in cache_clear_all(), and uses the same clearing 25 * policy than page cache (node, comment, user, taxonomy added or updated...). 26 * Blocks requiring more fine-grained clearing might consider disabling the 27 * built-in block cache (BLOCK_NO_CACHE) and roll their own. 28 * 29 * Note that user 1 is excluded from block caching. 30 */ 31 32 /** 33 * The block should not get cached. This setting should be used: 34 * - for simple blocks (notably those that do not perform any db query), 35 * where querying the db cache would be more expensive than directly generating 36 * the content. 37 * - for blocks that change too frequently. 38 */ 39 define('BLOCK_NO_CACHE', -1); 40 41 /** 42 * The block can change depending on the roles the user viewing the page belongs to. 43 * This is the default setting, used when the block does not specify anything. 44 */ 45 define('BLOCK_CACHE_PER_ROLE', 0x0001); 46 47 /** 48 * The block can change depending on the user viewing the page. 49 * This setting can be resource-consuming for sites with large number of users, 50 * and thus should only be used when BLOCK_CACHE_PER_ROLE is not sufficient. 51 */ 52 define('BLOCK_CACHE_PER_USER', 0x0002); 53 54 /** 55 * The block can change depending on the page being viewed. 56 */ 57 define('BLOCK_CACHE_PER_PAGE', 0x0004); 58 59 /** 60 * The block is the same for every user on every page where it is visible. 61 */ 62 define('BLOCK_CACHE_GLOBAL', 0x0008); 63 64 /** 65 * Implementation of hook_help(). 66 */ 67 function block_help($path, $arg) { 68 switch ($path) { 69 case 'admin/help#block': 70 $output = '<p>'. t('Blocks are boxes of content rendered into an area, or region, of a web page. The default theme Garland, for example, implements the regions "left sidebar", "right sidebar", "content", "header", and "footer", and a block may appear in any one of these areas. The <a href="@blocks">blocks administration page</a> provides a drag-and-drop interface for assigning a block to a region, and for controlling the order of blocks within regions.', array('@blocks' => url('admin/build/block'))) .'</p>'; 71 $output .= '<p>'. t('Although blocks are usually generated automatically by modules (like the <em>User login</em> block, for example), administrators can also define custom blocks. Custom blocks have a title, description, and body. The body of the block can be as long as necessary, and can contain content supported by any available <a href="@input-format">input format</a>.', array('@input-format' => url('admin/settings/filters'))) .'</p>'; 72 $output .= '<p>'. t('When working with blocks, remember that:') .'</p>'; 73 $output .= '<ul><li>'. t('since not all themes implement the same regions, or display regions in the same way, blocks are positioned on a per-theme basis.') .'</li>'; 74 $output .= '<li>'. t('disabled blocks, or blocks not in a region, are never shown.') .'</li>'; 75 $output .= '<li>'. t('when throttle module is enabled, throttled blocks (blocks with the <em>Throttle</em> checkbox selected) are hidden during high server loads.') .'</li>'; 76 $output .= '<li>'. t('blocks can be configured to be visible only on certain pages.') .'</li>'; 77 $output .= '<li>'. t('blocks can be configured to be visible only when specific conditions are true.') .'</li>'; 78 $output .= '<li>'. t('blocks can be configured to be visible only for certain user roles.') .'</li>'; 79 $output .= '<li>'. t('when allowed by an administrator, specific blocks may be enabled or disabled on a per-user basis using the <em>My account</em> page.') .'</li>'; 80 $output .= '<li>'. t('some dynamic blocks, such as those generated by modules, will be displayed only on certain pages.') .'</li></ul>'; 81 $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@block">Block module</a>.', array('@block' => 'http://drupal.org/handbook/modules/block/')) .'</p>'; 82 return $output; 83 case 'admin/build/block': 84 $throttle = module_exists('throttle'); 85 $output = '<p>'. t('This page provides a drag-and-drop interface for assigning a block to a region, and for controlling the order of blocks within regions. To change the region or order of a block, grab a drag-and-drop handle under the <em>Block</em> column and drag the block to a new location in the list. (Grab a handle by clicking and holding the mouse while hovering over a handle icon.) Since not all themes implement the same regions, or display regions in the same way, blocks are positioned on a per-theme basis. Remember that your changes will not be saved until you click the <em>Save blocks</em> button at the bottom of the page.') .'</p>'; 86 if ($throttle) { 87 $output .= '<p>'. t('To reduce CPU usage, database traffic or bandwidth, blocks may be automatically disabled during high server loads by selecting their <em>Throttle</em> checkbox. Adjust throttle thresholds on the <a href="@throttleconfig">throttle configuration page</a>.', array('@throttleconfig' => url('admin/settings/throttle'))) .'</p>'; 88 } 89 $output .= '<p>'. t('Click the <em>configure</em> link next to each block to configure its specific title and visibility settings. Use the <a href="@add-block">add block page</a> to create a custom block.', array('@add-block' => url('admin/build/block/add'))) .'</p>'; 90 return $output; 91 case 'admin/build/block/add': 92 return '<p>'. t('Use this page to create a new custom block. New blocks are disabled by default, and must be moved to a region on the <a href="@blocks">blocks administration page</a> to be visible.', array('@blocks' => url('admin/build/block'))) .'</p>'; 93 } 94 } 95 96 /** 97 * Implementation of hook_theme() 98 */ 99 function block_theme() { 100 return array( 101 'block_admin_display_form' => array( 102 'template' => 'block-admin-display-form', 103 'file' => 'block.admin.inc', 104 'arguments' => array('form' => NULL), 105 ), 106 ); 107 } 108 109 /** 110 * Implementation of hook_perm(). 111 */ 112 function block_perm() { 113 return array('administer blocks', 'use PHP for block visibility'); 114 } 115 116 /** 117 * Implementation of hook_menu(). 118 */ 119 function block_menu() { 120 $items['admin/build/block'] = array( 121 'title' => 'Blocks', 122 'description' => 'Configure what block content appears in your site\'s sidebars and other regions.', 123 'page callback' => 'block_admin_display', 124 'access arguments' => array('administer blocks'), 125 'file' => 'block.admin.inc', 126 ); 127 $items['admin/build/block/list'] = array( 128 'title' => 'List', 129 'type' => MENU_DEFAULT_LOCAL_TASK, 130 'weight' => -10, 131 ); 132 $items['admin/build/block/list/js'] = array( 133 'title' => 'JavaScript List Form', 134 'page callback' => 'block_admin_display_js', 135 'access arguments' => array('administer blocks'), 136 'type' => MENU_CALLBACK, 137 'file' => 'block.admin.inc', 138 ); 139 $items['admin/build/block/configure'] = array( 140 'title' => 'Configure block', 141 'page callback' => 'drupal_get_form', 142 'page arguments' => array('block_admin_configure'), 143 'access arguments' => array('administer blocks'), 144 'type' => MENU_CALLBACK, 145 'file' => 'block.admin.inc', 146 ); 147 $items['admin/build/block/delete'] = array( 148 'title' => 'Delete block', 149 'page callback' => 'drupal_get_form', 150 'page arguments' => array('block_box_delete'), 151 'access arguments' => array('administer blocks'), 152 'type' => MENU_CALLBACK, 153 'file' => 'block.admin.inc', 154 ); 155 $items['admin/build/block/add'] = array( 156 'title' => 'Add block', 157 'page callback' => 'drupal_get_form', 158 'page arguments' => array('block_add_block_form'), 159 'access arguments' => array('administer blocks'), 160 'type' => MENU_LOCAL_TASK, 161 'file' => 'block.admin.inc', 162 ); 163 $default = variable_get('theme_default', 'garland'); 164 foreach (list_themes() as $key => $theme) { 165 $items['admin/build/block/list/'. $key] = array( 166 'title' => check_plain($theme->info['name']), 167 'page arguments' => array($key), 168 'type' => $key == $default ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK, 169 'weight' => $key == $default ? -10 : 0, 170 'file' => 'block.admin.inc', 171 'access callback' => '_block_themes_access', 172 'access arguments' => array($theme), 173 ); 174 } 175 return $items; 176 } 177 178 /** 179 * Menu item access callback - only admin or enabled themes can be accessed 180 */ 181 function _block_themes_access($theme) { 182 return user_access('administer blocks') && ($theme->status || $theme->name == variable_get('admin_theme', '0')); 183 } 184 185 /** 186 * Implementation of hook_block(). 187 * 188 * Generates the administrator-defined blocks for display. 189 */ 190 function block_block($op = 'list', $delta = 0, $edit = array()) { 191 switch ($op) { 192 case 'list': 193 $blocks = array(); 194 195 $result = db_query('SELECT bid, info FROM {boxes} ORDER BY info'); 196 while ($block = db_fetch_object($result)) { 197 $blocks[$block->bid]['info'] = $block->info; 198 // Not worth caching. 199 $blocks[$block->bid]['cache'] = BLOCK_NO_CACHE; 200 } 201 return $blocks; 202 203 case 'configure': 204 $box = array('format' => FILTER_FORMAT_DEFAULT); 205 if ($delta) { 206 $box = block_box_get($delta); 207 } 208 if (filter_access($box['format'])) { 209 return block_box_form($box); 210 } 211 break; 212 213 case 'save': 214 block_box_save($edit, $delta); 215 break; 216 217 case 'view': 218 $block = db_fetch_object(db_query('SELECT body, format FROM {boxes} WHERE bid = %d', $delta)); 219 $data['content'] = check_markup($block->body, $block->format, FALSE); 220 return $data; 221 } 222 } 223 224 /** 225 * Update the 'blocks' DB table with the blocks currently exported by modules. 226 * 227 * @return 228 * Blocks currently exported by modules. 229 */ 230 function _block_rehash() { 231 global $theme_key; 232 233 init_theme(); 234 235 $result = db_query("SELECT * FROM {blocks} WHERE theme = '%s'", $theme_key); 236 $old_blocks = array(); 237 while ($old_block = db_fetch_array($result)) { 238 $old_blocks[$old_block['module']][$old_block['delta']] = $old_block; 239 } 240 241 $blocks = array(); 242 // Valid region names for the theme. 243 $regions = system_region_list($theme_key); 244 245 foreach (module_list() as $module) { 246 $module_blocks = module_invoke($module, 'block', 'list'); 247 if ($module_blocks) { 248 foreach ($module_blocks as $delta => $block) { 249 if (empty($old_blocks[$module][$delta])) { 250 // If it's a new block, add identifiers. 251 $block['module'] = $module; 252 $block['delta'] = $delta; 253 $block['theme'] = $theme_key; 254 if (!isset($block['pages'])) { 255 // {block}.pages is type 'text', so it cannot have a 256 // default value, and not null, so we need to provide 257 // value if the module did not. 258 $block['pages'] = ''; 259 } 260 // Add defaults and save it into the database. 261 drupal_write_record('blocks', $block); 262 // Set region to none if not enabled. 263 $block['region'] = $block['status'] ? $block['region'] : BLOCK_REGION_NONE; 264 // Add to the list of blocks we return. 265 $blocks[] = $block; 266 } 267 else { 268 // If it's an existing block, database settings should overwrite 269 // the code. But aside from 'info' everything that's definable in 270 // code is stored in the database and we do not store 'info', so we 271 // do not need to update the database here. 272 // Add 'info' to this block. 273 $old_blocks[$module][$delta]['info'] = $block['info']; 274 // If the region name does not exist, disable the block and assign it to none. 275 if (!empty($old_blocks[$module][$delta]['region']) && !isset($regions[$old_blocks[$module][$delta]['region']])) { 276 drupal_set_message(t('The block %info was assigned to the invalid region %region and has been disabled.', array('%info' => $old_blocks[$module][$delta]['info'], '%region' => $old_blocks[$module][$delta]['region'])), 'warning'); 277 $old_blocks[$module][$delta]['status'] = 0; 278 $old_blocks[$module][$delta]['region'] = BLOCK_REGION_NONE; 279 } 280 else { 281 $old_blocks[$module][$delta]['region'] = $old_blocks[$module][$delta]['status'] ? $old_blocks[$module][$delta]['region'] : BLOCK_REGION_NONE; 282 } 283 // Add this block to the list of blocks we return. 284 $blocks[] = $old_blocks[$module][$delta]; 285 // Remove this block from the list of blocks to be deleted. 286 unset($old_blocks[$module][$delta]); 287 } 288 } 289 } 290 } 291 292 // Remove blocks that are no longer defined by the code from the database. 293 foreach ($old_blocks as $module => $old_module_blocks) { 294 foreach ($old_module_blocks as $delta => $block) { 295 db_query("DELETE FROM {blocks} WHERE module = '%s' AND delta = '%s' AND theme = '%s'", $module, $delta, $theme_key); 296 } 297 } 298 return $blocks; 299 } 300 301 /** 302 * Returns information from database about a user-created (custom) block. 303 * 304 * @param $bid 305 * ID of the block to get information for. 306 * @return 307 * Associative array of information stored in the database for this block. 308 * Array keys: 309 * - bid: Block ID. 310 * - info: Block description. 311 * - body: Block contents. 312 * - format: Filter ID of the filter format for the body. 313 */ 314 function block_box_get($bid) { 315 return db_fetch_array(db_query("SELECT * FROM {boxes} WHERE bid = %d", $bid)); 316 } 317 318 /** 319 * Define the custom block form. 320 */ 321 function block_box_form($edit = array()) { 322 $edit += array( 323 'info' => '', 324 'body' => '', 325 ); 326 $form['info'] = array( 327 '#type' => 'textfield', 328 '#title' => t('Block description'), 329 '#default_value' => $edit['info'], 330 '#maxlength' => 64, 331 '#description' => t('A brief description of your block. Used on the <a href="@overview">block overview page</a>.', array('@overview' => url('admin/build/block'))), 332 '#required' => TRUE, 333 '#weight' => -19, 334 ); 335 $form['body_field']['#weight'] = -17; 336 $form['body_field']['body'] = array( 337 '#type' => 'textarea', 338 '#title' => t('Block body'), 339 '#default_value' => $edit['body'], 340 '#rows' => 15, 341 '#description' => t('The content of the block as shown to the user.'), 342 '#weight' => -17, 343 ); 344 if (!isset($edit['format'])) { 345 $edit['format'] = FILTER_FORMAT_DEFAULT; 346 } 347 $form['body_field']['format'] = filter_form($edit['format'], -16); 348 349 return $form; 350 } 351 352 /** 353 * Saves a user-created block in the database. 354 * 355 * @param $edit 356 * Associative array of fields to save. Array keys: 357 * - info: Block description. 358 * - body: Block contents. 359 * - format: Filter ID of the filter format for the body. 360 * @param $delta 361 * Block ID of the block to save. 362 * @return 363 * Always returns TRUE. 364 */ 365 function block_box_save($edit, $delta) { 366 if (!filter_access($edit['format'])) { 367 $edit['format'] = FILTER_FORMAT_DEFAULT; 368 } 369 370 db_query("UPDATE {boxes} SET body = '%s', info = '%s', format = %d WHERE bid = %d", $edit['body'], $edit['info'], $edit['format'], $delta); 371 372 return TRUE; 373 } 374 375 /** 376 * Implementation of hook_user(). 377 * 378 * Allow users to decide which custom blocks to display when they visit 379 * the site. 380 */ 381 function block_user($type, $edit, &$account, $category = NULL) { 382 switch ($type) { 383 case 'form': 384 if ($category == 'account') { 385 $rids = array_keys($account->roles); 386 $result = db_query("SELECT DISTINCT b.* FROM {blocks} b LEFT JOIN {blocks_roles} r ON b.module = r.module AND b.delta = r.delta WHERE b.status = 1 AND b.custom != 0 AND (r.rid IN (". db_placeholders($rids) .") OR r.rid IS NULL) ORDER BY b.weight, b.module", $rids); 387 $form['block'] = array('#type' => 'fieldset', '#title' => t('Block configuration'), '#weight' => 3, '#collapsible' => TRUE, '#tree' => TRUE); 388 while ($block = db_fetch_object($result)) { 389 $data = module_invoke($block->module, 'block', 'list'); 390 if ($data[$block->delta]['info']) { 391 $return = TRUE; 392 $form['block'][$block->module][$block->delta] = array('#type' => 'checkbox', '#title' => check_plain($data[$block->delta]['info']), '#default_value' => isset($account->block[$block->module][$block->delta]) ? $account->block[$block->module][$block->delta] : ($block->custom == 1)); 393 } 394 } 395 396 if (!empty($return)) { 397 return $form; 398 } 399 } 400 401 break; 402 case 'validate': 403 if (empty($edit['block'])) { 404 $edit['block'] = array(); 405 } 406 return $edit; 407 } 408 } 409 410 /** 411 * Return all blocks in the specified region for the current user. 412 * 413 * @param $region 414 * The name of a region. 415 * 416 * @return 417 * An array of block objects, indexed with <i>module</i>_<i>delta</i>. 418 * If you are displaying your blocks in one or two sidebars, you may check 419 * whether this array is empty to see how many columns are going to be 420 * displayed. 421 * 422 * @todo 423 * Now that the blocks table has a primary key, we should use that as the 424 * array key instead of <i>module</i>_<i>delta</i>. 425 */ 426 function block_list($region) { 427 global $user, $theme_key; 428 429 static $blocks = array(); 430 431 if (!count($blocks)) { 432 $rids = array_keys($user->roles); 433 $result = db_query(db_rewrite_sql("SELECT DISTINCT b.* FROM {blocks} b LEFT JOIN {blocks_roles} r ON b.module = r.module AND b.delta = r.delta WHERE b.theme = '%s' AND b.status = 1 AND (r.rid IN (". db_placeholders($rids) .") OR r.rid IS NULL) ORDER BY b.region, b.weight, b.module", 'b', 'bid'), array_merge(array($theme_key), $rids)); 434 while ($block = db_fetch_object($result)) { 435 if (!isset($blocks[$block->region])) { 436 $blocks[$block->region] = array(); 437 } 438 // Use the user's block visibility setting, if necessary 439 if ($block->custom != 0) { 440 if ($user->uid && isset($user->block[$block->module][$block->delta])) { 441 $enabled = $user->block[$block->module][$block->delta]; 442 } 443 else { 444 $enabled = ($block->custom == 1); 445 } 446 } 447 else { 448 $enabled = TRUE; 449 } 450 451 // Match path if necessary 452 if ($block->pages) { 453 if ($block->visibility < 2) { 454 $path = drupal_get_path_alias($_GET['q']); 455 // Compare with the internal and path alias (if any). 456 $page_match = drupal_match_path($path, $block->pages); 457 if ($path != $_GET['q']) { 458 $page_match = $page_match || drupal_match_path($_GET['q'], $block->pages); 459 } 460 // When $block->visibility has a value of 0, the block is displayed on 461 // all pages except those listed in $block->pages. When set to 1, it 462 // is displayed only on those pages listed in $block->pages. 463 $page_match = !($block->visibility xor $page_match); 464 } 465 else { 466 $page_match = drupal_eval($block->pages); 467 } 468 } 469 else { 470 $page_match = TRUE; 471 } 472 $block->enabled = $enabled; 473 $block->page_match = $page_match; 474 $blocks[$block->region]["{$block->module}_{$block->delta}"] = $block; 475 } 476 } 477 478 // Create an empty array if there were no entries 479 if (!isset($blocks[$region])) { 480 $blocks[$region] = array(); 481 } 482 483 foreach ($blocks[$region] as $key => $block) { 484 // Render the block content if it has not been created already. 485 if (!isset($block->content)) { 486 // Erase the block from the static array - we'll put it back if it has content. 487 unset($blocks[$region][$key]); 488 if ($block->enabled && $block->page_match) { 489 // Check the current throttle status and see if block should be displayed 490 // based on server load. 491 if (!($block->throttle && (module_invoke('throttle', 'status') > 0))) { 492 // Try fetching the block from cache. Block caching is not compatible with 493 // node_access modules. We also preserve the submission of forms in blocks, 494 // by fetching from cache only if the request method is 'GET'. 495 if (!count(module_implements('node_grants')) && $_SERVER['REQUEST_METHOD'] == 'GET' && ($cid = _block_get_cache_id($block)) && ($cache = cache_get($cid, 'cache_block'))) { 496 $array = $cache->data; 497 } 498 else { 499 $array = module_invoke($block->module, 'block', 'view', $block->delta); 500 if (isset($cid)) { 501 cache_set($cid, $array, 'cache_block', CACHE_TEMPORARY); 502 } 503 } 504 505 if (isset($array) && is_array($array)) { 506 foreach ($array as $k => $v) { 507 $block->$k = $v; 508 } 509 } 510 } 511 if (isset($block->content) && $block->content) { 512 // Override default block title if a custom display title is present. 513 if ($block->title) { 514 // Check plain here to allow module generated titles to keep any markup. 515 $block->subject = $block->title == '<none>' ? '' : check_plain($block->title); 516 } 517 if (!isset($block->subject)) { 518 $block->subject = ''; 519 } 520 $blocks[$block->region]["{$block->module}_{$block->delta}"] = $block; 521 } 522 } 523 } 524 } 525 return $blocks[$region]; 526 } 527 528 /** 529 * Assemble the cache_id to use for a given block. 530 * 531 * The cache_id string reflects the viewing context for the current block 532 * instance, obtained by concatenating the relevant context information 533 * (user, page, ...) according to the block's cache settings (BLOCK_CACHE_* 534 * constants). Two block instances can use the same cached content when 535 * they share the same cache_id. 536 * 537 * Theme and language contexts are automatically differenciated. 538 * 539 * @param $block 540 * @return 541 * The string used as cache_id for the block. 542 */ 543 function _block_get_cache_id($block) { 544 global $theme, $base_root, $user; 545 546 // User 1 being out of the regular 'roles define permissions' schema, 547 // it brings too many chances of having unwanted output get in the cache 548 // and later be served to other users. We therefore exclude user 1 from 549 // block caching. 550 if (variable_get('block_cache', 0) && $block->cache != BLOCK_NO_CACHE && $user->uid != 1) { 551 $cid_parts = array(); 552 553 // Start with common sub-patterns: block identification, theme, language. 554 $cid_parts[] = $block->module; 555 $cid_parts[] = $block->delta; 556 $cid_parts[] = $theme; 557 if (module_exists('locale')) { 558 global $language; 559 $cid_parts[] = $language->language; 560 } 561 562 // 'PER_ROLE' and 'PER_USER' are mutually exclusive. 'PER_USER' can be a 563 // resource drag for sites with many users, so when a module is being 564 // equivocal, we favor the less expensive 'PER_ROLE' pattern. 565 if ($block->cache & BLOCK_CACHE_PER_ROLE) { 566 $cid_parts[] = 'r.'. implode(',', array_keys($user->roles)); 567 } 568 elseif ($block->cache & BLOCK_CACHE_PER_USER) { 569 $cid_parts[] = "u.$user->uid"; 570 } 571 572 if ($block->cache & BLOCK_CACHE_PER_PAGE) { 573 $cid_parts[] = $base_root . request_uri(); 574 } 575 576 return implode(':', $cid_parts); 577 } 578 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |