annotate modules/block/block.module @ 5:2427550111ae 6.2

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