silverstripe-blog/code/widgets/BlogArchiveWidget.php

136 lines
2.8 KiB
PHP
Raw Normal View History

2013-08-04 18:38:26 +02:00
<?php
2015-05-09 16:33:12 +02:00
if(!class_exists('Widget')) {
return;
}
2013-08-04 18:38:26 +02:00
2015-05-09 16:33:12 +02: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
*/
private static $db = array(
'NumberToDisplay' => 'Int',
2015-05-13 23:12:48 +02:00
'ArchiveType' => 'Enum(\'Monthly,Yearly\', \'Monthly\')',
2015-05-09 16:33:12 +02:00
);
/**
* @var array
*/
private static $defaults = array(
'NumberOfMonths' => 12,
);
/**
* @var array
*/
private static $has_one = array(
'Blog' => 'Blog',
);
/**
* {@inheritdoc}
*/
public function getCMSFields() {
2015-05-13 23:12:48 +02:00
$self =& $this;
2015-05-09 16:33:12 +02:00
$this->beforeUpdateCMSFields(function ($fields) use ($self) {
2015-05-13 23:12:48 +02:00
/**
* @var Enum $archiveType
*/
$archiveType = $self->dbObject('ArchiveType');
$type = $archiveType->enumValues();
2015-05-09 16:33:12 +02:00
foreach($type as $k => $v) {
$type[$k] = _t('BlogArchiveWidget.' . ucfirst(strtolower($v)), $v);
}
2013-08-04 18:38:26 +02:00
2015-05-13 23:12:48 +02:00
/**
* @var FieldList $fields
*/
2015-05-09 16:33:12 +02:00
$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'))
));
});
2013-08-04 18:38:26 +02:00
2015-05-09 16:33:12 +02:00
return parent::getCMSFields();
}
2013-08-04 18:38:26 +02:00
2015-05-09 16:33:12 +02:00
/**
* Returns a list of months where blog posts are present.
*
* @return DataList
*/
public function getArchive() {
$query = $this->Blog()->getBlogPosts()->dataQuery();
if($this->ArchiveType == 'Yearly') {
2015-05-14 01:11:50 +02:00
$query->groupBy('DATE_FORMAT("PublishDate", \'%Y\')');
2015-05-09 16:33:12 +02:00
} else {
2015-05-14 01:11:50 +02:00
$query->groupBy('DATE_FORMAT("PublishDate", \'%Y-%M\')');
2013-10-10 00:09:28 +02:00
}
2013-08-04 18:38:26 +02:00
2015-05-09 16:33:12 +02:00
$posts = $this->Blog()->getBlogPosts()->setDataQuery($query);
2013-08-04 18:38:26 +02:00
2015-05-09 16:33:12 +02:00
if($this->NumberToDisplay > 0) {
$posts = $posts->limit($this->NumberToDisplay);
}
2013-10-10 00:09:28 +02:00
2015-05-09 16:33:12 +02:00
$archive = new ArrayList();
if($posts->count() > 0) {
foreach($posts as $post) {
/**
* @var BlogPost $post
*/
$date = new Date();
$date->setValue($post->PublishDate);
2015-05-09 16:33:12 +02:00
if($this->ArchiveType == 'Yearly') {
$year = $date->FormatI18N("%Y");
2015-05-09 16:33:12 +02:00
$month = null;
$title = $year;
} else {
$year = $date->FormatI18N("%Y");
$month = $date->FormatI18N("%m");
$title = $date->FormatI18N("%B %Y");
2013-08-04 18:38:26 +02:00
}
2015-05-09 16:33:12 +02:00
$archive->push(new ArrayData(array(
'Title' => $title,
'Link' => Controller::join_links($this->Blog()->Link('archive'), $year, $month)
)));
2013-08-04 18:38:26 +02:00
}
}
2013-10-10 00:09:28 +02:00
2015-05-09 16:33:12 +02:00
return $archive;
2013-08-04 18:38:26 +02:00
}
2015-05-09 16:33:12 +02:00
}
2013-08-04 18:38:26 +02:00
2015-05-09 16:33:12 +02:00
class BlogArchiveWidget_Controller extends Widget_Controller {
2013-08-04 18:38:26 +02:00
2013-10-10 00:09:28 +02:00
}