2012-05-21 04:58:26 +02:00
|
|
|
<?php
|
2013-08-14 23:21:07 +02:00
|
|
|
|
|
|
|
if (class_exists('Widget')) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Presents a list of items from an RSS feed url
|
|
|
|
*
|
|
|
|
* @package blog
|
|
|
|
*/
|
2013-04-11 12:35:22 +02:00
|
|
|
class RSSWidget extends Widget {
|
2013-08-14 23:21:07 +02:00
|
|
|
|
|
|
|
private static $db = array(
|
2013-04-11 12:35:22 +02:00
|
|
|
"RSSTitle" => "Text",
|
|
|
|
"RssUrl" => "Text",
|
|
|
|
"NumberToShow" => "Int"
|
2012-05-21 04:58:26 +02:00
|
|
|
);
|
2013-08-14 23:21:07 +02:00
|
|
|
|
|
|
|
private static $defaults = array(
|
2013-04-11 12:35:22 +02:00
|
|
|
"NumberToShow" => 10,
|
|
|
|
"RSSTitle" => 'RSS Feed'
|
|
|
|
);
|
2013-08-14 23:21:07 +02:00
|
|
|
|
|
|
|
private static $cmsTitle = "RSS Feed";
|
|
|
|
|
|
|
|
private static $description = "Downloads another page's RSS feed and displays items in a list.";
|
|
|
|
|
2013-04-11 12:35:22 +02:00
|
|
|
/**
|
|
|
|
* If the RssUrl is relative, convert it to absolute with the
|
|
|
|
* current baseURL to avoid confusing simplepie.
|
|
|
|
* Passing relative URLs to simplepie will result
|
|
|
|
* in strange DNS lookups and request timeouts.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function getAbsoluteRssUrl() {
|
|
|
|
$urlParts = parse_url($this->RssUrl);
|
|
|
|
if(!isset($urlParts['host']) || !$urlParts['host']) {
|
|
|
|
return Director::absoluteBaseURL() . $this->RssUrl;
|
|
|
|
} else {
|
|
|
|
return $this->RssUrl;
|
|
|
|
}
|
2012-05-21 04:58:26 +02:00
|
|
|
}
|
|
|
|
|
2013-04-11 12:35:22 +02:00
|
|
|
function getCMSFields() {
|
|
|
|
$fields = parent::getCMSFields();
|
|
|
|
|
|
|
|
$fields->merge(
|
|
|
|
new FieldList(
|
|
|
|
new TextField("RSSTitle", _t('RSSWidget.CT', "Custom title for the feed")),
|
2013-08-14 23:21:07 +02:00
|
|
|
new TextField("RssUrl", _t(
|
|
|
|
'RSSWidget.URL',
|
|
|
|
"URL of the other page's RSS feed. Please make sure this URL points to an RSS feed."
|
|
|
|
)),
|
2013-04-11 12:35:22 +02:00
|
|
|
new NumericField("NumberToShow", _t('RSSWidget.NTS', "Number of Items to show"))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->extend('updateCMSFields', $fields);
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
2013-08-14 23:21:07 +02:00
|
|
|
|
2013-04-11 12:35:22 +02:00
|
|
|
function Title() {
|
2013-08-14 23:21:07 +02:00
|
|
|
return ($this->RSSTitle) ? $this->RSSTitle : _t('RSSWidget.DEFAULTTITLE', 'RSS Feed');
|
2013-04-11 12:35:22 +02:00
|
|
|
}
|
2012-05-21 04:58:26 +02:00
|
|
|
|
2013-04-11 12:35:22 +02:00
|
|
|
function getFeedItems() {
|
|
|
|
$output = new ArrayList();
|
|
|
|
|
|
|
|
// Protection against infinite loops when an RSS widget pointing to this page is added to this page
|
|
|
|
if(stristr($_SERVER['HTTP_USER_AGENT'], 'SimplePie')) {
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2013-07-01 10:00:08 +02:00
|
|
|
if(!class_exists('SimplePie')) {
|
|
|
|
throw new LogicException(
|
|
|
|
'Please install the "simplepie/simplepie" library by adding it to the "require" '
|
|
|
|
+ 'section of your composer.json'
|
|
|
|
);
|
|
|
|
}
|
2013-04-11 12:35:22 +02:00
|
|
|
|
|
|
|
$t1 = microtime(true);
|
2013-07-01 10:00:08 +02:00
|
|
|
$feed = new SimplePie();
|
|
|
|
$feed->set_feed_url($this->AbsoluteRssUrl);
|
|
|
|
$feed->set_cache_location(TEMP_FOLDER);
|
2013-04-11 12:35:22 +02:00
|
|
|
$feed->init();
|
|
|
|
if($items = $feed->get_items(0, $this->NumberToShow)) {
|
|
|
|
foreach($items as $item) {
|
|
|
|
|
|
|
|
// Cast the Date
|
|
|
|
$date = new Date('Date');
|
|
|
|
$date->setValue($item->get_date());
|
2012-05-21 04:58:26 +02:00
|
|
|
|
2013-04-11 12:35:22 +02:00
|
|
|
// Cast the Title
|
|
|
|
$title = new Text('Title');
|
2013-07-29 05:16:21 +02:00
|
|
|
$title->setValue(html_entity_decode($item->get_title()));
|
2012-05-21 04:58:26 +02:00
|
|
|
|
2013-04-11 12:35:22 +02:00
|
|
|
$output->push(new ArrayData(array(
|
|
|
|
'Title' => $title,
|
|
|
|
'Date' => $date,
|
|
|
|
'Link' => $item->get_link()
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
return $output;
|
2012-05-21 04:58:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-14 23:21:07 +02:00
|
|
|
}
|