Merge pull request #232 from creative-commoners/pulls/3.0/bulk-editing-compat

API Use concrete Handler implementations for Spam and Approve bulk editing
This commit is contained in:
Robbie Averill 2018-02-27 09:29:26 +13:00 committed by GitHub
commit 90c42ff027
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 110 additions and 97 deletions

View File

@ -0,0 +1,28 @@
<?php
namespace SilverStripe\Comments\Admin\CommentsGridFieldBulkAction;
use SilverStripe\Comments\Model\Comment;
/**
* A {@link Handler} for bulk approving comments
*/
class ApproveHandler extends CommentHandler
{
private static $url_segment = 'approve';
protected $buttonClasses = 'font-icon-tick';
protected $label = 'Approve';
/**
* @param Comment $comment
*
* @return Comment
*/
public function updateComment($comment)
{
$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

@ -1,72 +0,0 @@
<?php
namespace SilverStripe\Comments\Admin\CommentsGridFieldBulkAction;
use Colymba\BulkManager\BulkAction\Handler as GridFieldBulkActionHandler;
use SilverStripe\Core\Convert;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
/**
* A {@link GridFieldBulkActionHandler} for bulk marking comments as spam
*
* @package comments
*/
class Handler extends GridFieldBulkActionHandler
{
private static $allowed_actions = array(
'spam',
'approve',
);
private static $url_handlers = array(
'spam' => 'spam',
'approve' => 'approve',
);
/**
* @param HTTPRequest $request
* @return HTTPResponse
*/
public function spam(HTTPRequest $request)
{
$ids = array();
foreach ($this->getRecords() as $record) {
array_push($ids, $record->ID);
$record->markSpam();
}
$response = new HTTPResponse(Convert::raw2json(array(
'done' => true,
'records' => $ids
)));
$response->addHeader('Content-Type', 'text/json');
return $response;
}
/**
* @param HTTPRequest $request
* @return HTTPResponse
*/
public function approve(HTTPRequest $request)
{
$ids = array();
foreach ($this->getRecords() as $record) {
array_push($ids, $record->ID);
$record->markApproved();
}
$response = new HTTPResponse(Convert::raw2json(array(
'done' => true,
'records' => $ids
)));
$response->addHeader('Content-Type', 'text/json');
return $response;
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace SilverStripe\Comments\Admin\CommentsGridFieldBulkAction;
use SilverStripe\Comments\Model\Comment;
/**
* A {@link Handler} for bulk marking comments as spam
*/
class SpamHandler extends CommentHandler
{
private static $url_segment = 'spam';
protected $buttonClasses = 'font-icon-cross-mark';
protected $label = 'Spam';
/**
* @param Comment $comment
*
* @return Comment
*/
public function updateComment($comment)
{
$comment->markSpam();
return $comment;
}
}

View File

@ -3,7 +3,8 @@
namespace SilverStripe\Comments\Admin;
use Colymba\BulkManager\BulkManager;
use SilverStripe\Comments\Admin\CommentsGridFieldBulkAction\Handler;
use SilverStripe\Comments\Admin\CommentsGridFieldBulkAction\ApproveHandler;
use SilverStripe\Comments\Admin\CommentsGridFieldBulkAction\SpamHandler;
use SilverStripe\Core\Convert;
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
use SilverStripe\Forms\GridField\GridFieldDataColumns;
@ -31,32 +32,14 @@ class CommentsGridFieldConfig extends GridFieldConfig_RecordEditor
));
// Add bulk option
$manager = new BulkManager();
$manager = new BulkManager(null, false);
$manager->addBulkAction(
'spam',
_t(__CLASS__ . '.SPAM', 'Spam'),
Handler::class,
array(
'isAjax' => true,
'icon' => 'cross',
'isDestructive' => false
)
);
$spamAction = SpamHandler::create()->setLabel(_t(__CLASS__ . '.SPAM', 'Spam'));
$approveAction = ApproveHandler::create()->setLabel(_t(__CLASS__ . '.APPROVE', 'Approve'));
$manager->addBulkAction(
'approve',
_t(__CLASS__ . '.APPROVE', 'Approve'),
Handler::class,
array(
'isAjax' => true,
'icon' => 'cross',
'isDestructive' => false
)
);
$manager->removeBulkAction('bulkEdit');
$manager->removeBulkAction('unLink');
$manager
->addBulkAction($spamAction)
->addBulkAction($approveAction);
$this->addComponent($manager);
}