Same name and namespace in other branches
  1. 4.6.x modules/aggregator.module \aggregator_block()
  2. 4.7.x modules/aggregator.module \aggregator_block()
  3. 6.x modules/aggregator/aggregator.module \aggregator_block()

Implementation of hook_block().

Generates blocks for the latest news items in each category and feed.

File

modules/aggregator/aggregator.module, line 251
Used to aggregate syndicated content (RSS, RDF, and Atom).

Code

function aggregator_block($op = 'list', $delta = 0, $edit = array()) {
  if (user_access('access news feeds')) {
    if ($op == 'list') {
      $result = db_query('SELECT cid, title FROM {aggregator_category} ORDER BY title');
      while ($category = db_fetch_object($result)) {
        $block['category-' . $category->cid]['info'] = t('!title category latest items', array(
          '!title' => $category->title,
        ));
      }
      $result = db_query('SELECT fid, title FROM {aggregator_feed} ORDER BY fid');
      while ($feed = db_fetch_object($result)) {
        $block['feed-' . $feed->fid]['info'] = t('!title feed latest items', array(
          '!title' => $feed->title,
        ));
      }
    }
    else {
      if ($op == 'configure') {
        list($type, $id) = explode('-', $delta);
        if ($type == 'category') {
          $value = db_result(db_query('SELECT block FROM {aggregator_category} WHERE cid = %d', $id));
        }
        else {
          $value = db_result(db_query('SELECT block FROM {aggregator_feed} WHERE fid = %d', $id));
        }
        $form['block'] = array(
          '#type' => 'select',
          '#title' => t('Number of news items in block'),
          '#default_value' => $value,
          '#options' => drupal_map_assoc(array(
            2,
            3,
            4,
            5,
            6,
            7,
            8,
            9,
            10,
            11,
            12,
            13,
            14,
            15,
            16,
            17,
            18,
            19,
            20,
          )),
        );
        return $form;
      }
      else {
        if ($op == 'save') {
          list($type, $id) = explode('-', $delta);
          if ($type == 'category') {
            $value = db_query('UPDATE {aggregator_category} SET block = %d WHERE cid = %d', $edit['block'], $id);
          }
          else {
            $value = db_query('UPDATE {aggregator_feed} SET block = %d WHERE fid = %d', $edit['block'], $id);
          }
        }
        else {
          if ($op == 'view') {
            list($type, $id) = explode('-', $delta);
            switch ($type) {
              case 'feed':
                if ($feed = db_fetch_object(db_query('SELECT fid, title, block FROM {aggregator_feed} WHERE fid = %d', $id))) {
                  $block['subject'] = check_plain($feed->title);
                  $result = db_query_range('SELECT * FROM {aggregator_item} WHERE fid = %d ORDER BY timestamp DESC, iid DESC', $feed->fid, 0, $feed->block);
                  $read_more = '<div class="more-link">' . l(t('more'), 'aggregator/sources/' . $feed->fid, array(
                    'title' => t("View this feed's recent news."),
                  )) . '</div>';
                }
                break;
              case 'category':
                if ($category = db_fetch_object(db_query('SELECT cid, title, block FROM {aggregator_category} WHERE cid = %d', $id))) {
                  $block['subject'] = check_plain($category->title);
                  $result = db_query_range('SELECT i.* FROM {aggregator_category_item} ci LEFT JOIN {aggregator_item} i ON ci.iid = i.iid WHERE ci.cid = %d ORDER BY i.timestamp DESC, i.iid DESC', $category->cid, 0, $category->block);
                  $read_more = '<div class="more-link">' . l(t('more'), 'aggregator/categories/' . $category->cid, array(
                    'title' => t("View this category's recent news."),
                  )) . '</div>';
                }
                break;
            }
            $items = array();
            while ($item = db_fetch_object($result)) {
              $items[] = theme('aggregator_block_item', $item);
            }

            // Only display the block if there are items to show.
            if (count($items) > 0) {
              $block['content'] = theme('item_list', $items) . $read_more;
            }
          }
        }
      }
    }
    return $block;
  }
}