2009-05-14 23:51:54 +02:00
|
|
|
<?php
|
2010-12-17 02:01:12 +01:00
|
|
|
|
2009-05-14 23:51:54 +02:00
|
|
|
/**
|
|
|
|
* Special request handler for admin/batchaction
|
2009-05-14 23:56:35 +02:00
|
|
|
*
|
|
|
|
* @package cms
|
|
|
|
* @subpackage batchaction
|
2009-05-14 23:51:54 +02:00
|
|
|
*/
|
|
|
|
class CMSBatchActionHandler extends RequestHandler {
|
2010-12-17 02:01:12 +01:00
|
|
|
|
2009-11-21 04:19:02 +01:00
|
|
|
static $batch_actions = array();
|
2009-05-14 23:51:54 +02:00
|
|
|
|
|
|
|
static $url_handlers = array(
|
2010-04-13 08:42:21 +02:00
|
|
|
'$BatchAction/applicablepages' => 'handleApplicablePages',
|
2010-10-04 07:25:44 +02:00
|
|
|
'$BatchAction/confirmation' => 'handleConfirmation',
|
2010-04-13 08:42:21 +02:00
|
|
|
'$BatchAction' => 'handleAction',
|
2009-05-14 23:51:54 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
protected $parentController;
|
2009-11-21 04:19:02 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var String
|
|
|
|
*/
|
2009-05-14 23:51:54 +02:00
|
|
|
protected $urlSegment;
|
|
|
|
|
2009-11-21 04:19:02 +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.
|
|
|
|
*/
|
|
|
|
protected $recordClass = 'SiteTree';
|
|
|
|
|
2009-05-14 23:51:54 +02:00
|
|
|
/**
|
|
|
|
* Register a new batch action. Each batch action needs to be represented by a subclass
|
2009-11-21 04:19:02 +01:00
|
|
|
* of {@link CMSBatchAction}.
|
2009-05-14 23:51:54 +02:00
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
2009-11-21 04:19:02 +01:00
|
|
|
static function register($urlSegment, $batchActionClass, $recordClass = 'SiteTree') {
|
2009-05-14 23:51:54 +02:00
|
|
|
if(is_subclass_of($batchActionClass, 'CMSBatchAction')) {
|
2009-11-21 04:19:02 +01:00
|
|
|
self::$batch_actions[$urlSegment] = array(
|
|
|
|
'class' => $batchActionClass,
|
|
|
|
'recordClass' => $recordClass
|
|
|
|
);
|
2009-05-14 23:51:54 +02:00
|
|
|
} else {
|
2009-06-28 04:49:37 +02:00
|
|
|
user_error("CMSBatchActionHandler::register() - Bad class '$batchActionClass'", E_USER_ERROR);
|
2009-05-14 23:51:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-21 04:19:02 +01:00
|
|
|
/**
|
|
|
|
* @param string $parentController
|
|
|
|
* @param string $urlSegment
|
|
|
|
* @param string $recordClass
|
|
|
|
*/
|
|
|
|
function __construct($parentController, $urlSegment, $recordClass = null) {
|
2009-05-14 23:51:54 +02:00
|
|
|
$this->parentController = $parentController;
|
|
|
|
$this->urlSegment = $urlSegment;
|
2009-11-21 04:19:02 +01:00
|
|
|
if($recordClass) $this->recordClass = $recordClass;
|
|
|
|
|
2009-05-14 23:51:54 +02:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
function Link() {
|
|
|
|
return Controller::join_links($this->parentController->Link(), $this->urlSegment);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleAction($request) {
|
|
|
|
// This method can't be called without ajax.
|
2010-10-13 04:10:28 +02:00
|
|
|
if(!$this->parentController->isAjax()) {
|
|
|
|
$this->parentController->redirectBack();
|
2009-05-14 23:51:54 +02:00
|
|
|
return;
|
|
|
|
}
|
2010-11-01 02:29:02 +01:00
|
|
|
|
|
|
|
// Protect against CSRF on destructive action
|
|
|
|
if(!SecurityToken::inst()->checkRequest($request)) return $this->httpError(400);
|
2009-05-14 23:51:54 +02:00
|
|
|
|
2009-11-21 04:19:02 +01:00
|
|
|
$actions = $this->batchActions();
|
|
|
|
$actionClass = $actions[$request->param('BatchAction')]['class'];
|
2009-05-14 23:51:54 +02:00
|
|
|
$actionHandler = new $actionClass();
|
|
|
|
|
|
|
|
// Sanitise ID list and query the database for apges
|
|
|
|
$ids = split(' *, *', trim($request->requestVar('csvIDs')));
|
|
|
|
foreach($ids as $k => $v) if(!is_numeric($v)) unset($ids[$k]);
|
|
|
|
|
|
|
|
if($ids) {
|
2010-12-17 02:01:12 +01:00
|
|
|
if(Object::has_extension('SiteTree','Translatable')) Translatable::disable_locale_filter();
|
|
|
|
|
2009-11-21 04:19:02 +01:00
|
|
|
$pages = DataObject::get(
|
|
|
|
$this->recordClass,
|
|
|
|
sprintf(
|
|
|
|
'"%s"."ID" IN (%s)',
|
|
|
|
ClassInfo::baseDataClass($this->recordClass),
|
|
|
|
implode(", ", $ids)
|
|
|
|
)
|
|
|
|
);
|
2009-05-14 23:51:54 +02:00
|
|
|
|
2010-12-17 02:01:12 +01:00
|
|
|
if(Object::has_extension('SiteTree','Translatable')) Translatable::enable_locale_filter();
|
|
|
|
|
2009-11-21 04:19:02 +01:00
|
|
|
if(Object::has_extension($this->recordClass, 'Versioned')) {
|
|
|
|
// 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);
|
|
|
|
if($pages) $pages->merge($livePages);
|
|
|
|
else $pages = $livePages;
|
|
|
|
}
|
2009-05-14 23:51:54 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$pages = new DataObjectSet();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $actionHandler->run($pages);
|
|
|
|
}
|
2010-04-13 08:42:21 +02:00
|
|
|
|
|
|
|
function handleApplicablePages($request) {
|
|
|
|
// Find the action handler
|
|
|
|
$actions = Object::get_static($this->class, 'batch_actions');
|
|
|
|
$actionClass = $actions[$request->param('BatchAction')];
|
|
|
|
$actionHandler = new $actionClass['class']();
|
|
|
|
|
|
|
|
// Sanitise ID list and query the database for apges
|
|
|
|
$ids = split(' *, *', trim($request->requestVar('csvIDs')));
|
|
|
|
foreach($ids as $k => $id) $ids[$k] = (int)$id;
|
|
|
|
$ids = array_filter($ids);
|
|
|
|
|
|
|
|
if($actionHandler->hasMethod('applicablePages')) {
|
|
|
|
$applicableIDs = $actionHandler->applicablePages($ids);
|
|
|
|
} else {
|
|
|
|
$applicableIDs = $ids;
|
|
|
|
}
|
2011-03-22 04:45:13 +01:00
|
|
|
|
|
|
|
$response = new SS_HTTPResponse(Convert::raw2json(array_values($applicableIDs)));
|
2010-04-13 08:42:21 +02:00
|
|
|
$response->addHeader("Content-type", "application/json");
|
|
|
|
return $response;
|
|
|
|
}
|
2009-05-14 23:51:54 +02:00
|
|
|
|
2010-10-04 07:25:44 +02:00
|
|
|
function handleConfirmation($request) {
|
|
|
|
// Find the action handler
|
|
|
|
$actions = Object::get_static($this->class, 'batch_actions');
|
|
|
|
$actionClass = $actions[$request->param('BatchAction')];
|
|
|
|
$actionHandler = new $actionClass();
|
|
|
|
|
|
|
|
// Sanitise ID list and query the database for apges
|
|
|
|
$ids = split(' *, *', trim($request->requestVar('csvIDs')));
|
|
|
|
foreach($ids as $k => $id) $ids[$k] = (int)$id;
|
|
|
|
$ids = array_filter($ids);
|
|
|
|
|
|
|
|
if($actionHandler->hasMethod('confirmationDialog')) {
|
2010-02-05 02:27:29 +01:00
|
|
|
$response = new SS_HTTPResponse(json_encode($actionHandler->confirmationDialog($ids)));
|
2010-10-04 07:25:44 +02:00
|
|
|
} else {
|
2010-02-05 02:27:29 +01:00
|
|
|
$response = new SS_HTTPResponse(json_encode(array('alert' => false)));
|
2010-10-04 07:25:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$response->addHeader("Content-type", "application/json");
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2009-05-14 23:51:54 +02:00
|
|
|
/**
|
|
|
|
* Return a DataObjectSet of ArrayData objects containing the following pieces of info
|
|
|
|
* about each batch action:
|
|
|
|
* - Link
|
|
|
|
* - Title
|
|
|
|
*/
|
|
|
|
function batchActionList() {
|
2009-11-21 04:19:02 +01:00
|
|
|
$actions = $this->batchActions();
|
2009-05-14 23:51:54 +02:00
|
|
|
$actionList = new DataObjectSet();
|
|
|
|
|
2009-11-21 04:19:02 +01:00
|
|
|
foreach($actions as $urlSegment => $action) {
|
|
|
|
$actionClass = $action['class'];
|
2009-05-14 23:51:54 +02:00
|
|
|
$actionObj = new $actionClass();
|
2010-04-13 06:24:48 +02:00
|
|
|
if($actionObj->canView()) {
|
|
|
|
$actionDef = new ArrayData(array(
|
|
|
|
"Link" => Controller::join_links($this->Link(), $urlSegment),
|
2010-01-13 01:09:30 +01:00
|
|
|
"Title" => $actionObj->getActionTitle(),
|
2010-04-13 06:24:48 +02:00
|
|
|
));
|
|
|
|
$actionList->push($actionDef);
|
|
|
|
}
|
2009-05-14 23:51:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $actionList;
|
|
|
|
}
|
|
|
|
|
2009-11-21 04:19:02 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
function batchActions() {
|
|
|
|
$actions = Object::get_static($this->class, 'batch_actions');
|
|
|
|
if($actions) foreach($actions as $action) {
|
|
|
|
if($action['recordClass'] != $this->recordClass) unset($action);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $actions;
|
|
|
|
}
|
2009-05-14 23:51:54 +02:00
|
|
|
|
|
|
|
}
|