2007-09-07 00:33:58 +02:00
|
|
|
<?php
|
2008-12-10 07:58:28 +01:00
|
|
|
/**
|
|
|
|
* 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 {
|
2009-01-20 05:01:45 +01:00
|
|
|
|
2007-09-07 00:33:58 +02:00
|
|
|
static $db = array(
|
2008-12-10 07:58:28 +01:00
|
|
|
'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(
|
2008-12-10 07:58:28 +01:00
|
|
|
'DisplayMode' => 'month'
|
2007-09-07 00:33:58 +02:00
|
|
|
);
|
|
|
|
|
2008-12-10 07:58:28 +01: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
|
|
|
|
2008-12-10 07:58:28 +01:00
|
|
|
function getCMSFields() {
|
2007-09-07 00:33:58 +02:00
|
|
|
return new FieldSet(
|
2008-12-10 07:58:28 +01:00
|
|
|
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() {
|
2008-12-10 07:58:28 +01:00
|
|
|
Requirements::themedCSS('archivewidget');
|
|
|
|
|
2007-09-07 00:33:58 +02:00
|
|
|
$results = new DataObjectSet();
|
2009-05-01 00:05:54 +02:00
|
|
|
$container = BlogTree::current();
|
|
|
|
$ids = $container->BlogHolderIDs();
|
2007-09-07 00:33:58 +02:00
|
|
|
|
2010-03-11 02:28:37 +01:00
|
|
|
if(empty($ids)) return $results;
|
|
|
|
|
2008-12-16 05:35:28 +01:00
|
|
|
$stage = Versioned::current_stage();
|
|
|
|
$suffix = (!$stage || $stage == 'Stage') ? "" : "_$stage";
|
|
|
|
|
2008-12-10 07:58:28 +01:00
|
|
|
if($this->DisplayMode == 'month') {
|
2009-10-26 23:03:52 +01:00
|
|
|
if(defined('DB::USE_ANSI_SQL')) {
|
2009-05-11 04:48:24 +02:00
|
|
|
$sqlResults = DB::query("
|
2009-08-05 05:38:23 +02:00
|
|
|
SELECT DISTINCT MONTH(\"Date\") AS \"Month\", YEAR(\"Date\") AS \"Year\"
|
2009-05-11 04:48:24 +02:00
|
|
|
FROM \"SiteTree$suffix\" INNER JOIN \"BlogEntry$suffix\" ON \"SiteTree$suffix\".\"ID\" = \"BlogEntry$suffix\".\"ID\"
|
|
|
|
WHERE \"ParentID\" IN (" . implode(', ', $ids) . ")
|
2009-08-05 05:38:23 +02:00
|
|
|
ORDER BY \"Year\" DESC, \"Month\" DESC;"
|
2009-05-11 04:48:24 +02:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$sqlResults = DB::query("
|
|
|
|
SELECT DISTINCT MONTH(`Date`) AS `Month`, YEAR(`Date`) AS `Year`
|
|
|
|
FROM `SiteTree$suffix` NATURAL JOIN `BlogEntry$suffix`
|
|
|
|
WHERE `ParentID` IN (" . implode(', ', $ids) . ")
|
2009-08-05 05:38:23 +02:00
|
|
|
ORDER BY `Year` DESC, `Month` DESC;"
|
2009-05-11 04:48:24 +02:00
|
|
|
);
|
|
|
|
}
|
2008-12-10 07:58:28 +01:00
|
|
|
} else {
|
2009-10-26 23:03:52 +01:00
|
|
|
if(defined('DB::USE_ANSI_SQL')) {
|
2009-05-11 04:48:24 +02:00
|
|
|
$sqlResults = DB::query("
|
2009-08-05 05:38:23 +02:00
|
|
|
SELECT DISTINCT YEAR(\"Date\") AS \"Year\"
|
2009-05-11 04:48:24 +02:00
|
|
|
FROM \"SiteTree$suffix\" INNER JOIN \"BlogEntry$suffix\" ON \"SiteTree$suffix\".\"ID\" = \"BlogEntry$suffix\".\"ID\"
|
|
|
|
WHERE \"ParentID\" IN (" . implode(', ', $ids) . ")
|
2009-08-05 05:38:23 +02:00
|
|
|
ORDER BY \"Year\" DESC"
|
2009-05-11 04:48:24 +02:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$sqlResults = DB::query("
|
|
|
|
SELECT DISTINCT YEAR(`Date`) AS `Year`
|
|
|
|
FROM `SiteTree$suffix` NATURAL JOIN `BlogEntry$suffix`
|
|
|
|
WHERE `ParentID` in (".implode(', ',$ids).")
|
2009-08-05 05:38:23 +02:00
|
|
|
ORDER BY `Year` DESC"
|
2009-05-11 04:48:24 +02:00
|
|
|
);
|
|
|
|
}
|
2007-09-07 00:33:58 +02:00
|
|
|
}
|
|
|
|
|
2009-07-27 04:52:42 +02:00
|
|
|
if($sqlResults) foreach($sqlResults as $sqlResult) {
|
|
|
|
$isMonthDisplay = $this->DisplayMode == 'month';
|
|
|
|
|
2009-08-04 04:18:38 +02:00
|
|
|
$monthVal = (isset($sqlResult['Month'])) ? (int) $sqlResult['Month'] : 1;
|
2009-07-27 04:52:42 +02:00
|
|
|
$month = ($isMonthDisplay) ? $monthVal : 1;
|
|
|
|
$year = ($sqlResult['Year']) ? (int) $sqlResult['Year'] : date('Y');
|
2007-09-07 00:33:58 +02:00
|
|
|
|
2009-07-27 04:52:42 +02:00
|
|
|
$date = DBField::create('Date', array(
|
2008-12-10 07:58:28 +01:00
|
|
|
'Day' => 1,
|
2009-07-27 04:52:42 +02:00
|
|
|
'Month' => $month,
|
|
|
|
'Year' => $year
|
2007-09-07 00:33:58 +02:00
|
|
|
));
|
|
|
|
|
2009-07-27 04:52:42 +02:00
|
|
|
if($isMonthDisplay) {
|
2009-10-29 04:15:01 +01:00
|
|
|
$link = $container->Link($sqlResult['Year']) . '/' . sprintf("%'02d", $monthVal);
|
2008-12-10 07:58:28 +01:00
|
|
|
} else {
|
2009-10-29 04:15:01 +01:00
|
|
|
$link = $container->Link($sqlResult['Year']);
|
2007-09-07 00:33:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$results->push(new ArrayData(array(
|
2008-12-10 07:58:28 +01:00
|
|
|
'Date' => $date,
|
|
|
|
'Link' => $link
|
2007-09-07 00:33:58 +02:00
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $results;
|
|
|
|
}
|
2009-07-27 04:52:42 +02:00
|
|
|
|
2009-10-29 04:15:01 +01:00
|
|
|
}
|