You are here

function feedapi_aggregator_block in FeedAPI 5

Implementation of hook_block().

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

File

feedapi_aggregator/feedapi_aggregator.module, line 249

Code

function feedapi_aggregator_block($op = 'list', $delta = 0, $edit = array()) {
  if (user_access('access news feeds')) {
    if ($op == 'list') {
      $result = db_query('SELECT cid, title FROM {feedapi_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 nid FROM {feedapi} ORDER BY nid');
      while ($node = db_fetch_object($result)) {
        $node = node_load($node->nid);
        if (is_array($node->feed->processors)) {
          if (in_array('feedapi_aggregator', $node->feed->processors)) {
            $block['feed-' . $node->feed->nid]['info'] = t('!title feed latest items', array(
              '!title' => $node->title,
            ));
          }
        }
      }
    }
    else {
      if ($op == 'configure') {
        list($type, $id) = explode('-', $delta);
        if ($type == 'category') {
          $value = db_result(db_query('SELECT block FROM {feedapi_aggregator_category} WHERE cid = %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 {feedapi_aggregator_category} SET block = %d WHERE cid = %d', $edit['block'], $id);
          }
        }
        else {
          if ($op == 'view') {
            list($type, $id) = explode('-', $delta);
            switch ($type) {
              case 'feed':
                if ($node = node_load($id)) {
                  $block['subject'] = check_plain($node->title);
                  $elems_in_block = is_numeric($node->feed->settings['processors']['feedapi_aggregator']['block']) ? $node->feed->settings['processors']['feedapi_aggregator']['block'] : 3;
                  $result = db_query_range('SELECT * FROM {feedapi_aggregator_item} WHERE feed_nid = %d ORDER BY timestamp DESC, iid DESC', $node->nid, 0, $elems_in_block);
                  $read_more = '<div class="more-link">' . l(t('more'), 'aggregator/sources/' . $node->nid, 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 {feedapi_aggregator_category} WHERE cid = %d', $id))) {
                  $block['subject'] = check_plain($category->title);
                  $result = db_query_range('SELECT i.* FROM {feedapi_aggregator_category_item} ci LEFT JOIN {feedapi_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('feedapi_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;
  }
}