<?php
// $Id: aggregator2_logo.module,v 1.3 2005/12/09 17:59:22 ahwayakchih Exp $

/*
 * Aggregator2 Logo filter
 *
 * @file
 * Adds Aggregator2 logo link to feed and it's items body.
 * @author ahwayakchih <ahwayakchih@gmail.com>
 */


/**
* Implementation of hook_help().
*/
function aggregator2_logo_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Adds logo image to feed and it\'s items body.');

    case 'admin/settings/aggregator2_logo':
      return '<p>' . t('If the %url include %image or %logo tag information,
      then this module will automatically add link with image (generated by aggregator2 module)
      to text content of feed and/or it\'s items.<br />Image link will be shown only if "Show full article/source site link" option of feed allows that.', array('%url' => l('RSS feeds you are aggregating', 'admin/aggregator2'), '%image' => theme('placeholder', '<image>'), '%logo' => theme('placeholder', '<logo>'))). '</p>';
  }
}

/**
* Implementation of hook_nodeapi().
*/
function aggregator2_logo_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  if ($node->type == 'aggregator2-item' && $op == 'load' && !$node->image) {
    // 'Cache' logos, so we don't run SQL query for each item when they have are from the same feed
    global $aggregator2_feed_logos;
    if (!$aggregator2_feed_logos[$node->fid]) {
      $aggregator2_feed_logos[$node->fid] = db_result(db_query('SELECT image FROM {aggregator2_feed} WHERE nid = %d', $node->fid));
    }
    $node->image = $aggregator2_feed_logos[$node->fid];
  }

  if (($node->type != 'aggregator2-item' && $node->type != 'aggregator2-feed') || $op != 'view' || !$node->image) {
    return;
  }

  if (($node->type == 'aggregator2-item' && !variable_get('aggregator2_logo_items', 0))
    || ($node->type == 'aggregator2-feed' && !variable_get('aggregator2_logo_feed', 0))) {
    return;
  }

  if (($node->item_show_link == AGGREGATOR2_SHOW_LINK_ALWAYS) ||
    ($teaser && $node->item_show_link == AGGREGATOR2_SHOW_LINK_TEASER_ONLY) ||
    (!$teaser && $node->item_show_link == AGGREGATOR2_SHOW_LINK_PAGE_ONLY)) {
      if ($teaser) {
        $node->teaser = theme('feed_logo', $node->image).$node->teaser;
      }
      else {
        $node->body = theme('feed_logo', $node->image).$node->body;
      }
  }
}

/**
 * Implementation of hook_settings().
 */
function aggregator2_logo_settings() {
  $output = '';

  $output .= form_checkbox(t('Add image to feed description'), 'aggregator2_logo_feed', 1, variable_get('aggregator2_logo_feed', 0), t('If enabled, feed nodes will be shown with feed logo link added to their description.'));
  $output .= form_checkbox(t('Add image to feed items description'), 'aggregator2_logo_items', 1, variable_get('aggregator2_logo_items', 0), t('If enabled, feed items will be shown with feed logo link added to their description.'));

  return $output;
}

/**
 * Theme feed logo/link.
 */
function theme_feed_logo(&$image) {
  return '<div class="feed_logo">'.$image.'</div>';
}
?>
