Merge pull request #113 from tractorcow/pulls/2.0/comment-gridfield-actions

BUG Only show correct gridfield options for comments
This commit is contained in:
Christopher Pitt 2015-04-16 15:47:16 +12:00
commit 0c698a813a
5 changed files with 56 additions and 42 deletions

View File

@ -41,6 +41,7 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act
$field = ""; $field = "";
if(!$record->IsSpam || !$record->Moderated) {
$field .= GridField_FormAction::create( $field .= GridField_FormAction::create(
$gridField, $gridField,
'CustomAction' . $record->ID, 'CustomAction' . $record->ID,
@ -48,7 +49,9 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act
'spam', 'spam',
array('RecordID' => $record->ID) array('RecordID' => $record->ID)
)->Field(); )->Field();
}
if($record->IsSpam || !$record->Moderated) {
$field .= GridField_FormAction::create( $field .= GridField_FormAction::create(
$gridField, $gridField,
'CustomAction' . $record->ID, 'CustomAction' . $record->ID,
@ -56,6 +59,7 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act
'approve', 'approve',
array('RecordID' => $record->ID) array('RecordID' => $record->ID)
)->Field(); )->Field();
}
return $field; return $field;
} }
@ -73,10 +77,7 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act
public function handleAction(GridField $gridField, $actionName, $arguments, $data) { public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
if($actionName == 'spam') { if($actionName == 'spam') {
$comment = Comment::get()->byID($arguments["RecordID"]); $comment = Comment::get()->byID($arguments["RecordID"]);
$comment->markSpam();
$comment->Moderated = true;
$comment->IsSpam = true;
$comment->write();
// output a success message to the user // output a success message to the user
Controller::curr()->getResponse()->setStatusCode( Controller::curr()->getResponse()->setStatusCode(
@ -87,10 +88,7 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act
if($actionName == 'approve') { if($actionName == 'approve') {
$comment = Comment::get()->byID($arguments["RecordID"]); $comment = Comment::get()->byID($arguments["RecordID"]);
$comment->markApproved();
$comment->Moderated = true;
$comment->IsSpam = false;
$comment->write();
// output a success message to the user // output a success message to the user
Controller::curr()->getResponse()->setStatusCode( Controller::curr()->getResponse()->setStatusCode(

View File

@ -30,10 +30,7 @@ class CommentsGridFieldBulkAction_Handlers extends CommentsGridFieldBulkAction {
foreach($this->getRecords() as $record) { foreach($this->getRecords() as $record) {
array_push($ids, $record->ID); array_push($ids, $record->ID);
$record->markSpam();
$record->Moderated = 1;
$record->IsSpam = 1;
$record->write();
} }
$response = new SS_HTTPResponse(Convert::raw2json(array( $response = new SS_HTTPResponse(Convert::raw2json(array(
@ -52,10 +49,7 @@ class CommentsGridFieldBulkAction_Handlers extends CommentsGridFieldBulkAction {
foreach($this->getRecords() as $record) { foreach($this->getRecords() as $record) {
array_push($ids, $record->ID); array_push($ids, $record->ID);
$record->markApproved();
$record->Moderated = 1;
$record->IsSpam = 0;
$record->write();
} }
$response = new SS_HTTPResponse(Convert::raw2json(array( $response = new SS_HTTPResponse(Convert::raw2json(array(

View File

@ -14,8 +14,8 @@ class CommentsGridFieldConfig extends GridFieldConfig_RecordEditor {
'ParentTitle' => function($value, &$item) { 'ParentTitle' => function($value, &$item) {
return sprintf( return sprintf(
'<a href="%s" class="cms-panel-link external-link action" target="_blank">%s</a>', '<a href="%s" class="cms-panel-link external-link action" target="_blank">%s</a>',
Convert::raw2xml($item->Link()), Convert::raw2att($item->Link()),
Convert::raw2xml($value) $item->obj('ParentTitle')->forTemplate()
); );
} }
)); ));

View File

@ -225,9 +225,7 @@ class CommentingController extends Controller {
} }
if(!$comment->getSecurityToken()->checkRequest($this->request)) return $this->httpError(400); if(!$comment->getSecurityToken()->checkRequest($this->request)) return $this->httpError(400);
$comment->IsSpam = true; $comment->markSpam();
$comment->Moderated = true;
$comment->write();
return $this->request->isAjax() return $this->request->isAjax()
? $comment->renderWith('CommentsInterface_singlecomment') ? $comment->renderWith('CommentsInterface_singlecomment')
@ -245,9 +243,7 @@ class CommentingController extends Controller {
} }
if(!$comment->getSecurityToken()->checkRequest($this->request)) return $this->httpError(400); if(!$comment->getSecurityToken()->checkRequest($this->request)) return $this->httpError(400);
$comment->IsSpam = false; $comment->markApproved();
$comment->Moderated = true;
$comment->write();
return $this->request->isAjax() return $this->request->isAjax()
? $comment->renderWith('CommentsInterface_singlecomment') ? $comment->renderWith('CommentsInterface_singlecomment')
@ -265,9 +261,7 @@ class CommentingController extends Controller {
} }
if(!$comment->getSecurityToken()->checkRequest($this->request)) return $this->httpError(400); if(!$comment->getSecurityToken()->checkRequest($this->request)) return $this->httpError(400);
$comment->IsSpam = false; $comment->markApproved();
$comment->Moderated = true;
$comment->write();
return $this->request->isAjax() return $this->request->isAjax()
? $comment->renderWith('CommentsInterface_singlecomment') ? $comment->renderWith('CommentsInterface_singlecomment')

View File

@ -446,6 +446,34 @@ class Comment extends DataObject {
} }
} }
/**
* Mark this comment as spam
*/
public function markSpam() {
$this->IsSpam = true;
$this->Moderated = true;
$this->write();
$this->extend('afterMarkSpam');
}
/**
* Mark this comment as approved
*/
public function markApproved() {
$this->IsSpam = false;
$this->Moderated = true;
$this->write();
$this->extend('afterMarkApproved');
}
/**
* Mark this comment as unapproved
*/
public function markUnapproved() {
$this->Moderated = false;
$this->write();
$this->extend('afterMarkUnapproved');
}
/** /**
* @return string * @return string
*/ */