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

103 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\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(ModuleLoader::getModule('silverstripe/blog')->getRelativeResourcePath('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 SS_Datetime $lastEdited
*/
$lastEdited = $record->dbObject('LastEdited');
return '<i class="btn-icon gridfield-icon btn-icon-pencil"></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 SS_Datetime $publishDate
*/
$publishDate = $record->dbObject('PublishDate');
if (strtotime($record->PublishDate) > time()) {
return '<i class="gridfield-icon blog-icon-timer"></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="btn-icon gridfield-icon btn-icon-accept"></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
}
}