silverstripe-blog/code/widgets/ArchiveWidget.php

120 lines
3.1 KiB
PHP
Raw Normal View History

2012-05-21 04:58:26 +02:00
<?php
2013-04-11 12:35:22 +02:00
if(class_exists('Widget')) {
2018-02-20 14:31:31 +01:00
2013-04-11 12:35:22 +02:00
/**
* Shows a widget with viewing blog entries
* by months or years.
2018-02-20 14:24:43 +01:00
*
2013-04-11 12:35:22 +02:00
* @package blog
*/
class ArchiveWidget extends Widget {
2018-02-20 14:31:31 +01:00
private static $db = array(
2013-04-11 12:35:22 +02:00
'DisplayMode' => 'Varchar'
2012-05-21 04:58:26 +02:00
);
2018-02-20 14:24:43 +01:00
private static $defaults = array(
2013-04-11 12:35:22 +02:00
'DisplayMode' => 'month'
);
2018-02-20 14:24:43 +01:00
private static $title = 'Browse by Date';
2018-02-20 14:24:43 +01:00
private static $cmsTitle = 'Blog Archive';
2018-02-20 14:24:43 +01:00
private static $description =
'Show a list of months or years in which there are blog posts, and provide links to them.';
2018-02-20 14:24:43 +01:00
2013-04-11 12:35:22 +02:00
function getCMSFields() {
2018-02-20 14:31:31 +01:00
$fields = parent::getCMSFields();
2018-02-20 14:24:43 +01:00
2018-02-20 14:31:31 +01:00
$fields->merge(
2012-05-21 05:58:40 +02:00
2013-04-11 12:35:22 +02:00
new FieldList(
new OptionsetField(
'DisplayMode',
_t('ArchiveWidget.DispBY', 'Display by'),
array(
'month' => _t('ArchiveWidget.MONTH', 'month'),
'year' => _t('ArchiveWidget.YEAR', 'year')
)
2012-05-21 04:58:26 +02:00
)
)
2012-05-21 04:58:26 +02:00
);
2018-02-20 14:24:43 +01:00
2013-04-11 12:35:22 +02:00
$this->extend('updateCMSFields', $fields);
2018-02-20 14:24:43 +01:00
2013-04-11 12:35:22 +02:00
return $fields;
2012-05-21 04:58:26 +02:00
}
2018-02-20 14:24:43 +01:00
2013-04-11 12:35:22 +02:00
function getDates() {
Requirements::themedCSS('archivewidget');
2018-02-20 14:24:43 +01:00
2013-04-11 12:35:22 +02:00
$results = new ArrayList();
$container = BlogTree::current();
$ids = $container->BlogHolderIDs();
2018-02-20 14:24:43 +01:00
2013-04-11 12:35:22 +02:00
$stage = Versioned::current_stage();
$suffix = (!$stage || $stage == 'Stage') ? "" : "_$stage";
if(method_exists(DB::getConn(), 'formattedDatetimeClause')) {
$monthclause = DB::getConn()->formattedDatetimeClause('"Date"', '%m');
$yearclause = DB::getConn()->formattedDatetimeClause('"Date"', '%Y');
} else {
$monthclause = 'MONTH("Date")';
$yearclause = 'YEAR("Date")';
}
2013-04-11 12:35:22 +02:00
2014-05-12 10:19:55 +02:00
// Changed the WHERE clause from where ParentID to WHERE SiteTree$suffix.ParentID as it was ambiguous.
2018-02-20 14:24:43 +01:00
2013-04-11 12:35:22 +02:00
if($this->DisplayMode == 'month') {
$sqlResults = DB::query("
SELECT DISTINCT CAST($monthclause AS " . DB::getConn()->dbDataType('unsigned integer') . ")
2014-05-12 10:19:55 +02:00
AS \"Month\",
$yearclause AS \"Year\"
FROM \"SiteTree$suffix\" INNER JOIN \"BlogEntry$suffix\"
ON \"SiteTree$suffix\".\"ID\" = \"BlogEntry$suffix\".\"ID\"
WHERE \"SiteTree$suffix\".\"ParentID\" IN (" . implode(', ', $ids) . ")
ORDER BY \"Year\" DESC, \"Month\" DESC;"
2013-04-11 12:35:22 +02:00
);
2012-05-21 04:58:26 +02:00
} else {
2013-04-11 12:35:22 +02:00
$sqlResults = DB::query("
2014-05-12 10:19:55 +02:00
SELECT DISTINCT $yearclause AS \"Year\"
FROM \"SiteTree$suffix\" INNER JOIN \"BlogEntry$suffix\"
ON \"SiteTree$suffix\".\"ID\" = \"BlogEntry$suffix\".\"ID\"
WHERE \"SiteTree$suffix\".\"ParentID\" IN (" . implode(', ', $ids) . ")
ORDER BY \"Year\" DESC"
2013-04-11 12:35:22 +02:00
);
2012-05-21 04:58:26 +02:00
}
2018-02-20 14:24:43 +01:00
2013-04-11 12:35:22 +02:00
if($sqlResults) foreach($sqlResults as $sqlResult) {
$isMonthDisplay = $this->DisplayMode == 'month';
2018-02-20 14:24:43 +01:00
2013-04-11 12:35:22 +02:00
$monthVal = (isset($sqlResult['Month'])) ? (int) $sqlResult['Month'] : 1;
$month = ($isMonthDisplay) ? $monthVal : 1;
$year = ($sqlResult['Year']) ? (int) $sqlResult['Year'] : date('Y');
2018-02-20 14:24:43 +01:00
2013-04-11 12:35:22 +02:00
$date = DBField::create_field('Date', array(
'Day' => 1,
'Month' => $month,
'Year' => $year
));
2018-02-20 14:24:43 +01:00
2013-04-11 12:35:22 +02:00
if($isMonthDisplay) {
$link = $container->Link('date') . '/' . $sqlResult['Year'] . '/' . sprintf("%'02d", $monthVal);
} else {
$link = $container->Link('date') . '/' . $sqlResult['Year'];
}
2018-02-20 14:24:43 +01:00
2013-04-11 12:35:22 +02:00
$results->push(new ArrayData(array(
'Date' => $date,
'Link' => $link
)));
}
2018-02-20 14:24:43 +01:00
2013-04-11 12:35:22 +02:00
return $results;
2007-09-07 00:33:58 +02:00
}
2013-04-11 12:35:22 +02:00
}
2012-05-21 04:58:26 +02:00
}