MINOR Tidy up of ArchiveWidget

ENHANCEMENT Use of instanceof instead of is_a() so BlogHolder and BlogEntry subclasses still work properly
This commit is contained in:
Sean Harvey 2008-12-10 06:58:28 +00:00
parent 4b6a22f6a3
commit 3e8754e50d

View File

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