From 96617d578a227ed4015e0628fe818591cf368688 Mon Sep 17 00:00:00 2001 From: Mellisa Hankins Date: Thu, 11 Jul 2013 17:08:07 +1000 Subject: [PATCH] FEATURE: Added publish and unpublish bulk actions request handler --- .../VersionedGridFieldBulkManager_Request.php | 87 +++++++++++++++++++ javascript/GridFieldBulkManager.js | 26 ++++-- 2 files changed, 107 insertions(+), 6 deletions(-) create mode 100644 code/VersionedGridFieldBulkManager_Request.php diff --git a/code/VersionedGridFieldBulkManager_Request.php b/code/VersionedGridFieldBulkManager_Request.php new file mode 100644 index 0000000..5b25f13 --- /dev/null +++ b/code/VersionedGridFieldBulkManager_Request.php @@ -0,0 +1,87 @@ +getPOSTRecordList($request); + $records = $this->gridField->List->byIDs($recordList); + + foreach($records as $record) { + if(!$this->doPublish($record)) + unset($recordList[$record->ID]); + } + + $this->completeAction($recordList); + } + + public function unpublish(SS_HTTPRequest $request) { + $recordList = $this->getPOSTRecordList($request); + $records = $this->gridField->List->byIDs($recordList); + + foreach($records as $record) { + if(!$this->doUnpublish($record)) + unset($recordList[$record->ID]); + } + + $this->completeAction($recordList); + } + + protected function doPublish($record) { + if(!$record || !$record->hasExtension('Versioned') || ($record->hasMethod('canPublish') && !$record->canPublish())) { + return false; + } + + if($record->hasMethod('doPublish')) + $record->doPublish(); + else + $record->publish("Stage", "Live"); + + return true; + } + + protected function doUnpublish($record) { + if(!$record || !$record->hasExtension('Versioned') || ($record->hasMethod('canDeleteFromLive') && !$record->canDeleteFromLive())) { + return false; + } + + if($record->hasMethod('doPublish')) + $record->doUnpublish(); + else { + $origStage = Versioned::current_stage(); + Versioned::reading_stage('Live'); + + // This way our ID won't be unset + $clone = clone $record; + $clone->delete(); + + Versioned::reading_stage($origStage); + } + + return true; + } + + private function completeAction($recordList = null) { + $response = new SS_HTTPResponse(Convert::raw2json(array($recordList))); + $response->addHeader('Content-Type', 'text/plain'); + return $response; + } +} \ No newline at end of file diff --git a/javascript/GridFieldBulkManager.js b/javascript/GridFieldBulkManager.js index 0ea55ec..5344b23 100644 --- a/javascript/GridFieldBulkManager.js +++ b/javascript/GridFieldBulkManager.js @@ -58,22 +58,36 @@ case 'edit': $(btn).removeClass('ss-ui-action-destructive'); $(btn).attr('data-icon', 'pencil'); - $(icon).removeClass('btn-icon-decline btn-icon-pencil').addClass('btn-icon-pencil'); - + $(icon).removeClass('btn-icon-decline btn-icon-pencil btn-icon-unpublish btn-icon-accept').addClass('btn-icon-pencil'); + $(btn).attr('href', $(btn).data('url')+'/edit'); break; - + case 'unlink': $(btn).removeClass('ss-ui-action-destructive'); $(btn).attr('data-icon', 'chain--minus'); - $(icon).removeClass('btn-icon-decline btn-icon-pencil').addClass('btn-icon-chain--minus'); + $(icon).removeClass('btn-icon-decline btn-icon-pencil btn-icon-unpublish btn-icon-accept').addClass('btn-icon-chain--minus'); $(btn).removeAttr('href'); break; - + case 'delete': $(btn).addClass('ss-ui-action-destructive'); $(btn).attr('data-icon', 'decline'); - $(icon).removeClass('btn-icon-decline btn-icon-pencil').addClass('btn-icon-decline'); + $(icon).removeClass('btn-icon-decline btn-icon-pencil btn-icon-unpublish btn-icon-accept').addClass('btn-icon-decline'); + $(btn).removeAttr('href'); + break; + + case 'publish': + $(btn).removeClass('ss-ui-action-destructive'); + $(btn).attr('data-icon', 'accept'); + $(icon).removeClass('btn-icon-decline btn-icon-pencil btn-icon-unpublish btn-icon-chain--minus').addClass('btn-icon-accept'); + $(btn).removeAttr('href'); + break; + + case 'unpublish': + $(btn).removeClass('ss-ui-action-destructive'); + $(btn).attr('data-icon', 'unpublish'); + $(icon).removeClass('btn-icon-decline btn-icon-pencil btn-icon-accept').addClass('btn-icon-unpublish'); $(btn).removeAttr('href'); break; }