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