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>
|
|
|
|
*
|
|
|
|
* @package cms
|
|
|
|
* @subpackage batchaction
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
abstract function getActionTitle();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run this action for the given set of pages.
|
|
|
|
* Return a set of status-updated JavaScript to return to the CMS.
|
|
|
|
*/
|
2011-05-02 09:14:05 +02:00
|
|
|
abstract function run(SS_List $objs);
|
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-03-29 06:54:39 +02:00
|
|
|
* @param $objs The DataObjectSet 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
|
|
|
|
|
|
|
// Perform the action
|
2011-03-29 06:54:39 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
$response = Controller::curr()->getResponse();
|
|
|
|
if($response) {
|
|
|
|
$response->setStatusCode(
|
|
|
|
200,
|
2011-03-29 06:54:39 +02:00
|
|
|
sprintf($successMessage, $objs->Count(), count($status['error']))
|
2011-03-23 10:51:00 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Convert::raw2json($status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
*/
|
|
|
|
function applicablePagesHelper($ids, $methodName, $checkStagePages = true, $checkLivePages = true) {
|
|
|
|
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();
|
|
|
|
|
|
|
|
$SQL_ids = implode(', ', array_filter($ids, 'is_numeric'));
|
2011-03-29 06:54:39 +02:00
|
|
|
$draftPages = DataObject::get(
|
|
|
|
$this->managedClass,
|
|
|
|
sprintf(
|
|
|
|
"\"%s\".\"ID\" IN (%s)",
|
|
|
|
ClassInfo::baseDataClass($this->managedClass),
|
|
|
|
$SQL_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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-29 06:54:39 +02:00
|
|
|
if(Object::has_extension($this->managedClass, 'Versioned')) {
|
|
|
|
// Get the pages that only exist on live (deleted from stage)
|
|
|
|
if($checkLivePages && $onlyOnLive) {
|
|
|
|
$SQL_ids = implode(', ', array_keys($onlyOnLive));
|
|
|
|
$livePages = Versioned::get_by_stage(
|
|
|
|
$this->managedClass, "Live",
|
|
|
|
sprintf(
|
|
|
|
"\"%s\".\"ID\" IN (%s)",
|
|
|
|
ClassInfo::baseDataClass($this->managedClass),
|
|
|
|
$SQL_ids
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if($livePages) foreach($livePages as $obj) {
|
|
|
|
if($obj->$methodName()) $applicableIDs[] = $obj->ID;
|
|
|
|
}
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $applicableIDs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// if your batchaction has parameters, return a fieldset here
|
|
|
|
function getParameterFields() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If you wish to restrict the batch action to some users, overload this function.
|
|
|
|
*/
|
|
|
|
function canView() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|