| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: block_with_node_grants.module,v 1.1.2.1 2009/06/02 18:35:58 swentel 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 function block_box_get($bid) { 302 return db_fetch_array(db_query("SELECT * FROM {boxes} WHERE bid = %d", $bid)); 303 } 304 305 /** 306 * Define the custom block form. 307 */ 308 function block_box_form($edit = array()) { 309 $edit += array( 310 'info' => '', 311 'body' => '', 312 ); 313 $form['info'] = array( 314 '#type' => 'textfield', 315 '#title' => t('Block description'), 316 '#default_value' => $edit['info'], 317 '#maxlength' => 64, 318 '#description' => t('A brief description of your block. Used on the <a href="@overview">block overview page</a>.', array('@overview' => url('admin/build/block'))), 319 '#required' => TRUE, 320 '#weight' => -19, 321 ); 322 $form['body_field']['#weight'] = -17; 323 $form['body_field']['body'] = array( 324 '#type' => 'textarea', 325 '#title' => t('Block body'), 326 '#default_value' => $edit['body'], 327 '#rows' => 15, 328 '#description' => t('The content of the block as shown to the user.'), 329 '#weight' => -17, 330 ); 331 if (!isset($edit['format'])) { 332 $edit['format'] = FILTER_FORMAT_DEFAULT; 333 } 334 $form['body_field']['format'] = filter_form($edit['format'], -16); 335 336 return $form; 337 } 338 339 function block_box_save($edit, $delta) { 340 if (!filter_access($edit['format'])) { 341 $edit['format'] = FILTER_FORMAT_DEFAULT; 342 } 343 344 db_query("UPDATE {boxes} SET body = '%s', info = '%s', format = %d WHERE bid = %d", $edit['body'], $edit['info'], $edit['format'], $delta); 345 346 return TRUE; 347 } 348 349 /** 350 * Implementation of hook_user(). 351 * 352 * Allow users to decide which custom blocks to display when they visit 353 * the site. 354 */ 355 function block_user($type, $edit, &$account, $category = NULL) { 356 switch ($type) { 357 case 'form': 358 if ($category == 'account') { 359 $rids = array_keys($account->roles); 360 $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); 361 $form['block'] = array('#type' => 'fieldset', '#title' => t('Block configuration'), '#weight' => 3, '#collapsible' => TRUE, '#tree' => TRUE); 362 while ($block = db_fetch_object($result)) { 363 $data = module_invoke($block->module, 'block', 'list'); 364 if ($data[$block->delta]['info']) { 365 $return = TRUE; 366 $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)); 367 } 368 } 369 370 if (!empty($return)) { 371 return $form; 372 } 373 } 374 375 break; 376 case 'validate': 377 if (empty($edit['block'])) { 378 $edit['block'] = array(); 379 } 380 return $edit; 381 } 382 } 383 384 /** 385 * Return all blocks in the specified region for the current user. 386 * 387 * @param $region 388 * The name of a region. 389 * 390 * @return 391 * An array of block objects, indexed with <i>module</i>_<i>delta</i>. 392 * If you are displaying your blocks in one or two sidebars, you may check 393 * whether this array is empty to see how many columns are going to be 394 * displayed. 395 * 396 * @todo 397 * Now that the blocks table has a primary key, we should use that as the 398 * array key instead of <i>module</i>_<i>delta</i>. 399 */ 400 function block_list($region) { 401 global $user, $theme_key; 402 403 static $blocks = array(); 404 405 if (!count($blocks)) { 406 $rids = array_keys($user->roles); 407 $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)); 408 while ($block = db_fetch_object($result)) { 409 if (!isset($blocks[$block->region])) { 410 $blocks[$block->region] = array(); 411 } 412 // Use the user's block visibility setting, if necessary 413 if ($block->custom != 0) { 414 if ($user->uid && isset($user->block[$block->module][$block->delta])) { 415 $enabled = $user->block[$block->module][$block->delta]; 416 } 417 else { 418 $enabled = ($block->custom == 1); 419 } 420 } 421 else { 422 $enabled = TRUE; 423 } 424 425 // Match path if necessary 426 if ($block->pages) { 427 if ($block->visibility < 2) { 428 $path = drupal_get_path_alias($_GET['q']); 429 // Compare with the internal and path alias (if any). 430 $page_match = drupal_match_path($path, $block->pages); 431 if ($path != $_GET['q']) { 432 $page_match = $page_match || drupal_match_path($_GET['q'], $block->pages); 433 } 434 // When $block->visibility has a value of 0, the block is displayed on 435 // all pages except those listed in $block->pages. When set to 1, it 436 // is displayed only on those pages listed in $block->pages. 437 $page_match = !($block->visibility xor $page_match); 438 } 439 else { 440 $page_match = drupal_eval($block->pages); 441 } 442 } 443 else { 444 $page_match = TRUE; 445 } 446 $block->enabled = $enabled; 447 $block->page_match = $page_match; 448 $blocks[$block->region]["{$block->module}_{$block->delta}"] = $block; 449 } 450 } 451 452 // Create an empty array if there were no entries 453 if (!isset($blocks[$region])) { 454 $blocks[$region] = array(); 455 } 456 457 $time = time(); 458 foreach ($blocks[$region] as $key => $block) { 459 // Render the block content if it has not been created already. 460 if (!isset($block->content)) { 461 // Erase the block from the static array - we'll put it back if it has content. 462 unset($blocks[$region][$key]); 463 if ($block->enabled && $block->page_match) { 464 // Check the current throttle status and see if block should be displayed 465 // based on server load. 466 if (!($block->throttle && (module_invoke('throttle', 'status') > 0))) { 467 // Try fetching the block from cache. Block caching is not compatible with 468 // node_access modules. We also preserve the submission of forms in blocks, 469 // by fetching from cache only if the request method is 'GET'. 470 if (!count(module_implements('node_grants')) && $_SERVER['REQUEST_METHOD'] == 'GET' && 471 ($cid = _block_get_cache_id($block)) && ($cache = cache_get($cid, 'cache_block')) && 472 (_blockcache_alter_check_expire($cache, $time))) { 473 $array = $cache->data; 474 } 475 else { 476 $array = module_invoke($block->module, 'block', 'view', $block->delta); 477 if (isset($cid)) { 478 $blocklife = variable_get('bc_life_' . $block->module .'_' . $block->delta, ''); 479 $blocklife = (int)$blocklife; 480 if (!empty($blocklife)) 481 cache_set($cid, $array, 'cache_block', $blocklife + time()); 482 else 483 cache_set($cid, $array, 'cache_block', CACHE_PERMANENT); 484 if (variable_get('bca_debug', FALSE) && user_access('administer site configuration')) { 485 drupal_set_message('Block re-cached: ' . $block->title . '_' .$block->module . '_' . $block->delta . '_' . $blocklife . '_' . time()); 486 } 487 } 488 } 489 490 if (isset($array) && is_array($array)) { 491 foreach ($array as $k => $v) { 492 $block->$k = $v; 493 } 494 } 495 } 496 if (isset($block->content) && $block->content) { 497 // Override default block title if a custom display title is present. 498 if ($block->title) { 499 // Check plain here to allow module generated titles to keep any markup. 500 $block->subject = $block->title == '<none>' ? '' : check_plain($block->title); 501 } 502 if (!isset($block->subject)) { 503 $block->subject = ''; 504 } 505 $blocks[$block->region]["{$block->module}_{$block->delta}"] = $block; 506 } 507 } 508 } 509 } 510 return $blocks[$region]; 511 } 512 513 /** 514 * Assemble the cache_id to use for a given block. 515 * 516 * The cache_id string reflects the viewing context for the current block 517 * instance, obtained by concatenating the relevant context information 518 * (user, page, ...) according to the block's cache settings (BLOCK_CACHE_* 519 * constants). Two block instances can use the same cached content when 520 * they share the same cache_id. 521 * 522 * Theme and language contexts are automatically differenciated. 523 * 524 * @param $block 525 * @return 526 * The string used as cache_id for the block. 527 */ 528 function _block_get_cache_id($block) { 529 global $theme, $base_root, $user; 530 531 // User 1 being out of the regular 'roles define permissions' schema, 532 // it brings too many chances of having unwanted output get in the cache 533 // and later be served to other users. We therefore exclude user 1 from 534 // block caching. 535 if (variable_get('block_cache', 0) && $block->cache != BLOCK_NO_CACHE && $user->uid != 1) { 536 $cid_parts = array(); 537 538 // Start with common sub-patterns: block identification, theme, language. 539 $cid_parts[] = $block->module; 540 $cid_parts[] = $block->delta; 541 $cid_parts[] = $theme; 542 if (module_exists('locale')) { 543 global $language; 544 $cid_parts[] = $language->language; 545 } 546 547 // 'PER_ROLE' and 'PER_USER' are mutually exclusive. 'PER_USER' can be a 548 // resource drag for sites with many users, so when a module is being 549 // equivocal, we favor the less expensive 'PER_ROLE' pattern. 550 if ($block->cache & BLOCK_CACHE_PER_ROLE) { 551 $cid_parts[] = 'r.'. implode(',', array_keys($user->roles)); 552 } 553 elseif ($block->cache & BLOCK_CACHE_PER_USER) { 554 $cid_parts[] = "u.$user->uid"; 555 } 556 557 if ($block->cache & BLOCK_CACHE_PER_PAGE) { 558 $cid_parts[] = $base_root . request_uri(); 559 } 560 561 return implode(':', $cid_parts); 562 } 563 }
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 |