function homebox_load_blocks_in_regions in Homebox 6
Loads available blocks for user
Return value
An array of regions/columns containing blocks ordered by region / weight (see home-box.tpl.php)
1 call to homebox_load_blocks_in_regions()
- homebox_build in ./
homebox.module - Responsible for firing the hook_theme()
File
- ./
homebox.module, line 230 - Home box main file, takes care of global functions settings constants, etc.
Code
function homebox_load_blocks_in_regions($pid) {
global $user;
$regions = array();
$column_count = variable_get('homebox_column_count_' . $pid, 3);
$blocks = _homebox_db_fetch_all_blocks(db_query("SELECT bid, region, weight, status, open\n FROM {homebox_default}\n WHERE pid = %d ORDER BY region ASC, weight ASC", $pid));
// Retrieve customization from user
// and adds new blocks to the user table
if ($user->uid != 0) {
$blocks = _homebox_apply_user_settings($blocks, $pid);
}
else {
$message = t("Please be sure to <a href='!login_url'>login</a> on our site to have your preferences saved. If you do not have an account please <a href='!register_url'>register</a>, and then you will be able to save your preferences.", array(
'!login_url' => url('user/login'),
'!register_url' => url('user/register'),
));
drupal_set_message($message, $type = 'warning', $repeat = FALSE);
}
$is_cache_enabled = (bool) variable_get('homebox_cache_enabled_' . $pid, 0);
// Preparing blocks object for theming
foreach ($blocks as $key => $block_settings) {
// Retrieve information from blocks Drupal table
$block = db_query_range("SELECT * FROM {blocks} WHERE bid = %d", array(
$block_settings['bid'],
), $from = 0, $to = 1);
$block = db_fetch_object($block);
// Check for permissions set at the block level
$allowed_roles = db_query("SELECT rid FROM {blocks_roles} WHERE module = '%s' AND delta = '%s'", $block->module, $block->delta);
// We need to first grant access, because no record means no role restriction
$can_access_block = TRUE;
$stop = FALSE;
while (($role = db_fetch_object($allowed_roles)) && $stop == FALSE && $user->uid != 1) {
// We found at least one record, so we become pessimistic
$can_access_block = FALSE;
if (isset($user->roles[$role->rid])) {
// User has permission to view this block
$can_access_block = TRUE;
// We don't need to continue
$stop = TRUE;
}
}
// If the user can't access this block
// stop here and go to the next block
if ($can_access_block == FALSE) {
continue;
}
// Block created with Views, check access
$can_access_view = FALSE;
if ($block->module == 'views') {
$can_access_view = _homebox_check_views_block_access($block);
if (!$can_access_view) {
// Skip end of the foreach()
// start a new iteration on next block
continue;
}
}
// Attempt to find a block in the cache table
if ($is_cache_enabled == TRUE && !count(module_implements('node_grants')) && $_SERVER['REQUEST_METHOD'] == 'GET' && ($cid = _block_get_cache_id($block)) && ($cache = cache_get($cid, 'cache_block'))) {
$array = $cache->data;
}
else {
$array = module_invoke($block->module, 'block', 'view', $block->delta);
if (isset($cid)) {
cache_set($cid, $array, 'cache_block', CACHE_TEMPORARY);
}
}
// Render block content
if (isset($array) && is_array($array)) {
foreach ($array as $k => $v) {
$block->{$k} = $v;
}
}
// We don't continue to assign this block
// since Drupal didn't returned any content
// which could be permissions rules applied by any module
if ($block->content && trim($block->content) != '' || $can_access_view) {
// If no title provided we try to get one from blocks table
if (!$block->subject) {
$block->subject = db_result(db_query("SELECT title FROM {blocks} WHERE bid = %d", $block->bid));
}
if (!$block->subject && $block->module == 'views') {
$block->subject = _homebox_get_view_name($block);
}
if (!$block->subject) {
$module_blocks = module_invoke($block->module, 'block', 'list');
$block->subject = $module_blocks[$block->delta]['info'];
}
// Fail safe
if (!$block->subject) {
// Is this still necessary?
$block->subject = t('<em>No title defined</em>');
}
// Mostly ugly for now, but we need this for performance issue when saving blocks ordering this prevent many queries!
$block->content .= "<input type='hidden' class='homebox' value='" . $block->bid . "' />";
$block->content .= "<input type='hidden' class='pid' value='" . $pid . "' />";
// Override core region parameter with homebox one
$block->region = (int) $block_settings['region'];
// Override core weight parameter with homebox one
$block->weight = (int) $block_settings['weight'];
// Homebox specific paramters
$block->status = isset($block_settings['status']) ? (bool) $block_settings['status'] : TRUE;
$block->open = isset($block_settings['open']) ? (bool) $block_settings['open'] : TRUE;
$block->homebox_classes = _homebox_get_css_classes_for_block($block_settings, $pid);
// If user defined region is greate than
// real column count put block in the last column/region
if ($block->region > $column_count) {
$block->region = $column_count;
}
// Adds block to its regions
$regions[$block->region][$block->weight][] = $block;
}
}
// Sort each region/column based on key value
for ($i = 1; $i <= count($regions); $i++) {
if (is_array($regions[$i]) && count($regions[$i]) > 0) {
ksort($regions[$i]);
}
}
// Fill region array to match defined column count
for ($i = 1; $i <= $column_count; $i++) {
if (!isset($regions[$i])) {
$regions[$i] = array();
}
}
// Sorts region/column
ksort($regions);
return $regions;
}