Move comment handles to shared parent class

This commit is contained in:
Daniel Hensby 2018-02-26 11:40:49 +00:00
parent 192ddbb4b5
commit 909066c0a8
No known key found for this signature in database
GPG Key ID: B00D1E9767F0B06E
3 changed files with 62 additions and 58 deletions

View File

@ -2,48 +2,27 @@
namespace SilverStripe\Comments\Admin\CommentsGridFieldBulkAction;
use Colymba\BulkManager\BulkAction\Handler;
use SilverStripe\Core\Convert;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Comments\Model\Comment;
/**
* A {@link Handler} for bulk approving comments
*/
class ApproveHandler extends Handler
class ApproveHandler extends CommentHandler
{
private static $allowed_actions = ['index'];
private static $url_segment = 'approve';
protected $xhr = true;
protected $buttonClasses = 'font-icon-tick';
protected $destructive = false;
protected $label = 'Approve';
/**
* @param HTTPRequest $request
* @return HTTPResponse
* @param Comment $comment
*
* @return Comment
*/
public function index(HTTPRequest $request)
public function updateComment($comment)
{
$ids = [];
foreach ($this->getRecords() as $record) {
array_push($ids, $record->ID);
$record->markApproved();
}
$response = new HTTPResponse(Convert::raw2json([
'done' => true,
'records' => $ids,
]));
$response->addHeader('Content-Type', 'text/json');
return $response;
$comment->markApproved();
return $comment;
}
}

View File

@ -0,0 +1,46 @@
<?php
namespace SilverStripe\Comments\Admin\CommentsGridFieldBulkAction;
use Colymba\BulkManager\BulkAction\Handler;
use SilverStripe\Comments\Model\Comment;
use SilverStripe\Core\Convert;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
abstract class CommentHandler extends Handler
{
protected $xhr = true;
protected $destructive = false;
/**
* @param HTTPRequest $request
* @return HTTPResponse
*/
public function index(HTTPRequest $request)
{
$ids = [];
foreach ($this->getRecords() as $comment) {
array_push($ids, $comment->ID);
$this->updateComment($comment);
}
$response = new HTTPResponse(Convert::raw2json([
'done' => true,
'records' => $ids,
]));
$response->addHeader('Content-Type', 'application/json');
return $response;
}
/**
* @param Comment $comment
*
* @return Comment
*/
abstract public function updateComment($comment);
}

View File

@ -2,48 +2,27 @@
namespace SilverStripe\Comments\Admin\CommentsGridFieldBulkAction;
use Colymba\BulkManager\BulkAction\Handler;
use SilverStripe\Core\Convert;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Comments\Model\Comment;
/**
* A {@link Handler} for bulk marking comments as spam
*/
class SpamHandler extends Handler
class SpamHandler extends CommentHandler
{
private static $allowed_actions = ['index'];
private static $url_segment = 'spam';
protected $xhr = true;
protected $buttonClasses = 'font-icon-cross-mark';
protected $destructive = false;
protected $label = 'Spam';
/**
* @param HTTPRequest $request
* @return HTTPResponse
* @param Comment $comment
*
* @return Comment
*/
public function index(HTTPRequest $request)
public function updateComment($comment)
{
$ids = [];
foreach ($this->getRecords() as $record) {
array_push($ids, $record->ID);
$record->markSpam();
}
$response = new HTTPResponse(Convert::raw2json([
'done' => true,
'records' => $ids,
]));
$response->addHeader('Content-Type', 'text/json');
return $response;
$comment->markSpam();
return $comment;
}
}