2009-05-14 23:51:54 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2009-11-21 04:15:40 +01:00
|
|
|
* A class representing back actions.
|
|
|
|
* See cms/javascript/CMSMain.BatchActions.js on how to add custom javascript
|
|
|
|
* functionality.
|
2009-05-14 23:51:54 +02:00
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* CMSMain::register_batch_action('publishitems', new CMSBatchAction('doPublish',
|
2009-05-14 23:56:35 +02:00
|
|
|
* _t('CMSBatchActions.PUBLISHED_PAGES', 'published %d pages')));
|
2009-05-14 23:51:54 +02:00
|
|
|
* </code>
|
2009-05-14 23:56:35 +02:00
|
|
|
*
|
|
|
|
* @package cms
|
|
|
|
* @subpackage batchaction
|
2009-05-14 23:51:54 +02:00
|
|
|
*/
|
|
|
|
abstract class CMSBatchAction extends Object {
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
abstract function run(DataObjectSet $pages);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method for processing batch actions.
|
|
|
|
* Returns a set of status-updating JavaScript to return to the CMS.
|
|
|
|
*
|
|
|
|
* @param $pages The DataObjectSet of SiteTree objects to perform this batch action
|
|
|
|
* on.
|
|
|
|
* @param $helperMethod The method to call on each of those objects.
|
2009-11-21 04:15:40 +01:00
|
|
|
* @return JSON encoded map in the following format:
|
|
|
|
* {
|
|
|
|
* 'modified': {
|
|
|
|
* 3: {'TreeTitle': 'Page3'},
|
|
|
|
* 5: {'TreeTitle': 'Page5'}
|
|
|
|
* },
|
|
|
|
* 'deleted': {
|
|
|
|
* // all deleted pages
|
|
|
|
* }
|
|
|
|
* }
|
2009-05-14 23:51:54 +02:00
|
|
|
*/
|
2009-11-21 04:20:43 +01:00
|
|
|
public function batchaction(DataObjectSet $pages, $helperMethod, $successMessage, $arguments = array()) {
|
2009-11-21 04:15:40 +01:00
|
|
|
$status = array('modified' => array(), 'error' => array());
|
2009-10-30 04:27:27 +01:00
|
|
|
|
2009-05-14 23:51:54 +02:00
|
|
|
foreach($pages as $page) {
|
2009-10-16 00:44:08 +02:00
|
|
|
|
2009-05-14 23:51:54 +02:00
|
|
|
// Perform the action
|
2009-10-30 04:27:27 +01:00
|
|
|
if (!call_user_func_array(array($page, $helperMethod), $arguments)) {
|
2009-11-21 04:15:40 +01:00
|
|
|
$status['error'][$page->ID] = '';
|
2009-10-30 04:27:27 +01:00
|
|
|
}
|
2009-05-14 23:51:54 +02:00
|
|
|
|
|
|
|
// Now make sure the tree title is appropriately updated
|
|
|
|
$publishedRecord = DataObject::get_by_id('SiteTree', $page->ID);
|
2009-08-02 23:57:26 +02:00
|
|
|
if ($publishedRecord) {
|
2009-11-21 04:15:40 +01:00
|
|
|
$status['modified'][$publishedRecord->ID] = array(
|
|
|
|
'TreeTitle' => $publishedRecord->TreeTitle,
|
|
|
|
);
|
2009-08-02 23:57:26 +02:00
|
|
|
}
|
2009-05-14 23:51:54 +02:00
|
|
|
$page->destroy();
|
|
|
|
unset($page);
|
|
|
|
}
|
|
|
|
|
2010-12-16 09:33:06 +01:00
|
|
|
$response = Controller::curr()->getResponse();
|
|
|
|
if($response) {
|
|
|
|
$response->setStatusCode(
|
|
|
|
200,
|
|
|
|
sprintf($successMessage, $pages->Count(), count($status['error']))
|
|
|
|
);
|
|
|
|
}
|
2009-05-14 23:51:54 +02:00
|
|
|
|
2009-11-21 04:15:40 +01:00
|
|
|
return Convert::raw2json($status);
|
2009-05-14 23:51:54 +02:00
|
|
|
}
|
2010-04-13 05:58:16 +02: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)
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2010-04-13 06:24:48 +02:00
|
|
|
|
2010-04-13 05:58:16 +02:00
|
|
|
return $applicableIDs;
|
|
|
|
}
|
2011-02-14 23:50:24 +01:00
|
|
|
|
2009-05-14 23:51:54 +02:00
|
|
|
|
2009-10-16 00:44:08 +02:00
|
|
|
// if your batchaction has parameters, return a fieldset here
|
|
|
|
function getParameterFields() {
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-13 06:24:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If you wish to restrict the batch action to some users, overload this function.
|
|
|
|
*/
|
|
|
|
function canView() {
|
|
|
|
return true;
|
|
|
|
}
|
2009-05-14 23:51:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Publish items batch action.
|
2009-05-14 23:56:35 +02:00
|
|
|
*
|
|
|
|
* @package cms
|
|
|
|
* @subpackage batchaction
|
2009-05-14 23:51:54 +02:00
|
|
|
*/
|
|
|
|
class CMSBatchAction_Publish extends CMSBatchAction {
|
|
|
|
function getActionTitle() {
|
|
|
|
return _t('CMSBatchActions.PUBLISH_PAGES', 'Publish');
|
|
|
|
}
|
2011-02-21 22:52:56 +01:00
|
|
|
function getDoingText() {
|
|
|
|
return _t('CMSBatchActions.PUBLISHING_PAGES', 'Publishing selected pages');
|
|
|
|
}
|
2009-05-14 23:51:54 +02:00
|
|
|
|
|
|
|
function run(DataObjectSet $pages) {
|
|
|
|
return $this->batchaction($pages, 'doPublish',
|
2009-10-30 01:55:37 +01:00
|
|
|
_t('CMSBatchActions.PUBLISHED_PAGES', 'Published %d pages, %d failures')
|
2009-05-14 23:51:54 +02:00
|
|
|
);
|
|
|
|
}
|
2010-04-13 05:58:16 +02:00
|
|
|
|
|
|
|
function applicablePages($ids) {
|
|
|
|
return $this->applicablePagesHelper($ids, 'canPublish', true, false);
|
|
|
|
}
|
2009-05-14 23:51:54 +02:00
|
|
|
}
|
|
|
|
|
2009-11-21 04:15:40 +01:00
|
|
|
/**
|
|
|
|
* Un-publish items batch action.
|
|
|
|
*
|
|
|
|
* @package cms
|
|
|
|
* @subpackage batchaction
|
|
|
|
*/
|
|
|
|
class CMSBatchAction_Unpublish extends CMSBatchAction {
|
|
|
|
function getActionTitle() {
|
|
|
|
return _t('CMSBatchActions.UNPUBLISH_PAGES', 'Un-publish');
|
|
|
|
}
|
2011-02-21 22:52:56 +01:00
|
|
|
function getDoingText() {
|
|
|
|
return _t('CMSBatchActions.UNPUBLISHING_PAGES', 'Un-publishing selected pages');
|
|
|
|
}
|
2009-11-21 04:15:40 +01:00
|
|
|
|
|
|
|
function run(DataObjectSet $pages) {
|
|
|
|
return $this->batchaction($pages, 'doUnpublish',
|
|
|
|
_t('CMSBatchActions.UNPUBLISHED_PAGES', 'Un-published %d pages')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-14 23:51:54 +02:00
|
|
|
/**
|
|
|
|
* Delete items batch action.
|
2009-05-14 23:56:35 +02:00
|
|
|
*
|
|
|
|
* @package cms
|
|
|
|
* @subpackage batchaction
|
2009-05-14 23:51:54 +02:00
|
|
|
*/
|
|
|
|
class CMSBatchAction_Delete extends CMSBatchAction {
|
|
|
|
function getActionTitle() {
|
2010-04-13 05:53:50 +02:00
|
|
|
return _t('CMSBatchActions.DELETE_DRAFT_PAGES', 'Delete from draft site');
|
2009-05-14 23:51:54 +02:00
|
|
|
}
|
|
|
|
function getDoingText() {
|
2010-01-13 01:07:40 +01:00
|
|
|
return _t('CMSBatchActions.DELETING_DRAFT_PAGES', 'Deleting selected pages from the draft site');
|
2009-05-14 23:51:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function run(DataObjectSet $pages) {
|
2009-11-21 04:15:40 +01:00
|
|
|
$status = array(
|
|
|
|
'modified'=>array(),
|
2010-04-12 11:57:31 +02:00
|
|
|
'deleted'=>array(),
|
|
|
|
'error'=>array()
|
2009-11-21 04:15:40 +01:00
|
|
|
);
|
|
|
|
|
2009-05-14 23:51:54 +02:00
|
|
|
foreach($pages as $page) {
|
|
|
|
$id = $page->ID;
|
|
|
|
|
|
|
|
// Perform the action
|
|
|
|
if($page->canDelete()) $page->delete();
|
2010-04-12 11:57:31 +02:00
|
|
|
else $status['error'][$page->ID] = true;
|
2009-05-14 23:51:54 +02:00
|
|
|
|
2009-11-21 04:15:40 +01:00
|
|
|
// check to see if the record exists on the live site,
|
|
|
|
// if it doesn't remove the tree node
|
2009-05-21 07:11:14 +02:00
|
|
|
$liveRecord = Versioned::get_one_by_stage( 'SiteTree', 'Live', "\"SiteTree\".\"ID\"=$id");
|
2009-05-14 23:51:54 +02:00
|
|
|
if($liveRecord) {
|
|
|
|
$liveRecord->IsDeletedFromStage = true;
|
2009-11-21 04:15:40 +01:00
|
|
|
$status['modified'][$liveRecord->ID] = array(
|
|
|
|
'TreeTitle' => $liveRecord->TreeTitle,
|
|
|
|
);
|
2009-05-14 23:51:54 +02:00
|
|
|
} else {
|
2009-11-21 04:15:40 +01:00
|
|
|
$status['deleted'][$id] = array();
|
2009-05-14 23:51:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-11-21 04:15:40 +01:00
|
|
|
return Convert::raw2json($status);
|
2009-05-14 23:51:54 +02:00
|
|
|
}
|
2010-04-13 05:58:16 +02:00
|
|
|
|
|
|
|
function applicablePages($ids) {
|
|
|
|
return $this->applicablePagesHelper($ids, 'canDelete', true, false);
|
|
|
|
}
|
2009-05-14 23:51:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-11-10 22:14:08 +01:00
|
|
|
* Unpublish (delete from live site) items batch action.
|
2009-05-14 23:56:35 +02:00
|
|
|
*
|
|
|
|
* @package cms
|
|
|
|
* @subpackage batchaction
|
2009-05-14 23:51:54 +02:00
|
|
|
*/
|
|
|
|
class CMSBatchAction_DeleteFromLive extends CMSBatchAction {
|
|
|
|
function getActionTitle() {
|
|
|
|
return _t('CMSBatchActions.DELETE_PAGES', 'Delete from published site');
|
|
|
|
}
|
|
|
|
function getDoingText() {
|
|
|
|
return _t('CMSBatchActions.DELETING_PAGES', 'Deleting selected pages from the published site');
|
|
|
|
}
|
|
|
|
|
|
|
|
function run(DataObjectSet $pages) {
|
2009-11-21 04:15:40 +01:00
|
|
|
$status = array(
|
|
|
|
'modified'=>array(),
|
|
|
|
'deleted'=>array()
|
2009-10-30 01:55:37 +01:00
|
|
|
);
|
|
|
|
|
2009-11-21 04:15:40 +01:00
|
|
|
foreach($pages as $page) {
|
|
|
|
$id = $page->ID;
|
|
|
|
|
|
|
|
// Perform the action
|
|
|
|
if($page->canDelete()) $page->doDeleteFromLive();
|
2009-05-14 23:51:54 +02:00
|
|
|
|
2010-04-12 11:57:31 +02:00
|
|
|
// check to see if the record exists on the stage site, if it doesn't remove the tree node
|
2009-05-21 07:11:14 +02:00
|
|
|
$stageRecord = Versioned::get_one_by_stage( 'SiteTree', 'Stage', "\"SiteTree\".\"ID\"=$id");
|
2009-05-14 23:51:54 +02:00
|
|
|
if($stageRecord) {
|
|
|
|
$stageRecord->IsAddedToStage = true;
|
2009-11-21 04:15:40 +01:00
|
|
|
$status['modified'][$stageRecord->ID] = array(
|
|
|
|
'TreeTitle' => $stageRecord->TreeTitle,
|
|
|
|
);
|
2009-05-14 23:51:54 +02:00
|
|
|
} else {
|
2009-11-21 04:16:32 +01:00
|
|
|
$status['deleted'][$id] = array();
|
2009-05-14 23:51:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-11-21 04:15:40 +01:00
|
|
|
return Convert::raw2json($status);
|
2009-05-14 23:51:54 +02:00
|
|
|
}
|
2010-04-13 05:58:16 +02:00
|
|
|
|
|
|
|
function applicablePages($ids) {
|
|
|
|
return $this->applicablePagesHelper($ids, 'canDelete', false, true);
|
|
|
|
}
|
2010-04-12 12:05:08 +02:00
|
|
|
}
|