API CHANGE: Made batch actions pluggable through CMSBatchActionHandler::register()

ENHANCEMENT: Added batch actions for unpublish and delete from published.

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/branches/2.3@76666 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2009-05-12 06:22:24 +00:00
parent 6196f7bbbb
commit 0518e205c7
5 changed files with 351 additions and 189 deletions

178
code/CMSBatchAction.php Normal file
View File

@ -0,0 +1,178 @@
<?php
/**
* A class representing back actions
*
* <code>
* CMSMain::register_batch_action('publishitems', new CMSBatchAction('doPublish',
_t('CMSBatchActions.PUBLISHED_PAGES', 'published %d pages')));
* </code>
*/
abstract class CMSBatchAction extends Object {
/**
* The the text to show in the dropdown for this action
*/
abstract function getActionTitle();
/**
* Get text to be shown while the action is being processed, of the form
* "publishing pages".
*/
abstract function getDoingText();
/**
* 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.
* @para
*/
public function batchaction(DataObjectSet $pages, $helperMethod, $successMessage) {
foreach($pages as $page) {
// Perform the action
$page->$helperMethod();
// Now make sure the tree title is appropriately updated
$publishedRecord = DataObject::get_by_id('SiteTree', $page->ID);
$JS_title = Convert::raw2js($publishedRecord->TreeTitle());
FormResponse::add("\$('sitetree').setNodeTitle($page->ID, '$JS_title');");
$page->destroy();
unset($page);
}
$message = sprintf($successMessage, $pages->Count());
FormResponse::add('statusMessage("'.$message.'","good");');
return FormResponse::respond();
}
}
/**
* Publish items batch action.
*/
class CMSBatchAction_Publish extends CMSBatchAction {
function getActionTitle() {
return _t('CMSBatchActions.PUBLISH_PAGES', 'Publish');
}
function getDoingText() {
return _t('CMSBatchActions.PUBLISHING_PAGES', 'Publishing pages');
}
function run(DataObjectSet $pages) {
return $this->batchaction($pages, 'doPublish',
_t('CMSBatchActions.PUBLISHED_PAGES', 'Published %d pages')
);
}
}
/**
* Un-publish items batch action.
*/
class CMSBatchAction_Unpublish extends CMSBatchAction {
function getActionTitle() {
return _t('CMSBatchActions.UNPUBLISH_PAGES', 'Un-publish');
}
function getDoingText() {
return _t('CMSBatchActions.UNPUBLISHING_PAGES', 'Un-publishing pages');
}
function run(DataObjectSet $pages) {
return $this->batchaction($pages, 'doUnpublish',
_t('CMSBatchActions.UNPUBLISHED_PAGES', 'Un-published %d pages')
);
}
}
/**
* Delete items batch action.
*/
class CMSBatchAction_Delete extends CMSBatchAction {
function getActionTitle() {
return _t('CMSBatchActions.DELETE_PAGES', 'Delete from draft');
}
function getDoingText() {
return _t('CMSBatchActions.DELETING_PAGES', 'Deleting selected pages from draft');
}
function run(DataObjectSet $pages) {
foreach($pages as $page) {
$id = $page->ID;
// Perform the action
if($page->canDelete()) $page->delete();
// check to see if the record exists on the live site, if it doesn't remove the tree node
$liveRecord = Versioned::get_one_by_stage( 'SiteTree', 'Live', "`SiteTree`.`ID`=$id");
if($liveRecord) {
$liveRecord->IsDeletedFromStage = true;
$title = Convert::raw2js($liveRecord->TreeTitle());
FormResponse::add("$('sitetree').setNodeTitle($id, '$title');");
FormResponse::add("$('Form_EditForm').reloadIfSetTo($id);");
} else {
FormResponse::add("var node = $('sitetree').getTreeNodeByIdx('$id');");
FormResponse::add("if(node && node.parentTreeNode) node.parentTreeNode.removeTreeNode(node);");
FormResponse::add("$('Form_EditForm').reloadIfSetTo($id);");
}
$page->destroy();
unset($page);
}
$message = sprintf(_t('CMSBatchActions.DELETED_PAGES', 'Deleted %d pages from the draft site'), $pages->Count());
FormResponse::add('statusMessage("'.$message.'","good");');
return FormResponse::respond();
}
}
/**
* Delete items batch action.
*/
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) {
foreach($pages as $page) {
$id = $page->ID;
// Perform the action
if($page->canDelete()) $page->doDeleteFromLive();
// check to see if the record exists on the live site, if it doesn't remove the tree node
$stageRecord = Versioned::get_one_by_stage( 'SiteTree', 'Stage', "`SiteTree`.`ID`=$id");
if($stageRecord) {
$stageRecord->IsAddedToStage = true;
$title = Convert::raw2js($stageRecord->TreeTitle());
FormResponse::add("$('sitetree').setNodeTitle($id, '$title');");
FormResponse::add("$('Form_EditForm').reloadIfSetTo($id);");
} else {
FormResponse::add("var node = $('sitetree').getTreeNodeByIdx('$id');");
FormResponse::add("if(node && node.parentTreeNode) node.parentTreeNode.removeTreeNode(node);");
FormResponse::add("$('Form_EditForm').reloadIfSetTo($id);");
}
$page->destroy();
unset($page);
}
$message = sprintf(_t('CMSBatchActions.DELETED_PAGES', 'Deleted %d pages from the published site'), $pages->Count());
FormResponse::add('statusMessage("'.$message.'","good");');
return FormResponse::respond();
}
}

