/[drupal]/drupal/modules/block.module
ViewVC logotype

Contents of /drupal/modules/block.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.206.2.8 - (show annotations) (download) (as text)
Sun Mar 25 21:07:19 2007 UTC (2 years, 8 months ago) by killes
Branch: DRUPAL-4-7
Changes since 1.206.2.7: +3 -2 lines
File MIME type: text/x-php
#80963 by Dries et al: fixed race condition in _block_rehash().
1 <?php
2 // $Id: block.module,v 1.206.2.7 2006/11/14 10:46:07 killes Exp $
3
4 /**
5 * @file
6 * Controls the boxes that are displayed around the main content.
7 */
8
9 /**
10 * Implementation of hook_help().
11 */
12 function block_help($section) {
13 switch ($section) {
14 case 'admin/help#block':
15 $output = '<p>'. t('Blocks are boxes of content that may be rendered into certain regions of your web pages, for example, into sidebars. Blocks are usually generated automatically by modules (e.g., Recent Forum Topics), but administrators can also define custom blocks.') .'</p>';
16 $output .= '<p>'. t('The region each block appears in depends on both which theme you are using (some themes allow greater control over block placement than others), and on the settings in the block administration section.') .'</p>';
17 $output .= '<p>'. t('The block administration screen lets you specify the vertical placement of the blocks within a region. You do this by assigning a weight to each block. Lighter blocks (those having a smaller weight) "float up" towards the top of the region; heavier ones "sink".') .'</p>';
18 $output .= t('<p>A block\'s visibility depends on:</p>
19 <ul>
20 <li>Its enabled checkbox. Disabled blocks are never shown.</li>
21 <li>Its throttle checkbox. Throttled blocks are hidden during high server loads.</li>
22 <li>Its page visibility settings. Blocks can be configured to be visible/hidden on certain pages.</li>
23 <li>Its custom visibility settings. Blocks can be configured to be visible only when specific conditions are true.</li>
24 <li>Its user visibility settings. Administrators can choose to let users decide whether to show/hide certain blocks.</li>
25 <li>Its function. Some dynamic blocks, such as those generated by modules, will be displayed only on certain pages.</li>
26 </ul>
27 ');
28 $output .= '<h3>'. t('Module blocks') .'</h3>';
29 $output .= '<p>'. t('Some modules generate blocks that become available when the modules are enabled. These blocks can be administered via the <a href="%admin-block">blocks administration page</a>.</p>', array('%admin-block' => url('admin/block'))) .'</p>';
30 $output .= '<h3>'. t('Administrator defined blocks') .'</h3>';
31 $output .= '<p>'. t('Administrators can also define custom blocks. These blocks consist of a title, a description, and a body which can be as long as you wish. Block content can be in any of the input formats supported for other content.') .'</p>';
32 $output .= t('<p>You can</p>
33 <ul>
34 <li>enable, throttle and configure blocks at <a href="%admin-block">administer &gt;&gt; blocks</a>.</li>
35 <li>add an administrator-defined block at <a href="%admin-block-add">administer &gt;&gt; blocks &gt;&gt; add block</a>.</li>
36 </ul>
37 ', array('%admin-block' => url('admin/block'), '%admin-block-add' => url('admin/block/add')));
38 $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="%block">Block page</a>.', array('%block' => 'http://drupal.org/handbook/modules/block/')) .'</p>';
39 return $output;
40 case 'admin/modules#description':
41 return t('Controls the boxes that are displayed around the main content.');
42 case 'admin/block':
43 return t("
44 <p>Blocks are boxes of content that may be rendered into certain regions of your web pages, for example, into sidebars. They are usually generated automatically by modules, but administrators can create blocks manually.</p>
45 <p>Only enabled blocks are shown. You can position blocks by specifying which area of the page they should appear in (e.g., a sidebar). Highlighted labels on this page show the regions into which blocks can be rendered. You can specify where within a region a block will appear by adjusting its weight.</p>
46 <p>If you want certain blocks to disable themselves temporarily during high server loads, check the 'Throttle' box. You can configure the auto-throttle on the <a href=\"%throttle\">throttle configuration page</a> after having enabled the throttle module.</p>
47 <p>You can configure the behaviour of each block (for example, specifying on which pages and for what users it will appear) by clicking the 'configure' link for each block.</p>
48 ", array('%throttle' => url('admin/settings/throttle')));
49 case 'admin/block/add':
50 return t('<p>Here you can create a new block. Once you have created this block you must make it active and give it a place on the page using <a href="%overview">blocks</a>. The title is used when displaying the block. The description is used in the "block" column on the <a href="%overview">blocks</a> page.</p>', array('%overview' => url('admin/block')));
51 }
52 }
53
54 /**
55 * Implementation of hook_perm().
56 */
57 function block_perm() {
58 return array('administer blocks', 'use PHP for block visibility');
59 }
60
61 /**
62 * Implementation of hook_menu().
63 */
64 function block_menu($may_cache) {
65 $items = array();
66
67 if ($may_cache) {
68 $items[] = array('path' => 'admin/block', 'title' => t('blocks'),
69 'access' => user_access('administer blocks'),
70 'callback' => 'block_admin_display');
71 $items[] = array('path' => 'admin/block/list', 'title' => t('list'),
72 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10);
73 $items[] = array('path' => 'admin/block/configure', 'title' => t('configure block'),
74 'access' => user_access('administer blocks'),
75 'callback' => 'block_admin_configure',
76 'type' => MENU_CALLBACK);
77 $items[] = array('path' => 'admin/block/delete', 'title' => t('delete block'),
78 'access' => user_access('administer blocks'),
79 'callback' => 'block_box_delete',
80 'type' => MENU_CALLBACK);
81 $items[] = array('path' => 'admin/block/add', 'title' => t('add block'),
82 'access' => user_access('administer blocks'),
83 'callback' => 'block_box_add',
84 'type' => MENU_LOCAL_TASK);
85 foreach (list_themes() as $key => $theme) {
86 if ($theme->status) {
87 if ($key == variable_get('theme_default', 'bluemarine')) {
88 $items[] = array('path' => 'admin/block/list/' . $key, 'title' => t('%key settings', array('%key' => $key)),
89 'access' => user_access('administer blocks'), 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10);
90 }
91 else {
92 $items[] = array('path' => 'admin/block/list/' . $key, 'title' => t('%key settings', array('%key' => $key)),
93 'access' => user_access('administer blocks'), 'type' => MENU_LOCAL_TASK);
94 }
95 }
96 }
97 }
98
99 return $items;
100 }
101
102 /**
103 * Implementation of hook_block().
104 *
105 * Generates the administrator-defined blocks for display.
106 */
107 function block_block($op = 'list', $delta = 0, $edit = array()) {
108 switch ($op) {
109 case 'list':
110 $blocks = array();
111
112 $result = db_query('SELECT bid, title, info FROM {boxes} ORDER BY title');
113 while ($block = db_fetch_object($result)) {
114 $blocks[$block->bid]['info'] = $block->info ? check_plain($block->info) : check_plain($block->title);
115 }
116 return $blocks;
117
118 case 'configure':
119 $box = block_box_get($delta);
120 if (filter_access($box['format'])) {
121 return block_box_form($box);
122 }
123 break;
124
125 case 'save':
126 block_box_save($edit, $delta);
127 break;
128
129 case 'view':
130 $block = db_fetch_object(db_query('SELECT * FROM {boxes} WHERE bid = %d', $delta));
131 $data['subject'] = check_plain($block->title);
132 $data['content'] = check_markup($block->body, $block->format, FALSE);
133 return $data;
134 }
135 }
136
137 /**
138 * Update the 'blocks' DB table with the blocks currently exported by modules.
139 *
140 * @return
141 * Blocks currently exported by modules.
142 */
143 function _block_rehash() {
144 global $theme_key;
145
146 init_theme();
147
148 db_lock_table('blocks');
149
150 $result = db_query("SELECT * FROM {blocks} WHERE theme = '%s'", $theme_key);
151 while ($old_block = db_fetch_object($result)) {
152 $old_blocks[$old_block->module][$old_block->delta] = $old_block;
153 }
154
155 $blocks = array();
156
157 foreach (module_list() as $module) {
158 $module_blocks = module_invoke($module, 'block', 'list');
159 if ($module_blocks) {
160 foreach ($module_blocks as $delta => $block) {
161 $block['module'] = $module;
162 $block['delta'] = $delta;
163 // If previously written to database, load values.
164 if ($old_blocks[$module][$delta]) {
165 $block['status'] = $old_blocks[$module][$delta]->status;
166 $block['weight'] = $old_blocks[$module][$delta]->weight;
167 $block['region'] = $old_blocks[$module][$delta]->region;
168 $block['visibility'] = $old_blocks[$module][$delta]->visibility;
169 $block['pages'] = $old_blocks[$module][$delta]->pages;
170 $block['custom'] = $old_blocks[$module][$delta]->custom;
171 $block['throttle'] = $old_blocks[$module][$delta]->throttle;
172 }
173 // Otherwise, use any set values, or else substitute defaults.
174 else {
175 $properties = array('status' => 0, 'weight' => 0, 'region' => 'left', 'pages' => '', 'custom' => 0);
176 foreach ($properties as $property => $default) {
177 if (!isset($block[$property])) {
178 $block[$property] = $default;
179 }
180 }
181 }
182
183 $blocks[] = $block;
184 }
185 }
186 }
187
188 // Remove all blocks from table.
189 db_query("DELETE FROM {blocks} WHERE theme = '%s'", $theme_key);
190
191 // Reinsert new set of blocks into table.
192 foreach ($blocks as $block) {
193 db_query("INSERT INTO {blocks} (module, delta, theme, status, weight, region, visibility, pages, custom, throttle) VALUES ('%s', '%s', '%s', %d, %d, '%s', %d, '%s', %d, %d)", $block['module'], $block['delta'], $theme_key, $block['status'], $block['weight'], $block['region'], $block['visibility'], $block['pages'], $block['custom'], $block['throttle']);
194 }
195 db_unlock_tables();
196
197 return $blocks;
198 }
199
200 /**
201 * Generate main block administration form.
202 */
203 function block_admin_display() {
204 global $theme_key, $custom_theme;
205
206 // If non-default theme configuration has been selected, set the custom theme.
207 if (arg(3)) {
208 $custom_theme = arg(3);
209 }
210 else {
211 $custom_theme = variable_get('theme_default', 'bluemarine');
212 }
213 init_theme();
214
215 // Fetch and sort blocks
216 $blocks = _block_rehash();
217 usort($blocks, '_block_compare');
218
219 $throttle = module_exist('throttle');
220 $block_regions = system_region_list($theme_key);
221
222 // Build form tree
223 $form['#action'] = arg(3) ? url('admin/block/list/' . $theme_key) : url('admin/block');
224 $form['#tree'] = TRUE;
225 foreach ($blocks as $i => $block) {
226 $form[$i]['module'] = array('#type' => 'value', '#value' => $block['module']);
227 $form[$i]['delta'] = array('#type' => 'value', '#value' => $block['delta']);
228 $form[$i]['info'] = array('#value' => $block['info']);
229 $form[$i]['status'] = array('#type' => 'checkbox', '#default_value' => $block['status']);
230 $form[$i]['theme'] = array('#type' => 'hidden', '#value' => $theme_key);
231 $form[$i]['weight'] = array('#type' => 'weight', '#default_value' => $block['weight']);
232 $form[$i]['region'] = array('#type' => 'select',
233 '#default_value' => isset($block['region']) ? $block['region'] : system_default_region($theme_key),
234 '#options' => $block_regions,
235 );
236
237 if ($throttle) {
238 $form[$i]['throttle'] = array('#type' => 'checkbox', '#default_value' => $block['throttle']);
239 }
240 $form[$i]['configure'] = array('#value' => l(t('configure'), 'admin/block/configure/'. $block['module'] .'/'. $block['delta']));
241 if ($block['module'] == 'block') {
242 $form[$i]['delete'] = array('#value' => l(t('delete'), 'admin/block/delete/'. $block['delta']));
243 }
244 }
245 $form['submit'] = array('#type' => 'submit', '#value' => t('Save blocks'));
246
247 return drupal_get_form('block_admin_display', $form);
248 }
249
250 /**
251 * Helper function for sorting blocks on admin/block.
252 *
253 * Active blocks are sorted by region, then by weight.
254 * Disabled blocks are sorted by name.
255 */
256 function _block_compare($a, $b) {
257 $status = $b['status'] - $a['status'];
258 // Separate enabled from disabled.
259 if ($status) {
260 return $status;
261 }
262 // Enabled blocks
263 if ($a['status']) {
264 $place = strcmp($a['region'], $b['region']);
265 return $place ? $place : ($a['weight'] - $b['weight']);
266 }
267 // Disabled blocks
268 else {
269 return strcmp($a['info'], $b['info']);
270 }
271 }
272
273 /**
274 * Process main block administration form submission.
275 */
276 function block_admin_display_submit($form_id, $form_values) {
277 foreach ($form_values as $block) {
278 db_query("UPDATE {blocks} SET status = %d, weight = %d, region = '%s', throttle = %d WHERE module = '%s' AND delta = '%s' AND theme = '%s'", $block['status'], $block['weight'], $block['region'], $block['throttle'], $block['module'], $block['delta'], $block['theme']);
279 }
280 drupal_set_message(t('The block settings have been updated.'));
281 cache_clear_all();
282 }
283
284 /**
285 * Theme main block administration form submission.
286 *
287 * Note: the blocks are already sorted in the right order,
288 * grouped by status, region and weight.
289 */
290 function theme_block_admin_display($form) {
291 global $theme_key;
292
293 $throttle = module_exist('throttle');
294 $block_regions = system_region_list($theme_key);
295
296 // Highlight regions on page to provide visual reference.
297 foreach ($block_regions as $key => $value) {
298 drupal_set_content($key, '<div class="block-region">' . $value . '</div>');
299 }
300
301 // Build rows
302 $rows = array();
303 $last_region = '';
304 $last_status = 1;
305 foreach (element_children($form) as $i) {
306 $block = &$form[$i];
307 // Only take form elements that are blocks.
308 if (is_array($block['info'])) {
309 // Fetch values
310 $region = $block['region']['#default_value'];
311 $status = $block['status']['#default_value'];
312
313 // Output region header
314 if ($status && $region != $last_region) {
315 $region_title = t('%region', array('%region' => drupal_ucfirst($block_regions[$region])));
316 $rows[] = array(array('data' => $region_title, 'class' => 'region', 'colspan' => ($throttle ? 7 : 6)));
317 $last_region = $region;
318 }
319 // Output disabled header
320 elseif ($status != $last_status) {
321 $rows[] = array(array('data' => t('Disabled'), 'class' => 'region', 'colspan' => ($throttle ? 7 : 6)));
322 $last_status = $status;
323 }
324
325 // Generate block row
326 $row = array(
327 array('data' => form_render($block['info']), 'class' => 'block'),
328 form_render($block['status']) . form_render($block['theme']),
329 form_render($block['weight']),
330 form_render($block['region'])
331 );
332 if ($throttle) {
333 $row[] = form_render($block['throttle']);
334 }
335 $row[] = form_render($block['configure']);
336 $row[] = $block['delete'] ? form_render($block['delete']) : '';
337 $rows[] = $row;
338 }
339 }
340
341 // Finish table
342 $header = array(t('Block'), t('Enabled'), t('Weight'), t('Placement'));
343 if ($throttle) {
344 $header[] = t('Throttle');
345 }
346 $header[] = array('data' => t('Operations'), 'colspan' => 2);
347
348 $output = theme('table', $header, $rows, array('id' => 'blocks'));
349 $output .= form_render($form['submit']);
350 $output .= form_render($form);
351 return $output;
352 }
353
354 function block_box_get($bid) {
355 return db_fetch_array(db_query('SELECT * FROM {boxes} WHERE bid = %d', $bid));
356 }
357
358 /**
359 * Menu callback; displays the block configuration form.
360 */
361 function block_admin_configure($module = NULL, $delta = 0) {
362
363 $form['module'] = array('#type' => 'value', '#value' => $module);
364 $form['delta'] = array('#type' => 'value', '#value' => $delta);
365
366 $edit = db_fetch_array(db_query("SELECT pages, visibility, custom FROM {blocks} WHERE module = '%s' AND delta = '%s'", $module, $delta));
367
368 // Module-specific block configurations.
369 if ($settings = module_invoke($module, 'block', 'configure', $delta)) {
370 $form['block_settings'] = array(
371 '#type' => 'fieldset',
372 '#title' => t('Block specific settings'),
373 '#collapsible' => true,
374 );
375
376 foreach ($settings as $k => $v) {
377 $form['block_settings'][$k] = $v;
378 }
379 }
380
381 // Get the block subject for the page title.
382 $info = module_invoke($module, 'block', 'list');
383 drupal_set_title(t("'%name' block", array('%name' => $info[$delta]['info'])));
384
385 // Standard block configurations.
386
387 $form['user_vis_settings'] = array(
388 '#type' => 'fieldset',
389 '#title' => t('User specific visibility settings'),
390 '#collapsible' => true,
391 );
392 $form['user_vis_settings']['custom'] = array(
393 '#type' => 'radios',
394 '#title' => t('Custom visibility settings'),
395 '#options' => array(t('Users cannot control whether or not they see this block.'), t('Show this block by default, but let individual users hide it.'), t('Hide this block by default but let individual users show it.')),
396 '#description' => t('Allow individual users to customize the visibility of this block in their account settings.'),
397 '#default_value' => $edit['custom'],
398 );
399 $form['page_vis_settings'] = array(
400 '#type' => 'fieldset',
401 '#title' => t('Page specific visibility settings'),
402 '#collapsible' => true,
403 );
404 $access = user_access('use PHP for block visibility');
405
406 if ($edit['visibility'] == 2 && !$access) {
407 $form['page_vis_settings'] = array();
408 $form['page_vis_settings']['visibility'] = array('#type' => 'value', '#value' => 2);
409 $form['page_vis_settings']['pages'] = array('#type' => 'value', '#value' => $edit['pages']);
410 }
411 else {
412 $options = array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.'));
413 $description = t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are '%blog' for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => theme('placeholder', 'blog'), '%blog-wildcard' => theme('placeholder', 'blog/*'), '%front' => theme('placeholder', '<front>')));
414
415 if ($access) {
416 $options[] = t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
417 $description .= t('If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array('%php' => theme('placeholder', '<?php ?>')));
418 }
419 $form['page_vis_settings']['visibility'] = array(
420 '#type' => 'radios',
421 '#title' => t('Show block on specific pages'),
422 '#options' => $options,
423 '#default_value' => $edit['visibility'],
424 );
425 $form['page_vis_settings']['pages'] = array(
426 '#type' => 'textarea',
427 '#title' => t('Pages'),
428 '#default_value' => $edit['pages'],
429 '#description' => $description,
430 );
431 }
432
433 $form['submit'] = array(
434 '#type' => 'submit',
435 '#value' => t('Save block'),
436 );
437
438 return drupal_get_form('block_admin_configure', $form);
439 }
440
441 function block_admin_configure_validate($form_id, $form_values) {
442 if ($form_values['module'] == 'block') {
443 if (empty($form_values['info']) || db_num_rows(db_query("SELECT bid FROM {boxes} WHERE bid != %d AND info = '%s'", $form_values['delta'], $form_values['info']))) {
444 form_set_error('info', t('Please ensure that each block description is unique.'));
445 }
446 }
447 }
448
449 function block_admin_configure_submit($form_id, $form_values) {
450 if (!form_get_errors()) {
451 db_query("UPDATE {blocks} SET visibility = %d, pages = '%s', custom = %d WHERE module = '%s' AND delta = '%s'", $form_values['visibility'], trim($form_values['pages']), $form_values['custom'], $form_values['module'], $form_values['delta']);
452 module_invoke($form_values['module'], 'block', 'save', $form_values['delta'], $form_values);
453 drupal_set_message(t('The block configuration has been saved.'));
454 cache_clear_all();
455 return 'admin/block';
456 }
457 }
458
459 /**
460 * Menu callback; displays the block creation form.
461 */
462 function block_box_add() {
463 $form = block_box_form();
464 $form['submit'] = array('#type' => 'submit', '#value' => t('Save block'));
465
466 return drupal_get_form('block_box_add', $form);
467 }
468
469 function block_box_add_validate($form_id, $form_values) {
470 if (empty($form_values['info']) || db_num_rows(db_query("SELECT info FROM {boxes} WHERE info = '%s'", $form_values['info']))) {
471 form_set_error('info', t('Please ensure that each block description is unique.'));
472 }
473 }
474
475 function block_box_add_submit($form_id, $form_values) {
476 if (!form_get_errors()) {
477 if (block_box_save($form_values)) {
478 drupal_set_message(t('The block has been created.'));
479 return 'admin/block';
480 }
481 }
482 }
483
484 /**
485 * Menu callback; confirm deletion of custom blocks.
486 */
487 function block_box_delete($bid = 0) {
488 $box = block_box_get($bid);
489 $form['info'] = array('#type' => 'hidden', '#value' => $box['info'] ? $box['info'] : $box['title']);
490 $form['bid'] = array('#type' => 'hidden', '#value' => $bid);
491
492 return confirm_form('block_box_delete_confirm', $form, t('Are you sure you want to delete the block %name?', array('%name' => theme('placeholder', $box['info']))), 'admin/block', '', t('Delete'), t('Cancel'));
493 }
494
495 /**
496 * Deletion of custom blocks.
497 */
498 function block_box_delete_confirm_submit($form_id, $form_values) {
499 db_query('DELETE FROM {boxes} WHERE bid = %d', $form_values['bid']);
500 db_query("DELETE FROM {blocks} WHERE module = 'block' AND delta = %d", $form_values['bid']);
501 drupal_set_message(t('The block %name has been removed.', array('%name' => theme('placeholder', $form_values['info']))));
502 cache_clear_all();
503 return 'admin/block';
504 };
505
506 function block_box_form($edit = array()) {
507 $form['info'] = array(
508 '#type' => 'textfield',
509 '#title' => t('Block description'),
510 '#default_value' => $edit['info'],
511 '#maxlength' => 64,
512 '#description' => t('A brief description of your block. Used on the <a href="%overview">block overview page</a>.', array('%overview' => url('admin/block'))),
513 '#required' => TRUE,
514 '#weight' => -19,
515 );
516 $form['title'] = array(
517 '#type' => 'textfield',
518 '#title' => t('Block title'),
519 '#default_value' => $edit['title'],
520 '#maxlength' => 64,
521 '#description' => t('The title of the block as shown to the user.'),
522 '#weight' => -18,
523 );
524 $form['body_filter']['#weight'] = -17;
525 $form['body_filter']['body'] = array(
526 '#type' => 'textarea',
527 '#title' => t('Block body'),
528 '#default_value' => $edit['body'],
529 '#rows' => 15,
530 '#description' => t('The content of the block as shown to the user.'),
531 '#weight' => -17,
532 );
533 $form['body_filter']['format'] = filter_form($edit['format'], -16);
534
535 return $form;
536 }
537
538 function block_box_save($edit, $delta = NULL) {
539 if (!filter_access($edit['format'])) {
540 $edit['format'] = FILTER_FORMAT_DEFAULT;
541 }
542
543 if (isset($delta)) {
544 db_query("UPDATE {boxes} SET title = '%s', body = '%s', info = '%s', format = %d WHERE bid = %d", $edit['title'], $edit['body'], $edit['info'], $edit['format'], $delta);
545 }
546 else {
547 db_query("INSERT INTO {boxes} (title, body, info, format) VALUES ('%s', '%s', '%s', %d)", $edit['title'], $edit['body'], $edit['info'], $edit['format']);
548 }
549 return true;
550 }
551
552 /**
553 * Implementation of hook_user().
554 *
555 * Allow users to decide which custom blocks to display when they visit
556 * the site.
557 */
558 function block_user($type, $edit, &$user, $category = NULL) {
559 switch ($type) {
560 case 'form':
561 if ($category == 'account') {
562 $result = db_query('SELECT * FROM {blocks} WHERE status = 1 AND custom != 0 ORDER BY weight, module, delta');
563 $form['block'] = array('#type' => 'fieldset', '#title' => t('Block configuration'), '#weight' => 3, '#collapsible' => TRUE, '#tree' => TRUE);
564 while ($block = db_fetch_object($result)) {
565 $data = module_invoke($block->module, 'block', 'list');
566 if ($data[$block->delta]['info']) {
567 $return = TRUE;
568 $form['block'][$block->module][$block->delta] = array('#type' => 'checkbox', '#title' => $data[$block->delta]['info'], '#default_value' => isset($user->block[$block->module][$block->delta]) ? $user->block[$block->module][$block->delta] : ($block->custom == 1));
569 }
570 }
571
572 if ($return) {
573 return $form;
574 }
575 }
576
577 break;
578 case 'validate':
579 if (!$edit['block']) {
580 $edit['block'] = array();
581 }
582 return $edit;
583 }
584 }
585
586 /**
587 * Return all blocks in the specified region for the current user.
588 *
589 * @param $region
590 * The name of a region.
591 *
592 * @return
593 * An array of block objects, indexed with <i>module</i>_<i>delta</i>.
594 * If you are displaying your blocks in one or two sidebars, you may check
595 * whether this array is empty to see how many columns are going to be
596 * displayed.
597 *
598 * @todo
599 * Add a proper primary key (bid) to the blocks table so we don't have
600 * to mess around with this <i>module</i>_<i>delta</i> construct.
601 * Currently, the blocks table has no primary key defined!
602 */
603 function block_list($region) {
604 global $user, $theme_key;
605
606 static $blocks = array();
607
608 if (!count($blocks)) {
609 $result = db_query("SELECT * FROM {blocks} WHERE theme = '%s' AND status = 1 ORDER BY region, weight, module", $theme_key);
610 while ($block = db_fetch_object($result)) {
611 if (!isset($blocks[$block->region])) {
612 $blocks[$block->region] = array();
613 }
614 // Use the user's block visibility setting, if necessary
615 if ($block->custom != 0) {
616 if ($user->uid && isset($user->block[$block->module][$block->delta])) {
617 $enabled = $user->block[$block->module][$block->delta];
618 }
619 else {
620 $enabled = ($block->custom == 1);
621 }
622 }
623 else {
624 $enabled = TRUE;
625 }
626
627 // Match path if necessary
628 if ($block->pages) {
629 if ($block->visibility < 2) {
630 $path = drupal_get_path_alias($_GET['q']);
631 $regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($block->pages, '/')) .')$/';
632 // Compare with the internal and path alias (if any).
633 $page_match = preg_match($regexp, $path);
634 if ($path != $_GET['q']) {
635 $page_match = $page_match || preg_match($regexp, $_GET['q']);
636 }
637 // When $block->visibility has a value of 0, the block is displayed on
638 // all pages except those listed in $block->pages. When set to 1, it
639 // is displayed only on those pages listed in $block->pages.
640 $page_match = !($block->visibility xor $page_match);
641 }
642 else {
643 $page_match = drupal_eval($block->pages);
644 }
645 }
646 else {
647 $page_match = TRUE;
648 }
649
650 if ($enabled && $page_match) {
651 // Check the current throttle status and see if block should be displayed
652 // based on server load.
653 if (!($block->throttle && (module_invoke('throttle', 'status') > 0))) {
654 $array = module_invoke($block->module, 'block', 'view', $block->delta);
655 if (isset($array) && is_array($array)) {
656 foreach ($array as $k => $v) {
657 $block->$k = $v;
658 }
659 }
660 }
661 if (isset($block->content) && $block->content) {
662 $blocks[$block->region]["{$block->module}_{$block->delta}"] = $block;
663 }
664 }
665 }
666 }
667 // Create an empty array if there were no entries
668 if (!isset($blocks[$region])) {
669 $blocks[$region] = array();
670 }
671 return $blocks[$region];
672 }
673
674

  ViewVC Help
Powered by ViewVC 1.1.2