mirror of
https://github.com/silverstripe/silverstripe-cms
synced 2024-10-22 08:05:56 +02:00
3498e41b0a
Conflicts: _config.php code/AssetAdmin.php code/AssetTableField.php code/CMSBatchAction.php code/CMSBatchActionHandler.php code/CMSMain.php code/CMSSiteTreeFilter.php code/CommentAdmin.php code/CommentTableField.php code/GroupImportForm.php code/LeftAndMain.php code/MemberImportForm.php code/MemberTableField.php code/ModelAdmin.php code/PermissionRoleAdmin.php code/ReportAdmin.php code/SecurityAdmin.php code/SideReport.php code/reports/BrokenLinksReport.php code/sitefeatures/MathSpamProtection.php code/sitefeatures/PageComment.php code/sitefeatures/PageCommentInterface.php code/sitefeatures/SSAkismet.php css/cms_left.css css/cms_right.css css/layout.css css/silverstripe.tabs.css images/loading.gif javascript/AssetAdmin.js javascript/AssetTableField.js javascript/CMSMain_left.js javascript/CMSMain_right.js javascript/LangSelector.js javascript/LeftAndMain.Tree.js javascript/LeftAndMain.js javascript/LeftAndMain_right.js javascript/MemberImportForm.js javascript/MemberTableField.js javascript/ModelAdmin.js javascript/PageCommentInterface.js javascript/ReportAdmin_left.js javascript/ReportAdmin_right.js javascript/SecurityAdmin_right.js javascript/SideReports.js javascript/SideTabs.js javascript/SitetreeAccess.js javascript/TranslationTab.js javascript/WidgetAreaEditor.js javascript/lang/cs_CZ.js javascript/lang/en_US.js javascript/lang/sk_SK.js javascript/tinymce_ssbuttons/editor_plugin_src.js javascript/tinymce_ssmacron/editor_plugin_src.js lang/en_US.php templates/Includes/AssetAdmin_left.ss templates/Includes/AssetTableField.ss templates/Includes/CMSMain_left.ss templates/Includes/CommentTableField.ss templates/Includes/ModelAdmin_left.ss templates/Includes/SecurityAdmin_left.ss templates/PageCommentInterface.ss templates/PageCommentInterface_singlecomment.ss templates/ReportAdminForm.ss tests/CMSMainTest.php tests/CMSMainTest.yml tests/LeftAndMainTest.php tests/MemberTableFieldTest.php tests/MemberTableFieldTest.yml thirdparty/multifile/multifile.js
210 lines
6.2 KiB
PHP
210 lines
6.2 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Special request handler for admin/batchaction
|
|
*
|
|
* @package cms
|
|
* @subpackage batchaction
|
|
*/
|
|
class CMSBatchActionHandler extends RequestHandler {
|
|
|
|
static $batch_actions = array();
|
|
|
|
static $url_handlers = array(
|
|
'$BatchAction/applicablepages' => 'handleApplicablePages',
|
|
'$BatchAction/confirmation' => 'handleConfirmation',
|
|
'$BatchAction' => 'handleAction',
|
|
);
|
|
|
|
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
|
|
*/
|
|
static function register($urlSegment, $batchActionClass, $recordClass = 'SiteTree') {
|
|
if(is_subclass_of($batchActionClass, 'CMSBatchAction')) {
|
|
self::$batch_actions[$urlSegment] = array(
|
|
'class' => $batchActionClass,
|
|
'recordClass' => $recordClass
|
|
);
|
|
} else {
|
|
user_error("CMSBatchActionHandler::register() - Bad class '$batchActionClass'", E_USER_ERROR);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param string $parentController
|
|
* @param string $urlSegment
|
|
* @param string $recordClass
|
|
*/
|
|
function __construct($parentController, $urlSegment, $recordClass = null) {
|
|
$this->parentController = $parentController;
|
|
$this->urlSegment = $urlSegment;
|
|
if($recordClass) $this->recordClass = $recordClass;
|
|
|
|
parent::__construct();
|
|
}
|
|
|
|
function Link() {
|
|
return Controller::join_links($this->parentController->Link(), $this->urlSegment);
|
|
}
|
|
|
|
function handleAction($request) {
|
|
// This method can't be called without ajax.
|
|
if(!$this->parentController->isAjax()) {
|
|
$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
|
|
$ids = split(' *, *', trim($request->requestVar('csvIDs')));
|
|
foreach($ids as $k => $v) if(!is_numeric($v)) unset($ids[$k]);
|
|
|
|
if($ids) {
|
|
if(Object::has_extension('SiteTree','Translatable')) Translatable::disable_locale_filter();
|
|
|
|
$pages = DataObject::get(
|
|
$this->recordClass,
|
|
sprintf(
|
|
'"%s"."ID" IN (%s)',
|
|
ClassInfo::baseDataClass($this->recordClass),
|
|
implode(", ", $ids)
|
|
)
|
|
);
|
|
|
|
if(Object::has_extension('SiteTree','Translatable')) Translatable::enable_locale_filter();
|
|
|
|
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;
|
|
}
|
|
}
|
|
} else {
|
|
$pages = new DataObjectSet();
|
|
}
|
|
|
|
return $actionHandler->run($pages);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
$response = new SS_HTTPResponse(json_encode($applicableIDs));
|
|
$response->addHeader("Content-type", "application/json");
|
|
return $response;
|
|
}
|
|
|
|
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')) {
|
|
$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;
|
|
}
|
|
|
|
/**
|
|
* Return a DataObjectSet of ArrayData objects containing the following pieces of info
|
|
* about each batch action:
|
|
* - Link
|
|
* - Title
|
|
* - DoingText
|
|
*/
|
|
function batchActionList() {
|
|
$actions = $this->batchActions();
|
|
$actionList = new DataObjectSet();
|
|
|
|
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(),
|
|
"DoingText" => $actionObj->getDoingText(),
|
|
));
|
|
$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.
|
|
*/
|
|
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;
|
|
}
|
|
|
|
} |