Skip to content

Commit

Permalink
Merge pull request #113 from tractorcow/pulls/2.0/comment-gridfield-a…
Browse files Browse the repository at this point in the history
…ctions

BUG Only show correct gridfield options for comments
  • Loading branch information
assertchris committed Apr 16, 2015
2 parents 26361cc + 85653fe commit 0c698a8
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 42 deletions.
42 changes: 20 additions & 22 deletions code/admin/CommentsGridFieldAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,25 @@ public function getColumnContent($gridField, $record, $columnName) {

$field = "";

$field .= GridField_FormAction::create(
$gridField,
'CustomAction' . $record->ID,
'Spam',
'spam',
array('RecordID' => $record->ID)
)->Field();
if(!$record->IsSpam || !$record->Moderated) {
$field .= GridField_FormAction::create(
$gridField,
'CustomAction' . $record->ID,
'Spam',
'spam',
array('RecordID' => $record->ID)
)->Field();
}

$field .= GridField_FormAction::create(
$gridField,
'CustomAction' . $record->ID,
'Approve',
'approve',
array('RecordID' => $record->ID)
)->Field();
if($record->IsSpam || !$record->Moderated) {
$field .= GridField_FormAction::create(
$gridField,
'CustomAction' . $record->ID,
'Approve',
'approve',
array('RecordID' => $record->ID)
)->Field();
}

return $field;
}
Expand All @@ -73,10 +77,7 @@ public function getActions($gridField) {
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();
$comment->markSpam();

// output a success message to the user
Controller::curr()->getResponse()->setStatusCode(
Expand All @@ -87,10 +88,7 @@ public function handleAction(GridField $gridField, $actionName, $arguments, $dat

if($actionName == 'approve') {
$comment = Comment::get()->byID($arguments["RecordID"]);

$comment->Moderated = true;
$comment->IsSpam = false;
$comment->write();
$comment->markApproved();

// output a success message to the user
Controller::curr()->getResponse()->setStatusCode(
Expand Down
10 changes: 2 additions & 8 deletions code/admin/CommentsGridFieldBulkAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ public function spam(SS_HTTPRequest $request) {

foreach($this->getRecords() as $record) {
array_push($ids, $record->ID);

$record->Moderated = 1;
$record->IsSpam = 1;
$record->write();
$record->markSpam();
}

$response = new SS_HTTPResponse(Convert::raw2json(array(
Expand All @@ -52,10 +49,7 @@ public function approve(SS_HTTPRequest $request) {

foreach($this->getRecords() as $record) {
array_push($ids, $record->ID);

$record->Moderated = 1;
$record->IsSpam = 0;
$record->write();
$record->markApproved();
}

$response = new SS_HTTPResponse(Convert::raw2json(array(
Expand Down
4 changes: 2 additions & 2 deletions code/admin/CommentsGridFieldConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public function __construct($itemsPerPage = 25) {
'ParentTitle' => function($value, &$item) {
return sprintf(
'<a href="%s" class="cms-panel-link external-link action" target="_blank">%s</a>',
Convert::raw2xml($item->Link()),
Convert::raw2xml($value)
Convert::raw2att($item->Link()),
$item->obj('ParentTitle')->forTemplate()
);
}
));
Expand Down
14 changes: 4 additions & 10 deletions code/controllers/CommentingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,7 @@ public function spam() {
}
if(!$comment->getSecurityToken()->checkRequest($this->request)) return $this->httpError(400);

$comment->IsSpam = true;
$comment->Moderated = true;
$comment->write();
$comment->markSpam();

return $this->request->isAjax()
? $comment->renderWith('CommentsInterface_singlecomment')
Expand All @@ -244,10 +242,8 @@ public function ham() {
return Security::permissionFailure($this, 'You do not have permission to edit this comment');
}
if(!$comment->getSecurityToken()->checkRequest($this->request)) return $this->httpError(400);

$comment->IsSpam = false;
$comment->Moderated = true;
$comment->write();

$comment->markApproved();

return $this->request->isAjax()
? $comment->renderWith('CommentsInterface_singlecomment')
Expand All @@ -265,9 +261,7 @@ public function approve() {
}
if(!$comment->getSecurityToken()->checkRequest($this->request)) return $this->httpError(400);

$comment->IsSpam = false;
$comment->Moderated = true;
$comment->write();
$comment->markApproved();

return $this->request->isAjax()
? $comment->renderWith('CommentsInterface_singlecomment')
Expand Down
28 changes: 28 additions & 0 deletions code/dataobjects/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,34 @@ public function ApproveLink($member = null) {
}
}

/**
* 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
*/
Expand Down

0 comments on commit 0c698a8

Please sign in to comment.