Completed
Push — master ( 82c4a1...4bf0a8 )
by Robbie
10:38
created

CommentsGridFieldAction::handleAction()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 20
Ratio 83.33 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 20
loc 24
rs 8.9713
c 1
b 0
f 0
cc 3
eloc 13
nc 4
nop 4
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
class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_ActionProvider
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
17
    public function augmentColumns($gridField, &$columns)
18
    {
19
        if (!in_array('Actions', $columns)) {
20
            $columns[] = 'Actions';
21
        }
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function getColumnAttributes($gridField, $record, $columnName)
28
    {
29
        return array('class' => 'col-buttons');
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function getColumnMetadata($gridField, $columnName)
36
    {
37
        if ($columnName == 'Actions') {
38
            return array('title' => '');
39
        }
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getColumnsHandled($gridField)
46
    {
47
        return array('Actions');
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getColumnContent($gridField, $record, $columnName)
54
    {
55
        if (!$record->canEdit()) {
56
            return;
57
        }
58
59
        $field = '';
60
61 View Code Duplication
        if (!$record->IsSpam || !$record->Moderated) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
62
            $field .= GridField_FormAction::create(
63
                $gridField,
64
                'CustomAction' . $record->ID . 'Spam',
65
                _t(__CLASS__ . '.SPAM', 'Spam'),
66
                'spam',
67
                array('RecordID' => $record->ID)
68
            )
69
                ->addExtraClass('btn btn-secondary grid-field__icon-action')
70
                ->Field();
71
        }
72
73 View Code Duplication
        if ($record->IsSpam || !$record->Moderated) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
74
            $field .= GridField_FormAction::create(
75
                $gridField,
76
                'CustomAction' . $record->ID . 'Approve',
77
                _t(__CLASS__ . '.APPROVE', 'Approve'),
78
                'approve',
79
                array('RecordID' => $record->ID)
80
            )
81
                ->addExtraClass('btn btn-secondary grid-field__icon-action')
82
                ->Field();
83
        }
84
85
        return $field;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function getActions($gridField)
92
    {
93
        return array('spam', 'approve');
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function handleAction(GridField $gridField, $actionName, $arguments, $data)
100
    {
101 View Code Duplication
        if ($actionName == 'spam') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
102
            $comment = Comment::get()->byID($arguments['RecordID']);
103
            $comment->markSpam();
104
105
            // output a success message to the user
106
            Controller::curr()->getResponse()->setStatusCode(
107
                200,
108
                _t(__CLASS__ . '.COMMENTMARKEDSPAM', 'Comment marked as spam.')
109
            );
110
        }
111
112 View Code Duplication
        if ($actionName == 'approve') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
113
            $comment = Comment::get()->byID($arguments['RecordID']);
114
            $comment->markApproved();
115
116
            // output a success message to the user
117
            Controller::curr()->getResponse()->setStatusCode(
118
                200,
119
                _t(__CLASS__ . '.COMMENTAPPROVED', 'Comment approved.')
120
            );
121
        }
122
    }
123
}
124