From ad92b8073798e549392221e4aa8597bc9142abe4 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Wed, 13 Jan 2010 00:08:27 +0000 Subject: [PATCH] ENHANCEMENT: Update the checkboxes available to batch-actions to show only the applicable pages for that particular action. API CHANGE: Allow for an applicablePages($idArray) method to be defined on a CMSBatchAction class. (from r94761) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/branches/2.4@96819 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- code/CMSBatchActionHandler.php | 25 +++++++++++++++++- code/SecurityAdmin.php | 2 +- javascript/CMSMain_left.js | 47 +++++++++++++++++++++++++++++++++- javascript/LeftAndMain_left.js | 2 +- 4 files changed, 72 insertions(+), 4 deletions(-) diff --git a/code/CMSBatchActionHandler.php b/code/CMSBatchActionHandler.php index 3ccadcee..bed33ba3 100644 --- a/code/CMSBatchActionHandler.php +++ b/code/CMSBatchActionHandler.php @@ -13,7 +13,8 @@ class CMSBatchActionHandler extends RequestHandler { ); static $url_handlers = array( - '$BatchAction' => 'handleAction' + '$BatchAction/applicablepages' => 'handleApplicablePages', + '$BatchAction' => 'handleAction', ); protected $parentController; @@ -80,6 +81,28 @@ class CMSBatchActionHandler extends RequestHandler { return $actionHandler->run($pages); } + + function handleApplicablePages($request) { + // Find the action handler + $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 => $id) $ids[$k] = (int)$id; + $ids = array_filter($ids); + + if($actionHandler->hasMethod('applicablePages')) { + $applicableIDs = $actionHandler->applicablePages($ids); + } else { + $applicableIDs = $ids; + } + + $response = new HTTPResponse(json_encode($applicableIDs)); + $response->addHeader("Content-type", "application/json"); + return $response; + } /** * Return a DataObjectSet of ArrayData objects containing the following pieces of info diff --git a/code/SecurityAdmin.php b/code/SecurityAdmin.php index 743f8fd1..3a18553a 100644 --- a/code/SecurityAdmin.php +++ b/code/SecurityAdmin.php @@ -236,7 +236,7 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider { // getChildrenAsUL is a flexible and complex way of traversing the tree $siteTreeList = $obj->getChildrenAsUL( '', - '"
  • ID\" class=\"$child->class " . ($child->Locked ? " nodelete" : "") . $child->markingClasses() . ($extraArg->isCurrentPage($child) ? " current" : "") . "\">" . ' . + '"
  • ID\" class=\"$child->class " . $child->markingClasses() . ($extraArg->isCurrentPage($child) ? " current" : "") . "\">" . ' . '"Link(),0,-1), "show", $child->ID) . "\" >" . $child->TreeTitle() . "" ', $this, true diff --git a/javascript/CMSMain_left.js b/javascript/CMSMain_left.js index 83793f04..1a584db2 100755 --- a/javascript/CMSMain_left.js +++ b/javascript/CMSMain_left.js @@ -257,11 +257,13 @@ batchactionsclass.prototype = { onclick : function() { if(treeactions.toggleSelection(this)) { this.multiselectTransform(); + this.actionChanged(); } return false; }, actionChanged: function() { + // Show parameters form, if necessary var urlSegment = $('choose_batch_action').value.split('/').pop(); if ($('BatchActionParameters_'+urlSegment)) { jQuery('#BatchActionParameters .params').hide(); @@ -270,6 +272,8 @@ batchactionsclass.prototype = { } else { jQuery('#BatchActionParameters').hide(); } + + batchActionGlobals.refreshSelected(); }, multiselectTransform : function() { @@ -378,12 +382,53 @@ batchActionGlobals = { getCsvIds : function() { return (batchActionGlobals.getIds().toString()); }, - refreshSelected : function() { + refreshSelected : function(rootNode) { var st = $('sitetree'); + for(var idx in batchActionGlobals.selectedNodes) { st.getTreeNodeByIdx(idx).addNodeClass('selected'); st.getTreeNodeByIdx(idx).selected = true; } + + // Default to refreshing the entire tree + if(rootNode == null) rootNode = st; + + /// If batch actions is enabled, then enable/disable the appropriate tree fields + if($('batchactionsforms').style.display != 'none' && $('choose_batch_action').value) { + // Collect list of visible tree IDs + var ids = []; + jQuery(rootNode).find('li').each(function() { + var id = parseInt(this.id.replace('record-','')); + if(id) ids.push(id); + + // Disable the nodes while the ajax request is being processed + this.addNodeClass('nodelete'); + }); + + // Post to the server to ask which pages can have this batch action applied + var applicablePagesURL = $('choose_batch_action').value + '/applicablepages?csvIDs=' + ids.join(',') + ',horse'; + jQuery.getJSON(applicablePagesURL, function(applicableIDs) { + var i; + var applicableIDMap = {}; + for(i=0;i