Completed
Pull Request — master (#421)
by Robbie
03:13 queued 52s
created

GridFieldBlogPostState::getColumnAttributes()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 13
nc 5
nop 3
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\View\Requirements;
8
9
10
/**
11
 * Provides a component to the {@link GridField} which tells the user whether or not a blog post
12
 * has been published and when.
13
 *
14
 * @package silverstripe
15
 * @subpackage blog
16
 */
17
class GridFieldBlogPostState extends GridFieldSiteTreeState
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function getColumnContent($gridField, $record, $columnName)
23
    {
24
        if ($columnName == 'State') {
25
            Requirements::css(BLOGGER_DIR . '/css/cms.css');
26
            if ($record instanceof BlogPost) {
27
                $modifiedLabel = '';
28
29
                if ($record->isModifiedOnStage) {
30
                    $modifiedLabel = '<span class="modified">' . _t('GridFieldBlogPostState.Modified') . '</span>';
31
                }
32
33
                if (!$record->isPublished()) {
34
                    /**
35
                     * @var SS_Datetime $lastEdited
36
                     */
37
                    $lastEdited = $record->dbObject('LastEdited');
38
39
                    return _t(
40
                        'GridFieldBlogPostState.Draft',
41
                        '<i class="btn-icon gridfield-icon btn-icon-pencil"></i> Saved as Draft on {date}',
42
                        'State for when a post is saved.',
43
                        array(
44
                            'date' => $lastEdited->FormatFromSettings(),
45
                        )
46
                    );
47
                }
48
49
                /**
50
                 * @var SS_Datetime $publishDate
51
                 */
52
                $publishDate = $record->dbObject('PublishDate');
53
54
                if (strtotime($record->PublishDate) > time()) {
55
                    return _t(
56
                        'GridFieldBlogPostState.Timer',
57
                        '<i class="gridfield-icon blog-icon-timer"></i> Publish at {date}',
58
                        'State for when a post is published.',
59
                        array(
60
                            'date' => $publishDate->FormatFromSettings(),
61
                        )
62
                    ) . $modifiedLabel;
63
                }
64
65
                return _t(
66
                    'GridFieldBlogPostState.Published',
67
                    '<i class="btn-icon gridfield-icon btn-icon-accept"></i> Published on {date}',
68
                    'State for when a post is published.',
69
                    array(
70
                        'date' => $publishDate->FormatFromSettings(),
71
                    )
72
                ) . $modifiedLabel;
73
            }
74
        }
75
76
        return '';
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function getColumnAttributes($gridField, $record, $columnName)
83
    {
84
        if ($columnName == 'State') {
85
            if ($record instanceof BlogPost) {
86
                $published = $record->isPublished();
87
88
                if (!$published) {
89
                    $class = 'gridfield-icon draft';
90
                } elseif (strtotime($record->PublishDate) > time()) {
91
                    $class = 'gridfield-icon timer';
92
                } else {
93
                    $class = 'gridfield-icon published';
94
                }
95
96
                return array(
97
                    'class' => $class,
98
                );
99
            }
100
        }
101
102
        return array();
103
    }
104
}
105