MINOR Making managed class configurable in CMSBatchAction in order to remove hard dependency to cms module (still defaults to SiteTree)

This commit is contained in:
Ingo Schommer 2011-03-29 17:54:39 +13:00
parent 95dcc74c10
commit a467eda0d2

View File

@ -14,6 +14,9 @@
* @subpackage batchaction * @subpackage batchaction
*/ */
abstract class CMSBatchAction extends Object { abstract class CMSBatchAction extends Object {
protected $managedClass = 'SiteTree';
/** /**
* The the text to show in the dropdown for this action * The the text to show in the dropdown for this action
*/ */
@ -23,13 +26,13 @@ abstract class CMSBatchAction extends Object {
* Run this action for the given set of pages. * Run this action for the given set of pages.
* Return a set of status-updated JavaScript to return to the CMS. * Return a set of status-updated JavaScript to return to the CMS.
*/ */
abstract function run(DataObjectSet $pages); abstract function run(DataObjectSet $objs);
/** /**
* Helper method for processing batch actions. * Helper method for processing batch actions.
* Returns a set of status-updating JavaScript to return to the CMS. * Returns a set of status-updating JavaScript to return to the CMS.
* *
* @param $pages The DataObjectSet of SiteTree objects to perform this batch action * @param $objs The DataObjectSet of objects to perform this batch action
* on. * on.
* @param $helperMethod The method to call on each of those objects. * @param $helperMethod The method to call on each of those objects.
* @return JSON encoded map in the following format: * @return JSON encoded map in the following format:
@ -43,32 +46,32 @@ abstract class CMSBatchAction extends Object {
* } * }
* } * }
*/ */
public function batchaction(DataObjectSet $pages, $helperMethod, $successMessage, $arguments = array()) { public function batchaction(DataObjectSet $objs, $helperMethod, $successMessage, $arguments = array()) {
$status = array('modified' => array(), 'error' => array()); $status = array('modified' => array(), 'error' => array());
foreach($pages as $page) { foreach($objs as $obj) {
// Perform the action // Perform the action
if (!call_user_func_array(array($page, $helperMethod), $arguments)) { if (!call_user_func_array(array($obj, $helperMethod), $arguments)) {
$status['error'][$page->ID] = ''; $status['error'][$obj->ID] = '';
} }
// Now make sure the tree title is appropriately updated // Now make sure the tree title is appropriately updated
$publishedRecord = DataObject::get_by_id('SiteTree', $page->ID); $publishedRecord = DataObject::get_by_id($this->managedClass, $obj->ID);
if ($publishedRecord) { if ($publishedRecord) {
$status['modified'][$publishedRecord->ID] = array( $status['modified'][$publishedRecord->ID] = array(
'TreeTitle' => $publishedRecord->TreeTitle, 'TreeTitle' => $publishedRecord->TreeTitle,
); );
} }
$page->destroy(); $obj->destroy();
unset($page); unset($obj);
} }
$response = Controller::curr()->getResponse(); $response = Controller::curr()->getResponse();
if($response) { if($response) {
$response->setStatusCode( $response->setStatusCode(
200, 200,
sprintf($successMessage, $pages->Count(), count($status['error'])) sprintf($successMessage, $objs->Count(), count($status['error']))
); );
} }
@ -92,23 +95,39 @@ abstract class CMSBatchAction extends Object {
$applicableIDs = array(); $applicableIDs = array();
$SQL_ids = implode(', ', array_filter($ids, 'is_numeric')); $SQL_ids = implode(', ', array_filter($ids, 'is_numeric'));
$draftPages = DataObject::get("SiteTree", "\"SiteTree\".\"ID\" IN ($SQL_ids)"); $draftPages = DataObject::get(
$this->managedClass,
sprintf(
"\"%s\".\"ID\" IN (%s)",
ClassInfo::baseDataClass($this->managedClass),
$SQL_ids
)
);
$onlyOnLive = array_fill_keys($ids, true); $onlyOnLive = array_fill_keys($ids, true);
if($checkStagePages) { if($checkStagePages) {
foreach($draftPages as $page) { foreach($draftPages as $obj) {
unset($onlyOnLive[$page->ID]); unset($onlyOnLive[$obj->ID]);
if($page->$methodName()) $applicableIDs[] = $page->ID; if($obj->$methodName()) $applicableIDs[] = $obj->ID;
} }
} }
if(Object::has_extension($this->managedClass, 'Versioned')) {
// Get the pages that only exist on live (deleted from stage) // Get the pages that only exist on live (deleted from stage)
if($checkLivePages && $onlyOnLive) { if($checkLivePages && $onlyOnLive) {
$SQL_ids = implode(', ', array_keys($onlyOnLive)); $SQL_ids = implode(', ', array_keys($onlyOnLive));
$livePages = Versioned::get_by_stage("SiteTree", "Live", "\"SiteTree\".\"ID\" IN ($SQL_ids)"); $livePages = Versioned::get_by_stage(
$this->managedClass, "Live",
sprintf(
"\"%s\".\"ID\" IN (%s)",
ClassInfo::baseDataClass($this->managedClass),
$SQL_ids
)
);
if($livePages) foreach($livePages as $page) { if($livePages) foreach($livePages as $obj) {
if($page->$methodName()) $applicableIDs[] = $page->ID; if($obj->$methodName()) $applicableIDs[] = $obj->ID;
}
} }
} }