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

CommentsGridFieldApproveAction::getGroup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 6
Ratio 100 %

Importance

Changes 0
Metric Value
dl 6
loc 6
rs 10
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 CommentsGridFieldApproveAction 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__ . '.APPROVE', 'Approve');
28
    }
29
30
    public function getExtraData($gridField, $record, $columnName)
31
    {
32
33
        $field = $this->getApproveAction($gridField, $record, $columnName);
34
35
        if ($field) {
36
            return $field->getAttributes();
37
        }
38
39
        return null;
40
    }
41
    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...
42
    {
43
        $field = $this->getApproveAction($gridField, $record, $columnName);
44
45
        return $field ? GridField_ActionMenuItem::DEFAULT_GROUP: null;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getColumnAttributes($gridField, $record, $columnName)
52
    {
53
        return ['class' => 'col-buttons grid-field__col-compact'];
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getColumnMetadata($gridField, $columnName)
60
    {
61
        if ($columnName === 'Actions') {
62
            return ['title' => ''];
63
        }
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getColumnsHandled($gridField)
70
    {
71
        return ['Actions'];
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function getColumnContent($gridField, $record, $columnName)
78
    {
79
        if (!$record->canEdit()) {
80
            return;
81
        }
82
83
        $field = $this->getApproveAction($gridField, $record, $columnName);
84
85
        return $field ? $field->Field() : null;
86
    }
87
88
    /**
89
     * Returns the FormAction object, used by other methods to get properties
90
     *
91
     * @return GridField_FormAction
92
     */
93
    public function getApproveAction($gridField, $record, $columnName)
94
    {
95
        $field = GridField_FormAction::create(
96
            $gridField,
97
            'CustomAction' . $record->ID . 'Approve',
98
            _t(__CLASS__ . '.APPROVE', 'Approve'),
99
            'approve',
100
            ['RecordID' => $record->ID]
101
        )
102
            ->addExtraClass(implode(' ', [
103
                'btn',
104
                'btn-secondary',
105
                'grid-field__icon-action',
106
                'action-menu--handled',
107
                'font-icon-check-mark',
108
            ]))
109
            ->setAttribute('classNames', 'font-icon-check-mark');
110
111
        return ($record->IsSpam || !$record->Moderated) ? $field : null;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function getActions($gridField)
118
    {
119
        return ['approve'];
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function handleAction(GridField $gridField, $actionName, $arguments, $data)
126
    {
127
        /** @var Comment $comment */
128
        $comment = Comment::get()->byID($arguments['RecordID']);
129
        $comment->markApproved();
130
131
        // output a success message to the user
132
        Controller::curr()->getResponse()->setStatusCode(
133
            200,
134
            _t(__CLASS__ . '.COMMENTAPPROVED', 'Comment approved.')
135
        );
136
    }
137
}
138