silverstripe-blog/code/ArchiveWidget.php

89 lines
2.2 KiB
PHP
Raw Normal View History

2007-09-07 00:33:58 +02:00
<?php
/**
* Shows a widget with viewing blog entries
* by months or years.
*
* @package blog
*/
2007-09-07 00:33:58 +02:00
class ArchiveWidget extends Widget {
static $db = array(
'DisplayMode' => 'Varchar'
2007-09-07 00:33:58 +02:00
);
static $defaults = array(
'DisplayMode' => 'month'
2007-09-07 00:33:58 +02:00
);
static $title = 'Browse by Date';
static $cmsTitle = 'Blog Archive';
static $description = 'Show a list of months or years in which there are blog posts, and provide links to them.';
2007-09-07 00:33:58 +02:00
function getBlogHolder() {
$page = Director::currentPage();
if($page instanceof BlogHolder) {
2007-09-07 00:33:58 +02:00
return $page;
} elseif(($page instanceof BlogEntry) && ($page->getParent() instanceof BlogHolder)) {
2007-09-07 00:33:58 +02:00
return $page->getParent();
} else {
return DataObject::get_one('BlogHolder');
2007-09-07 00:33:58 +02:00
}
}
function getCMSFields() {
2007-09-07 00:33:58 +02:00
return new FieldSet(
new OptionsetField(
'DisplayMode',
_t('ArchiveWidget.DispBY', 'Display by'),
array(
'month' => _t('ArchiveWidget.MONTH', 'month'),
'year' => _t('ArchiveWidget.YEAR', 'year')
)
)
2007-09-07 00:33:58 +02:00
);
}
function Dates() {
Requirements::themedCSS('archivewidget');
2007-09-07 00:33:58 +02:00
$results = new DataObjectSet();
$blogHolder = $this->getBlogHolder();
$id = $blogHolder->ID;
if($this->DisplayMode == 'month') {
2007-09-07 00:33:58 +02:00
$sqlResults = DB::query("SELECT DISTINCT MONTH(`Date`) AS `Month`, YEAR(`Date`) AS `Year` FROM `SiteTree` NATURAL JOIN `BlogEntry` WHERE `ParentID` = $id ORDER BY `Date` DESC");
} else {
2007-09-07 00:33:58 +02:00
$sqlResults = DB::query("SELECT DISTINCT YEAR(`Date`) AS `Year` FROM `SiteTree` NATURAL JOIN `BlogEntry` WHERE `ParentID` = $id ORDER BY `Date` DESC");
}
if(!$sqlResults) return new DataObjectSet();
2007-09-07 00:33:58 +02:00
foreach($sqlResults as $sqlResult) {
$date = new Date('Date');
$month = ($this->DisplayMode == 'month') ? (int)$sqlResult['Month'] : 1;
2007-09-07 00:33:58 +02:00
$date->setValue(array(
'Day' => 1,
'Month' => $month,
'Year' => (int) $sqlResult['Year']
2007-09-07 00:33:58 +02:00
));
if($this->DisplayMode == 'month') {
2007-09-07 00:33:58 +02:00
$link = $blogHolder->Link() . $sqlResult['Year']. '/' . sprintf("%'02d", $sqlResult['Month']);
} else {
2007-09-07 00:33:58 +02:00
$link = $blogHolder->Link() . $sqlResult['Year'];
}
$results->push(new ArrayData(array(
'Date' => $date,
'Link' => $link
2007-09-07 00:33:58 +02:00
)));
}
return $results;
}
}
?>