Skip to content
block.module 20.8 KiB
Newer Older
Dries Buytaert's avatar
 
Dries Buytaert committed
<?php
Dries Buytaert's avatar
Dries Buytaert committed

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * @file
 * Controls the boxes that are displayed around the main content.
 */

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * Implementation of hook_help().
 */
function block_help($section) {
Dries Buytaert's avatar
 
Dries Buytaert committed
  switch ($section) {
Dries Buytaert's avatar
 
Dries Buytaert committed
    case 'admin/help#block':
<p>Blocks are the boxes visible in the sidebar(s) of your web site and other regions. These are usually generated automatically by modules (e.g. recent forum topics), but you can also create your own blocks.</p>
<p>The region each block appears in depends on both which theme you are using (some are left-only, some right, some both, and some may offer other regions), 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>');
Dries Buytaert's avatar
 
Dries Buytaert committed
    case 'admin/modules#description':
Dries Buytaert's avatar
 
Dries Buytaert committed
      return t('Controls the boxes that are displayed around the main content.');
Dries Buytaert's avatar
 
Dries Buytaert committed
    case 'admin/block':
<p>Blocks are content rendered into regions, often 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 area of the page they will show up on (e.g., a sidebar) and in which order they appear (weight).  Highlighting on this page shows the regions where content will be rendered.</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>
", array('%throttle' => url('admin/settings/throttle')));
Dries Buytaert's avatar
 
Dries Buytaert committed
    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')));
Dries Buytaert's avatar
 
Dries Buytaert committed
  }
Dries Buytaert's avatar
 
Dries Buytaert committed
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * Implementation of hook_perm().
 */
Dries Buytaert's avatar
 
Dries Buytaert committed
function block_perm() {
Dries Buytaert's avatar
 
Dries Buytaert committed
  return array('administer blocks');
Dries Buytaert's avatar
 
Dries Buytaert committed
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
Dries Buytaert's avatar
 
Dries Buytaert committed
 * Implementation of hook_menu().
Dries Buytaert's avatar
 
Dries Buytaert committed
 */
Dries Buytaert's avatar
 
Dries Buytaert committed
function block_menu($may_cache) {
Dries Buytaert's avatar
 
Dries Buytaert committed
  $items = array();
Dries Buytaert's avatar
 
Dries Buytaert committed

  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'),
Dries Buytaert's avatar
 
Dries Buytaert committed
      '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',
Dries Buytaert's avatar
 
Dries Buytaert committed
      'type' => MENU_CALLBACK);
Dries Buytaert's avatar
 
Dries Buytaert committed
    $items[] = array('path' => 'admin/block/add', 'title' => t('add block'),
Dries Buytaert's avatar
 
Dries Buytaert committed
      'access' => user_access('administer blocks'),
Dries Buytaert's avatar
 
Dries Buytaert committed
      '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);
        }
      }
    }
Dries Buytaert's avatar
 
Dries Buytaert committed
  }

Dries Buytaert's avatar
 
Dries Buytaert committed
  return $items;
Dries Buytaert's avatar
 
