silverstripe-blog/src/Forms/GridField/GridFieldBlogPostState.php

101 lines
3.1 KiB
PHP
Raw Normal View History

2015-11-21 07:17:29 +01:00
<?php
namespace SilverStripe\Blog\Forms\GridField;
use SilverStripe\Blog\Model\BlogPost;
use SilverStripe\Lumberjack\Forms\GridFieldSiteTreeState;
use SilverStripe\ORM\FieldType\DBDatetime;
2015-11-21 07:17:29 +01:00
/**
* Provides a component to the {@link GridField} which tells the user whether or not a blog post
* has been published and when.
*
*/
class GridFieldBlogPostState extends GridFieldSiteTreeState
{
/**
* {@inheritdoc}
*/
public function getColumnContent($gridField, $record, $columnName)
{
if ($columnName == 'State') {
if ($record instanceof BlogPost) {
$modifiedLabel = '';
if ($record->isModifiedOnDraft()) {
2018-02-19 23:05:54 +01:00
$modifiedLabel = '<span class="modified">' . _t(__CLASS__ . '.Modified', 'Modified') . '</span>';
2015-11-21 07:17:29 +01:00
}
if (!$record->isPublished()) {
/**
* @var DBDatetime $lastEdited
2015-11-21 07:17:29 +01:00
*/
$lastEdited = $record->dbObject('LastEdited');
return '<i class="font-icon-edit mr-2"></i> ' . _t(
2017-09-14 00:27:40 +02:00
__CLASS__ . '.Draft',
'Saved as Draft on {date}',
2015-11-21 07:17:29 +01:00
'State for when a post is saved.',
2017-09-14 00:59:01 +02:00
[
'date' => $lastEdited->FormatFromSettings(),
2017-09-14 00:59:01 +02:00
]
2015-11-21 07:17:29 +01:00
);
}
/**
* @var DBDatetime $publishDate
2015-11-21 07:17:29 +01:00
*/
$publishDate = $record->dbObject('PublishDate');
2022-04-14 05:16:37 +02:00
if (strtotime($record->PublishDate ?? '') > time()) {
2018-01-24 20:46:05 +01:00
return '<i class="font-icon-clock mr-2"></i> ' . _t(
2017-09-14 00:59:01 +02:00
__CLASS__ . '.Timer',
'Publish at {date}',
2015-11-21 07:17:29 +01:00
'State for when a post is published.',
2017-09-14 00:59:01 +02:00
[
'date' => $publishDate->FormatFromSettings(),
2017-09-14 00:59:01 +02:00
]
2015-11-21 07:17:29 +01:00
) . $modifiedLabel;
}
return '<i class="font-icon-check-mark-circle text-success mr-2"></i> ' . _t(
2017-09-14 00:59:01 +02:00
__CLASS__ . '.Published',
'Published on {date}',
2015-11-21 07:17:29 +01:00
'State for when a post is published.',
2017-09-14 00:59:01 +02:00
[
'date' => $publishDate->FormatFromSettings(),
2017-09-14 00:59:01 +02:00
]
2015-11-21 07:17:29 +01:00
) . $modifiedLabel;
}
}
return '';
}
/**
* {@inheritdoc}
*/
public function getColumnAttributes($gridField, $record, $columnName)
{
if ($columnName == 'State') {
if ($record instanceof BlogPost) {
$published = $record->isPublished();
if (!$published) {
$class = 'gridfield-icon draft';
2022-04-14 05:16:37 +02:00
} elseif (strtotime($record->PublishDate ?? '') > time()) {
2015-11-21 07:17:29 +01:00
$class = 'gridfield-icon timer';
} else {
$class = 'gridfield-icon published';
}
2017-09-14 00:59:01 +02:00
return [
2015-11-21 07:17:29 +01:00
'class' => $class,
2017-09-14 00:59:01 +02:00
];
2015-11-21 07:17:29 +01:00
}
}
2017-09-14 00:59:01 +02:00
return [];
2015-11-21 07:17:29 +01:00
}
}