From a19334c5d7b00718537baf5595a5570d20ce10a4 Mon Sep 17 00:00:00 2001 From: Mellisa Hankins Date: Thu, 11 Jul 2013 18:08:35 +1000 Subject: [PATCH] Now displays messages when an action is performed --- code/GridFieldBulkManager_Request.php | 29 ++++++++++++++----- .../VersionedGridFieldBulkManager_Request.php | 10 ++----- javascript/GridFieldBulkManager.js | 16 ++++++++-- 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/code/GridFieldBulkManager_Request.php b/code/GridFieldBulkManager_Request.php index 367c926..2d62979 100644 --- a/code/GridFieldBulkManager_Request.php +++ b/code/GridFieldBulkManager_Request.php @@ -220,11 +220,13 @@ class GridFieldBulkManager_Request extends RequestHandler { public function unlink(SS_HTTPRequest $request) { $recordList = $this->getPOSTRecordList($request); + $original = $this->gridField->list->count(); + $this->gridField->list->removeMany($recordList); - - $response = new SS_HTTPResponse(Convert::raw2json(array($recordList))); - $response->addHeader('Content-Type', 'text/plain'); - return $response; + + $count = count($recordList) - ($original - $this->gridField->list->count()); + + return $this->completeAction($recordList, 'Unlinked {count} items', $count); } /** @@ -243,10 +245,8 @@ class GridFieldBulkManager_Request extends RequestHandler { $res = DataObject::delete_by_id($recordClass, $id); array_push($result, array($id => $res)); } - - $response = new SS_HTTPResponse(Convert::raw2json(array($result))); - $response->addHeader('Content-Type', 'text/plain'); - return $response; + + return $this->completeAction($result, 'Deleted {count} items'); } @@ -279,4 +279,17 @@ class GridFieldBulkManager_Request extends RequestHandler { ))); return $items; } + + protected function completeAction($recordList = null, $text = 'No changes', $count = null) { + if($recordList && (count($recordList) || $count)) { + if(!$count) $count = count($recordList); + $text = _t('GridFieldBulkManager.' . strtoupper(str_replace(array('{count}', ' '), '_', $text)), $text, array('count' => $count)); + } + else + $text = _t('GridFieldBulkManager.NO_CHANGES', 'No changes'); + + $response = new SS_HTTPResponse(json_encode(array('records' => $recordList, 'message' => $text))); + $response->addHeader('Content-Type', 'text/json'); + return $response; + } } \ No newline at end of file diff --git a/code/VersionedGridFieldBulkManager_Request.php b/code/VersionedGridFieldBulkManager_Request.php index ff0c3dc..867eb6f 100644 --- a/code/VersionedGridFieldBulkManager_Request.php +++ b/code/VersionedGridFieldBulkManager_Request.php @@ -35,7 +35,7 @@ class VersionedGridFieldBulkManager_Request extends GridFieldBulkManager_Request } } - return $this->completeAction($recordList); + return $this->completeAction($recordList, 'Published {count} items'); } public function unpublish(SS_HTTPRequest $request) { @@ -50,7 +50,7 @@ class VersionedGridFieldBulkManager_Request extends GridFieldBulkManager_Request } } - return $this->completeAction($recordList); + return $this->completeAction($recordList, 'Unpublished {count} items'); } protected function doPublish($record) { @@ -86,10 +86,4 @@ class VersionedGridFieldBulkManager_Request extends GridFieldBulkManager_Request 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 5344b23..fc3a86f 100644 --- a/javascript/GridFieldBulkManager.js +++ b/javascript/GridFieldBulkManager.js @@ -134,9 +134,19 @@ data: data, type: "POST", context: $(this) - }).done(function() { - $(this).parents('.ss-gridfield').entwine('.').entwine('ss').reload(); - }); + }).done(function(r) { + // A bit hacky, but it checks if the response is json + try { + $.parseJSON(r); + + if(r.message) { + var msgType = r.messageType ? r.messageType : 'good'; + statusMessage(decodeURIComponent(r.message), msgType); + } + } catch(e) { } + + $(this).parents('.ss-gridfield').entwine('.').entwine('ss').reload(); + }); } }