2015-04-13 05:41:18 +02:00
|
|
|
<?php
|
|
|
|
|
2017-01-16 20:57:37 +01:00
|
|
|
namespace SilverStripe\Comments\Admin;
|
|
|
|
|
|
|
|
use SilverStripe\Comments\Model\Comment;
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Forms\GridField\GridField;
|
|
|
|
use SilverStripe\Forms\GridField\GridField_ActionProvider;
|
|
|
|
use SilverStripe\Forms\GridField\GridField_ColumnProvider;
|
|
|
|
use SilverStripe\Forms\GridField\GridField_FormAction;
|
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_ActionProvider
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function augmentColumns($gridField, &$columns)
|
|
|
|
{
|
|
|
|
if (!in_array('Actions', $columns)) {
|
|
|
|
$columns[] = 'Actions';
|
|
|
|
}
|
|
|
|
}
|
2015-04-13 05:41:18 +02:00
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getColumnAttributes($gridField, $record, $columnName)
|
|
|
|
{
|
2018-09-24 18:16:03 +02:00
|
|
|
return ['class' => 'col-buttons'];
|
2016-02-19 01:48:25 +01:00
|
|
|
}
|
2015-04-13 05:41:18 +02:00
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getColumnMetadata($gridField, $columnName)
|
|
|
|
{
|
2018-09-24 18:16:03 +02:00
|
|
|
if ($columnName === 'Actions') {
|
|
|
|
return ['title' => ''];
|
2016-02-19 01:48:25 +01:00
|
|
|
}
|
|
|
|
}
|
2015-04-13 05:41:18 +02:00
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getColumnsHandled($gridField)
|
|
|
|
{
|
2018-09-24 18:16:03 +02:00
|
|
|
return ['Actions'];
|
2016-02-19 01:48:25 +01:00
|
|
|
}
|
2015-04-13 05:41:18 +02:00
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getColumnContent($gridField, $record, $columnName)
|
|
|
|
{
|
|
|
|
if (!$record->canEdit()) {
|
|
|
|
return;
|
|
|
|
}
|
2015-04-13 05:41:18 +02:00
|
|
|
|
2017-01-16 20:57:37 +01:00
|
|
|
$field = '';
|
2015-04-13 05:41:18 +02:00
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
if (!$record->IsSpam || !$record->Moderated) {
|
|
|
|
$field .= GridField_FormAction::create(
|
|
|
|
$gridField,
|
|
|
|
'CustomAction' . $record->ID . 'Spam',
|
2017-12-14 04:08:43 +01:00
|
|
|
_t(__CLASS__ . '.SPAM', 'Spam'),
|
2016-02-19 01:48:25 +01:00
|
|
|
'spam',
|
2018-09-24 18:16:03 +02:00
|
|
|
['RecordID' => $record->ID]
|
2017-12-14 04:08:43 +01:00
|
|
|
)
|
|
|
|
->addExtraClass('btn btn-secondary grid-field__icon-action')
|
|
|
|
->Field();
|
2016-02-19 01:48:25 +01:00
|
|
|
}
|
2015-04-13 05:41:18 +02:00
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
if ($record->IsSpam || !$record->Moderated) {
|
|
|
|
$field .= GridField_FormAction::create(
|
|
|
|
$gridField,
|
|
|
|
'CustomAction' . $record->ID . 'Approve',
|
2017-12-14 04:08:43 +01:00
|
|
|
_t(__CLASS__ . '.APPROVE', 'Approve'),
|
2016-02-19 01:48:25 +01:00
|
|
|
'approve',
|
2018-09-24 18:16:03 +02:00
|
|
|
['RecordID' => $record->ID]
|
2017-12-14 04:08:43 +01:00
|
|
|
)
|
|
|
|
->addExtraClass('btn btn-secondary grid-field__icon-action')
|
|
|
|
->Field();
|
2016-02-19 01:48:25 +01:00
|
|
|
}
|
2015-04-13 05:41:18 +02:00
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
return $field;
|
|
|
|
}
|
2015-04-13 05:41:18 +02:00
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getActions($gridField)
|
|
|
|
{
|
2018-09-24 18:16:03 +02:00
|
|
|
return ['spam', 'approve'];
|
2016-02-19 01:48:25 +01:00
|
|
|
}
|
2015-04-13 05:41:18 +02:00
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function handleAction(GridField $gridField, $actionName, $arguments, $data)
|
|
|
|
{
|
2018-09-24 18:16:03 +02:00
|
|
|
if ($actionName === 'spam') {
|
|
|
|
/** @var Comment $comment */
|
2017-01-16 20:57:37 +01:00
|
|
|
$comment = Comment::get()->byID($arguments['RecordID']);
|
2016-02-19 01:48:25 +01:00
|
|
|
$comment->markSpam();
|
2015-04-13 05:41:18 +02:00
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
// output a success message to the user
|
|
|
|
Controller::curr()->getResponse()->setStatusCode(
|
|
|
|
200,
|
2017-12-14 04:08:43 +01:00
|
|
|
_t(__CLASS__ . '.COMMENTMARKEDSPAM', 'Comment marked as spam.')
|
2016-02-19 01:48:25 +01:00
|
|
|
);
|
|
|
|
}
|
2015-04-13 05:41:18 +02:00
|
|
|
|
2018-09-24 18:16:03 +02:00
|
|
|
if ($actionName === 'approve') {
|
|
|
|
/** @var Comment $comment */
|
2017-01-16 20:57:37 +01:00
|
|
|
$comment = Comment::get()->byID($arguments['RecordID']);
|
2016-02-19 01:48:25 +01:00
|
|
|
$comment->markApproved();
|
2015-04-13 05:41:18 +02:00
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
// output a success message to the user
|
|
|
|
Controller::curr()->getResponse()->setStatusCode(
|
|
|
|
200,
|
2017-12-14 04:08:43 +01:00
|
|
|
_t(__CLASS__ . '.COMMENTAPPROVED', 'Comment approved.')
|
2016-02-19 01:48:25 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2015-11-17 01:12:21 +01:00
|
|
|
}
|