2015-11-21 07:17:29 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
if (!class_exists('Widget')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method Blog Blog()
|
|
|
|
*
|
|
|
|
* @property string $ArchiveType
|
|
|
|
* @property int $NumberToDisplay
|
|
|
|
*/
|
|
|
|
class BlogArchiveWidget extends Widget
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $title = 'Archive';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $cmsTitle = 'Archive';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $description = 'Displays an archive list of posts.';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $db = array(
|
|
|
|
'NumberToDisplay' => 'Int',
|
|
|
|
'ArchiveType' => 'Enum(\'Monthly,Yearly\', \'Monthly\')',
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $defaults = array(
|
|
|
|
'NumberOfMonths' => 12,
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $has_one = array(
|
|
|
|
'Blog' => 'Blog',
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getCMSFields()
|
|
|
|
{
|
|
|
|
$self =& $this;
|
|
|
|
|
|
|
|
$this->beforeUpdateCMSFields(function ($fields) use ($self) {
|
|
|
|
/**
|
|
|
|
* @var Enum $archiveType
|
|
|
|
*/
|
|
|
|
$archiveType = $self->dbObject('ArchiveType');
|
|
|
|
|
|
|
|
$type = $archiveType->enumValues();
|
|
|
|
|
|
|
|
foreach ($type as $k => $v) {
|
|
|
|
$type[$k] = _t('BlogArchiveWidget.' . ucfirst(strtolower($v)), $v);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var FieldList $fields
|
|
|
|
*/
|
|
|
|
$fields->merge(array(
|
|
|
|
DropdownField::create('BlogID', _t('BlogArchiveWidget.Blog', 'Blog'), Blog::get()->map()),
|
|
|
|
DropdownField::create('ArchiveType', _t('BlogArchiveWidget.ArchiveType', 'ArchiveType'), $type),
|
|
|
|
NumericField::create('NumberToDisplay', _t('BlogArchiveWidget.NumberToDisplay', 'No. to Display'))
|
|
|
|
));
|
|
|
|
});
|
|
|
|
|
|
|
|
return parent::getCMSFields();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of months where blog posts are present.
|
|
|
|
*
|
2017-06-17 09:20:29 +02:00
|
|
|
* @return ArrayList
|
2015-11-21 07:17:29 +01:00
|
|
|
*/
|
|
|
|
public function getArchive()
|
|
|
|
{
|
2017-06-17 09:20:29 +02:00
|
|
|
$format = ($this->ArchiveType == 'Yearly') ? '%Y' : '%Y-%m';
|
|
|
|
$publishDate = DB::get_conn()->formattedDatetimeClause('"PublishDate"', $format);
|
|
|
|
$fields = array(
|
|
|
|
'PublishDate' => $publishDate,
|
2017-09-26 06:17:53 +02:00
|
|
|
'Total' => "COUNT('\"PublishDate\"')"
|
2017-06-17 09:20:29 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$stage = Versioned::current_stage();
|
2017-09-26 05:15:57 +02:00
|
|
|
$suffix = ($stage === 'Live') ? '_Live' : '';
|
2017-09-26 06:17:53 +02:00
|
|
|
$query = SQLSelect::create($fields, '"BlogPost' . $suffix . '"')
|
2017-06-17 09:20:29 +02:00
|
|
|
->addGroupBy($publishDate)
|
2017-09-26 06:17:53 +02:00
|
|
|
->addOrderBy('"PublishDate" DESC')
|
|
|
|
->addWhere(array('"PublishDate" < ?' => SS_Datetime::now()->Format('Y-m-d')));
|
2017-06-17 09:20:29 +02:00
|
|
|
|
|
|
|
$posts = $query->execute();
|
|
|
|
$result = new ArrayList();
|
|
|
|
while ($next = $posts->next()) {
|
|
|
|
if ($this->ArchiveType == 'Yearly') {
|
2017-09-22 04:54:44 +02:00
|
|
|
$year = $next['PublishDate'];
|
2017-06-17 09:20:29 +02:00
|
|
|
$month = null;
|
|
|
|
$title = $year;
|
|
|
|
} else {
|
2017-09-22 04:54:44 +02:00
|
|
|
$date = Date::create();
|
|
|
|
$date->setValue(strtotime($next['PublishDate']));
|
|
|
|
|
|
|
|
$year = $date->Format('Y');
|
2017-06-17 09:20:29 +02:00
|
|
|
$month = $date->Format('m');
|
|
|
|
$title = $date->FormatI18N('%B %Y');
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
2017-06-17 09:20:29 +02:00
|
|
|
|
|
|
|
$result->push(new ArrayData(array(
|
|
|
|
'Title' => $title,
|
|
|
|
'Link' => Controller::join_links($this->Blog()->Link('archive'), $year, $month)
|
|
|
|
)));
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
|
2017-06-17 09:20:29 +02:00
|
|
|
$this->extend('updateGetArchive', $result);
|
2017-09-26 05:15:57 +02:00
|
|
|
|
2017-06-17 09:20:29 +02:00
|
|
|
return $result;
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class BlogArchiveWidget_Controller extends Widget_Controller
|
|
|
|
{
|
|
|
|
}
|