Default PublishDate to NULL for drafts and sort them at the top

This commit is contained in:
JorisDebonnet 2015-10-14 00:01:51 +02:00 committed by Joris Debonnet
parent bb9beb3953
commit f311305efb
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();
} }
@ -414,7 +423,7 @@ class BlogPost extends Page {
if(!parent::canView($member)) { if(!parent::canView($member)) {
return false; return false;
} }
/** /**
* @var SS_Datetime $publishDate * @var SS_Datetime $publishDate
*/ */
@ -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);
} }