<?php
function block_help($section) {
switch ($section) {
case 'admin/help#block':
$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>';
$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>';
$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>';
$output .= t('<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 page visibility settings. Blocks can be configured to be visible/hidden on certain pages.</li>
<li>Its custom visibility settings. Blocks can be configured to be visible only when specific conditions are true.</li>
<li>Its user visibility settings. Administrators can choose to let users decide whether to show/hide certain blocks.</li>
<li>Its function. Some dynamic blocks, such as those generated by modules, will be displayed only on certain pages.</li>
</ul>
');
$output .= '<h3>'. t('Module blocks') .'</h3>';
$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>';
$output .= '<h3>'. t('Administrator defined blocks') .'</h3>';
$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>';
$output .= t('<p>You can</p>
<ul>
<li>enable, throttle and configure blocks at <a href="%admin-block">administer >> blocks</a>.</li>
<li>add an administrator-defined block at <a href="%admin-block-add">administer >> blocks >> add block</a>.</li>
</ul>
', array('%admin-block' => url('admin/block'), '%admin-block-add' => url('admin/block/add')));
$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>';
return $output;
case 'admin/modules#description':
return t('Controls the boxes that are displayed around the main content.');
case 'admin/block':
return t("
<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>
<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>
<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>
<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>
", 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')));
}
}
function block_perm() {
return array('administer blocks', 'use PHP for block visibility');
}
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_display');
$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);
foreach (list_themes() as $key => $theme) {
if ($theme->status) {
if ($key == variable_get('theme_default', 'bluemarine')) {
$items[] = array('path' => 'admin/block/list/' . $key, 'title' => t('%key settings', array('%key' => $key)),
'access' => user_access('administer blocks'), 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10);
}
else {
$items[] = array('path' => 'admin/block/list/' . $key, 'title' => t('%key settings', array('%key' => $key)),
'access' => user_access('administer blocks'), 'type' => MENU_LOCAL_TASK);
}
}
}
}
return $items;
}
function block_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks = array();
$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_markup($block->body, $block->format, FALSE);
return $data;
}
}
function _block_rehash() {
global $theme_key;
init_theme();
$result = db_query("SELECT * FROM {blocks} WHERE theme = '%s'", $theme_key);
while ($old_block = db_fetch_object($result)) {
$old_blocks[$old_block->module][$old_block->delta] = $old_block;
}
$blocks = array();
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;
}
else {
$properties = array('status' => 0, 'weight' => 0, 'region' => 'left', 'pages' => '', 'custom' => 0);
foreach ($properties as $property => $default) {
if (!isset($block[$property])) {
$block[$property] = $default;
}
}
}
$blocks[] = $block;
}
}
}
db_lock_table('blocks');
db_query("DELETE FROM {blocks} WHERE theme = '%s'", $theme_key);
foreach ($blocks as $block) {
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']);
}
db_unlock_tables();
return $blocks;
}
function block_admin_display() {
global $theme_key, $custom_theme;
if (arg(3)) {
$custom_theme = arg(3);
}
else {
$custom_theme = variable_get('theme_default', 'bluemarine');
}
init_theme();
$blocks = _block_rehash();
usort($blocks, '_block_compare');
$throttle = module_exist('throttle');
$block_regions = system_region_list($theme_key);
$form['#action'] = arg(3) ? url('admin/block/list/' . $theme_key) : url('admin/block');
$form['#tree'] = TRUE;
foreach ($blocks as $i => $block) {
$form[$i]['module'] = array('#type' => 'value', '#value' => $block['module']);
$form[$i]['delta'] = array('#type' => 'value', '#value' => $block['delta']);
$form[$i]['info'] = array('#value' => $block['info']);
$form[$i]['status'] = array('#type' => 'checkbox', '#default_value' => $block['status']);
$form[$i]['theme'] = array('#type' => 'hidden', '#value' => $theme_key);
$form[$i]['weight'] = array('#type' => 'weight', '#default_value' => $block['weight']);
$form[$i]['region'] = array('#type' => 'select',
'#default_value' => isset($block['region']) ? $block['region'] : system_default_region($theme_key),
'#options' => $block_regions,
);
if ($throttle) {
$form[$i]['throttle'] = array('#type' => 'checkbox', '#default_value' => $block['throttle']);
}
$form[$i]['configure'] = array('#value' => l(t('configure'), 'admin/block/configure/'. $block['module'] .'/'. $block['delta']));
if ($block['module'] == 'block') {
$form[$i]['delete'] = array('#value' => l(t('delete'), 'admin/block/delete/'. $block['delta']));
}
}
$form['submit'] = array('#type' => 'submit', '#value' => t('Save blocks'));
return drupal_get_form('block_admin_display', $form);
}
function _block_compare($a, $b) {
$status = $b['status'] - $a['status'];
if ($status) {
return $status;
}
if ($a['status']) {
$place = strcmp($a['region'], $b['region']);
return $place ? $place : ($a['weight'] - $b['weight']);
}
else {
return strcmp($a['info'], $b['info']);
}
}
function block_admin_display_submit($form_id, $form_values) {
foreach ($form_values as $block) {
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']);
}
drupal_set_message(t('The block settings have been updated.'));
cache_clear_all();
}
function theme_block_admin_display($form) {
global $theme_key;
$throttle = module_exist('throttle');
$block_regions = system_region_list($theme_key);
foreach ($block_regions as $key => $value) {
drupal_set_content($key, '<div class="block-region">' . $value . '</div>');
}
$rows = array();
$last_region = '';
$last_status = 1;
foreach (element_children($form) as $i) {
$block = &$form[$i];
if (is_array($block['info'])) {
$region = $block['region']['#default_value'];
$status = $block['status']['#default_value'];
if ($status && $region != $last_region) {
$region_title = t('%region', array('%region' => drupal_ucfirst($block_regions[$region])));
$rows[] = array(array('data' => $region_title, 'class' => 'region', 'colspan' => ($throttle ? 7 : 6)));
$last_region = $region;
}
elseif ($status != $last_status) {
$rows[] = array(array('data' => t('Disabled'), 'class' => 'region', 'colspan' => ($throttle ? 7 : 6)));
$last_status = $status;
}
$row = array(
array('data' => form_render($block['info']), 'class' => 'block'),
form_render($block['status']) . form_render($block['theme']),
form_render($block['weight']),
form_render($block['region'])
);
if ($throttle) {
$row[] = form_render($block['throttle']);
}
$row[] = form_render($block['configure']);
$row[] = $block['delete'] ? form_render($block['delete']) : '';
$rows[] = $row;
}
}
$header = array(t('Block'), t('Enabled'), t('Weight'), t('Placement'));
if ($throttle) {
$header[] = t('Throttle');
}
$header[] = array('data' => t('Operations'), 'colspan' => 2);
$output = theme('table', $header, $rows, array('id' => 'blocks'));
$output .= form_render($form['submit']);
$output .= form_render($form);
return $output;
}
function block_box_get($bid) {
return db_fetch_array(db_query('SELECT * FROM {boxes} WHERE bid = %d', $bid));
}
function block_admin_configure($module = NULL, $delta = 0) {
$form['module'] = array('#type' => 'value', '#value' => $module);
$form['delta'] = array('#type' => 'value', '#value' => $delta);
$edit = db_fetch_array(db_query("SELECT pages, visibility, custom FROM {blocks} WHERE module = '%s' AND delta = '%s'", $module, $delta));
if ($settings = module_invoke($module, 'block', 'configure', $delta)) {
$form['block_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Block specific settings'),
'#collapsible' => true,
);
foreach ($settings as $k => $v) {
$form['block_settings'][$k] = $v;
}
}
$info = module_invoke($module, 'block', 'list');
drupal_set_title(t("'%name' block", array('%name' => $info[$delta]['info'])));
$form['user_vis_settings'] = array(
'#type' => 'fieldset',
'#title' => t('User specific visibility settings'),
'#collapsible' => true,
);
$form['user_vis_settings']['custom'] = array(
'#type' => 'radios',
'#title' => t('Custom visibility settings'),
'#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.')),
'#description' => t('Allow individual users to customize the visibility of this block in their account settings.'),
'#default_value' => $edit['custom'],
);
$form['page_vis_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Page specific visibility settings'),
'#collapsible' => true,
);
$access = user_access('use PHP for block visibility');
if ($edit['visibility'] == 2 && !$access) {
$form['page_vis_settings'] = array();
$form['page_vis_settings']['visibility'] = array('#type' => 'value', '#value' => 2);
$form['page_vis_settings']['pages'] = array('#type' => 'value', '#value' => $edit['pages']);
}
else {
$options = array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.'));
$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>')));
if ($access) {
$options[] = t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
$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 ?>')));
}
$form['page_vis_settings']['visibility'] = array(
'#type' => 'radios',
'#title' => t('Show block on specific pages'),
'#options' => $options,
'#default_value' => $edit['visibility'],
);
$form['page_vis_settings']['pages'] = array(
'#type' => 'textarea',
'#title' => t('Pages'),
'#default_value' => $edit['pages'],
'#description' => $description,
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save block'),
);
return drupal_get_form('block_admin_configure', $form);
}
function block_admin_configure_validate($form_id, $form_values) {
if ($form_values['module'] == 'block') {
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']))) {
form_set_error('info', t('Please ensure that each block description is unique.'));
}
}
}
function block_admin_configure_submit($form_id, $form_values) {
if (!form_get_errors()) {
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']);
module_invoke($form_values['module'], 'block', 'save', $form_values['delta'], $form_values);
drupal_set_message(t('The block configuration has been saved.'));
cache_clear_all();
return 'admin/block';
}
}
function block_box_add() {
$form = block_box_form();
$form['submit'] = array('#type' => 'submit', '#value' => t('Save block'));
return drupal_get_form('block_box_add', $form);
}
function block_box_add_validate($form_id, $form_values) {
if (empty($form_values['info']) || db_num_rows(db_query("SELECT info FROM {boxes} WHERE info = '%s'", $form_values['info']))) {
form_set_error('info', t('Please ensure that each block description is unique.'));
}
}
function block_box_add_submit($form_id, $form_values) {
if (!form_get_errors()) {
if (block_box_save($form_values)) {
drupal_set_message(t('The block has been created.'));
return 'admin/block';
}
}
}
function block_box_delete($bid = 0) {
$box = block_box_get($bid);
$form['info'] = array('#type' => 'hidden', '#value' => $box['info'] ? $box['info'] : $box['title']);
$form['bid'] = array('#type' => 'hidden', '#value' => $bid);
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'));
}
function block_box_delete_confirm_submit($form_id, $form_values) {
db_query('DELETE FROM {boxes} WHERE bid = %d', $form_values['bid']);
db_query("DELETE FROM {blocks} WHERE module = 'block' AND delta = %d", $form_values['bid']);
drupal_set_message(t('The block %name has been removed.', array('%name' => theme('placeholder', $form_values['info']))));
cache_clear_all();
return 'admin/block';
};
function block_box_form($edit = array()) {
$form['info'] = array(
'#type' => 'textfield',
'#title' => t('Block description'),
'#default_value' => $edit['info'],
'#maxlength' => 64,
'#description' => t('A brief description of your block. Used on the <a href="%overview">block overview page</a>.', array('%overview' => url('admin/block'))),
'#required' => TRUE,
'#weight' => -19,
);
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Block title'),
'#default_value' => $edit['title'],
'#maxlength' => 64,
'#description' => t('The title of the block as shown to the user.'),
'#weight' => -18,
);
$form['body_filter']['#weight'] = -17;
$form['body_filter']['body'] = array(
'#type' => 'textarea',
'#title' => t('Block body'),
'#default_value' => $edit['body'],
'#rows' => 15,
'#description' => t('The content of the block as shown to the user.'),
'#weight' => -17,
);
$form['body_filter']['format'] = filter_form($edit['format'], -16);
return $form;
}
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 {
db_query("INSERT INTO {boxes} (title, body, info, format) VALUES ('%s', '%s', '%s', %d)", $edit['title'], $edit['body'], $edit['info'], $edit['format']);
}
return true;
}
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');
$form['block'] = array('#type' => 'fieldset', '#title' => t('Block configuration'), '#weight' => 3, '#collapsible' => TRUE, '#tree' => TRUE);
while ($block = db_fetch_object($result)) {
$data = module_invoke($block->module, 'block', 'list');
if ($data[$block->delta]['info']) {
$return = TRUE;
$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));
}
}
if ($return) {
return $form;
}
}
break;
case 'validate':
if (!$edit['block']) {
$edit['block'] = array();
}
return $edit;
}
}
function block_list($region) {
global $user, $theme_key;
static $blocks = array();
if (!count($blocks)) {
$result = db_query("SELECT * FROM {blocks} WHERE theme = '%s' AND status = 1 ORDER BY region, weight, module", $theme_key);
while ($block = db_fetch_object($result)) {
if (!isset($blocks[$block->region])) {
$blocks[$block->region] = array();
}
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;
}
if ($block->pages) {
if ($block->visibility < 2) {
$path = drupal_get_path_alias($_GET['q']);
$regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($block->pages, '/')) .')$/';
$page_match = preg_match($regexp, $path);
if ($path != $_GET['q']) {
$page_match = $page_match || preg_match($regexp, $_GET['q']);
}
$page_match = !($block->visibility xor $page_match);
}
else {
$page_match = drupal_eval($block->pages);
}
}
else {
$page_match = TRUE;
}
if ($enabled && $page_match) {
if (!($block->throttle && (module_invoke('throttle', 'status') > 0))) {
$array = module_invoke($block->module, 'block', 'view', $block->delta);
if (isset($array) && is_array($array)) {
foreach ($array as $k => $v) {
$block->$k = $v;
}
}
}
if (isset($block->content) && $block->content) {
$blocks[$block->region]["{$block->module}_{$block->delta}"] = $block;
}
}
}
}
if (!isset($blocks[$region])) {
$blocks[$region] = array();
}
return $blocks[$region];
}