diff --git a/code/CMSBatchActionHandler.php b/code/CMSBatchActionHandler.php index 63338b09..b6195af4 100644 --- a/code/CMSBatchActionHandler.php +++ b/code/CMSBatchActionHandler.php @@ -9,7 +9,8 @@ class CMSBatchActionHandler extends RequestHandler { static $batch_actions = array(); static $url_handlers = array( - '$BatchAction' => 'handleAction' + '$BatchAction/applicablepages' => 'handleApplicablePages', + '$BatchAction' => 'handleAction', ); protected $parentController; @@ -99,7 +100,6 @@ class CMSBatchActionHandler extends RequestHandler { $this->recordClass, implode(", ", $idsFromLive) ); - Debug::message($sql); $livePages = Versioned::get_by_stage($this->recordClass, 'Live', $sql); if($pages) $pages->merge($livePages); else $pages = $livePages; @@ -111,6 +111,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['class'](); + + // 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 SS_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/javascript/LeftAndMain.BatchActions.js b/javascript/LeftAndMain.BatchActions.js index 57e3d8dd..9cf6076e 100644 --- a/javascript/LeftAndMain.BatchActions.js +++ b/javascript/LeftAndMain.BatchActions.js @@ -44,8 +44,8 @@ $(self.getTree()).removeClass('multiselect'); } else { self._multiselectTransform(); + self.serializeFromTree(); } - }); this._super(); @@ -85,34 +85,106 @@ return $('#TreeActions-batchactions').is(':visible'); }, - onsubmit: function(e) { - var ids = []; - var tree = this.getTree(); + /** + * Ajax callbacks determine which pages is selectable in a certain batch action. + * + * @param {Object} rootNode + */ + refreshSelected : function(rootNode) { + var self = this, st = this.getTree(), ids = this.getIDs(), allIds = []; + // Default to refreshing the entire tree + if(rootNode == null) rootNode = st; + + for(var idx in ids) { + $(st.getTreeNodeByIdx(idx)).addClass('selected').attr('selected', 'selected'); + } + + jQuery(rootNode).find('li').each(function() { + var id = parseInt(this.id.replace('record-',''), 10); + if(id) allIds.push(id); + + // Disable the nodes while the ajax request is being processed + $(this).removeClass('nodelete').addClass('treeloading'); + }); + + // Post to the server to ask which pages can have this batch action applied + var applicablePagesURL = this.find(':input[name=Action]').val() + '/applicablepages/?csvIDs=' + allIds.join(','); + jQuery.getJSON(applicablePagesURL, function(applicableIDs) { + var i; + var applicableIDMap = {}; + for(i=0;i