CommentsGridFieldAction   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
c 1
b 0
f 0
dl 0
loc 110
rs 10
wmc 16

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getColumnMetadata() 0 4 2
A getActions() 0 3 1
A getColumnsHandled() 0 3 1
A augmentColumns() 0 4 2
A getColumnAttributes() 0 3 1
B getColumnContent() 0 33 6
A handleAction() 0 23 3
1
<?php
2
3
namespace SilverStripe\Comments\Admin;
4
5
use SilverStripe\Comments\Model\Comment;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Forms\GridField\GridField;
8
use SilverStripe\Forms\GridField\GridField_ActionProvider;
9
use SilverStripe\Forms\GridField\GridField_ColumnProvider;
10
use SilverStripe\Forms\GridField\GridField_FormAction;
11
12
/**
13
 * @deprecated 3.2.0 {@see CommentsGridFieldApproveAction} and {@see CommentsGridFieldSpamAction} instead
14
 */
15
class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_ActionProvider
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function augmentColumns($gridField, &$columns)
21
    {
22
        if (!in_array('Actions', $columns)) {
23
            $columns[] = 'Actions';
24
        }
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function getColumnAttributes($gridField, $record, $columnName)
31
    {
32
        return ['class' => 'col-buttons'];
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getColumnMetadata($gridField, $columnName)
39
    {
40
        if ($columnName === 'Actions') {
41
            return ['title' => ''];
42
        }
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function getColumnsHandled($gridField)
49
    {
50
        return ['Actions'];
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function getColumnContent($gridField, $record, $columnName)
57
    {
58
        if (!$record->canEdit()) {
59
            return;
60
        }
61
62
        $field = '';
63
64
        if (!$record->IsSpam || !$record->Moderated) {
65
            $field .= GridField_FormAction::create(
66
                $gridField,
67
                'CustomAction' . $record->ID . 'Spam',
68
                _t(__CLASS__ . '.SPAM', 'Spam'),
69
                'spam',
70
                ['RecordID' => $record->ID]
71
            )
72
                ->addExtraClass('btn btn-secondary grid-field__icon-action')
73
                ->Field();
74
        }
75
76
        if ($record->IsSpam || !$record->Moderated) {
77
            $field .= GridField_FormAction::create(
78
                $gridField,
79
                'CustomAction' . $record->ID . 'Approve',
80
                _t(__CLASS__ . '.APPROVE', 'Approve'),
81
                'approve',
82
                ['RecordID' => $record->ID]
83
            )
84
                ->addExtraClass('btn btn-secondary grid-field__icon-action')
85
                ->Field();
86
        }
87
88
        return $field;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function getActions($gridField)
95
    {
96
        return ['spam', 'approve'];
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function handleAction(GridField $gridField, $actionName, $arguments, $data)
103
    {
104
        if ($actionName === 'spam') {
105
            /** @var Comment $comment */
106
            $comment = Comment::get()->byID($arguments['RecordID']);
107
            $comment->markSpam();
108
109
            // output a success message to the user
110
            Controller::curr()->getResponse()->setStatusCode(
111
                200,
112
                _t(__CLASS__ . '.COMMENTMARKEDSPAM', 'Comment marked as spam.')
113
            );
114
        }
115
116
        if ($actionName === 'approve') {
117
            /** @var Comment $comment */
118
            $comment = Comment::get()->byID($arguments['RecordID']);
119
            $comment->markApproved();
120
121
            // output a success message to the user
122
            Controller::curr()->getResponse()->setStatusCode(
123
                200,
124
                _t(__CLASS__ . '.COMMENTAPPROVED', 'Comment approved.')
125
            );
126
        }
127
    }
128
}
129