Dries Buytaert committed
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * 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_markup($block->body, $block->format, FALSE);
Dries Buytaert's avatar
Dries Buytaert committed
function block_admin_save($edit) {
Dries Buytaert's avatar
 
Dries Buytaert committed
  foreach ($edit as $module => $blocks) {
    foreach ($blocks as $delta => $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'], $module, $delta, $block['theme']);
Dries Buytaert's avatar
 
Dries Buytaert committed
    }
Dries Buytaert's avatar
Dries Buytaert committed
  }
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
Dries Buytaert's avatar
 
Dries Buytaert committed
 * Update the 'blocks' DB table with the blocks currently exported by modules.
Dries Buytaert's avatar
 
Dries Buytaert committed
 *
Dries Buytaert's avatar
 
Dries Buytaert committed
 * @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.
Dries Buytaert's avatar
 
Dries Buytaert committed
 * @return
 *   Blocks currently exported by modules, sorted by $order_by.
Dries Buytaert's avatar
 
Dries Buytaert committed
 */
Dries Buytaert's avatar
 
Dries Buytaert committed
function _block_rehash($order_by = array('weight')) {
  global $theme_key;

  if (empty($theme_key)) {
    init_theme();
  }

  $result = db_query("SELECT * FROM {blocks} WHERE theme = '%s'", $theme_key);
Dries Buytaert's avatar
 
Dries Buytaert committed
  while ($old_block = db_fetch_object($result)) {
    $old_blocks[$old_block->module][$old_block->delta] = $old_block;
  }

  db_query("DELETE FROM {blocks} WHERE theme = '%s'", $theme_key);
Dries Buytaert's avatar
 
Dries Buytaert committed

  foreach (module_list() as $module) {
Dries Buytaert's avatar
 
Dries Buytaert committed
    $module_blocks = module_invoke($module, 'block', 'list');
Dries Buytaert's avatar
 
Dries Buytaert committed
    if ($module_blocks) {
      foreach ($module_blocks as $delta => $block) {
Dries Buytaert's avatar
 
Dries Buytaert committed
        $block['module'] = $module;
        $block['delta']  = $delta;
        // If previously written to database, load values.
Dries Buytaert's avatar
 
Dries Buytaert committed
        if ($old_blocks[$module][$delta]) {
Dries Buytaert's avatar
 
Dries Buytaert committed
          $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;
Dries Buytaert's avatar
 
Dries Buytaert committed
          $block['custom'] = $old_blocks[$module][$delta]->custom;
          $block['throttle'] = $old_blocks[$module][$delta]->throttle;
Dries Buytaert's avatar
 
Dries Buytaert committed
        }
        // Otherwise, use any set values, or else substitute defaults.
Dries Buytaert's avatar
 
Dries Buytaert committed
        else {
          $properties = array ('status' => 0, 'weight' => 0, 'region' => 'left', 'pages' => '', 'custom' => 0);
          foreach ($properties as $property => $default) {
            if (!isset ($block[$property])) {
              $block[$property] = $default;
            }
          }
Dries Buytaert's avatar
 
Dries Buytaert committed
        }

        // reinsert blocks into table
        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']);
Dries Buytaert's avatar
 
Dries Buytaert committed
        $blocks[] = $block;
Dries Buytaert's avatar
 
Dries Buytaert committed

Dries Buytaert's avatar
 
Dries Buytaert committed
        // build array to sort on
        $order[$order_by[0]][] = $block[$order_by[0]];
Dries Buytaert's avatar
 
Dries Buytaert committed
      }
    }
  }

Dries Buytaert's avatar
 
Dries Buytaert committed
  // sort
  array_multisort($order[$order_by[0]], $order_by[1] ? $order_by[1] : SORT_ASC, $order_by[2] ? $order_by[2] : SORT_REGULAR, $blocks);

Dries Buytaert's avatar
 
Dries Buytaert committed
  return $blocks;
}
Dries Buytaert's avatar
 
Dries Buytaert committed

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * Prepare the main block administration form.
 */
Dries Buytaert's avatar
 
Dries Buytaert committed
function block_admin_display() {
  global $theme_key, $custom_theme;

  // If non-default theme configuration has been selected, set the custom theme.
  if (arg(3)) {
    $custom_theme = arg(3);
    init_theme();
  }

Dries Buytaert's avatar
 
Dries Buytaert committed
  $blocks = _block_rehash();
Dries Buytaert's avatar
 
Dries Buytaert committed

  $block_regions = system_region_list($theme_key);

  // Highlight regions on page, to provide visual reference.
  foreach ($block_regions as $key => $value) {
    drupal_set_content($key, '<div class="block-region">' . $value . '</div>');
  }

  $header = array(t('Block'), t('Enabled'), t('Weight'), t('Placement'));
Dries Buytaert's avatar
 
Dries Buytaert committed
  if (module_exist('throttle')) {
    $header[] = t('Throttle');
  }
  $header[] = array('data' => t('Operations'), 'colspan' => 2);

  $regions = array();
Dries Buytaert's avatar
 
Dries Buytaert committed
  foreach ($blocks as $block) {
    if ($block['module'] == 'block') {
      $delete = l(t('delete'), 'admin/block/delete/'. $block['delta']);
Dries Buytaert's avatar
 
Dries Buytaert committed
    else {
Dries Buytaert's avatar
 
Dries Buytaert committed
    }
Dries Buytaert's avatar
 
Dries Buytaert committed

    $row = array(array('data' => $block['info'], 'class' => 'block'),
      form_checkbox(NULL, $block['module'] .']['. $block['delta'] .'][status', 1, $block['status']) . form_hidden($block['module'] .']['. $block['delta'] .'][theme', $theme_key),
      form_weight(NULL, $block['module'] .']['. $block['delta'] .'][weight', $block['weight']),
      form_select(NULL, $block['module'] .']['. $block['delta'] .'][region', isset($block['region']) ? $block['region'] : system_default_region(),
      $block_regions));
Dries Buytaert's avatar
 
Dries Buytaert committed

    if (module_exist('throttle')) {
      $row[] = form_checkbox(NULL, $block['module'] .']['. $block['delta'] .'][throttle', 1, $block['throttle']);
Dries Buytaert's avatar
 
Dries Buytaert committed
    }
    $row[] = l(t('configure'), 'admin/block/configure/'. $block['module'] .'/'. $block['delta']);
    $row[] = $delete;
      foreach ($block_regions as $key => $value) {
        if ($block['region'] == $key) {
          $regions[$key][] = $row;
        }
Dries Buytaert's avatar
 
Dries Buytaert committed
    else if ($block['region'] <= 1) {
Dries Buytaert's avatar
Dries Buytaert committed
  }
Dries Buytaert's avatar
 
Dries Buytaert committed


  if (count($regions)) {
    foreach ($regions as $region => $row) {
      $region_title = t('%region', array ('%region' => ucfirst($block_regions[$region])));
      $rows[] = array(array('data' => $region_title, 'class' => 'region', 'colspan' => (module_exist('throttle') ? 7 : 6)));
      $rows = array_merge($rows, $row);
    }
  }
  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'));
Dries Buytaert's avatar
 
Dries Buytaert committed
  $output .= form_submit(t('Save blocks'));
Dries Buytaert's avatar
Dries Buytaert committed

  return form($output, 'post', arg(3) ? url('admin/block/list/' . $theme_key) : url('admin/block'));
Dries Buytaert's avatar
Dries Buytaert committed
}

Dries Buytaert's avatar
 
Dries Buytaert committed
  return db_fetch_array(db_query('SELECT * FROM {boxes} WHERE bid = %d', $bid));
}

/**
 * Menu callback; displays the block configuration form.
Dries Buytaert's avatar
 
Dries Buytaert committed
 */
function block_admin_configure($module = NULL, $delta = 0) {
Dries Buytaert's avatar
 
Dries Buytaert committed
  $edit = $_POST['edit'];
  $op = $_POST['op'];

      db_query("UPDATE {blocks} SET visibility = %d, pages = '%s', custom = %d WHERE module = '%s' AND delta = '%s'", $edit['visibility'], $edit['pages'], $edit['custom'], $module, $delta);
      module_invoke($module, 'block', 'save', $delta, $edit);
      drupal_set_message(t('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 FROM {blocks} WHERE module = '%s' AND delta = '%s'", $module, $delta));
      }

      // Module-specific block configurations.
      if ($settings = module_invoke($module, 'block', 'configure', $delta)) {
Dries Buytaert's avatar
Dries Buytaert committed
        $form = form_group(t('Block specific settings'), $settings);
      }

      // 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'])));
      $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.'), t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).')));
      $group_2 .= form_textarea(t('Pages'), 'pages', $edit['pages'], 60, 5, t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are '%blog' for the blog page and %blog1 for every personal blog. %front is the front page. If the PHP-mode is chosen, enter PHP code between %php.  Note that executing incorrect PHP-code can break your Drupal site.", array('%blog' => theme('placeholder', 'blog'), '%blog1' =>  theme('placeholder', 'blog/*'), '%front' => theme('placeholder', '<front>'), '%php' => theme('placeholder', '<?php ?>'))));
      $form .= form_group(t('User specific visibility settings'), $group_1);
      $form .= form_group(t('Page specific visibility settings'), $group_2);
Dries Buytaert's avatar
 
Dries Buytaert committed
      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'):
Dries Buytaert's avatar
 
Dries Buytaert committed
      if (block_box_save($edit)) {
        drupal_set_message(t('The block has been created.'));
Dries Buytaert's avatar
 
Dries Buytaert committed
        drupal_goto('admin/block');
      }
      // deliberate no break
Dries Buytaert's avatar
 
Dries Buytaert committed
      $form = block_box_form($edit);
      $form .= form_submit(t('Save block'));
      $output .= form($form);
  }

Dries Buytaert's avatar
 
Dries Buytaert committed
  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 removed.', 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))),
Dries Buytaert's avatar
 
Dries Buytaert committed
  }

Dries Buytaert's avatar
 
Dries Buytaert committed
  return $output;
}

