From 909066c0a8fbb1c0679947e3b4087e58c1155c8e Mon Sep 17 00:00:00 2001 From: Daniel Hensby Date: Mon, 26 Feb 2018 11:40:49 +0000 Subject: [PATCH] Move comment handles to shared parent class --- .../ApproveHandler.php | 37 ++++----------- .../CommentHandler.php | 46 +++++++++++++++++++ .../SpamHandler.php | 37 ++++----------- 3 files changed, 62 insertions(+), 58 deletions(-) create mode 100644 src/Admin/CommentsGridFieldBulkAction/CommentHandler.php diff --git a/src/Admin/CommentsGridFieldBulkAction/ApproveHandler.php b/src/Admin/CommentsGridFieldBulkAction/ApproveHandler.php index 6e7506f..7d31015 100644 --- a/src/Admin/CommentsGridFieldBulkAction/ApproveHandler.php +++ b/src/Admin/CommentsGridFieldBulkAction/ApproveHandler.php @@ -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; } } diff --git a/src/Admin/CommentsGridFieldBulkAction/CommentHandler.php b/src/Admin/CommentsGridFieldBulkAction/CommentHandler.php new file mode 100644 index 0000000..b22d04f --- /dev/null +++ b/src/Admin/CommentsGridFieldBulkAction/CommentHandler.php @@ -0,0 +1,46 @@ +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); +} diff --git a/src/Admin/CommentsGridFieldBulkAction/SpamHandler.php b/src/Admin/CommentsGridFieldBulkAction/SpamHandler.php index 8c6421b..991649c 100644 --- a/src/Admin/CommentsGridFieldBulkAction/SpamHandler.php +++ b/src/Admin/CommentsGridFieldBulkAction/SpamHandler.php @@ -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; } }