2007-09-07 00:33:58 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class RSSWidget extends Widget {
|
|
|
|
static $db = array(
|
2008-02-25 09:48:33 +01:00
|
|
|
"RSSTitle" => "Text",
|
2007-09-07 00:33:58 +02:00
|
|
|
"RssUrl" => "Text",
|
2008-02-25 09:48:33 +01:00
|
|
|
"NumberToShow" => "Int"
|
2007-09-07 00:33:58 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
static $defaults = array(
|
2008-02-24 09:23:04 +01:00
|
|
|
"NumberToShow" => 10,
|
2008-02-25 09:48:33 +01:00
|
|
|
"RSSTitle" => 'RSS Feed'
|
2007-09-07 00:33:58 +02:00
|
|
|
);
|
|
|
|
static $cmsTitle = "RSS Feed";
|
|
|
|
static $description = "Shows the latest entries of a RSS feed.";
|
|
|
|
|
2008-06-13 01:39:47 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-07 00:33:58 +02:00
|
|
|
function getCMSFields() {
|
|
|
|
return new FieldSet(
|
2008-02-25 09:48:33 +01:00
|
|
|
new TextField("RSSTitle", _t('RSSWidget.CT', "Custom title for the feed")),
|
2007-12-18 02:41:51 +01:00
|
|
|
new TextField("RssUrl", _t('RSSWidget.URL', "URL of RSS Feed")),
|
|
|
|
new NumericField("NumberToShow", _t('RSSWidget.NTS', "Number of Items to show"))
|
2007-09-07 00:33:58 +02:00
|
|
|
);
|
|
|
|
}
|
2008-02-25 09:48:33 +01:00
|
|
|
function Title() {
|
|
|
|
return ($this->RSSTitle) ? $this->RSSTitle : 'RSS Feed';
|
|
|
|
}
|
2007-09-07 00:33:58 +02:00
|
|
|
|
|
|
|
function FeedItems() {
|
|
|
|
$output = new DataObjectSet();
|
2008-10-24 01:07:19 +02:00
|
|
|
|
2008-12-10 00:15:11 +01:00
|
|
|
include_once(Director::getAbsFile(SAPPHIRE_DIR . '/thirdparty/simplepie/SimplePie.php'));
|
|
|
|
|
2008-12-13 00:14:47 +01:00
|
|
|
$t1 = microtime(true);
|
|
|
|
$this->feed = new SimplePie($this->AbsoluteRssUrl, TEMP_FOLDER);
|
2008-12-10 00:15:11 +01:00
|
|
|
$this->feed->init();
|
|
|
|
if($items = $this->feed->get_items(0, $this->NumberToShow)) {
|
|
|
|
foreach($items as $item) {
|
2008-12-10 01:58:17 +01:00
|
|
|
|
|
|
|
// Cast the Date
|
|
|
|
$date = new Date('Date');
|
|
|
|
$date->setValue($item->get_date());
|
|
|
|
|
|
|
|
// Cast the Title
|
|
|
|
$title = new Text('Title');
|
|
|
|
$title->setValue($item->get_title());
|
|
|
|
|
2008-12-10 00:15:11 +01:00
|
|
|
$output->push(new ArrayData(array(
|
2008-12-10 01:58:17 +01:00
|
|
|
'Title' => $title,
|
|
|
|
'Date' => $date,
|
|
|
|
'Link' => $item->get_link()
|
2008-12-10 00:15:11 +01:00
|
|
|
)));
|
2007-09-11 08:38:05 +02:00
|
|
|
}
|
2008-12-10 00:15:11 +01:00
|
|
|
return $output;
|
2007-09-07 00:33:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|