/[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.7 - (show annotations) (download) (as text)
Wed Oct 18 20:14:08 2006 UTC (3 years, 1 month ago) by killes
Branch: DRUPAL-4-6
CVS Tags: DRUPAL-4-6-11, DRUPAL-4-6-10
Changes since 1.162.2.6: +2 -1 lines
File MIME type: text/x-php
remove trailing spaces
1 <?php
2 // $Id: block.module,v 1.162.2.6 2006/08/22 19:32:56 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 unset($edit['token']);
114 foreach ($edit as $module => $blocks) {
115 foreach ($blocks as $delta => $block) {
116 db_query("UPDATE {blocks} SET region = %d, status = %d, weight = %d, throttle = %d WHERE module = '%s' AND delta = '%s'",
117 $block['region'], $block['status'], $block['weight'], $block['throttle'], $module, $delta);
118 }
119 }
120
121 return t('The block settings have been updated.');
122 }
123
124 /**
125 * Update the 'blocks' DB table with the blocks currently exported by modules.
126 *
127 * @param $order_by php <a
128 * href="http://www.php.net/manual/en/function.array-multisort.php">array_multisort()</a>
129 * style sort ordering, eg. "weight", SORT_ASC, SORT_STRING.
130 *
131 * @return
132 * Blocks currently exported by modules, sorted by $order_by.
133 */
134 function _block_rehash($order_by = array('weight')) {
135 $result = db_query('SELECT * FROM {blocks} ');
136 while ($old_block = db_fetch_object($result)) {
137 $old_blocks[$old_block->module][$old_block->delta] = $old_block;
138 }
139
140 db_query('DELETE FROM {blocks} ');
141
142 foreach (module_list() as $module) {
143 $module_blocks = module_invoke($module, 'block', 'list');
144 if ($module_blocks) {
145 foreach ($module_blocks as $delta => $block) {
146 $block['module'] = $module;
147 $block['delta'] = $delta;
148 if ($old_blocks[$module][$delta]) {
149 $block['status'] = $old_blocks[$module][$delta]->status;
150 $block['weight'] = $old_blocks[$module][$delta]->weight;
151 $block['region'] = $old_blocks[$module][$delta]->region;
152 $block['visibility'] = $old_blocks[$module][$delta]->visibility;
153 $block['pages'] = $old_blocks[$module][$delta]->pages;
154 $block['custom'] = $old_blocks[$module][$delta]->custom;
155 $block['throttle'] = $old_blocks[$module][$delta]->throttle;
156 $block['types'] = $old_blocks[$module][$delta]->types;
157 }
158 else {
159 $block['status'] = $block['weight'] = $block['region'] = $block['custom'] = 0;
160 $block['pages'] = $block['types'] = '';
161 }
162
163 // reinsert blocks into table
164 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')",
165 $block['module'], $block['delta'], $block['status'], $block['weight'], $block['region'], $block['visibility'], $block['pages'], $block['custom'], $block['throttle'], $block['types']);
166
167 $blocks[] = $block;
168
169 // build array to sort on
170 $order[$order_by[0]][] = $block[$order_by[0]];
171 }
172 }
173 }
174
175 // sort
176 array_multisort($order[$order_by[0]], $order_by[1] ? $order_by[1] : SORT_ASC, $order_by[2] ? $order_by[2] : SORT_REGULAR, $blocks);
177
178 return $blocks;
179 }
180
181 /**
182 * Prepare the main block administration form.
183 */
184 function block_admin_display() {
185 $blocks = _block_rehash();
186
187 $header = array(t('Block'), t('Enabled'), t('Weight'), t('Sidebar'));
188 if (module_exist('throttle')) {
189 $header[] = t('Throttle');
190 }
191 $header[] = array('data' => t('Operations'), 'colspan' => 2);
192
193 $left = array();
194 $right = array();
195 $disabled = array();
196 foreach ($blocks as $block) {
197 if ($block['module'] == 'block') {
198 $delete = l(t('delete'), 'admin/block/delete/'. $block['delta']);
199 }
200 else {
201 $delete = '';
202 }
203
204 $row = array(array('data' => $block['info'], 'class' => 'block'),
205 form_checkbox(NULL, $block['module'] .']['. $block['delta'] .'][status', 1, $block['status']),
206 form_weight(NULL, $block['module'] .']['. $block['delta'] .'][weight', $block['weight']),
207 form_radios(NULL, $block['module'] .']['. $block['delta'] .'][region', $block['region'],
208 array(t('left'), t('right'))));
209
210 if (module_exist('throttle')) {
211 $row[] = form_checkbox(NULL, $block['module'] .']['. $block['delta'] .'][throttle', 1, $block['throttle']);
212 }
213 $row[] = l(t('configure'), 'admin/block/configure/'. $block['module'] .'/'. $block['delta']);
214 $row[] = $delete;
215 if ($block['status']) {
216 if ($block['region'] == 0) {
217 $left[] = $row;
218 }
219 if ($block['region'] == 1) {
220 $right[] = $row;
221 }
222 }
223 else if ($block['region'] <= 1) {
224 $disabled[] = $row;
225 }
226 }
227
228 $rows = array();
229 if (count($left)) {
230 $rows[] = array(array('data' => t('Left sidebar'), 'class' => 'region', 'colspan' => (module_exist('throttle') ? 7 : 6)));
231 $rows = array_merge($rows, $left);
232 }
233 if (count($right)) {
234 $rows[] = array(array('data' => t('Right sidebar'), 'class' => 'region', 'colspan' => (module_exist('throttle') ? 7 : 6)));
235 $rows = array_merge($rows, $right);
236 }
237 if (count($disabled)) {
238 $rows[] = array(array('data' => t('Disabled'), 'class' => 'region', 'colspan' => (module_exist('throttle') ? 7 : 6)));
239 $rows = array_merge($rows, $disabled);
240 }
241 $output = theme('table', $header, $rows, array('id' => 'blocks'));
242 $output .= form_submit(t('Save blocks'));
243
244 return form($output, 'post', url('admin/block'));
245 }
246
247 function block_box_get($bid) {
248 return db_fetch_array(db_query('SELECT * FROM {boxes} WHERE bid = %d', $bid));
249 }
250
251 /**
252 * Menu callback; displays the block configuration form.
253 */
254 function block_admin_configure($module = NULL, $delta = 0) {
255 $edit = $_POST['edit'];
256 $op = $_POST['op'];
257
258 switch ($op) {
259 case t('Save block'):
260 if ($edit['types']) {
261 $types = implode(',', $edit['types']);
262 }
263 else {
264 $types = '';
265 }
266 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);
267 module_invoke($module, 'block', 'save', $delta, $edit);
268 drupal_set_message(t('The block configuration has been saved.'));
269 cache_clear_all();
270 drupal_goto('admin/block');
271
272 default:
273 // Always evaluates to TRUE, but a validation step may be added later.
274 if (!$edit) {
275 $edit = db_fetch_array(db_query("SELECT pages, visibility, custom, types FROM {blocks} WHERE module = '%s' AND delta = '%s'", $module, $delta));
276 }
277
278 // Module-specific block configurations.
279 if ($settings = module_invoke($module, 'block', 'configure', $delta)) {
280 $form = form_group(t('Block-specific settings'), $settings);
281 }
282
283 foreach (node_list() as $type) {
284 $content_types[$type] = node_invoke($type, 'node_name');
285 }
286 // Get the block subject for the page title.
287 $info = module_invoke($module, 'block', 'list');
288 drupal_set_title(t("'%name' block", array('%name' => $info[$delta]['info'])));
289
290 // Standard block configurations.
291 $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.'));
292 $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.')));
293 $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."));
294 $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);
295
296 $form .= form_group(t('User specific visibility settings'), $group_1);
297 $form .= form_group(t('Page specific visibility settings'), $group_2);
298 $form .= form_group(t('Content specific visibility settings'), $group_3);
299
300
301 $form .= form_submit(t('Save block'));
302
303 print theme('page', form($form));
304 }
305 }
306
307 /**
308 * Menu callback; displays the block creation form.
309 */
310 function block_box_add() {
311 $edit = $_POST['edit'];
312 $op = $_POST['op'];
313
314 switch ($op) {
315 case t('Save block'):
316 block_box_save($edit);
317 drupal_set_message(t('The new block has been added.'));
318 drupal_goto('admin/block');
319
320 default:
321 $form = block_box_form();
322 $form .= form_submit(t('Save block'));
323 $output .= form($form);
324 }
325
326 print theme('page', $output);
327 }
328
329 /**
330 * Menu callback; confirm and delete custom blocks.
331 */
332 function block_box_delete($bid = 0) {
333 $op = $_POST['op'];
334 $box = block_box_get($bid);
335 $info = $box['info'] ? $box['info'] : $box['title'];
336
337 if ($_POST['edit']['confirm']) {
338 db_query('DELETE FROM {boxes} WHERE bid = %d', $bid);
339 drupal_set_message(t('The block %name has been deleted.', array('%name' => theme('placeholder', $info))));
340 cache_clear_all();
341 drupal_goto('admin/block');
342 }
343 else {
344 $output = theme('confirm',
345 t('Are you sure you want to delete the block %name?', array('%name' => theme('placeholder', $info))),
346 'admin/block',
347 NULL,
348 t('Delete'));
349 }
350
351 print theme('page', $output);
352 }
353
354 function block_box_form($edit = array()) {
355 $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.'));
356 $output .= filter_form('format', $edit['format']);
357 $output .= form_textarea(t('Block body'), 'body', $edit['body'], 70, 10, t('The content of the block as shown to the user.'));
358 $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'))));
359
360 return $output;
361 }
362
363 function block_box_save($edit, $delta = NULL) {
364 if (!filter_access($edit['format'])) {
365 $edit['format'] = FILTER_FORMAT_DEFAULT;
366 }
367
368 if (isset($delta)) {
369 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);
370 }
371 else {
372 db_query("INSERT INTO {boxes} (title, body, info, format) VALUES ('%s', '%s', '%s', %d)", $edit['title'], $edit['body'], $edit['info'], $edit['format']);
373 }
374 }
375
376 /**
377 * Menu callback; displays the block overview page.
378 */
379 function block_admin() {
380 $edit = $_POST['edit'];
381 $op = $_POST['op'];
382
383 if ($op == t('Save blocks')) {
384 drupal_set_message(block_admin_save($edit));
385 cache_clear_all();
386 drupal_goto($_GET['q']);
387 }
388 print theme('page', block_admin_display());
389 }
390
391 /**
392 * Implementation of hook_user().
393 *
394 * Allow users to decide which custom blocks to display when they visit
395 * the site.
396 */
397 function block_user($type, $edit, &$user, $category = NULL) {
398 switch ($type) {
399 case 'form':
400 if ($category == 'account') {
401 $result = db_query('SELECT * FROM {blocks} WHERE status = 1 AND custom != 0 ORDER BY weight, module, delta');
402
403 while ($block = db_fetch_object($result)) {
404 $data = module_invoke($block->module, 'block', 'list');
405 if ($data[$block->delta]['info']) {
406 $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));
407 }
408 }
409
410 if (isset($form)) {
411 return array(array('title' => t('Block configuration'), 'data' => $form, 'weight' => 2));
412 }
413 }
414
415 break;
416 case 'validate':
417 if (!$edit['block']) {
418 $edit['block'] = array();
419 }
420 return $edit;
421 }
422 }
423
424 /**
425 * Return all blocks in the specied region for the current user. You may
426 * use this function to implement variable block regions. The default
427 * regions are 'left', 'right' and 'all', where 'all' means both left and
428 * right.
429 *
430 * @param $region
431 * This is a string which describes in a human readable form which region
432 * you need.
433 *
434 * @param $regions
435 * This is an optional array and contains map(s) from the string $region to
436 * the numerical region value(s) in the blocks table. See default value for
437 * examples.
438 *
439 * @return
440 * An array of block objects, indexed with <i>module</i>_<i>delta</i>.
441 * If you are displaying your blocks in one or two sidebars, you may check
442 * whether this array is empty to see how many columns are going to be
443 * displayed.
444 *
445 * @todo
446 * Add a proper primary key (bid) to the blocks table so we don't have
447 * to mess around with this <i>module</i>_<i>delta</i> construct.
448 * Currently, the blocks table has no primary key defined!
449 */
450 function block_list($region, $regions = array('left' => 0, 'right' => 1, 'all' => '0, 1')) {
451 global $user;
452 static $blocks = array();
453
454 if (!isset($blocks[$region])) {
455 $blocks[$region] = array();
456 $result = db_query("SELECT * FROM {blocks} WHERE status = 1 AND region IN (%s) ORDER BY weight, module", $regions[$region]);
457 while ($block = db_fetch_array($result)) {
458 // Use the user's block visibility setting, if necessary
459 if ($block['custom'] != 0) {
460 if ($user->uid && isset($user->block[$block['module']][$block['delta']])) {
461 $enabled = $user->block[$block['module']][$block['delta']];
462 }
463 else {
464 $enabled = ($block['custom'] == 1);
465 }
466 }
467 else {
468 $enabled = TRUE;
469 }
470
471 // Match path if necessary
472 if ($block['pages']) {
473 $path = drupal_get_path_alias($_GET['q']);
474 $regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($block['pages'], '/')) .')$/';
475 $page_match = !($block['visibility'] xor preg_match($regexp, $path));
476 }
477 else {
478 $page_match = TRUE;
479 }
480 // Match node type if necessary
481 $type_match = FALSE;
482 if ($block['types'] != '') {
483 if (arg(0) == 'node' && is_numeric(arg(1))) {
484 $node = node_load(array('nid' => arg(1)));
485 $types = explode(',', $block['types']);
486 //Match on any one selected type
487 foreach ($types as $type) {
488 if ($node->type == $type) {
489 $type_match = TRUE;
490 break;
491 }
492 }
493 }
494 }
495 else {
496 $type_match = TRUE;
497 }
498
499 if ($enabled && $page_match && $type_match) {
500 // Check the current throttle status and see if block should be displayed
501 // based on server load.
502 if (!($block['throttle'] && (module_invoke('throttle', 'status') > 0))) {
503 $array = module_invoke($block['module'], 'block', 'view', $block['delta']);
504 if (is_array($array)) {
505 $block = array_merge($block, $array);
506 }
507 }
508 if (isset($block['content']) && $block['content']) {
509 $blocks[$region]["$block[module]_$block[delta]"] = (object) $block;
510 }
511 }
512 }
513 }
514 return $blocks[$region];
515 }
516
517 ?>

  ViewVC Help
Powered by ViewVC 1.1.2