View File

@ -0,0 +1,109 @@
<?php
/**
* Special request handler for admin/batchaction
*/
class CMSBatchActionHandler extends RequestHandler {
static $batch_actions = array(
'publish' => 'CMSBatchAction_Publish',
'unpublish' => 'CMSBatchAction_Unpublish',
'delete' => 'CMSBatchAction_Delete',
'deletefromlive' => 'CMSBatchAction_DeleteFromLive',
);
static $url_handlers = array(
'$BatchAction' => 'handleAction'
);
protected $parentController;
protected $urlSegment;
/**
* Register a new batch action. Each batch action needs to be represented by a subclass
* of
*
* @param $urlSegment The URL Segment of the batch action - the URL used to process this
* action will be admin/batchactions/(urlSegment)
* @param $batchActionClass The name of the CMSBatchAction subclass to register
*/
static function register($urlSegment, $batchActionClass) {
if(is_subclass_of($batchActionClass, 'CMSBatchAction')) {
self::$batch_actions[$urlSegment] = $batchActionClass;
} else {
user_error("CMSBatchActionHandler::regiser() - Bad class '$batchActionClass'", E_USER_ERROR);
}
}
function __construct($parentController, $urlSegment) {
$this->parentController = $parentController;
$this->urlSegment = $urlSegment;
parent::__construct();
}
function Link() {
return Controller::join_links($this->parentController->Link(), $this->urlSegment);
}
function handleAction($request) {
// This method can't be called without ajax.
if(!Director::is_ajax()) {
Director::redirectBack();
return;
}
$actions = Object::get_static($this->class, 'batch_actions');
$actionClass = $actions[$request->param('BatchAction')];
$actionHandler = new $actionClass();
// Sanitise ID list and query the database for apges
$ids = split(' *, *', trim($request->requestVar('csvIDs')));
foreach($ids as $k => $v) if(!is_numeric($v)) unset($ids[$k]);
if($ids) {
$pages = DataObject::get('SiteTree', "`SiteTree`.ID IN (" . implode(", ", $ids) . ")");
// If we didn't query all the pages, then find the rest on the live site
if(!$pages || $pages->Count() < sizeof($ids)) {
foreach($ids as $id) $idsFromLive[$id] = true;
if($pages) foreach($pages as $page) unset($idsFromLive[$page->ID]);
$idsFromLive = array_keys($idsFromLive);
Debug::message("`SiteTree`.ID IN (" . implode(", ", $idsFromLive) . ")");
$livePages = Versioned::get_by_stage('SiteTree', 'Live', "`SiteTree`.ID IN (" . implode(", ", $idsFromLive) . ")");
if($pages) $pages->merge($livePages);
else $pages = $livePages;
}
} else {
$pages = new DataObjectSet();
}
return $actionHandler->run($pages);
}
/**
* Return a DataObjectSet of ArrayData objects containing the following pieces of info
* about each batch action:
* - Link
* - Title
* - DoingText
*/
function batchActionList() {
$actions = Object::get_static($this->class, 'batch_actions');
$actionList = new DataObjectSet();
foreach($actions as $urlSegment => $actionClass) {
$actionObj = new $actionClass();
$actionDef = new ArrayData(array(
"Link" => Controller::join_links($this->Link(), $urlSegment),
"Title" => $actionObj->getActionTitle(),
"DoingText" => $actionObj->getDoingText(),
));
$actionList->push($actionDef);
}
return $actionList;
}
}

