2011-03-23 10:51:00 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Special request handler for admin/batchaction
|
|
|
|
*
|
2013-10-15 00:26:23 +02:00
|
|
|
* @package framework
|
|
|
|
* @subpackage admin
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
|
|
|
class CMSBatchActionHandler extends RequestHandler {
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
/** @config */
|
|
|
|
private static $batch_actions = array();
|
2011-03-23 10:51:00 +01: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',
|
|
|
|
);
|
2011-03-23 10:51:00 +01:00
|
|
|
|
|
|
|
protected $parentController;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var String
|
|
|
|
*/
|
|
|
|
protected $urlSegment;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
protected $recordClass = 'SiteTree';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a new batch action. Each batch action needs to be represented by a subclass
|
|
|
|
* of {@link CMSBatchAction}.
|
|
|
|
*
|
|
|
|
* @param $urlSegment The URL Segment of the batch action - the URL used to process this
|
|
|
|
* action will be admin/batchactions/(urlSegment)
|
|
|
|
* @param $batchActionClass The name of the CMSBatchAction subclass to register
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function register($urlSegment, $batchActionClass, $recordClass = 'SiteTree') {
|
2011-03-23 10:51:00 +01:00
|
|
|
if(is_subclass_of($batchActionClass, 'CMSBatchAction')) {
|
2013-03-21 19:48:54 +01: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $parentController
|
|
|
|
* @param string $urlSegment
|
|
|
|
* @param string $recordClass
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($parentController, $urlSegment, $recordClass = null) {
|
2011-03-23 10:51:00 +01:00
|
|
|
$this->parentController = $parentController;
|
|
|
|
$this->urlSegment = $urlSegment;
|
|
|
|
if($recordClass) $this->recordClass = $recordClass;
|
|
|
|
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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()) {
|
2011-03-23 10:51:00 +01:00
|
|
|
$this->parentController->redirectBack();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Protect against CSRF on destructive action
|
|
|
|
if(!SecurityToken::inst()->checkRequest($request)) return $this->httpError(400);
|
|
|
|
|
|
|
|
$actions = $this->batchActions();
|
|
|
|
$actionClass = $actions[$request->param('BatchAction')]['class'];
|
|
|
|
$actionHandler = new $actionClass();
|
|
|
|
|
|
|
|
// Sanitise ID list and query the database for apges
|
2012-03-27 11:54:13 +02:00
|
|
|
$ids = preg_split('/ *, */', trim($request->requestVar('csvIDs')));
|
2011-03-23 10:51:00 +01:00
|
|
|
foreach($ids as $k => $v) if(!is_numeric($v)) unset($ids[$k]);
|
|
|
|
|
|
|
|
if($ids) {
|
2012-10-09 23:59:23 +02:00
|
|
|
if(class_exists('Translatable') && SiteTree::has_extension('Translatable')) {
|
2012-09-26 23:34:00 +02:00
|
|
|
Translatable::disable_locale_filter();
|
|
|
|
}
|
2011-03-23 10:51:00 +01:00
|
|
|
|
|
|
|
$pages = DataObject::get(
|
|
|
|
$this->recordClass,
|
|
|
|
sprintf(
|
|
|
|
'"%s"."ID" IN (%s)',
|
|
|
|
ClassInfo::baseDataClass($this->recordClass),
|
|
|
|
implode(", ", $ids)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2012-10-09 23:59:23 +02:00
|
|
|
if(class_exists('Translatable') && SiteTree::has_extension('Translatable')) {
|
2012-09-26 23:34:00 +02:00
|
|
|
Translatable::enable_locale_filter();
|
|
|
|
}
|
2011-03-23 10:51:00 +01:00
|
|
|
|
2012-10-09 23:59:23 +02:00
|
|
|
$record_class = $this->recordClass;
|
|
|
|
if($record_class::has_extension('Versioned')) {
|
2011-03-23 10:51:00 +01:00
|
|
|
// If we didn't query all the pages, then find the rest on the live site
|
|
|
|
if(!$pages || $pages->Count() < sizeof($ids)) {
|
|
|
|
foreach($ids as $id) $idsFromLive[$id] = true;
|
|
|
|
if($pages) foreach($pages as $page) unset($idsFromLive[$page->ID]);
|
|
|
|
$idsFromLive = array_keys($idsFromLive);
|
|
|
|
|
|
|
|
$sql = sprintf(
|
|
|
|
'"%s"."ID" IN (%s)',
|
|
|
|
$this->recordClass,
|
|
|
|
implode(", ", $idsFromLive)
|
|
|
|
);
|
|
|
|
$livePages = Versioned::get_by_stage($this->recordClass, 'Live', $sql);
|
2012-03-09 00:17:48 +01:00
|
|
|
if($pages) {
|
|
|
|
// Can't merge into a DataList, need to condense into an actual list first
|
|
|
|
// (which will retrieve all records as objects, so its an expensive operation)
|
|
|
|
$pages = new ArrayList($pages->toArray());
|
|
|
|
$pages->merge($livePages);
|
|
|
|
} else {
|
|
|
|
$pages = $livePages;
|
|
|
|
}
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2011-05-05 12:40:24 +02:00
|
|
|
$pages = new ArrayList();
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $actionHandler->run($pages);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function handleApplicablePages($request) {
|
2011-03-23 10:51:00 +01:00
|
|
|
// Find the action handler
|
2012-04-18 13:10:57 +02:00
|
|
|
$actions = Config::inst()->get($this->class, 'batch_actions', Config::FIRST_SET);
|
2011-03-23 10:51:00 +01:00
|
|
|
$actionClass = $actions[$request->param('BatchAction')];
|
|
|
|
$actionHandler = new $actionClass['class']();
|
|
|
|
|
|
|
|
// Sanitise ID list and query the database for apges
|
2012-03-27 11:54:13 +02:00
|
|
|
$ids = preg_split('/ *, */', trim($request->requestVar('csvIDs')));
|
2011-03-23 10:51:00 +01:00
|
|
|
foreach($ids as $k => $id) $ids[$k] = (int)$id;
|
|
|
|
$ids = array_filter($ids);
|
|
|
|
|
|
|
|
if($actionHandler->hasMethod('applicablePages')) {
|
|
|
|
$applicableIDs = $actionHandler->applicablePages($ids);
|
|
|
|
} else {
|
|
|
|
$applicableIDs = $ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = new SS_HTTPResponse(json_encode($applicableIDs));
|
|
|
|
$response->addHeader("Content-type", "application/json");
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function handleConfirmation($request) {
|
2011-03-23 10:51:00 +01:00
|
|
|
// Find the action handler
|
2012-04-18 13:10:57 +02:00
|
|
|
$actions = Config::inst()->get($this->class, 'batch_actions', Config::FIRST_SET);
|
2011-03-23 10:51:00 +01:00
|
|
|
$actionClass = $actions[$request->param('BatchAction')];
|
|
|
|
$actionHandler = new $actionClass();
|
|
|
|
|
|
|
|
// Sanitise ID list and query the database for apges
|
2012-03-27 11:54:13 +02:00
|
|
|
$ids = preg_split('/ *, */', trim($request->requestVar('csvIDs')));
|
2011-03-23 10:51:00 +01:00
|
|
|
foreach($ids as $k => $id) $ids[$k] = (int)$id;
|
|
|
|
$ids = array_filter($ids);
|
|
|
|
|
|
|
|
if($actionHandler->hasMethod('confirmationDialog')) {
|
|
|
|
$response = new SS_HTTPResponse(json_encode($actionHandler->confirmationDialog($ids)));
|
|
|
|
} else {
|
|
|
|
$response = new SS_HTTPResponse(json_encode(array('alert' => false)));
|
|
|
|
}
|
|
|
|
|
|
|
|
$response->addHeader("Content-type", "application/json");
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
*/
|
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();
|
2011-03-23 10:51:00 +01:00
|
|
|
|
|
|
|
foreach($actions as $urlSegment => $action) {
|
|
|
|
$actionClass = $action['class'];
|
|
|
|
$actionObj = new $actionClass();
|
|
|
|
if($actionObj->canView()) {
|
|
|
|
$actionDef = new ArrayData(array(
|
|
|
|
"Link" => Controller::join_links($this->Link(), $urlSegment),
|
|
|
|
"Title" => $actionObj->getActionTitle(),
|
|
|
|
));
|
|
|
|
$actionList->push($actionDef);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $actionList;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all registered actions through the static defaults set by {@link register()}.
|
|
|
|
* Filters for the currently set {@link recordClass}.
|
|
|
|
*
|
|
|
|
* @return array See {@link register()} for the returned format.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function batchActions() {
|
2012-04-18 13:11:53 +02:00
|
|
|
$actions = Config::inst()->get($this->class, 'batch_actions', Config::FIRST_SET);
|
2011-03-23 10:51:00 +01:00
|
|
|
if($actions) foreach($actions as $action) {
|
|
|
|
if($action['recordClass'] != $this->recordClass) unset($action);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $actions;
|
|
|
|
}
|
|
|
|
|
2012-03-27 11:54:13 +02:00
|
|
|
}
|