/[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.162.2.6 - (show annotations) (download) (as text)
Tue Aug 22 19:32:56 2006 UTC (3 years, 3 months ago) by dries
Branch: DRUPAL-4-6
Changes since 1.162.2.5: +2 -2 lines
File MIME type: text/x-php
- Patch #58187 by Zack: documentation improvement.
1 <?php
2 // $Id: block.module,v 1.162.2.5 2005/07/20 11:41:57 dries 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 return t('
16 <p>Blocks are the boxes visible in the sidebar(s) of your web site. These are usually generated automatically by modules (e.g. recent forum topics), but you can also create your own blocks.</p>
17 <p>The sidebar each block appears in depends on both which theme you are using (some are left-only, some right, some both), and on the settings in block management.</p>
18 <p>The block management screen lets you specify the vertical sort-order of the blocks within a sidebar. You do this by assigning a weight to each block. Lighter blocks (smaller weight) "float up" towards the top of the sidebar. Heavier ones "sink down" towards the bottom of it.</p>
19 <p>A block\'s visibility depends on:</p>
20 <ul>
21 <li>Its enabled checkbox. Disabled blocks are never shown.</li>
22 <li>Its throttle checkbox. Throttled blocks are hidden during high server loads.</li>
23 <li>Its path options. Blocks can be configured to only show/hide on certain pages.</li>
24 <li>User settings. You can choose to let your users decide whether to show/hide certain blocks.</li>
25 <li>Its function. Dynamic blocks (such as those defined by modules) may be empty on certain pages and will not be shown.</li>
26 </ul>
27
28 <h3>Administrator defined blocks</h3>
29 <p>An administrator defined block contains content supplied by you (as opposed to being generated automatically by a module). Each admin-defined block consists of a title, a description, and a body which can be as long as you wish. The Drupal engine will render the content of the block.</p>');
30 case 'admin/modules#description':
31 return t('Controls the boxes that are displayed around the main content.');
32 case 'admin/block':
33 return t("
34 <p>Blocks are the boxes in the left and right side bars of the web site. They are made available by modules or created manually.</p>
35 <p>Only enabled blocks are shown. You can position the blocks by deciding which side of the page they will show up on (sidebar) and in which order they appear (weight).</p>
36 <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>
37 ", array('%throttle' => url('admin/settings/throttle')));
38 case 'admin/block/add':
39 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')));
40 }
41 }
42
43 /**
44 * Implementation of hook_perm().
45 */
46 function block_perm() {
47 return array('administer blocks');
48 }
49
50 /**
51 * Implementation of hook_menu().
52 */
53 function block_menu($may_cache) {
54 $items = array();
55
56 if ($may_cache) {
57 $items[] = array('path' => 'admin/block', 'title' => t('blocks'),
58 'access' => user_access('administer blocks'),
59 'callback' => 'block_admin');
60 $items[] = array('path' => 'admin/block/list', 'title' => t('list'),
61 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10);
62 $items[] = array('path' => 'admin/block/configure', 'title' => t('configure block'),
63 'access' => user_access('administer blocks'),
64 'callback' => 'block_admin_configure',
65 'type' => MENU_CALLBACK);
66 $items[] = array('path' => 'admin/block/delete', 'title' => t('delete block'),
67 'access' => user_access('administer blocks'),
68 'callback' => 'block_box_delete',
69 'type' => MENU_CALLBACK);
70 $items[] = array('path' => 'admin/block/add', 'title' => t('add block'),
71 'access' => user_access('administer blocks'),
72 'callback' => 'block_box_add',
73 'type' => MENU_LOCAL_TASK);
74 }
75
76 return $items;
77 }
78
79 /**
80 * Implementation of hook_block().
81 *
82 * Generates the administrator-defined blocks for display.
83 */
84 function block_block($op = 'list', $delta = 0, $edit = array()) {
85 switch ($op) {
86 case 'list':
87 $result = db_query('SELECT bid, title, info FROM {boxes} ORDER BY title');
88 while ($block = db_fetch_object($result)) {
89 $blocks[$block->bid]['info'] = $block->info ? check_plain($block->info) : check_plain($block->title);
90 }
91 return $blocks;
92
93 case 'configure':
94 $box = block_box_get($delta);
95 if (filter_access($box['format'])) {
96 return block_box_form($box);
97 }
98 break;
99
100 case 'save':
101 block_box_save($edit, $delta);
102 break;
103
104 case 'view':
105 $block = db_fetch_object(db_query('SELECT * FROM {boxes} WHERE bid = %d', $delta));
106 $data['subject'] = check_plain($block->title);
107 $data['content'] = check_output($block->body, $block->format);
108 return $data;
109 }
110 }
111
112 function block_admin_save($edit) {
113 foreach ($edit as $module => $blocks) {
114 foreach ($blocks as $delta => $block) {
115 db_query("UPDATE {blocks} SET region = %d, status = %d, weight = %d, throttle = %d WHERE module = '%s' AND delta = '%s'",
116 $block['region'], $block['status'], $block['weight'], $block['throttle'], $module, $delta);
117 }
118 }
119
120 return t('The block settings have been updated.');
121 }
122
123 /**
124 * Update the 'blocks' DB table with the blocks currently exported by modules.
125 *
126 * @param $order_by php <a
127 * href="http://www.php.net/manual/en/function.array-multisort.php">array_multisort()</a>
128 * style sort ordering, eg. "weight", SORT_ASC, SORT_STRING.
129 *
130 * @return
131 * Blocks currently exported by modules, sorted by $order_by.
132 */
133 function _block_rehash($order_by = array('weight')) {
134 $result = db_query('SELECT * FROM {blocks} ');
135 while ($old_block = db_fetch_object($result)) {
136 $old_blocks[$old_block->module][$old_block->delta] = $old_block;
137 }
138
139 db_query('DELETE FROM {blocks} ');
140
141 foreach (module_list() as $module) {
142 $module_blocks = module_invoke($module, 'block', 'list');
143 if ($module_blocks) {
144 foreach ($module_blocks as $delta => $block) {
145 $block['module'] = $module;
146 $block['delta'] = $delta;
147 if ($old_blocks[$module][$delta]) {
148 $block['status'] = $old_blocks[$module][$delta]->status;
149 $block['weight'] = $old_blocks[$module][$delta]->weight;
150 $block['region'] = $old_blocks[$module][$delta]->region;
151 $block['visibility'] = $old_blocks[$module][$delta]->visibility;
152 $block['pages'] = $old_blocks[$module][$delta]->pages;
153 $block['custom'] = $old_blocks[$module][$delta]->custom;
154 $block['throttle'] = $old_blocks[$module][$delta]->throttle;
155 $block['types'] = $old_blocks[$module][$delta]->types;
156 }
157 else {
158 $block['status'] = $block['weight'] = $block['region'] = $block['custom'] = 0;
159 $block['pages'] = $block['types'] = '';
160 }
161
162 // reinsert blocks into table
163 db_query("INSERT INTO {blocks} (module, delta, status, weight, region, visibility, pages, custom, throttle, types) VALUES ('%s', '%s', %d, %d, %d, %d, '%s', %d, %d, '%s')",
164 $block['module'], $block['delta'], $block['status'], $block['weight'], $block['region'], $block['visibility'], $block['pages'], $block['custom'], $block['throttle'], $block['types']);
165
166 $blocks[] = $block;
167
168 // build array to sort on
169 $order[$order_by[0]][] = $block[$order_by[0]];
170 }
171 }
172 }
173
174 // sort
175 array_multisort($order[$order_by[0]], $order_by[1] ? $order_by[1] : SORT_ASC, $order_by[2] ? $order_by[2] : SORT_REGULAR, $blocks);
176
177 return $blocks;
178 }
179
180 /**
181 * Prepare the main block administration form.
182 */
183 function block_admin_display() {
184 $blocks = _block_rehash();
185
186 $header = array(t('Block'), t('Enabled'), t('Weight'), t('Sidebar'));
187 if (module_exist('throttle')) {
188 $header[] = t('Throttle');
189 }
190 $header[] = array('data' => t('Operations'), 'colspan' => 2);
191
192 $left = array();
193 $right = array();
194 $disabled = array();
195 foreach ($blocks as $block) {
196 if ($block['module'] == 'block') {
197 $delete = l(t('delete'), 'admin/block/delete/'. $block['delta']);
198 }
199 else {
200 $delete = '';
201 }
202
203 $row = array(array('data' => $block['info'], 'class' => 'block'),
204 form_checkbox(NULL, $block['module'] .']['. $block['delta'] .'][status', 1, $block['status']),
205 form_weight(NULL, $block['module'] .']['. $block['delta'] .'][weight', $block['weight']),
206 form_radios(NULL, $block['module'] .']['. $block['delta'] .'][region', $block['region'],
207 array(t('left'), t('right'))));
208
209 if (module_exist('throttle')) {
210 $row[] = form_checkbox(NULL, $block['module'] .']['. $block['delta'] .'][throttle', 1, $block['throttle']);
211 }
212 $row[] = l(t('configure'), 'admin/block/configure/'. $block['module'] .'/'. $block['delta']);
213 $row[] = $delete;
214 if ($block['status']) {
215 if ($block['region'] == 0) {
216 $left[] = $row;
217 }
218 if ($block['region'] == 1) {
219 $right[] = $row;
220 }
221 }
222 else if ($block['region'] <= 1) {
223 $disabled[] = $row;
224 }
225 }
226
227 $rows = array();
228 if (count($left)) {
229 $rows[] = array(array('data' => t('Left sidebar'), 'class' => 'region', 'colspan' => (module_exist('throttle') ? 7 : 6)));
230 $rows = array_merge($rows, $left);
231 }
232 if (count($right)) {
233 $rows[] = array(array('data' => t('Right sidebar'), 'class' => 'region', 'colspan' => (module_exist('throttle') ? 7 : 6)));
234 $rows = array_merge($rows, $right);
235 }
236 if (count($disabled)) {
237 $rows[] = array(array('data' => t('Disabled'), 'class' => 'region', 'colspan' => (module_exist('throttle') ? 7 : 6)));
238 $rows = array_merge($rows, $disabled);
239 }
240 $output = theme('table', $header, $rows, array('id' => 'blocks'));
241 $output .= form_submit(t('Save blocks'));
242
243 return form($output, 'post', url('admin/block'));
244 }
245
246 function block_box_get($bid) {
247 return db_fetch_array(db_query('SELECT * FROM {boxes} WHERE bid = %d', $bid));
248 }
249
250 /**
251 * Menu callback; displays the block configuration form.
252 */
253 function block_admin_configure($module = NULL, $delta = 0) {
254 $edit = $_POST['edit'];
255 $op = $_POST['op'];
256
257 switch ($op) {
258 case t('Save block'):
259 if ($edit['types']) {
260 $types = implode(',', $edit['types']);
261 }
262 else {
263 $types = '';
264 }
265 db_query("UPDATE {blocks} SET visibility = %d, pages = '%s', custom = %d, types = '%s' WHERE module = '%s' AND delta = '%s'", $edit['visibility'], $edit['pages'], $edit['custom'], $types, $module, $delta);
266 module_invoke($module, 'block', 'save', $delta, $edit);
267 drupal_set_message(t('The block configuration has been saved.'));
268 cache_clear_all();
269 drupal_goto('admin/block');
270
271 default:
272 // Always evaluates to TRUE, but a validation step may be added later.
273 if (!$edit) {
274 $edit = db_fetch_array(db_query("SELECT pages, visibility, custom, types FROM {blocks} WHERE module = '%s' AND delta = '%s'", $module, $delta));
275 }
276
277 // Module-specific block configurations.
278 if ($settings = module_invoke($module, 'block', 'configure', $delta)) {
279 $form = form_group(t('Block-specific settings'), $settings);
280 }
281
282 foreach (node_list() as $type) {
283 $content_types[$type] = node_invoke($type, 'node_name');
284 }
285 // Get the block subject for the page title.
286 $info = module_invoke($module, 'block', 'list');
287 drupal_set_title(t("'%name' block", array('%name' => $info[$delta]['info'])));
288
289 // Standard block configurations.
290 $group_1 = form_radios(t('Custom visibility settings'), 'custom', $edit['custom'], 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.')), t('Allow individual users to customize the visibility of this block in their account settings.'));
291 $group_2 = form_radios(t('Show block on specific pages'), 'visibility', $edit['visibility'], array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.')));
292 $group_2 .= form_textarea(t('Pages'), 'pages', $edit['pages'], 40, 5, t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are '<em>blog</em>' for the blog page and '<em>blog/*</em>' for every personal blog. '<em>&lt;front&gt;</em>' is the front page."));
293 $group_3 = form_checkboxes(t('Restrict block to specific content types'), 'types', explode(',', $edit['types']), $content_types, t('Selecting one or more content types will cause this block to only be shown on pages of the selected types. This feature works alone or in conjunction with page specific visibility settings. For example, you can specify that a block only appear on book pages in the \'FAQ\' path.'), NULL, FALSE);
294
295 $form .= form_group(t('User specific visibility settings'), $group_1);
296 $form .= form_group(t('Page specific visibility settings'), $group_2);
297 $form .= form_group(t('Content specific visibility settings'), $group_3);
298
299
300 $form .= form_submit(t('Save block'));
301
302 print theme('page', form($form));
303 }
304 }
305
306 /**
307 * Menu callback; displays the block creation form.
308 */
309 function block_box_add() {
310 $edit = $_POST['edit'];
311 $op = $_POST['op'];
312
313 switch ($op) {
314 case t('Save block'):
315 block_box_save($edit);
316 drupal_set_message(t('The new block has been added.'));
317 drupal_goto('admin/block');
318
319 default:
320 $form = block_box_form();
321 $form .= form_submit(t('Save block'));
322 $output .= form($form);
323 }
324
325 print theme('page', $output);
326 }
327
328 /**
329 * Menu callback; confirm and delete custom blocks.
330 */
331 function block_box_delete($bid = 0) {
332 $op = $_POST['op'];
333 $box = block_box_get($bid);
334 $info = $box['info'] ? $box['info'] : $box['title'];
335
336 if ($_POST['edit']['confirm']) {
337 db_query('DELETE FROM {boxes} WHERE bid = %d', $bid);
338 drupal_set_message(t('The block %name has been deleted.', array('%name' => theme('placeholder', $info))));
339 cache_clear_all();
340 drupal_goto('admin/block');
341 }
342 else {
343 $output = theme('confirm',
344 t('Are you sure you want to delete the block %name?', array('%name' => theme('placeholder', $info))),
345 'admin/block',
346 NULL,
347 t('Delete'));
348 }
349
350 print theme('page', $output);
351 }
352
353 function block_box_form($edit = array()) {
354 $output = form_textfield(t('Block title'), 'title', $edit['title'], 50, 64, t('The title of the block as shown to the user. Leave blank for no title.'));
355 $output .= filter_form('format', $edit['format']);
356 $output .= form_textarea(t('Block body'), 'body', $edit['body'], 70, 10, t('The content of the block as shown to the user.'));
357 $output .= form_textfield(t('Block description'), 'info', $edit['info'], 50, 64, t('A brief description of your block. Used on the <a href="%overview">block overview page</a>.', array('%overview' => url('admin/block'))));
358
359 return $output;
360 }
361
362 function block_box_save($edit, $delta = NULL) {
363 if (!filter_access($edit['format'])) {
364 $edit['format'] = FILTER_FORMAT_DEFAULT;
365 }
366
367 if (isset($delta)) {
368 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);
369 }
370 else {
371 db_query("INSERT INTO {boxes} (title, body, info, format) VALUES ('%s', '%s', '%s', %d)", $edit['title'], $edit['body'], $edit['info'], $edit['format']);
372 }
373 }
374
375 /**
376 * Menu callback; displays the block overview page.
377 */
378 function block_admin() {
379 $edit = $_POST['edit'];
380 $op = $_POST['op'];
381
382 if ($op == t('Save blocks')) {
383 drupal_set_message(block_admin_save($edit));
384 cache_clear_all();
385 drupal_goto($_GET['q']);
386 }
387 print theme('page', block_admin_display());
388 }
389
390 /**
391 * Implementation of hook_user().
392 *
393 * Allow users to decide which custom blocks to display when they visit
394 * the site.
395 */
396 function block_user($type, $edit, &$user, $category = NULL) {
397 switch ($type) {
398 case 'form':
399 if ($category == 'account') {
400 $result = db_query('SELECT * FROM {blocks} WHERE status = 1 AND custom != 0 ORDER BY weight, module, delta');
401
402 while ($block = db_fetch_object($result)) {
403 $data = module_invoke($block->module, 'block', 'list');
404 if ($data[$block->delta]['info']) {
405 $form .= form_checkbox($data[$block->delta]['info'], 'block]['. $block->module .']['. $block->delta, 1, isset($user->block[$block->module][$block->delta]) ? $user->block[$block->module][$block->delta] : ($block->custom == 1));
406 }
407 }
408
409 if (isset($form)) {
410 return array(array('title' => t('Block configuration'), 'data' => $form, 'weight' => 2));
411 }
412 }
413
414 break;
415 case 'validate':
416 if (!$edit['block']) {
417 $edit['block'] = array();
418 }
419 return $edit;
420 }
421 }
422
423 /**
424 * Return all blocks in the specied region for the current user. You may
425 * use this function to implement variable block regions. The default
426 * regions are 'left', 'right' and 'all', where 'all' means both left and
427 * right.
428 *
429 * @param $region
430 * This is a string which describes in a human readable form which region
431 * you need.
432 *
433 * @param $regions
434 * This is an optional array and contains map(s) from the string $region to
435 * the numerical region value(s) in the blocks table. See default value for
436 * examples.
437 *
438 * @return
439 * An array of block objects, indexed with <i>module</i>_<i>delta</i>.
440 * If you are displaying your blocks in one or two sidebars, you may check
441 * whether this array is empty to see how many columns are going to be
442 * displayed.
443 *
444 * @todo
445 * Add a proper primary key (bid) to the blocks table so we don't have
446 * to mess around with this <i>module</i>_<i>delta</i> construct.
447 * Currently, the blocks table has no primary key defined!
448 */
449 function block_list($region, $regions = array('left' => 0, 'right' => 1, 'all' => '0, 1')) {
450 global $user;
451 static $blocks = array();
452
453 if (!isset($blocks[$region])) {
454 $blocks[$region] = array();
455 $result = db_query("SELECT * FROM {blocks} WHERE status = 1 AND region IN (%s) ORDER BY weight, module", $regions[$region]);
456 while ($block = db_fetch_array($result)) {
457 // Use the user's block visibility setting, if necessary
458 if ($block['custom'] != 0) {
459 if ($user->uid && isset($user->block[$block['module']][$block['delta']])) {
460 $enabled = $user->block[$block['module']][$block['delta']];
461 }
462 else {
463 $enabled = ($block['custom'] == 1);
464 }
465 }
466 else {
467 $enabled = TRUE;
468 }
469
470 // Match path if necessary
471 if ($block['pages']) {
472 $path = drupal_get_path_alias($_GET['q']);
473 $regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($block['pages'], '/')) .')$/';
474 $page_match = !($block['visibility'] xor preg_match($regexp, $path));
475 }
476 else {
477 $page_match = TRUE;
478 }
479 // Match node type if necessary
480 $type_match = FALSE;
481 if ($block['types'] != '') {
482 if (arg(0) == 'node' && is_numeric(arg(1))) {
483 $node = node_load(array('nid' => arg(1)));
484 $types = explode(',', $block['types']);
485 //Match on any one selected type
486 foreach ($types as $type) {
487 if ($node->type == $type) {
488 $type_match = TRUE;
489 break;
490 }
491 }
492 }
493 }
494 else {
495 $type_match = TRUE;
496 }
497
498 if ($enabled && $page_match && $type_match) {
499 // Check the current throttle status and see if block should be displayed
500 // based on server load.
501 if (!($block['throttle'] && (module_invoke('throttle', 'status') > 0))) {
502 $array = module_invoke($block['module'], 'block', 'view', $block['delta']);
503 if (is_array($array)) {
504 $block = array_merge($block, $array);
505 }
506 }
507 if (isset($block['content']) && $block['content']) {
508 $blocks[$region]["$block[module]_$block[delta]"] = (object) $block;
509 }
510 }
511 }
512 }
513 return $blocks[$region];
514 }
515
516 ?>

  ViewVC Help
Powered by ViewVC 1.1.2