Completed
Pull Request — master (#196)
by Robbie
13:18
created

CommentsGridFieldAction::handleAction()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 20
Ratio 83.33 %

Importance

Changes 0
Metric Value
dl 20
loc 24
rs 8.9713
c 0
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('CommentsGridFieldAction.SPAM', 'Spam'),
66
                'spam',
67
                array('RecordID' => $record->ID)
68
            )->Field();
69
        }
70
71 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...
72
            $field .= GridField_FormAction::create(
73
                $gridField,
74
                'CustomAction' . $record->ID . 'Approve',
75
                _t('CommentsGridFieldAction.APPROVE', 'Approve'),
76
                'approve',
77
                array('RecordID' => $record->ID)
78
            )->Field();
79
        }
80
81
        return $field;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function getActions($gridField)
88
    {
89
        return array('spam', 'approve');
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function handleAction(GridField $gridField, $actionName, $arguments, $data)
96
    {
97 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...
98
            $comment = Comment::get()->byID($arguments['RecordID']);
99
            $comment->markSpam();
100
101
            // output a success message to the user
102
            Controller::curr()->getResponse()->setStatusCode(
103
                200,
104
                _t('CommentsGridFieldAction.COMMENTMARKEDSPAM', 'Comment marked as spam.')
105
            );
106
        }
107
108 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...
109
            $comment = Comment::get()->byID($arguments['RecordID']);
110
            $comment->markApproved();
111
112
            // output a success message to the user
113
            Controller::curr()->getResponse()->setStatusCode(
114
                200,
115
                _t('CommentsGridFieldAction.COMMENTAPPROVED', 'Comment approved.')
116
            );
117
        }
118
    }
119
}
120