<?php

/**
 * Snippet to read and draw an RSS feed.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package clearFusionCMS\snippet\rssfeed
 * @copyright Copyright (c) 2012 - 2014 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

/* @var $tplEngine cfTemplateEngine */

if(isset($feed)) {
	$items = isset($items) ? $items : 5;
	$class = isset($class) ? $class : 'rssfeed';
	$cacheMin = isset($cacheMin) ? $cacheMin : null;
	$deltaTime = isset($deltaTime) && $deltaTime == '1' ? true : false;
	$wrapperTpl = isset($wrapperTpl) ? $tplEngine->getChunk($wrapperTpl) : '<ul class="[[+class]]">[[+items]]</ul>';
	$itemTpl = isset($itemTpl) ? $tplEngine->getChunk($itemTpl) : '<li><a href="[[+link]]">[[+title]]</a><p>[[+description]] <span class="published">[[+date]]</span></p></li>';

	$client = new flRssClient(clearFusionCMS::getCache());
	if($channel = $client->fetch($feed, $items, false, $cacheMin)) {
		if(count($channel)) {
			$html = '';
			foreach($channel as $item) {
				$date = new flDate($item->pubDate, clearFusionCMS::getSysTimezone());
				if($deltaTime) {
					$d = new flDate();
					$date = $d->timeSpan($date);
					$date = "$date[offset] $date[units] ago";
				}
				else
					$date = $date->toString();

				$html .= $tplEngine->parseString($itemTpl, array(
					'link' => $item->link,
					'title' => fusionLib::htmlChars($item->title),
					'description' => $item->description,
					'date' => $date
				));
			}
			echo $tplEngine->parseString($wrapperTpl, array(
				'class' => $class,
				'items' => $html
			));
		}
	}
}

?>