2015-11-21 07:17:29 +01:00
|
|
|
<?php
|
|
|
|
|
2016-12-15 04:41:49 +01:00
|
|
|
namespace SilverStripe\Blog\Widgets;
|
|
|
|
|
2017-01-18 05:12:06 +01:00
|
|
|
if (!class_exists('\\SilverStripe\\Widgets\\Model\\Widget')) {
|
2015-11-21 07:17:29 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-18 05:12:06 +01:00
|
|
|
use SilverStripe\Blog\Model\Blog;
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Forms\DropdownField;
|
|
|
|
use SilverStripe\Forms\NumericField;
|
|
|
|
use SilverStripe\Widgets\Model\Widget;
|
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2017-09-14 00:59:01 +02:00
|
|
|
private static $db = [
|
2015-11-21 07:17:29 +01:00
|
|
|
'NumberToDisplay' => 'Int',
|
|
|
|
'ArchiveType' => 'Enum(\'Monthly,Yearly\', \'Monthly\')',
|
2017-09-14 00:59:01 +02:00
|
|
|
];
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-09-14 00:59:01 +02:00
|
|
|
private static $defaults = [
|
2015-11-21 07:17:29 +01:00
|
|
|
'NumberOfMonths' => 12,
|
2017-09-14 00:59:01 +02:00
|
|
|
];
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-09-14 00:59:01 +02:00
|
|
|
private static $has_one = [
|
2017-01-18 05:12:06 +01:00
|
|
|
'Blog' => Blog::class,
|
2017-09-14 00:59:01 +02:00
|
|
|
];
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getCMSFields()
|
|
|
|
{
|
2017-01-18 05:12:06 +01:00
|
|
|
$this->beforeUpdateCMSFields(function ($fields) {
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* @var Enum $archiveType
|
|
|
|
*/
|
2017-01-18 05:12:06 +01:00
|
|
|
$archiveType = $this->dbObject('ArchiveType');
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
$type = $archiveType->enumValues();
|
|
|
|
|
|
|
|
foreach ($type as $k => $v) {
|
2017-09-14 00:27:40 +02:00
|
|
|
$type[$k] = _t(__CLASS__ .'.' . ucfirst(strtolower($v)), $v);
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var FieldList $fields
|
|
|
|
*/
|
2017-09-14 00:59:01 +02:00
|
|
|
$fields->merge([
|
2017-01-18 05:12:06 +01:00
|
|
|
DropdownField::create(
|
|
|
|
'BlogID',
|
2017-09-14 00:27:40 +02:00
|
|
|
_t(__CLASS__ . '.Blog', 'Blog'),
|
2017-01-18 05:12:06 +01:00
|
|
|
Blog::get()->map()
|
|
|
|
),
|
2017-09-14 00:27:40 +02:00
|
|
|
DropdownField::create('ArchiveType', _t(__CLASS__ . '.ArchiveType', 'ArchiveType'), $type),
|
|
|
|
NumericField::create('NumberToDisplay', _t(__CLASS__ . '.NumberToDisplay', 'No. to Display'))
|
2017-09-14 00:59:01 +02:00
|
|
|
]);
|
2015-11-21 07:17:29 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return parent::getCMSFields();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of months where blog posts are present.
|
|
|
|
*
|
|
|
|
* @return DataList
|
|
|
|
*/
|
|
|
|
public function getArchive()
|
|
|
|
{
|
|
|
|
$query = $this->Blog()->getBlogPosts()->dataQuery();
|
|
|
|
|
|
|
|
if ($this->ArchiveType == 'Yearly') {
|
|
|
|
$query->groupBy('DATE_FORMAT("PublishDate", \'%Y\')');
|
|
|
|
} else {
|
|
|
|
$query->groupBy('DATE_FORMAT("PublishDate", \'%Y-%M\')');
|
|
|
|
}
|
|
|
|
|
|
|
|
$posts = $this->Blog()->getBlogPosts()->setDataQuery($query);
|
|
|
|
|
|
|
|
if ($this->NumberToDisplay > 0) {
|
|
|
|
$posts = $posts->limit($this->NumberToDisplay);
|
|
|
|
}
|
|
|
|
|
2017-09-14 01:49:27 +02:00
|
|
|
$archive = ArrayList::create();
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
if ($posts->count() > 0) {
|
|
|
|
foreach ($posts as $post) {
|
|
|
|
/**
|
|
|
|
* @var BlogPost $post
|
|
|
|
*/
|
|
|
|
$date = Date::create();
|
|
|
|
$date->setValue($post->PublishDate);
|
|
|
|
|
|
|
|
if ($this->ArchiveType == 'Yearly') {
|
2016-07-15 14:31:11 +02:00
|
|
|
$year = $date->Format("Y");
|
2015-11-21 07:17:29 +01:00
|
|
|
$month = null;
|
|
|
|
$title = $year;
|
|
|
|
} else {
|
2016-07-15 14:31:11 +02:00
|
|
|
$year = $date->Format("Y");
|
|
|
|
$month = $date->Format("m");
|
2015-11-21 07:17:29 +01:00
|
|
|
$title = $date->FormatI18N("%B %Y");
|
|
|
|
}
|
|
|
|
|
2017-09-14 01:49:27 +02:00
|
|
|
$archive->push(ArrayData::create([
|
2015-11-21 07:17:29 +01:00
|
|
|
'Title' => $title,
|
|
|
|
'Link' => Controller::join_links($this->Blog()->Link('archive'), $year, $month)
|
2017-09-14 00:59:01 +02:00
|
|
|
]));
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $archive;
|
|
|
|
}
|
|
|
|
}
|