Code Duplication    Length = 125-126 lines in 2 locations

src/Admin/CommentsGridFieldApproveAction.php 1 location

@@ 13-137 (lines=125) @@
10
use SilverStripe\Forms\GridField\GridField_ColumnProvider;
11
use SilverStripe\Forms\GridField\GridField_FormAction;
12
13
class CommentsGridFieldApproveAction implements GridField_ColumnProvider, GridField_ActionProvider, GridField_ActionMenuItem
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)
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

src/Admin/CommentsGridFieldSpamAction.php 1 location

@@ 13-138 (lines=126) @@
10
use SilverStripe\Forms\GridField\GridField_ColumnProvider;
11
use SilverStripe\Forms\GridField\GridField_FormAction;
12
13
class CommentsGridFieldSpamAction implements GridField_ColumnProvider, GridField_ActionProvider, GridField_ActionMenuItem
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)
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