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

104 lines
3.3 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\Core\Manifest\ModuleLoader;
use SilverStripe\Lumberjack\Forms\GridFieldSiteTreeState;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\View\Requirements;
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') {
Requirements::css('silverstripe/blog:css/cms.css');
2015-11-21 07:17:29 +01:00
if ($record instanceof BlogPost) {
$modifiedLabel = '';
if ($record->isModifiedOnStage) {
2017-09-14 00:27:40 +02:00
$modifiedLabel = '<span class="modified">' . _t(__CLASS__ . '.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');
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';
} elseif (strtotime($record->PublishDate) > time()) {
$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
}
}