2013-12-01 23:54:39 +01:00
|
|
|
<?php
|
2017-01-25 13:27:11 +01:00
|
|
|
|
|
|
|
namespace Colymba\BulkManager\BulkAction;
|
|
|
|
|
|
|
|
use Colymba\BulkManager\BulkAction\Handler;
|
2018-03-14 21:21:54 +01:00
|
|
|
use Colymba\BulkTools\HTTPBulkToolsResponse;
|
2017-01-25 13:27:11 +01:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
|
|
|
use SilverStripe\Control\HTTPResponse;
|
|
|
|
use SilverStripe\Core\Convert;
|
2018-03-14 21:21:54 +01:00
|
|
|
use Exception;
|
2017-01-25 13:27:11 +01:00
|
|
|
|
2013-12-01 23:54:39 +01:00
|
|
|
/**
|
2014-05-04 16:12:05 +02:00
|
|
|
* Bulk action handler for deleting records.
|
2017-01-25 13:27:11 +01:00
|
|
|
*
|
2013-12-01 23:54:39 +01:00
|
|
|
* @author colymba
|
|
|
|
*/
|
2017-01-25 13:27:11 +01:00
|
|
|
class DeleteHandler extends Handler
|
2015-12-15 13:08:57 +01:00
|
|
|
{
|
2018-02-12 14:11:49 +01:00
|
|
|
/**
|
|
|
|
* URL segment used to call this handler
|
|
|
|
* If none given, @BulkManager will fallback to the Unqualified class name
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $url_segment = 'delete';
|
|
|
|
|
2015-12-15 13:08:57 +01:00
|
|
|
/**
|
|
|
|
* RequestHandler allowed actions.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $allowed_actions = array('delete');
|
2013-12-01 23:54:39 +01:00
|
|
|
|
2015-12-15 13:08:57 +01:00
|
|
|
/**
|
|
|
|
* RequestHandler url => action map.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $url_handlers = array(
|
2018-02-12 14:11:49 +01:00
|
|
|
'' => 'delete',
|
2015-12-15 13:08:57 +01:00
|
|
|
);
|
2014-05-11 12:32:21 +02:00
|
|
|
|
2018-02-12 14:11:49 +01:00
|
|
|
/**
|
|
|
|
* Front-end label for this handler's action
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $label = 'Delete';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Front-end icon path for this handler's action.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $icon = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extra classes to add to the bulk action button for this handler
|
|
|
|
* Can also be used to set the button font-icon e.g. font-icon-trash
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $buttonClasses = 'font-icon-trash';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether this handler should be called via an XHR from the front-end
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $xhr = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set to true is this handler will destroy any data.
|
|
|
|
* A warning and confirmation will be shown on the front-end.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $destructive = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return i18n localized front-end label
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getI18nLabel()
|
|
|
|
{
|
|
|
|
return _t('GRIDFIELD_BULK_MANAGER.DELETE_SELECT_LABEL', $this->getLabel());
|
|
|
|
}
|
|
|
|
|
2015-12-15 13:08:57 +01:00
|
|
|
/**
|
|
|
|
* Delete the selected records passed from the delete bulk action.
|
|
|
|
*
|
2017-01-25 13:27:11 +01:00
|
|
|
* @param HTTPRequest $request
|
|
|
|
*
|
2018-03-14 21:21:54 +01:00
|
|
|
* @return HTTPBulkToolsResponse
|
2015-12-15 13:08:57 +01:00
|
|
|
*/
|
2017-01-25 13:27:11 +01:00
|
|
|
public function delete(HTTPRequest $request)
|
2015-12-15 13:08:57 +01:00
|
|
|
{
|
2018-03-14 21:21:54 +01:00
|
|
|
$response = new HTTPBulkToolsResponse(true, $this->gridField);
|
2013-12-01 23:54:39 +01:00
|
|
|
|
2018-03-14 21:21:54 +01:00
|
|
|
try {
|
|
|
|
foreach ($this->getRecords() as $record) {
|
|
|
|
$response->addSuccessRecord($record);
|
|
|
|
$record->delete();
|
|
|
|
}
|
2013-12-01 23:54:39 +01:00
|
|
|
|
2022-04-13 04:08:15 +02:00
|
|
|
$doneCount = count($response->getSuccessRecords() ?? []);
|
2018-03-14 21:21:54 +01:00
|
|
|
$message = sprintf(
|
|
|
|
'Deleted %1$d records.',
|
|
|
|
$doneCount
|
|
|
|
);
|
|
|
|
$response->setMessage($message);
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
$response->setStatusCode(500);
|
|
|
|
$response->setMessage($ex->getMessage());
|
|
|
|
}
|
2015-12-15 13:08:57 +01:00
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
}
|