silverstripe-blog/src/Widgets/BlogArchiveWidget.php

160 lines
4.3 KiB
PHP
Raw Normal View History

2015-11-21 07:17:29 +01:00
<?php
namespace SilverStripe\Blog\Widgets;
use SilverStripe\Blog\Model\Blog;
use SilverStripe\Control\Controller;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldList;
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;
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;
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 = [
'Blog' => Blog::class,
2017-09-14 00:59:01 +02:00
];
2015-11-21 07:17:29 +01:00
/**
* @var string
*/
private static $table_name = 'BlogArchiveWidget';
2015-11-21 07:17:29 +01:00
/**
* {@inheritdoc}
*/
public function getCMSFields()
{
$this->beforeUpdateCMSFields(function ($fields) {
2015-11-21 07:17:29 +01:00
/**
* @var DBEnum $archiveType
2015-11-21 07:17:29 +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([
DropdownField::create(
'BlogID',
2017-09-14 00:27:40 +02:00
_t(__CLASS__ . '.Blog', 'Blog'),
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 ArrayList
2015-11-21 07:17:29 +01:00
*/
public function getArchive()
{
$format = ($this->ArchiveType == 'Yearly') ? '%Y' : '%Y-%m';
$publishDate = DB::get_conn()->formattedDatetimeClause('"PublishDate"', $format);
2017-09-14 07:11:45 +02:00
$fields = [
'PublishDate' => $publishDate,
'Total' => "COUNT('\"PublishDate\"')"
2017-09-14 07:11:45 +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 : '';
$query = SQLSelect::create($fields, '"BlogPost' . $suffix . '"')
->addGroupBy($publishDate)
->addOrderBy('"PublishDate" DESC')
->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,
]);
$posts = $query->execute();
2017-09-14 07:11:45 +02:00
$result = ArrayList::create();
foreach ($posts as $post) {
if ($this->ArchiveType == 'Yearly') {
$year = $post['PublishDate'];
$month = null;
$title = $year;
} else {
$date = DBDate::create();
2022-04-14 05:16:37 +02:00
$date->setValue(strtotime($post['PublishDate'] ?? ''));
$year = $date->Format('y');
$month = $date->Format('MM');
$title = $date->Format('MMMM y');
2015-11-21 07:17:29 +01:00
}
2017-09-14 07:11:45 +02:00
$result->push(ArrayData::create([
'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
}
$this->extend('updateGetArchive', $result);
2017-09-26 05:15:57 +02:00
return $result;
2015-11-21 07:17:29 +01:00
}
}