You are here

function context_ui_block_list in Context 5

An alternative version of block_list() that provides any context_ui enabled blocks.

1 call to context_ui_block_list()
phptemplate_blocks in context_ui/context_ui.module
In order to add blocks we need to intercept theme_blocks and build the block content using an diffrent process. The current implementation assumes that the theme layer isn't going to define either 'phptemplate_blocks'…

File

context_ui/context_ui.module, line 558

Code

function context_ui_block_list($region) {
  global $user, $theme_key;
  static $cids = array();
  static $blocks = array();
  $defaults = array();
  if (!count($blocks)) {

    // generate list of active DB contexts
    // formerly an API function -- TODO: evaluate whether this may be useful otherwise
    $result = db_query("SELECT * FROM {context_ui} WHERE status = 1", 'context_ui');
    while ($context = db_fetch_object($result)) {
      if (context_get($context->namespace, $context->attribute) == $context->value) {
        $cids[$context->cid] = $context->cid;
      }
    }
    $rids = array_keys($user->roles);
    $placeholders = implode(',', array_fill(0, count($rids), '%d'));
    $result = db_query("\n      SELECT DISTINCT b.*, c.weight AS context_weight, c.region AS context_region, c.cid\n      FROM {blocks} b\n      LEFT JOIN {blocks_roles} r ON b.module = r.module AND b.delta = r.delta\n      LEFT JOIN {context_ui_block} c ON b.module = c.module AND b.delta = c.delta\n      WHERE b.theme = '%s' AND (r.rid IN ({$placeholders}) OR r.rid IS NULL)\n      ORDER BY b.region, b.weight, b.module", array_merge(array(
      $theme_key,
    ), $rids));
    while ($block = db_fetch_object($result)) {

      // we determine status as a combination of DB setting + context definition
      $status = FALSE;

      // prepare context blocks
      // if cid is in active contexts, use context weight + region
      if (isset($block->cid) && in_array($block->cid, $cids)) {
        $block->context_ui = TRUE;
        $block->region = $block->context_region ? $block->context_region : $block->region;
        $block->weight = $block->context_weight ? $block->context_weight : $block->weight;
        $status = TRUE;
      }
      else {
        $status = $block->status;
      }
      if ($status) {
        if (!isset($blocks[$block->region])) {
          $blocks[$block->region] = array();
        }
        $enabled = _context_ui_block_visibility('user', $block);
        $page_match = _context_ui_block_visibility('page', $block);
        $throttle = _context_ui_block_visibility('throttle', $block);
        if ($enabled && $page_match && $throttle) {

          // Invoke hook_block('view');
          $array = module_invoke($block->module, 'block', 'view', $block->delta);
          if (isset($array) && is_array($array) && $array['content'] && !empty($array['content'])) {
            foreach ($array as $k => $v) {
              $block->{$k} = $v;
            }

            // Override default block title if a custom display title is present.
            if ($block->title) {

              // Check plain here to allow module generated titles to keep any markup.
              $block->subject = $block->title == '<none>' ? '' : check_plain($block->title);
            }
            $blocks[$block->region]["{$block->module}_{$block->delta}"] = $block;
          }
        }
      }
    }

    // Custom sort since SQL order by won't give it to us for free
    foreach ($blocks as $key => $region_blocks) {
      uasort($region_blocks, '_context_ui_block_compare');
      $blocks[$key] = $region_blocks;
    }
  }

  // Create an empty array if there were no entries
  if (!isset($blocks[$region])) {
    $blocks[$region] = array();
  }
  return $blocks[$region];
}