silverstripe-blog/code/widgets/ArchiveWidget.php

111 lines
3.0 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
);
2009-01-20 05:01:45 +01:00
static $has_one = array();
static $has_many = array();
static $many_many = array();
static $belongs_many_many = array();
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 getCMSFields() {
$fields = parent::getCMSFields();
$fields->merge(
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
);
$this->extend('updateCMSFields', $fields);
return $fields;
2007-09-07 00:33:58 +02:00
}
function Dates() {
Requirements::themedCSS('archivewidget');
2007-09-07 00:33:58 +02:00
$results = new DataObjectSet();
$container = BlogTree::current();
$ids = $container->BlogHolderIDs();
2007-09-07 00:33:58 +02:00
$stage = Versioned::current_stage();
$suffix = (!$stage || $stage == 'Stage') ? "" : "_$stage";
$monthclause = method_exists(DB::getConn(), 'formattedDatetimeClause') ? DB::getConn()->formattedDatetimeClause('"Date"', '%m') : 'MONTH("Date")';
$yearclause = method_exists(DB::getConn(), 'formattedDatetimeClause') ? DB::getConn()->formattedDatetimeClause('"Date"', '%Y') : 'YEAR("Date")';
if($this->DisplayMode == 'month') {
$sqlResults = DB::query("
SELECT DISTINCT CAST($monthclause AS " . DB::getConn()->dbDataType('unsigned integer') . ") AS \"Month\", $yearclause AS \"Year\"
FROM \"SiteTree$suffix\" INNER JOIN \"BlogEntry$suffix\" ON \"SiteTree$suffix\".\"ID\" = \"BlogEntry$suffix\".\"ID\"
WHERE \"ParentID\" IN (" . implode(', ', $ids) . ")
ORDER BY \"Year\" DESC, \"Month\" DESC;"
);
} else {
$sqlResults = DB::query("
2010-02-02 05:27:06 +01:00
SELECT DISTINCT $yearclause AS \"Year\"
FROM \"SiteTree$suffix\" INNER JOIN \"BlogEntry$suffix\" ON \"SiteTree$suffix\".\"ID\" = \"BlogEntry$suffix\".\"ID\"
WHERE \"ParentID\" IN (" . implode(', ', $ids) . ")
ORDER BY \"Year\" DESC"
);
2007-09-07 00:33:58 +02:00
}
if($sqlResults) foreach($sqlResults as $sqlResult) {
$isMonthDisplay = $this->DisplayMode == 'month';
$monthVal = (isset($sqlResult['Month'])) ? (int) $sqlResult['Month'] : 1;
$month = ($isMonthDisplay) ? $monthVal : 1;
$year = ($sqlResult['Year']) ? (int) $sqlResult['Year'] : date('Y');
2007-09-07 00:33:58 +02:00
$date = DBField::create('Date', array(
'Day' => 1,
'Month' => $month,
'Year' => $year
2007-09-07 00:33:58 +02:00
));
if($isMonthDisplay) {
$link = $container->Link('date') . '/' . $sqlResult['Year'] . '/' . sprintf("%'02d", $monthVal);
} else {
$link = $container->Link('date') . '/' . $sqlResult['Year'];
2007-09-07 00:33:58 +02:00
}
$results->push(new ArrayData(array(
'Date' => $date,
'Link' => $link
2007-09-07 00:33:58 +02:00
)));
}
return $results;
}
2009-10-29 04:15:01 +01:00
}
?>