Merge pull request #302 from JorisDebonnet/patch-2

Sort BlogPosts in the CMS by PublishDate
This commit is contained in:
Damian Mooyman 2015-10-23 17:35:56 +13:00
commit 6321417bc4
2 changed files with 17 additions and 12 deletions

View File

@ -71,10 +71,10 @@ class BlogFilter extends Lumberjack {
$excluded = $this->owner->getExcludedSiteTreeClassNames(); $excluded = $this->owner->getExcludedSiteTreeClassNames();
if(!empty($excluded)) { if(!empty($excluded)) {
$pages = SiteTree::get()->filter(array( $pages = BlogPost::get()->filter(array(
'ParentID' => $this->owner->ID, 'ParentID' => $this->owner->ID,
'ClassName' => $excluded 'ClassName' => $excluded
))->sort('"SiteTree"."Created" DESC'); ));
$gridField = new BlogFilter_GridField( $gridField = new BlogFilter_GridField(
'ChildPages', 'ChildPages',

View File

@ -84,9 +84,11 @@ class BlogPost extends Page {
private static $allowed_children = array(); private static $allowed_children = array();
/** /**
* The default sorting lists BlogPosts with an empty PublishDate at the top.
*
* @var string * @var string
*/ */
private static $default_sort = 'PublishDate DESC'; private static $default_sort = '"PublishDate" IS NULL DESC, "PublishDate" DESC';
/** /**
* @var bool * @var bool
@ -211,8 +213,10 @@ class BlogPost extends Page {
_t('BlogPost.AdditionalCredits', 'Additional Credits'), _t('BlogPost.AdditionalCredits', 'Additional Credits'),
null, null,
1024 1024
)->setDescription(_t(
'BlogPost.PublishDate_Description',
'If some authors of this post don\'t have CMS access, enter their name(s) here. You can separate multiple names with a comma.')
); );
$authorNames->setDescription('If some authors of this post don\'t have CMS access, enter their name(s) here. You can separate multiple names with a comma.');
if(!$self->canEditAuthors()) { if(!$self->canEditAuthors()) {
$authorField = $authorField->performDisabledTransformation(); $authorField = $authorField->performDisabledTransformation();
@ -221,6 +225,12 @@ class BlogPost extends Page {
$publishDate = DatetimeField::create('PublishDate', _t('BlogPost.PublishDate', 'Publish Date')); $publishDate = DatetimeField::create('PublishDate', _t('BlogPost.PublishDate', 'Publish Date'));
$publishDate->getDateField()->setConfig('showcalendar', true); $publishDate->getDateField()->setConfig('showcalendar', true);
if(!$self->PublishDate) {
$publishDate->setDescription(_t(
'BlogPost.PublishDate_Description',
'Will be set to "now" if published without a value.')
);
}
// Get categories and tags // Get categories and tags
$parent = $self->Parent(); $parent = $self->Parent();
@ -365,8 +375,7 @@ class BlogPost extends Page {
/** /**
* {@inheritdoc} * {@inheritdoc}
* *
* Update the PublishDate to now, if being published for the first time, and the date hasn't * Update the PublishDate to now if the BlogPost would otherwise be published without a date.
* been set to the future.
*/ */
public function onBeforePublish() { public function onBeforePublish() {
/** /**
@ -374,7 +383,7 @@ class BlogPost extends Page {
*/ */
$publishDate = $this->dbObject('PublishDate'); $publishDate = $this->dbObject('PublishDate');
if($publishDate->InPast() && !$this->isPublished()) { if(!$publishDate->getValue()) {
$this->PublishDate = SS_Datetime::now()->getValue(); $this->PublishDate = SS_Datetime::now()->getValue();
$this->write(); $this->write();
} }
@ -625,10 +634,6 @@ class BlogPost extends Page {
protected function onBeforeWrite() { protected function onBeforeWrite() {
parent::onBeforeWrite(); parent::onBeforeWrite();
if(!$this->PublishDate) {
$this->PublishDate = SS_Datetime::now()->getValue();
}
if(!$this->exists() && ($member = Member::currentUser())) { if(!$this->exists() && ($member = Member::currentUser())) {
$this->Authors()->add($member); $this->Authors()->add($member);
} }