silverstripe-comments/code/admin/CommentsGridFieldAction.php
2015-04-14 12:57:30 +12:00

102 lines
2.0 KiB
PHP

<?php
class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_ActionProvider {
/**
* {@inheritdoc}
*/
public function augmentColumns($gridField, &$columns) {
if(!in_array('Actions', $columns)) {
$columns[] = 'Actions';
}
}
/**
* {@inheritdoc}
*/
public function getColumnAttributes($gridField, $record, $columnName) {
return array('class' => 'col-buttons');
}
/**
* {@inheritdoc}
*/
public function getColumnMetadata($gridField, $columnName) {
if($columnName == 'Actions') {
return array('title' => '');
}
}
/**
* {@inheritdoc}
*/
public function getColumnsHandled($gridField) {
return array('Actions');
}
/**
* {@inheritdoc}
*/
public function getColumnContent($gridField, $record, $columnName) {
if(!$record->canEdit()) return;
$field = "";
$field .= GridField_FormAction::create(
$gridField,
'CustomAction' . $record->ID,
'Mark As Spam',
'spam',
array('RecordID' => $record->ID)
)->Field();
$field .= GridField_FormAction::create(
$gridField,
'CustomAction' . $record->ID,
'Mark As Not Spam',
'not_spam',
array('RecordID' => $record->ID)
)->Field();
return $field;
}
/**
* {@inheritdoc}
*/
public function getActions($gridField) {
return array('spam', 'not_spam');
}
/**
* {@inheritdoc}
*/
public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
if($actionName == 'spam') {
$comment = Comment::get()->byID($arguments["RecordID"]);
$comment->Moderated = true;
$comment->IsSpam = true;
$comment->write();
// output a success message to the user
Controller::curr()->getResponse()->setStatusCode(
200,
'Comment marked as spam.'
);
}
if($actionName == 'not_spam') {
$comment = Comment::get()->byID($arguments["RecordID"]);
$comment->Moderated = true;
$comment->IsSpam = false;
$comment->write();
// output a success message to the user
Controller::curr()->getResponse()->setStatusCode(
200,
'Comment marked as not spam.'
);
}
}
}