Completed
Pull Request — master (#277)
by
unknown
01:45
created

CommentsGridFieldSpamAction::getExtraData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 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_ActionMenuItem;
9
use SilverStripe\Forms\GridField\GridField_ActionProvider;
10
use SilverStripe\Forms\GridField\GridField_ColumnProvider;
11
use SilverStripe\Forms\GridField\GridField_FormAction;
12
13 View Code Duplication
class CommentsGridFieldSpamAction implements GridField_ColumnProvider, GridField_ActionProvider, GridField_ActionMenuItem
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function augmentColumns($gridField, &$columns)
19
    {
20
        if (!in_array('Actions', $columns)) {
21
            $columns[] = 'Actions';
22
        }
23
    }
24
25
    public function getTitle($gridField, $record, $columnName)
26
    {
27
        return _t(__CLASS__ . '.SPAM', 'Spam');
28
    }
29
30
    public function getExtraData($gridField, $record, $columnName)
31
    {
32
33
        $field = $this->getSpamAction($gridField, $record, $columnName);
34
35
        if ($field) {
36
            return $field->getAttributes();
37
        }
38
39
        return null;
40
    }
41
42
    public function getGroup($gridField, $record, $columnName)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
43
    {
44
        $field = $this->getSpamAction($gridField, $record, $columnName);
45
46
        return $field ? GridField_ActionMenuItem::DEFAULT_GROUP: null;
47
    }
48
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getColumnAttributes($gridField, $record, $columnName)
54
    {
55
        return ['class' => 'col-buttons grid-field__col-compact'];
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getColumnMetadata($gridField, $columnName)
62
    {
63
        if ($columnName === 'Actions') {
64
            return ['title' => ''];
65
        }
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function getColumnsHandled($gridField)
72
    {
73
        return ['Actions'];
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function getColumnContent($gridField, $record, $columnName)
80
    {
81
        if (!$record->canEdit()) {
82
            return;
83
        }
84
85
        $field = $this->getSpamAction($gridField, $record, $columnName);
86
87
        return $field ? $field->Field() : null;
88
    }
89
90
    /**
91
     * Returns the FormAction object, used by other methods to get properties
92
     *
93
     * @return GridField_FormAction
94
     */
95
    public function getSpamAction($gridField, $record, $columnName)
96
    {
97
        $field = GridField_FormAction::create(
98
            $gridField,
99
            'CustomAction' . $record->ID . 'Spam',
100
            _t(__CLASS__ . '.SPAM', 'Spam'),
101
            'spam',
102
            ['RecordID' => $record->ID]
103
        )
104
            ->addExtraClass(implode(' ', [
105
                'btn',
106
                'btn-secondary',
107
                'grid-field__icon-action',
108
                'action-menu--handled',
109
                'font-icon-cross-mark',
110
            ]))
111
            ->setAttribute('classNames', 'font-icon-cross-mark');
112
        return (!$record->IsSpam || !$record->Moderated) ? $field : null;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function getActions($gridField)
119
    {
120
        return ['spam'];
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function handleAction(GridField $gridField, $actionName, $arguments, $data)
127
    {
128
        /** @var Comment $comment */
129
        $comment = Comment::get()->byID($arguments['RecordID']);
130
        $comment->markSpam();
131
132
        // output a success message to the user
133
        Controller::curr()->getResponse()->setStatusCode(
134
            200,
135
            _t(__CLASS__ . '.COMMENTMARKEDSPAM', 'Comment marked as spam.')
136
        );
137
    }
138
}
139