GridFieldBlogPostState   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 87
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getColumnContent() 0 55 6
A getColumnAttributes() 0 22 5
1
<?php
2
3
namespace SilverStripe\Blog\Forms\GridField;
4
5
use SilverStripe\Blog\Model\BlogPost;
6
use SilverStripe\Lumberjack\Forms\GridFieldSiteTreeState;
7
use SilverStripe\ORM\FieldType\DBDatetime;
8
9
/**
10
 * Provides a component to the {@link GridField} which tells the user whether or not a blog post
11
 * has been published and when.
12
 *
13
 */
14
class GridFieldBlogPostState extends GridFieldSiteTreeState
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function getColumnContent($gridField, $record, $columnName)
20
    {
21
        if ($columnName == 'State') {
22
            if ($record instanceof BlogPost) {
23
                $modifiedLabel = '';
24
25
                if ($record->isModifiedOnDraft()) {
26
                    $modifiedLabel = '<span class="modified">' . _t(__CLASS__ . '.Modified', 'Modified') . '</span>';
27
                }
28
29
                if (!$record->isPublished()) {
30
                    /**
31
                     * @var DBDatetime $lastEdited
32
                     */
33
                    $lastEdited = $record->dbObject('LastEdited');
34
35
                    return '<i class="font-icon-edit mr-2"></i> '  . _t(
36
                        __CLASS__ . '.Draft',
37
                        'Saved as Draft on {date}',
38
                        'State for when a post is saved.',
39
                        [
40
                            'date' => $lastEdited->FormatFromSettings(),
41
                        ]
42
                    );
43
                }
44
45
                /**
46
                 * @var DBDatetime $publishDate
47
                 */
48
                $publishDate = $record->dbObject('PublishDate');
49
50
                if (strtotime($record->PublishDate) > time()) {
51
                    return '<i class="font-icon-clock mr-2"></i> ' . _t(
52
                        __CLASS__ . '.Timer',
53
                        'Publish at {date}',
54
                        'State for when a post is published.',
55
                        [
56
                            'date' => $publishDate->FormatFromSettings(),
57
                        ]
58
                    ) . $modifiedLabel;
59
                }
60
61
                return '<i class="font-icon-check-mark-circle text-success mr-2"></i> ' . _t(
62
                    __CLASS__ . '.Published',
63
                    'Published on {date}',
64
                    'State for when a post is published.',
65
                    [
66
                        'date' => $publishDate->FormatFromSettings(),
67
                    ]
68
                ) . $modifiedLabel;
69
            }
70
        }
71
72
        return '';
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function getColumnAttributes($gridField, $record, $columnName)
79
    {
80
        if ($columnName == 'State') {
81
            if ($record instanceof BlogPost) {
82
                $published = $record->isPublished();
83
84
                if (!$published) {
85
                    $class = 'gridfield-icon draft';
86
                } elseif (strtotime($record->PublishDate) > time()) {
87
                    $class = 'gridfield-icon timer';
88
                } else {
89
                    $class = 'gridfield-icon published';
90
                }
91
92
                return [
93
                    'class' => $class,
94
                ];
95
            }
96
        }
97
98
        return [];
99
    }
100
}
101