/
GridFieldBlogPostState.php
100 lines (86 loc) · 3.14 KB
/
GridFieldBlogPostState.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace SilverStripe\Blog\Forms\GridField;
use SilverStripe\Blog\Model\BlogPost;
use SilverStripe\Lumberjack\Forms\GridFieldSiteTreeState;
use SilverStripe\ORM\FieldType\DBDatetime;
/**
* 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()) {
$modifiedLabel = '<span class="modified">' . _t(__CLASS__ . '.Modified', 'Modified') . '</span>';
}
if (!$record->isPublished()) {
/**
* @var DBDatetime $lastEdited
*/
$lastEdited = $record->dbObject('LastEdited');
return '<i class="font-icon-edit mr-2"></i> ' . _t(
__CLASS__ . '.Draft',
'Saved as Draft on {date}',
'State for when a post is saved.',
[
'date' => $lastEdited->FormatFromSettings(),
]
);
}
/**
* @var DBDatetime $publishDate
*/
$publishDate = $record->dbObject('PublishDate');
if (strtotime($record->PublishDate ?? '') > time()) {
return '<i class="font-icon-clock mr-2"></i> ' . _t(
__CLASS__ . '.Timer',
'Publish at {date}',
'State for when a post is published.',
[
'date' => $publishDate->FormatFromSettings(),
]
) . $modifiedLabel;
}
return '<i class="font-icon-check-mark-circle text-success mr-2"></i> ' . _t(
__CLASS__ . '.Published',
'Published on {date}',
'State for when a post is published.',
[
'date' => $publishDate->FormatFromSettings(),
]
) . $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';
}
return [
'class' => $class,
];
}
}
return [];
}
}