2011-03-23 10:51:00 +01:00
|
|
|
<?php
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
|
|
|
use SilverStripe\ORM\DB;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\ORM\SS_List;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\Versioning\Versioned;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\SecurityToken;
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Special request handler for admin/batchaction
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-10-15 00:26:23 +02:00
|
|
|
* @package framework
|
|
|
|
* @subpackage admin
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
|
|
|
class CMSBatchActionHandler extends RequestHandler {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
/** @config */
|
|
|
|
private static $batch_actions = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $url_handlers = array(
|
2011-03-23 10:51:00 +01:00
|
|
|
'$BatchAction/applicablepages' => 'handleApplicablePages',
|
|
|
|
'$BatchAction/confirmation' => 'handleConfirmation',
|
2013-01-24 20:44:39 +01:00
|
|
|
'$BatchAction' => 'handleBatchAction',
|
2011-03-23 10:51:00 +01:00
|
|
|
);
|
2013-06-28 10:07:57 +02:00
|
|
|
|
|
|
|
private static $allowed_actions = array(
|
|
|
|
'handleBatchAction',
|
|
|
|
'handleApplicablePages',
|
|
|
|
'handleConfirmation',
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
/**
|
|
|
|
* @var Controller
|
|
|
|
*/
|
2011-03-23 10:51:00 +01:00
|
|
|
protected $parentController;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* @var String
|
|
|
|
*/
|
|
|
|
protected $urlSegment;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* @var String $recordClass The classname that should be affected
|
|
|
|
* by any batch changes. Needs to be set in the actual {@link CMSBatchAction}
|
|
|
|
* implementations as well.
|
|
|
|
*/
|
2016-08-05 00:32:13 +02:00
|
|
|
protected $recordClass = 'SilverStripe\\CMS\\Model\\SiteTree';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-05-15 01:50:23 +02:00
|
|
|
/**
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param Controller $parentController
|
2015-05-15 01:50:23 +02:00
|
|
|
* @param string $urlSegment
|
|
|
|
* @param string $recordClass
|
|
|
|
*/
|
|
|
|
public function __construct($parentController, $urlSegment, $recordClass = null) {
|
|
|
|
$this->parentController = $parentController;
|
|
|
|
$this->urlSegment = $urlSegment;
|
|
|
|
if($recordClass) {
|
|
|
|
$this->recordClass = $recordClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Register a new batch action. Each batch action needs to be represented by a subclass
|
|
|
|
* of {@link CMSBatchAction}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param string $urlSegment The URL Segment of the batch action - the URL used to process this
|
2011-03-23 10:51:00 +01:00
|
|
|
* action will be admin/batchactions/(urlSegment)
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param string $batchActionClass The name of the CMSBatchAction subclass to register
|
|
|
|
* @param string $recordClass
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
2016-08-05 00:32:13 +02:00
|
|
|
public static function register($urlSegment, $batchActionClass, $recordClass = 'SilverStripe\\CMS\\Model\\SiteTree') {
|
2011-03-23 10:51:00 +01:00
|
|
|
if(is_subclass_of($batchActionClass, 'CMSBatchAction')) {
|
2015-05-15 01:50:23 +02:00
|
|
|
Config::inst()->update(
|
|
|
|
'CMSBatchActionHandler',
|
|
|
|
'batch_actions',
|
|
|
|
array(
|
|
|
|
$urlSegment => array(
|
|
|
|
'class' => $batchActionClass,
|
|
|
|
'recordClass' => $recordClass
|
|
|
|
)
|
|
|
|
)
|
2011-03-23 10:51:00 +01:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
user_error("CMSBatchActionHandler::register() - Bad class '$batchActionClass'", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Link() {
|
2011-03-23 10:51:00 +01:00
|
|
|
return Controller::join_links($this->parentController->Link(), $this->urlSegment);
|
|
|
|
}
|
|
|
|
|
2015-05-15 01:50:23 +02:00
|
|
|
/**
|
|
|
|
* Invoke a batch action
|
|
|
|
*
|
|
|
|
* @param SS_HTTPRequest $request
|
|
|
|
* @return SS_HTTPResponse
|
|
|
|
*/
|
2013-01-24 20:44:39 +01:00
|
|
|
public function handleBatchAction($request) {
|
2011-03-23 10:51:00 +01:00
|
|
|
// This method can't be called without ajax.
|
2012-04-05 14:44:42 +02:00
|
|
|
if(!$request->isAjax()) {
|
2015-05-15 01:50:23 +02:00
|
|
|
return $this->parentController->redirectBack();
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
// Protect against CSRF on destructive action
|
2015-05-15 01:50:23 +02:00
|
|
|
if(!SecurityToken::inst()->checkRequest($request)) {
|
|
|
|
return $this->httpError(400);
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-05-15 01:50:23 +02:00
|
|
|
// Find the action handler
|
|
|
|
$action = $request->param('BatchAction');
|
|
|
|
$actionHandler = $this->actionByName($action);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
// Sanitise ID list and query the database for apges
|
2015-05-15 01:50:23 +02:00
|
|
|
$csvIDs = $request->requestVar('csvIDs');
|
|
|
|
$ids = $this->cleanIDs($csvIDs);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-05-15 01:50:23 +02:00
|
|
|
// Filter ids by those which are applicable to this action
|
|
|
|
// Enforces front end filter in LeftAndMain.BatchActions.js:refreshSelected
|
|
|
|
$ids = $actionHandler->applicablePages($ids);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-05-15 01:50:23 +02:00
|
|
|
// Query ids and pass to action to process
|
|
|
|
$pages = $this->getPages($ids);
|
2011-03-23 10:51:00 +01:00
|
|
|
return $actionHandler->run($pages);
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2011-03-23 10:51:00 +01:00
|
|
|
|
2015-05-15 01:50:23 +02:00
|
|
|
/**
|
|
|
|
* Respond with the list of applicable pages for a given filter
|
|
|
|
*
|
|
|
|
* @param SS_HTTPRequest $request
|
|
|
|
* @return SS_HTTPResponse
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function handleApplicablePages($request) {
|
2011-03-23 10:51:00 +01:00
|
|
|
// Find the action handler
|
2015-05-15 01:50:23 +02:00
|
|
|
$action = $request->param('BatchAction');
|
|
|
|
$actionHandler = $this->actionByName($action);
|
2011-03-23 10:51:00 +01:00
|
|
|
|
|
|
|
// Sanitise ID list and query the database for apges
|
2015-05-15 01:50:23 +02:00
|
|
|
$csvIDs = $request->requestVar('csvIDs');
|
|
|
|
$ids = $this->cleanIDs($csvIDs);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-05-15 01:50:23 +02:00
|
|
|
// Filter by applicable pages
|
2015-06-16 00:52:42 +02:00
|
|
|
if($ids) {
|
|
|
|
$applicableIDs = $actionHandler->applicablePages($ids);
|
|
|
|
} else {
|
|
|
|
$applicableIDs = array();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
$response = new SS_HTTPResponse(json_encode($applicableIDs));
|
|
|
|
$response->addHeader("Content-type", "application/json");
|
|
|
|
return $response;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-05-15 01:50:23 +02:00
|
|
|
/**
|
|
|
|
* Check if this action has a confirmation step
|
|
|
|
*
|
|
|
|
* @param SS_HTTPRequest $request
|
|
|
|
* @return SS_HTTPResponse
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function handleConfirmation($request) {
|
2011-03-23 10:51:00 +01:00
|
|
|
// Find the action handler
|
2015-05-15 01:50:23 +02:00
|
|
|
$action = $request->param('BatchAction');
|
|
|
|
$actionHandler = $this->actionByName($action);
|
2011-03-23 10:51:00 +01:00
|
|
|
|
|
|
|
// Sanitise ID list and query the database for apges
|
2015-05-15 01:50:23 +02:00
|
|
|
$csvIDs = $request->requestVar('csvIDs');
|
|
|
|
$ids = $this->cleanIDs($csvIDs);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-05-15 01:50:23 +02:00
|
|
|
// Check dialog
|
2011-03-23 10:51:00 +01:00
|
|
|
if($actionHandler->hasMethod('confirmationDialog')) {
|
|
|
|
$response = new SS_HTTPResponse(json_encode($actionHandler->confirmationDialog($ids)));
|
|
|
|
} else {
|
|
|
|
$response = new SS_HTTPResponse(json_encode(array('alert' => false)));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
$response->addHeader("Content-type", "application/json");
|
|
|
|
return $response;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-05-15 01:50:23 +02:00
|
|
|
/**
|
|
|
|
* Get an action for a given name
|
|
|
|
*
|
|
|
|
* @param string $name Name of the action
|
|
|
|
* @return CMSBatchAction An instance of the given batch action
|
|
|
|
* @throws InvalidArgumentException if invalid action name is passed.
|
|
|
|
*/
|
|
|
|
protected function actionByName($name) {
|
|
|
|
// Find the action handler
|
|
|
|
$actions = $this->batchActions();
|
|
|
|
if(!isset($actions[$name]['class'])) {
|
|
|
|
throw new InvalidArgumentException("Invalid batch action: {$name}");
|
|
|
|
}
|
|
|
|
return $this->buildAction($actions[$name]['class']);
|
|
|
|
}
|
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
2011-10-26 08:09:04 +02:00
|
|
|
* Return a SS_List of ArrayData objects containing the following pieces of info
|
2011-03-23 10:51:00 +01:00
|
|
|
* about each batch action:
|
|
|
|
* - Link
|
|
|
|
* - Title
|
2015-05-15 01:50:23 +02:00
|
|
|
*
|
|
|
|
* @return ArrayList
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function batchActionList() {
|
2011-03-23 10:51:00 +01:00
|
|
|
$actions = $this->batchActions();
|
2011-05-05 12:40:24 +02:00
|
|
|
$actionList = new ArrayList();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
foreach($actions as $urlSegment => $action) {
|
2015-05-15 01:50:23 +02:00
|
|
|
$actionObj = $this->buildAction($action['class']);
|
2011-03-23 10:51:00 +01:00
|
|
|
if($actionObj->canView()) {
|
|
|
|
$actionDef = new ArrayData(array(
|
|
|
|
"Link" => Controller::join_links($this->Link(), $urlSegment),
|
|
|
|
"Title" => $actionObj->getActionTitle(),
|
|
|
|
));
|
|
|
|
$actionList->push($actionDef);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
return $actionList;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-05-15 01:50:23 +02:00
|
|
|
/**
|
|
|
|
* Safely generate batch action object for a given classname
|
|
|
|
*
|
|
|
|
* @param string $class Class name to check
|
|
|
|
* @return CMSBatchAction An instance of the given batch action
|
|
|
|
* @throws InvalidArgumentException if invalid action class is passed.
|
|
|
|
*/
|
|
|
|
protected function buildAction($class) {
|
|
|
|
if(!is_subclass_of($class, 'CMSBatchAction')) {
|
|
|
|
throw new InvalidArgumentException("{$class} is not a valid subclass of CMSBatchAction");
|
|
|
|
}
|
|
|
|
return $class::singleton();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sanitise ID list from string input
|
|
|
|
*
|
|
|
|
* @param string $csvIDs
|
|
|
|
* @return array List of IDs as ints
|
|
|
|
*/
|
|
|
|
protected function cleanIDs($csvIDs) {
|
|
|
|
$ids = preg_split('/ *, */', trim($csvIDs));
|
|
|
|
foreach($ids as $k => $id) {
|
|
|
|
$ids[$k] = (int)$id;
|
|
|
|
}
|
|
|
|
return array_filter($ids);
|
|
|
|
}
|
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Get all registered actions through the static defaults set by {@link register()}.
|
|
|
|
* Filters for the currently set {@link recordClass}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-03-23 10:51:00 +01:00
|
|
|
* @return array See {@link register()} for the returned format.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function batchActions() {
|
2015-05-15 01:50:23 +02:00
|
|
|
$actions = $this->config()->batch_actions;
|
|
|
|
$recordClass = $this->recordClass;
|
|
|
|
$actions = array_filter($actions, function($action) use ($recordClass) {
|
|
|
|
return $action['recordClass'] === $recordClass;
|
|
|
|
});
|
|
|
|
return $actions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Safely query and return all pages queried
|
|
|
|
*
|
|
|
|
* @param array $ids
|
|
|
|
* @return SS_List
|
|
|
|
*/
|
|
|
|
protected function getPages($ids) {
|
|
|
|
// Check empty set
|
|
|
|
if(empty($ids)) {
|
|
|
|
return new ArrayList();
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-05-15 01:50:23 +02:00
|
|
|
$recordClass = $this->recordClass;
|
|
|
|
|
|
|
|
// Bypass translatable filter
|
|
|
|
if(class_exists('Translatable') && $recordClass::has_extension('Translatable')) {
|
|
|
|
Translatable::disable_locale_filter();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bypass versioned filter
|
2016-06-15 06:03:16 +02:00
|
|
|
if($recordClass::has_extension('SilverStripe\\ORM\\Versioning\\Versioned')) {
|
2015-05-15 01:50:23 +02:00
|
|
|
// Workaround for get_including_deleted not supporting byIDs filter very well
|
|
|
|
// Ensure we select both stage / live records
|
|
|
|
$pages = Versioned::get_including_deleted($recordClass, array(
|
|
|
|
'"RecordID" IN ('.DB::placeholders($ids).')' => $ids
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
$pages = DataObject::get($recordClass)->byIDs($ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(class_exists('Translatable') && $recordClass::has_extension('Translatable')) {
|
|
|
|
Translatable::enable_locale_filter();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $pages;
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
|
|
|
|
2012-03-27 11:54:13 +02:00
|
|
|
}
|