| 1 |
<?php |
<?php |
| 2 |
// $Id: block.module,v 1.206.2.8 2007/03/25 21:07:19 killes Exp $ |
// $Id: block.module,v 1.210 2006/07/02 20:06:31 drumm Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 22 |
<li>Its page visibility settings. Blocks can be configured to be visible/hidden on certain pages.</li> |
<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> |
<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> |
<li>Its user visibility settings. Administrators can choose to let users decide whether to show/hide certain blocks.</li> |
| 25 |
|
<li>Its user-role visibility settings. Administrators can choose to let blocks be visible only for certain user roles.</li> |
| 26 |
<li>Its function. Some dynamic blocks, such as those generated by modules, will be displayed only on certain pages.</li> |
<li>Its function. Some dynamic blocks, such as those generated by modules, will be displayed only on certain pages.</li> |
| 27 |
</ul> |
</ul> |
| 28 |
'); |
'); |
| 151 |
$old_blocks[$old_block->module][$old_block->delta] = $old_block; |
$old_blocks[$old_block->module][$old_block->delta] = $old_block; |
| 152 |
} |
} |
| 153 |
|
|
| 154 |
$blocks = array(); |
db_query("DELETE FROM {blocks} WHERE theme = '%s'", $theme_key); |
| 155 |
|
|
| 156 |
foreach (module_list() as $module) { |
foreach (module_list() as $module) { |
| 157 |
$module_blocks = module_invoke($module, 'block', 'list'); |
$module_blocks = module_invoke($module, 'block', 'list'); |
| 179 |
} |
} |
| 180 |
} |
} |
| 181 |
|
|
| 182 |
|
// Reinsert blocks into table |
| 183 |
|
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)", |
| 184 |
|
$block['module'], $block['delta'], $theme_key, $block['status'], $block['weight'], $block['region'], $block['visibility'], $block['pages'], $block['custom'], $block['throttle']); |
| 185 |
$blocks[] = $block; |
$blocks[] = $block; |
| 186 |
} |
} |
| 187 |
} |
} |
| 188 |
} |
} |
| 189 |
|
|
|
db_lock_table('blocks'); |
|
|
// Remove all blocks from table. |
|
|
db_query("DELETE FROM {blocks} WHERE theme = '%s'", $theme_key); |
|
|
|
|
|
// Reinsert new set of blocks into table. |
|
|
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(); |
|
|
|
|
| 190 |
return $blocks; |
return $blocks; |
| 191 |
} |
} |
| 192 |
|
|
| 296 |
$last_region = ''; |
$last_region = ''; |
| 297 |
$last_status = 1; |
$last_status = 1; |
| 298 |
foreach (element_children($form) as $i) { |
foreach (element_children($form) as $i) { |
| 299 |
$block = &$form[$i]; |
$block = $form[$i]; |
| 300 |
// Only take form elements that are blocks. |
// Only take form elements that are blocks. |
| 301 |
if (is_array($block['info'])) { |
if (is_array($block['info'])) { |
| 302 |
// Fetch values |
// Fetch values |
| 340 |
|
|
| 341 |
$output = theme('table', $header, $rows, array('id' => 'blocks')); |
$output = theme('table', $header, $rows, array('id' => 'blocks')); |
| 342 |
$output .= form_render($form['submit']); |
$output .= form_render($form['submit']); |
| 343 |
$output .= form_render($form); |
// Also render the form_id as there is no form_render($form) call (as form_render does not appear to handle the |
| 344 |
|
// multi-dimensional block form array very well). |
| 345 |
|
$output .= form_render($form['form_id']); |
| 346 |
|
|
| 347 |
return $output; |
return $output; |
| 348 |
} |
} |
| 349 |
|
|
| 366 |
$form['block_settings'] = array( |
$form['block_settings'] = array( |
| 367 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 368 |
'#title' => t('Block specific settings'), |
'#title' => t('Block specific settings'), |
| 369 |
'#collapsible' => true, |
'#collapsible' => TRUE, |
| 370 |
); |
); |
| 371 |
|
|
| 372 |
foreach ($settings as $k => $v) { |
foreach ($settings as $k => $v) { |
| 383 |
$form['user_vis_settings'] = array( |
$form['user_vis_settings'] = array( |
| 384 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 385 |
'#title' => t('User specific visibility settings'), |
'#title' => t('User specific visibility settings'), |
| 386 |
'#collapsible' => true, |
'#collapsible' => TRUE, |
| 387 |
); |
); |
| 388 |
$form['user_vis_settings']['custom'] = array( |
$form['user_vis_settings']['custom'] = array( |
| 389 |
'#type' => 'radios', |
'#type' => 'radios', |
| 390 |
'#title' => t('Custom visibility settings'), |
'#title' => t('Custom visibility settings'), |
| 391 |
'#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.')), |
'#options' => array( |
| 392 |
|
t('Users cannot control whether or not they see this block.'), |
| 393 |
|
t('Show this block by default, but let individual users hide it.'), |
| 394 |
|
t('Hide this block by default but let individual users show it.') |
| 395 |
|
), |
| 396 |
'#description' => t('Allow individual users to customize the visibility of this block in their account settings.'), |
'#description' => t('Allow individual users to customize the visibility of this block in their account settings.'), |
| 397 |
'#default_value' => $edit['custom'], |
'#default_value' => $edit['custom'], |
| 398 |
); |
); |
| 399 |
|
|
| 400 |
|
// Role-based visibility settings |
| 401 |
|
$default_role_options = array(); |
| 402 |
|
$result = db_query("SELECT rid FROM {blocks_roles} WHERE module = '%s' AND delta = '%s'", $module, $delta); |
| 403 |
|
while ($role = db_fetch_object($result)) { |
| 404 |
|
$default_role_options[] = $role->rid; |
| 405 |
|
} |
| 406 |
|
$result = db_query('SELECT rid, name FROM {role} ORDER BY name'); |
| 407 |
|
$role_options = array(); |
| 408 |
|
while ($role = db_fetch_object($result)) { |
| 409 |
|
$role_options[$role->rid] = $role->name; |
| 410 |
|
} |
| 411 |
|
$form['role_vis_settings'] = array( |
| 412 |
|
'#type' => 'fieldset', |
| 413 |
|
'#title' => t('Role specific visibility settings'), |
| 414 |
|
'#collapsible' => TRUE, |
| 415 |
|
); |
| 416 |
|
$form['role_vis_settings']['roles'] = array( |
| 417 |
|
'#type' => 'checkboxes', |
| 418 |
|
'#title' => t('Show block for specific roles'), |
| 419 |
|
'#default_value' => $default_role_options, |
| 420 |
|
'#options' => $role_options, |
| 421 |
|
'#description' => t('Show this block only for the selected role(s). If you select no roles, the block will be visible to all users.'), |
| 422 |
|
); |
| 423 |
|
|
| 424 |
$form['page_vis_settings'] = array( |
$form['page_vis_settings'] = array( |
| 425 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 426 |
'#title' => t('Page specific visibility settings'), |
'#title' => t('Page specific visibility settings'), |
| 427 |
'#collapsible' => true, |
'#collapsible' => TRUE, |
| 428 |
); |
); |
| 429 |
$access = user_access('use PHP for block visibility'); |
$access = user_access('use PHP for block visibility'); |
| 430 |
|
|
| 473 |
|
|
| 474 |
function block_admin_configure_submit($form_id, $form_values) { |
function block_admin_configure_submit($form_id, $form_values) { |
| 475 |
if (!form_get_errors()) { |
if (!form_get_errors()) { |
| 476 |
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']); |
db_query("UPDATE {blocks} SET visibility = %d, pages = '%s', custom = %d WHERE module = '%s' AND delta = '%s'", $form_values['visibility'], $form_values['pages'], $form_values['custom'], $form_values['module'], $form_values['delta']); |
| 477 |
|
db_query("DELETE FROM {blocks_roles} WHERE module = '%s' AND delta = '%s'", $form_values['module'], $form_values['delta']); |
| 478 |
|
foreach (array_filter($form_values['roles']) as $rid) { |
| 479 |
|
db_query("INSERT INTO {blocks_roles} (rid, module, delta) VALUES (%d, '%s', '%s')", $rid, $form_values['module'], $form_values['delta']); |
| 480 |
|
} |
| 481 |
module_invoke($form_values['module'], 'block', 'save', $form_values['delta'], $form_values); |
module_invoke($form_values['module'], 'block', 'save', $form_values['delta'], $form_values); |
| 482 |
drupal_set_message(t('The block configuration has been saved.')); |
drupal_set_message(t('The block configuration has been saved.')); |
| 483 |
cache_clear_all(); |
cache_clear_all(); |
| 526 |
*/ |
*/ |
| 527 |
function block_box_delete_confirm_submit($form_id, $form_values) { |
function block_box_delete_confirm_submit($form_id, $form_values) { |
| 528 |
db_query('DELETE FROM {boxes} WHERE bid = %d', $form_values['bid']); |
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']); |
|
| 529 |
drupal_set_message(t('The block %name has been removed.', array('%name' => theme('placeholder', $form_values['info'])))); |
drupal_set_message(t('The block %name has been removed.', array('%name' => theme('placeholder', $form_values['info'])))); |
| 530 |
cache_clear_all(); |
cache_clear_all(); |
| 531 |
return 'admin/block'; |
return 'admin/block'; |
| 574 |
else { |
else { |
| 575 |
db_query("INSERT INTO {boxes} (title, body, info, format) VALUES ('%s', '%s', '%s', %d)", $edit['title'], $edit['body'], $edit['info'], $edit['format']); |
db_query("INSERT INTO {boxes} (title, body, info, format) VALUES ('%s', '%s', '%s', %d)", $edit['title'], $edit['body'], $edit['info'], $edit['format']); |
| 576 |
} |
} |
| 577 |
return true; |
return TRUE; |
| 578 |
} |
} |
| 579 |
|
|
| 580 |
/** |
/** |
| 584 |
* the site. |
* the site. |
| 585 |
*/ |
*/ |
| 586 |
function block_user($type, $edit, &$user, $category = NULL) { |
function block_user($type, $edit, &$user, $category = NULL) { |
| 587 |
|
global $user; |
| 588 |
switch ($type) { |
switch ($type) { |
| 589 |
case 'form': |
case 'form': |
| 590 |
if ($category == 'account') { |
if ($category == 'account') { |
| 591 |
$result = db_query('SELECT * FROM {blocks} WHERE status = 1 AND custom != 0 ORDER BY weight, module, delta'); |
$result = db_query("SELECT DISTINCT b.* FROM {blocks} b LEFT JOIN {blocks_roles} r ON b.module = r.module AND b.delta = r.delta WHERE b.status = 1 AND b.custom != 0 AND (r.rid IN (%s) OR r.rid IS NULL) ORDER BY b.weight, b.module", implode(',', array_keys($user->roles))); |
| 592 |
$form['block'] = array('#type' => 'fieldset', '#title' => t('Block configuration'), '#weight' => 3, '#collapsible' => TRUE, '#tree' => TRUE); |
$form['block'] = array('#type' => 'fieldset', '#title' => t('Block configuration'), '#weight' => 3, '#collapsible' => TRUE, '#tree' => TRUE); |
| 593 |
while ($block = db_fetch_object($result)) { |
while ($block = db_fetch_object($result)) { |
| 594 |
$data = module_invoke($block->module, 'block', 'list'); |
$data = module_invoke($block->module, 'block', 'list'); |
| 635 |
static $blocks = array(); |
static $blocks = array(); |
| 636 |
|
|
| 637 |
if (!count($blocks)) { |
if (!count($blocks)) { |
| 638 |
$result = db_query("SELECT * FROM {blocks} WHERE theme = '%s' AND status = 1 ORDER BY region, weight, module", $theme_key); |
$result = db_query("SELECT DISTINCT b.* FROM {blocks} b LEFT JOIN {blocks_roles} r ON b.module = r.module AND b.delta = r.delta WHERE b.theme = '%s' AND b.status = 1 AND (r.rid IN (%s) OR r.rid IS NULL) ORDER BY b.region, b.weight, b.module", $theme_key, implode(',', array_keys($user->roles))); |
| 639 |
while ($block = db_fetch_object($result)) { |
while ($block = db_fetch_object($result)) { |
| 640 |
if (!isset($blocks[$block->region])) { |
if (!isset($blocks[$block->region])) { |
| 641 |
$blocks[$block->region] = array(); |
$blocks[$block->region] = array(); |
| 658 |
if ($block->visibility < 2) { |
if ($block->visibility < 2) { |
| 659 |
$path = drupal_get_path_alias($_GET['q']); |
$path = drupal_get_path_alias($_GET['q']); |
| 660 |
$regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($block->pages, '/')) .')$/'; |
$regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($block->pages, '/')) .')$/'; |
| 661 |
// Compare with the internal and path alias (if any). |
$page_match = !($block->visibility xor preg_match($regexp, $path)); |
|
$page_match = preg_match($regexp, $path); |
|
|
if ($path != $_GET['q']) { |
|
|
$page_match = $page_match || preg_match($regexp, $_GET['q']); |
|
|
} |
|
|
// When $block->visibility has a value of 0, the block is displayed on |
|
|
// all pages except those listed in $block->pages. When set to 1, it |
|
|
// is displayed only on those pages listed in $block->pages. |
|
|
$page_match = !($block->visibility xor $page_match); |
|
| 662 |
} |
} |
| 663 |
else { |
else { |
| 664 |
$page_match = drupal_eval($block->pages); |
$page_match = drupal_eval($block->pages); |
| 691 |
} |
} |
| 692 |
return $blocks[$region]; |
return $blocks[$region]; |
| 693 |
} |
} |
|
|
|
|
|
|