From feeb1e40fad12d5e32a9bbd2d70b1a924b860745 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Tue, 13 Apr 2010 03:58:16 +0000 Subject: [PATCH] API CHANGE: Added applicablePagesHelper to CMSBatchAction to ease the process of creating new applicable page methods. ENHANCEMENT: Added applicable pages checks to delete from live, delete from draft, and publish (from r94775) (from r96821) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@102680 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- code/CMSBatchAction.php | 52 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/code/CMSBatchAction.php b/code/CMSBatchAction.php index 84a366a4..16da258d 100644 --- a/code/CMSBatchAction.php +++ b/code/CMSBatchAction.php @@ -71,6 +71,46 @@ abstract class CMSBatchAction extends Object { 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')); + $draftPages = DataObject::get("SiteTree", "\"SiteTree\".\"ID\" IN ($SQL_ids)"); + + $onlyOnLive = array_fill_keys($ids, true); + if($checkStagePages) { + foreach($draftPages as $page) { + unset($onlyOnLive[$page->ID]); + if($page->$methodName()) $applicableIDs[] = $page->ID; + } + } + + // 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("SiteTree", "Live", "\"SiteTree\".\"ID\" IN ($SQL_ids)"); + + if($livePages) foreach($livePages as $page) { + if($page->$methodName()) $applicableIDs[] = $page->ID; + } + } + + return $applicableIDs; + } // if your batchaction has parameters, return a fieldset here function getParameterFields() { @@ -94,6 +134,10 @@ class CMSBatchAction_Publish extends CMSBatchAction { _t('CMSBatchActions.PUBLISHED_PAGES', 'Published %d pages, %d failures') ); } + + function applicablePages($ids) { + return $this->applicablePagesHelper($ids, 'canPublish', true, false); + } } /** @@ -155,6 +199,10 @@ class CMSBatchAction_Delete extends CMSBatchAction { return Convert::raw2json($status); } + + function applicablePages($ids) { + return $this->applicablePagesHelper($ids, 'canDelete', true, false); + } } /** @@ -195,4 +243,8 @@ class CMSBatchAction_DeleteFromLive extends CMSBatchAction { return Convert::raw2json($status); } + + function applicablePages($ids) { + return $this->applicablePagesHelper($ids, 'canDelete', false, true); + } } \ No newline at end of file