2011-03-23 10:51:00 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A class representing back actions.
|
2011-03-23 11:07:31 +01:00
|
|
|
* See CMSMain.BatchActions.js on how to add custom javascript
|
2011-03-23 10:51:00 +01:00
|
|
|
* functionality.
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* CMSMain::register_batch_action('publishitems', new CMSBatchAction('doPublish',
|
|
|
|
* _t('CMSBatchActions.PUBLISHED_PAGES', 'published %d pages')));
|
|
|
|
* </code>
|
|
|
|
*
|
2013-10-15 00:26:23 +02:00
|
|
|
* @package framework
|
|
|
|
* @subpackage admin
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
|
|
|
abstract class CMSBatchAction extends Object {
|
2011-03-29 06:54:39 +02:00
|
|
|
|
|
|
|
protected $managedClass = 'SiteTree';
|
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* The the text to show in the dropdown for this action
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
abstract public function getActionTitle();
|
2011-03-23 10:51:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Run this action for the given set of pages.
|
|
|
|
* Return a set of status-updated JavaScript to return to the CMS.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
abstract public function run(SS_List $objs);
|
2012-06-22 03:59:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method for responding to a back action request
|
|
|
|
* @param $successMessage string - The message to return as a notification.
|
|
|
|
* Can have up to two %d's in it. The first will be replaced by the number of successful
|
|
|
|
* changes, the second by the number of failures
|
|
|
|
* @param $status array - A status array like batchactions builds. Should be
|
|
|
|
* key => value pairs, the key can be any string: "error" indicates errors, anything
|
|
|
|
* else indicates a type of success. The value is an array. We don't care what's in it,
|
|
|
|
* we just use count($value) to find the number of items that succeeded or failed
|
|
|
|
*/
|
|
|
|
public function response($successMessage, $status) {
|
|
|
|
$count = 0;
|
|
|
|
$errors = 0;
|
|
|
|
|
|
|
|
foreach($status as $k => $v) {
|
|
|
|
if ($k == 'errors') $errors = count($v);
|
|
|
|
else $count += count($v);
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = Controller::curr()->getResponse();
|
|
|
|
|
|
|
|
if($response) {
|
|
|
|
$response->setStatusCode(
|
|
|
|
200,
|
|
|
|
sprintf($successMessage, $count, $errors)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Convert::raw2json($status);
|
|
|
|
}
|
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Helper method for processing batch actions.
|
|
|
|
* Returns a set of status-updating JavaScript to return to the CMS.
|
|
|
|
*
|
2011-10-26 08:09:04 +02:00
|
|
|
* @param $objs The SS_List of objects to perform this batch action
|
2011-03-23 10:51:00 +01:00
|
|
|
* on.
|
|
|
|
* @param $helperMethod The method to call on each of those objects.
|
|
|
|
* @return JSON encoded map in the following format:
|
|
|
|
* {
|
|
|
|
* 'modified': {
|
|
|
|
* 3: {'TreeTitle': 'Page3'},
|
|
|
|
* 5: {'TreeTitle': 'Page5'}
|
|
|
|
* },
|
|
|
|
* 'deleted': {
|
|
|
|
* // all deleted pages
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*/
|
2011-05-02 09:14:05 +02:00
|
|
|
public function batchaction(SS_List $objs, $helperMethod, $successMessage, $arguments = array()) {
|
2011-03-23 10:51:00 +01:00
|
|
|
$status = array('modified' => array(), 'error' => array());
|
|
|
|
|
2011-03-29 06:54:39 +02:00
|
|
|
foreach($objs as $obj) {
|
2011-03-23 10:51:00 +01:00
|
|
|
|
2013-05-30 06:05:56 +02:00
|
|
|
// Perform the action
|
|
|
|
if (!call_user_func_array(array($obj, $helperMethod), $arguments)) {
|
|
|
|
$status['error'][$obj->ID] = '';
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now make sure the tree title is appropriately updated
|
2011-03-29 06:54:39 +02:00
|
|
|
$publishedRecord = DataObject::get_by_id($this->managedClass, $obj->ID);
|
2011-03-23 10:51:00 +01:00
|
|
|
if ($publishedRecord) {
|
|
|
|
$status['modified'][$publishedRecord->ID] = array(
|
|
|
|
'TreeTitle' => $publishedRecord->TreeTitle,
|
|
|
|
);
|
|
|
|
}
|
2011-03-29 06:54:39 +02:00
|
|
|
$obj->destroy();
|
|
|
|
unset($obj);
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
|
|
|
|
2012-06-22 03:59:08 +02:00
|
|
|
return $this->response($successMessage, $status);
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method for applicablePages() methods. Acts as a skeleton implementation.
|
|
|
|
*
|
|
|
|
* @param $ids The IDs passed to applicablePages
|
|
|
|
* @param $methodName The canXXX() method to call on each page to check if the action is applicable
|
|
|
|
* @param $checkStagePages Set to true if you want to check stage pages
|
|
|
|
* @param $checkLivePages Set to true if you want to check live pages (e.g, for deleted-from-draft)
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function applicablePagesHelper($ids, $methodName, $checkStagePages = true, $checkLivePages = true) {
|
2011-03-23 10:51:00 +01:00
|
|
|
if(!is_array($ids)) user_error("Bad \$ids passed to applicablePagesHelper()", E_USER_WARNING);
|
|
|
|
if(!is_string($methodName)) user_error("Bad \$methodName passed to applicablePagesHelper()", E_USER_WARNING);
|
|
|
|
|
|
|
|
$applicableIDs = array();
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$managedClass = $this->managedClass;
|
|
|
|
$draftPages = DataObject::get($managedClass)->byIDs($ids);
|
2011-03-23 10:51:00 +01:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Filter out the live-only ids
|
2011-03-23 10:51:00 +01:00
|
|
|
$onlyOnLive = array_fill_keys($ids, true);
|
|
|
|
if($checkStagePages) {
|
2011-03-29 06:54:39 +02:00
|
|
|
foreach($draftPages as $obj) {
|
|
|
|
unset($onlyOnLive[$obj->ID]);
|
|
|
|
if($obj->$methodName()) $applicableIDs[] = $obj->ID;
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
$onlyOnLive = array_keys($onlyOnLive);
|
2011-03-23 10:51:00 +01:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
if($checkLivePages && $onlyOnLive && $managedClass::has_extension('Versioned')) {
|
2011-03-29 06:54:39 +02:00
|
|
|
// Get the pages that only exist on live (deleted from stage)
|
2013-06-21 00:32:08 +02:00
|
|
|
$livePages = Versioned::get_by_stage($managedClass, "Live")->byIDs($onlyOnLive);
|
|
|
|
foreach($livePages as $obj) {
|
|
|
|
if($obj->$methodName()) $applicableIDs[] = $obj->ID;
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $applicableIDs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-28 03:37:27 +02:00
|
|
|
// if your batchaction has parameters, return a FieldList here
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getParameterFields() {
|
2011-03-23 10:51:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If you wish to restrict the batch action to some users, overload this function.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function canView() {
|
2011-03-23 10:51:00 +01:00
|
|
|
return true;
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|