function aggregator_block

6.x aggregator.module aggregator_block($op = 'list', $delta = 0, $edit = array())

Implementation of hook_block().

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

File

drupal-6.x/modules/aggregator/aggregator.module, line 300
Used to aggregate syndicated content (RSS, RDF, and Atom).

Code

function aggregator_block($op = 'list', $delta = 0, $edit = array()) {
  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') {
    if (user_access('access news feeds')) {
      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 = theme('more_link', url('aggregator/sources/' . $feed->fid), t("View this feed's recent news."));
          }
          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 = theme('more_link', url('aggregator/categories/' . $category->cid), t("View this category's recent news."));
          }
          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;
      }
    }
  }
  if (isset($block)) {
    return $block;
  }
}