2011-03-23 10:51:00 +01:00
|
|
|
<?php
|
|
|
|
|
2016-08-11 01:14:02 +02:00
|
|
|
namespace SilverStripe\Admin;
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\SS_List;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\Versioning\Versioned;
|
2016-08-11 01:14:02 +02:00
|
|
|
use Object;
|
|
|
|
use Controller;
|
|
|
|
use Convert;
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-03-23 10:51:00 +01:00
|
|
|
* <code>
|
2014-08-15 08:53:05 +02:00
|
|
|
* CMSMain::register_batch_action('publishitems', new CMSBatchAction('doPublish',
|
2011-03-23 10:51:00 +01:00
|
|
|
* _t('CMSBatchActions.PUBLISHED_PAGES', 'published %d pages')));
|
|
|
|
* </code>
|
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
|
|
|
*/
|
|
|
|
abstract class CMSBatchAction extends Object {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-08-05 00:32:13 +02:00
|
|
|
protected $managedClass = 'SilverStripe\\CMS\\Model\\SiteTree';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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.
|
2016-04-01 05:27:59 +02:00
|
|
|
*
|
|
|
|
* @param SS_List $objs
|
|
|
|
* @return string
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
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
|
2016-04-01 05:27:59 +02:00
|
|
|
* @param string $successMessage The message to return as a notification.
|
2012-06-22 03:59:08 +02:00
|
|
|
* 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
|
2016-04-01 05:27:59 +02:00
|
|
|
* @param array $status A status array like batchactions builds. Should be
|
2012-06-22 03:59:08 +02:00
|
|
|
* 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
|
2016-04-01 05:27:59 +02:00
|
|
|
* @return string
|
2012-06-22 03:59:08 +02:00
|
|
|
*/
|
|
|
|
public function response($successMessage, $status) {
|
|
|
|
$count = 0;
|
|
|
|
$errors = 0;
|
|
|
|
|
|
|
|
foreach($status as $k => $v) {
|
2015-05-15 01:50:23 +02:00
|
|
|
switch($k) {
|
|
|
|
case 'error':
|
|
|
|
$errors += count($v);
|
|
|
|
break;
|
|
|
|
case 'success':
|
|
|
|
$count += count($v);
|
|
|
|
break;
|
|
|
|
}
|
2012-06-22 03:59:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$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.
|
|
|
|
*
|
2016-04-01 05:27:59 +02:00
|
|
|
* @param SS_List $objs The SS_List of objects to perform this batch action
|
2011-03-23 10:51:00 +01:00
|
|
|
* on.
|
2016-04-01 05:27:59 +02:00
|
|
|
* @param string $helperMethod The method to call on each of those objects.
|
|
|
|
* @param string $successMessage
|
|
|
|
* @param array $arguments
|
|
|
|
* @return string JSON encoded map in the following format:
|
2011-03-23 10:51:00 +01:00
|
|
|
* {
|
|
|
|
* '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()) {
|
2015-05-15 01:50:23 +02:00
|
|
|
$status = array('modified' => array(), 'error' => array(), 'deleted' => array(), 'success' => array());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-29 06:54:39 +02:00
|
|
|
foreach($objs as $obj) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-30 06:05:56 +02:00
|
|
|
// Perform the action
|
2015-05-15 01:50:23 +02:00
|
|
|
$id = $obj->ID;
|
2013-05-30 06:05:56 +02:00
|
|
|
if (!call_user_func_array(array($obj, $helperMethod), $arguments)) {
|
2015-05-15 01:50:23 +02:00
|
|
|
$status['error'][$id] = $id;
|
|
|
|
} else {
|
|
|
|
$status['success'][$id] = $id;
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
// Now make sure the tree title is appropriately updated
|
2015-05-15 01:50:23 +02:00
|
|
|
$publishedRecord = DataObject::get_by_id($this->managedClass, $id);
|
2011-03-23 10:51:00 +01:00
|
|
|
if ($publishedRecord) {
|
2015-05-15 01:50:23 +02:00
|
|
|
$status['modified'][$id] = array(
|
2011-03-23 10:51:00 +01:00
|
|
|
'TreeTitle' => $publishedRecord->TreeTitle,
|
|
|
|
);
|
2015-05-15 01:50:23 +02:00
|
|
|
} else {
|
|
|
|
$status['deleted'][$id] = $id;
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method for applicablePages() methods. Acts as a skeleton implementation.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-04-01 05:27:59 +02:00
|
|
|
* @param array $ids The IDs passed to applicablePages
|
|
|
|
* @param string $methodName The canXXX() method to call on each page to check if the action is applicable
|
|
|
|
* @param bool $checkStagePages Set to true if you want to check stage pages
|
|
|
|
* @param bool $checkLivePages Set to true if you want to check live pages (e.g, for deleted-from-draft)
|
|
|
|
* @return array
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
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);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
$applicableIDs = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$managedClass = $this->managedClass;
|
|
|
|
$draftPages = DataObject::get($managedClass)->byIDs($ids);
|
2014-08-15 08:53:05 +02: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);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
if($checkLivePages && $onlyOnLive && Object::has_extension($managedClass, 'SilverStripe\\ORM\\Versioning\\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;
|
|
|
|
}
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2015-05-15 01:50:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a list of object IDs, filter out which items can have this batch action applied
|
|
|
|
* to them.
|
|
|
|
*
|
|
|
|
* @param array $ids List of object ids
|
|
|
|
* @return array Filtered list of $ids
|
|
|
|
*/
|
|
|
|
public function applicablePages($ids) {
|
|
|
|
return $ids;
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|