function block_box_form($edit = array()) {
  $output  = form_textfield(t('Block title'), 'title', $edit['title'], 60, 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'], 60, 15, t('The content of the block as shown to the user.'));
  $output .= form_textfield(t('Block description'), 'info', $edit['info'], 60, 64, t('A brief description of your block. Used on the <a href="%overview">block overview page</a>.', array('%overview' => url('admin/block'))), NULL, TRUE);
function block_box_save($edit, $delta = NULL) {
  if (!filter_access($edit['format'])) {
    $edit['format'] = FILTER_FORMAT_DEFAULT;
Dries Buytaert's avatar
 
Dries Buytaert committed
  }

  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);
Dries Buytaert's avatar
 
Dries Buytaert committed
    if (empty($edit['info']) || db_num_rows(db_query("SELECT info FROM {boxes} WHERE info = '%s'", $edit['info']))) {
      form_set_error('info', t('Please ensure that each block description is unique.'));
Dries Buytaert's avatar
 
Dries Buytaert committed
      return false;
    }
    db_query("INSERT INTO {boxes} (title, body, info, format) VALUES  ('%s', '%s', '%s', %d)", $edit['title'], $edit['body'], $edit['info'], $edit['format']);
Dries Buytaert's avatar
 
Dries Buytaert committed
  return true;
Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * Menu callback; displays the block overview page.
Dries Buytaert's avatar
 
Dries Buytaert committed
 */
Dries Buytaert's avatar
Dries Buytaert committed
function block_admin() {
Dries Buytaert's avatar
 
Dries Buytaert committed
  $edit = $_POST['edit'];
  $op = $_POST['op'];
Dries Buytaert's avatar
 
Dries Buytaert committed

Dries Buytaert's avatar
 
Dries Buytaert committed
  if ($op == t('Save blocks')) {
    block_admin_save($edit);
    drupal_set_message(t('The blocks have been saved.'));
Dries Buytaert's avatar
 
Dries Buytaert committed
    cache_clear_all();
Dries Buytaert's avatar
 
Dries Buytaert committed
  }
Dries Buytaert's avatar
 
Dries Buytaert committed
  return block_admin_display();
Dries Buytaert's avatar
Dries Buytaert committed
}
Dries Buytaert's avatar
 
Dries Buytaert committed

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * 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) {
      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));
        }
