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
|
|
|
use SilverStripe\Blog\Model\Blog;
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Forms\DropdownField;
|
2018-04-06 00:21:26 +02:00
|
|
|
use SilverStripe\Forms\FieldList;
|
2017-01-18 05:12:06 +01:00
|
|
|
use SilverStripe\Forms\NumericField;
|
2017-09-14 07:11:45 +02:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
|
|
|
use SilverStripe\ORM\DB;
|
|
|
|
use SilverStripe\ORM\FieldType\DBDate;
|
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
2018-04-06 00:21:26 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBEnum;
|
2017-09-14 07:11:45 +02:00
|
|
|
use SilverStripe\ORM\Queries\SQLSelect;
|
|
|
|
use SilverStripe\Versioned\Versioned;
|
|
|
|
use SilverStripe\View\ArrayData;
|
2017-01-18 05:12:06 +01:00
|
|
|
use SilverStripe\Widgets\Model\Widget;
|
|
|
|
|
2017-09-27 00:48:41 +02:00
|
|
|
if (!class_exists(Widget::class)) {
|
2015-11-21 07:17:29 +01:00
|
|
|
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
|
|
|
|
*/
|
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
|
|
|
|
2017-12-15 05:24:20 +01:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $table_name = 'BlogArchiveWidget';
|
|
|
|
|
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
|
|
|
/**
|
2018-04-06 00:21:26 +02:00
|
|
|
* @var DBEnum $archiveType
|
2015-11-21 07:17:29 +01:00
|
|
|
*/
|
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) {
|
2022-04-14 05:16:37 +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.
|
|
|
|
*
|
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);
|
2017-09-14 07:11:45 +02:00
|
|
|
$fields = [
|
2017-06-17 09:20:29 +02:00
|
|
|
'PublishDate' => $publishDate,
|
2017-09-26 06:17:53 +02:00
|
|
|
'Total' => "COUNT('\"PublishDate\"')"
|
2017-09-14 07:11:45 +02:00
|
|
|
];
|
2017-06-17 09:20:29 +02:00
|
|
|
|
2017-09-14 07:11:45 +02:00
|
|
|
$stage = Versioned::get_stage();
|
2017-09-27 00:48:41 +02:00
|
|
|
$suffix = ($stage === Versioned::LIVE) ? '_' . Versioned::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')
|
2018-06-26 05:47:47 +02:00
|
|
|
->addLeftJoin('SiteTree' . $suffix, '"SiteTree' . $suffix . '"."ID" = "BlogPost' . $suffix . '"."ID"')
|
|
|
|
->addWhere([
|
|
|
|
'"PublishDate" <= ?' => DBDatetime::now()->Format(DBDatetime::ISO_DATETIME),
|
2018-07-06 07:47:38 +02:00
|
|
|
'"SiteTree' . $suffix . '"."ParentID"' => $this->BlogID,
|
2018-06-26 05:47:47 +02:00
|
|
|
]);
|
2017-06-17 09:20:29 +02:00
|
|
|
|
|
|
|
$posts = $query->execute();
|
2017-09-14 07:11:45 +02:00
|
|
|
$result = ArrayList::create();
|
2017-09-28 04:08:30 +02:00
|
|
|
foreach ($posts as $post) {
|
2017-06-17 09:20:29 +02:00
|
|
|
if ($this->ArchiveType == 'Yearly') {
|
2017-09-28 04:08:30 +02:00
|
|
|
$year = $post['PublishDate'];
|
2017-06-17 09:20:29 +02:00
|
|
|
$month = null;
|
|
|
|
$title = $year;
|
|
|
|
} else {
|
2017-09-27 00:49:57 +02:00
|
|
|
$date = DBDate::create();
|
2022-04-14 05:16:37 +02:00
|
|
|
$date->setValue(strtotime($post['PublishDate'] ?? ''));
|
2017-09-22 04:54:44 +02:00
|
|
|
|
2017-09-27 00:49:57 +02:00
|
|
|
$year = $date->Format('y');
|
|
|
|
$month = $date->Format('MM');
|
|
|
|
$title = $date->Format('MMMM y');
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
2017-06-17 09:20:29 +02:00
|
|
|
|
2017-09-14 07:11:45 +02:00
|
|
|
$result->push(ArrayData::create([
|
2017-06-17 09:20:29 +02:00
|
|
|
'Title' => $title,
|
|
|
|
'Link' => Controller::join_links($this->Blog()->Link('archive'), $year, $month)
|
2017-09-14 07:11:45 +02:00
|
|
|
]));
|
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
|
|
|
}
|
|
|
|
}
|