From 9e9cc995b390ee4554909a267291697748cfe5f3 Mon Sep 17 00:00:00 2001 From: Julian Seidenberg Date: Tue, 7 Aug 2012 14:38:40 +1200 Subject: [PATCH] ENHANCEMENT: warning when deleting a page with documents that exist only on this page. Also, when actually deleting the page, delete the associated documents that only exist on that page --- code/DMSSiteTreeExtension.php | 11 +++++++++ javascript/DMSGridField.js | 42 +++++++++++++++++++++++++++++++++++ tests/DMSDocumentTest.php | 22 ++++++++++++++++++ 3 files changed, 75 insertions(+) diff --git a/code/DMSSiteTreeExtension.php b/code/DMSSiteTreeExtension.php index 2fbeab3..fb47f0c 100644 --- a/code/DMSSiteTreeExtension.php +++ b/code/DMSSiteTreeExtension.php @@ -54,4 +54,15 @@ class DMSSiteTreeExtension extends DataExtension { ) ); } + + function onBeforeDelete() { + $dmsDocuments = $this->owner->Documents(); + foreach($dmsDocuments as $document) { + //if the document is only associated with one page, i.e. only associated with this page + if ($document->Pages()->Count() <= 1) { + //delete the document before deleting this page + $document->delete(); + } + } + } } \ No newline at end of file diff --git a/javascript/DMSGridField.js b/javascript/DMSGridField.js index feea8dd..4e53c48 100644 --- a/javascript/DMSGridField.js +++ b/javascript/DMSGridField.js @@ -32,6 +32,48 @@ } }); + $('.cms-content-actions.south .ss-ui-action-destructive').entwine({ + confirmBeforeDelete: function() { + var deleteButtons = $('button.dms-delete[data-pages-count=1]'); + + //we have page with DMSDocuments on it, and we have documents that only exist on this page + if (deleteButtons.length > 0) { + var message = "Are you sure you want to delete this page? Deleting this page will delete "+deleteButtons.length; + if (deleteButtons.length == 1) { + message += " document that is associated only with this page. This document is:\n\n"; + } else { + message += " documents that are associated only with this page. These documents are:\n\n"; + } + + //create a list of documents and their IDs + deleteButtons.each(function(){ + var tr = $(this).closest('tr'); + message += tr.find('.col-ID').text() +' - '+ tr.find('.col-Title').text() +"\n"; + }); + + if(!confirm(message)) { + return false; + } + } + + return true; + } + }) + + $('#Form_EditForm_action_deletefromlive').entwine({ + onclick: function(e) { + if (this.confirmBeforeDelete()) this._super(e); + else return false; + } + }); + + $('#Form_EditForm_action_delete').entwine({ + onclick: function(e) { + if (this.confirmBeforeDelete()) this._super(e); + else return false; + } + }); + }); }(jQuery)); diff --git a/tests/DMSDocumentTest.php b/tests/DMSDocumentTest.php index 06f1552..7da8b2c 100644 --- a/tests/DMSDocumentTest.php +++ b/tests/DMSDocumentTest.php @@ -72,4 +72,26 @@ class DMSDocumentTest extends SapphireTest { $documentsArray = $documents->toArray(); $this->assertNotContains($doc, $documentsArray, "Document no longer associated with page"); } + + function testDeletingPageWithAssociatedDocuments() { + $s1 = $this->objFromFixture('SiteTree','s1'); + $s2 = $this->objFromFixture('SiteTree','s2'); + + $doc = new DMSDocument(); + $doc->Filename = "delete test file"; + $doc->Folder = "0"; + $doc->write(); + + $doc->addPage($s1); + $doc->addPage($s2); + + $s1->delete(); + + $documents = DataObject::get("DMSDocument","Filename = 'delete test file'"); + $this->assertEquals($documents->Count(),'1',"Deleting one of the associated page doesn't affect the single document we created"); + + $s2->delete(); + $documents = DataObject::get("DMSDocument","Filename = 'delete test file'"); + $this->assertEquals($documents->Count(),'0',"However, deleting the last page that a document is associated with causes that document to be deleted as well"); + } } \ No newline at end of file