View File

@ -54,7 +54,8 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
'EditForm',
'AddPageOptionsForm',
'SiteTreeAsUL',
'getshowdeletedsubtree'
'getshowdeletedsubtree',
'batchactions'
);
/**
@ -953,146 +954,17 @@ JS;
}
/**
* Publishes a number of items.
* Called by AJAX
* Batch Actions Handler
*/
public function publishitems() {
// This method can't be called without ajax.
if(!Director::is_ajax()) {
Director::redirectBack();
return;
}
$ids = split(' *, *', $this->requestParams['csvIDs']);
$notifications = array();
$idList = array();
// make sure all the ids are numeric.
// Add all the children to the list of IDs if they are missing
foreach($ids as $id) {
$brokenPageList = '';
if(is_numeric($id)) {
$record = DataObject::get_by_id($this->stat('tree_class'), $id);
if($record) {
if($record && !$record->canPublish()) return Security::permissionFailure($this);
// Publish this page
$record->doPublish();
// Now make sure the 'changed' icon is removed
$publishedRecord = DataObject::get_by_id($this->stat('tree_class'), $id);
$JS_title = Convert::raw2js($publishedRecord->TreeTitle());
FormResponse::add("\$('sitetree').setNodeTitle($id, '$JS_title');");
FormResponse::add("$('Form_EditForm').reloadIfSetTo($record->ID);");
$record->destroy();
unset($record);
}
}
}
if (sizeof($ids) > 1) $message = sprintf(_t('CMSMain.PAGESPUB', "%d pages published "), sizeof($ids));
else $message = sprintf(_t('CMSMain.PAGEPUB', "%d page published "), sizeof($ids));
FormResponse::add('statusMessage("'.$message.'","good");');
return FormResponse::respond();
function batchactions() {
return new CMSBatchActionHandler($this, 'batchactions');
}
/**
* Delete a number of items.
* This code supports notification
* Returns a list of batch actions
*/
public function deleteitems() {
// This method can't be called without ajax.
if(!Director::is_ajax()) {
Director::redirectBack();
return;
}
$ids = split(' *, *', $_REQUEST['csvIDs']);
$notifications = array();
$idList = array();
// make sure all the ids are numeric.
// Add all the children to the list of IDs if they are missing
foreach($ids as $id) {
$brokenPageList = '';
if(is_numeric($id)) {
$record = DataObject::get_by_id($this->stat('tree_class'), $id);
if($record) {
if($record && !$record->canDelete()) return Security::permissionFailure($this);
// add all the children for this record if they are not already in the list
// this check is a little slower but will prevent circular dependencies
// (should they exist, which they probably shouldn't) from causing
// the function to not terminate
$children = $record->AllChildren();
if( $children )
foreach( $children as $child )
if( array_search( $child->ID, $ids ) !== FALSE )
$ids[] = $child->ID;
if($record->hasMethod('BackLinkTracking')) {
$brokenPages = $record->BackLinkTracking();
foreach($brokenPages as $brokenPage) {
$brokenPageList .= "<li style=\"font-size: 65%\">" . $brokenPage->Breadcrumbs(3, true) . "</li>";
$brokenPage->HasBrokenLink = true;
$notifications[$brokenPage->OwnerID][] = $brokenPage;
$brokenPage->writeWithoutVersion();
}
}
$oldID = $record->ID;
$record->delete();
$record->destroy();
// DataObject::delete_by_id($this->stat('tree_class'), $id);
// check to see if the record exists on the live site, if it doesn't remove the tree node
$liveRecord = Versioned::get_one_by_stage( $this->stat('tree_class'), 'Live', "`{$this->stat('tree_class')}`.`ID`={$id}");
if($liveRecord) {
$liveRecord->IsDeletedFromStage = true;
$title = Convert::raw2js($liveRecord->TreeTitle());
FormResponse::add("$('sitetree').setNodeTitle($oldID, '$title');");
FormResponse::add("$('Form_EditForm').reloadIfSetTo($oldID);");
} else {
FormResponse::add("var node = $('sitetree').getTreeNodeByIdx('$id');");
FormResponse::add("if(node && node.parentTreeNode) node.parentTreeNode.removeTreeNode(node);");
FormResponse::add("$('Form_EditForm').reloadIfSetTo($oldID);");
}
}
}
}
if($notifications) foreach($notifications as $memberID => $pages) {
if(class_exists('Page_BrokenLinkEmail')) {
$email = new Page_BrokenLinkEmail();
$email->populateTemplate(new ArrayData(array(
"Recipient" => DataObject::get_by_id("Member", $memberID),
"BrokenPages" => new DataObjectSet($pages),
)));
$email->debug();
$email->send();
}
}
if (sizeof($ids) > 1) $message = sprintf(_t('CMSMain.PAGESDEL', "%d pages deleted "), sizeof($ids));
else $message = sprintf(_t('CMSMain.PAGEDEL', "%d page deleted "), sizeof($ids));
if(isset($brokenPageList) && $brokenPageList != '') {
$message .= _t('CMSMain.NOWBROKEN'," The following pages now have broken links:")."<ul>" . addslashes($brokenPageList) . "</ul>" . _t('CMSMain.NOWBROKEN2',"Their owners have been emailed and they will fix up those pages.");
}
FormResponse::add('statusMessage("'.$message.'","good");');
return FormResponse::respond();
function BatchActionList() {
return $this->batchactions()->batchActionList();
}
function buildbrokenlinks() {

View File

@ -334,25 +334,39 @@ batchActionGlobals = {
* Publish selected pages action
*/
publishpage = Class.create();
publishpage.applyTo('#publishpage_options');
publishpage.applyTo('#batchactions_options');
publishpage.prototype = {
onsubmit : function() {
csvIDs = batchActionGlobals.getCsvIds();
if(csvIDs) {
var optionEl = $('choose_batch_action').options[$('choose_batch_action').selectedIndex];
var actionText = optionEl.text;
var optionParams = eval(optionEl.className);
var ingText = optionParams.doingText;
// Confirmation
if(!confirm("You have " + batchActionGlobals.count + " pages selected.\n\nDo your really want to " + actionText.toLowerCase() + "?")) {
return false;
}
this.elements.csvIDs.value = csvIDs;
statusMessage(ss.i18n._t('CMSMAIN.PUBLISHINGPAGES'));
// Select form submission URL
this.action = $('choose_batch_action').value;
// Put an AJAXY loading icon on the button
$('action_publish_selected').className = 'loading';
// Loading indicator
statusMessage(ingText);
$('batchactions_go').className = 'loading';
// Submit form
Ajax.SubmitForm(this, null, {
onSuccess : function(response) {
Ajax.Evaluator(response);
$('action_publish_selected').className = '';
$('batchactions_go').className = '';
treeactions.closeSelection($('batchactions'));
},
onFailure : function(response) {
errorMessage(ss.i18n._t('CMSMAIN.ERRORPUBLISHING'), response);
errorMessage('Error ' + ingText, response);
}
});
} else {

View File

@ -55,54 +55,43 @@
</select>
</div>
</div>
</form>
<div id="batchactionsforms" style="display: none">
<form class="actionparams" style="border:0" id="batchactions_options" action="">
<p><% _t('SELECTPAGESACTIONS','Select the pages that you want to change &amp; then click an action:') %></p>
<div id="batchactionsforms" style="display: none">
<form class="actionparams" style="border:0" id="deletepage_options" action="admin/deleteitems">
<p><% _t('SELECTPAGESACTIONS','Select the pages that you want to change &amp; then click an action:') %></p>
<div>
<input type="hidden" name="csvIDs" />
<input type="submit" id="action_delete_selected" class="action delete" value="<% _t('DELETECONFIRM','Delete the selected pages') %>" />
</div>
</form>
<input type="hidden" name="csvIDs" />
<div>
<form class="actionparams" style="border:0" id="publishpage_options" action="admin/publishitems">
<input type="hidden" name="csvIDs" />
<div id="ShowChanged">
<input type="checkbox" id="publishpage_show_drafts" /> <label for="publishpage_show_drafts"><% _t('SHOWONLYCHANGED','Show only changed pages') %></label>
</div>
<input type="submit" id="action_publish_selected" class="action" value="<% _t('PUBLISHCONFIRM','Publish the selected pages') %>" />
</form>
</div>
</div>
<% control DuplicatePagesOptionsForm %>
<form class="actionparams" id="duplicate_options" style="display: none" action="admin/duplicateSiteTree">
<p><% _t('SELECTPAGESDUP','Select the pages that you want to duplicate, whether it\'s children should be included, and where you want the duplicates placed') %></p>
<div>
<input type="hidden" name="csvIDs" />
<input type="submit" value="Duplicate" />
<select id="choose_batch_action">
<% control BatchActionList %>
<option value="$Link" class="{doingText:'$DoingText'}">$Title</option>
<% end_control %>
</select>
<input id="batchactions_go" type="submit" class="action" value="Go" />
</div>
</form>
<% end_control %>
<div class="checkboxAboveTree">
<div>
<input type="checkbox" id="sortitems" /> <label for="sortitems"><% _t('ENABLEDRAGGING','Allow drag &amp; drop reordering', PR_HIGH) %></label>
</div>
<div>
<input type="checkbox" id="showdeletedpages" /> <label for="showdeletedpages"><% _t('SHOW_DELETED_PAGES','Show deleted pages', PR_HIGH) %></label>
</div>
</div>
</div>
<% if IsTranslatableEnabled %>
<div id="LangSelector_holder">
Language: $LangSelector
<div class="checkboxAboveTree">
<div>
<input type="checkbox" id="sortitems" /> <label for="sortitems"><% _t('ENABLEDRAGGING','Allow drag &amp; drop reordering', PR_HIGH) %></label>
</div>
<% end_if %>
<div>
<input type="checkbox" id="publishpage_show_drafts" /> <label for="publishpage_show_drafts"><% _t('SHOWONLYCHANGED','Show only changed pages') %></label>
</div>
<div>
<input type="checkbox" id="showdeletedpages" /> <label for="showdeletedpages"><% _t('SHOW_DELETED_PAGES','Show deleted pages', PR_HIGH) %></label>
</div>
</div>
<% if IsTranslatableEnabled %>
<div id="LangSelector_holder">
Language: $LangSelector
</div>
<% end_if %>
</div>
<div id="sitetree_ul">
$SiteTreeAsUL