Dries Buytaert's avatar
 
Dries Buytaert committed

      break;
Dries Buytaert's avatar
 
Dries Buytaert committed
    case 'validate':
      if (!$edit['block']) {
        $edit['block'] = array();
/**
 * Return all blocks in the specified region for the current user.
 *
 * @param $region
 *   The name of a region.
 *
 * @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) {
  global $user, $theme_key;

  static $blocks = array();

  if (!isset($blocks[$region])) {
    $blocks[$region] = array();
    $result = db_query("SELECT * FROM {blocks} WHERE theme = '%s' AND status = 1 AND region = '%s' ORDER BY weight, module", $theme_key, $region);
Dries Buytaert's avatar
 
Dries Buytaert committed
    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;
        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 = !($block['visibility'] xor preg_match($regexp, $path));
        }
        else {
          $page_match = drupal_eval($block['pages']);
      if ($enabled && $page_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']);
Dries Buytaert's avatar
 
Dries Buytaert committed
          if (is_array($array)) {
Dries Buytaert's avatar
 
Dries Buytaert committed
        }
Dries Buytaert's avatar
 
Dries Buytaert committed
        if (isset($block['content']) && $block['content']) {
          $blocks[$region]["$block[module]_$block[delta]"] = (object) $block;
        }
      }
    }
  }
  return $blocks[$region];
}