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