From bd75a41f9b813850f748e928a7f528abb38a9dcb Mon Sep 17 00:00:00 2001 From: Julian Seidenberg Date: Tue, 21 Aug 2012 17:57:06 +1200 Subject: [PATCH] ENHANCEMENT: warning when deleting a document that is referenced on other pages --- code/cms/DMSGridFieldDeleteAction.php | 2 +- code/cms/DMSGridFieldDetailForm.php | 7 +++++-- code/tools/ShortCodeRelationFinder.php | 5 +++++ javascript/DMSDocumentCMSFields.js | 20 ++++++++++++++++++-- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/code/cms/DMSGridFieldDeleteAction.php b/code/cms/DMSGridFieldDeleteAction.php index c9496d1..2f6b30e 100644 --- a/code/cms/DMSGridFieldDeleteAction.php +++ b/code/cms/DMSGridFieldDeleteAction.php @@ -47,7 +47,7 @@ class DMSGridFieldDeleteAction extends GridFieldDeleteAction implements GridFiel //set a class telling JS what kind of warning to display when clicking the delete button if ($numberOfRelations > 1) $field->addExtraClass('dms-delete-link-only'); - else $field->addExtraClasS('dms-delete-last-warning'); + else $field->addExtraClass('dms-delete-last-warning'); //set a class to show if the document is hidden if ($record->isHidden()) { diff --git a/code/cms/DMSGridFieldDetailForm.php b/code/cms/DMSGridFieldDetailForm.php index d98d6c8..5e432ab 100644 --- a/code/cms/DMSGridFieldDetailForm.php +++ b/code/cms/DMSGridFieldDetailForm.php @@ -11,10 +11,13 @@ class DMSGridFieldDetailForm_ItemRequest extends GridFieldDetailForm_ItemRequest //add a data attribute specifying how many pages this document is referenced on if ($record = $this->record) { - $numberOfRelations = $record->Pages()->Count(); + $numberOfPageRelations = $record->Pages()->Count(); + $relations = new ShortCodeRelationFinder(); + $numberOfInlineRelations = $relations->findPageCount($record->ID); //add the number of pages attached to this field as a data-attribute - $form->setAttribute('data-pages-count', $numberOfRelations); + $form->setAttribute('data-pages-count', $numberOfPageRelations); + $form->setAttribute('data-relation-count', $numberOfInlineRelations); } return $form; } diff --git a/code/tools/ShortCodeRelationFinder.php b/code/tools/ShortCodeRelationFinder.php index 8e29199..afcceec 100644 --- a/code/tools/ShortCodeRelationFinder.php +++ b/code/tools/ShortCodeRelationFinder.php @@ -30,6 +30,11 @@ class ShortCodeRelationFinder { return $found; } + function findPageCount($number) { + $list = $this->getList($number); + return $list->count(); + } + /** * @return DataList */ diff --git a/javascript/DMSDocumentCMSFields.js b/javascript/DMSDocumentCMSFields.js index f926682..c248cb8 100644 --- a/javascript/DMSDocumentCMSFields.js +++ b/javascript/DMSDocumentCMSFields.js @@ -114,11 +114,27 @@ //work out how many pages are left attached to this document var form = this.closest('form'); var pagesCount = form.data('pages-count'); + var relationCount = form.data('relation-count'); //display an appropriate message var message = ''; - if (pagesCount > 1) { - message = "Permanently delete this document and remove it from all pages where it is referenced?\n\nWarning: this document is attached to a total of "+pagesCount+" pages. Deleting it here will permanently delete it from this page and all other pages where it is referenced."; + if (pagesCount > 1 || relationCount > 0) { + var pages = ''; + if (pagesCount > 1) { + pages = "\nWarning: doc is attached to a total of "+pagesCount+" pages. "; + } + var references = ''; + var referencesWarning = ''; + if (relationCount > 0) { + var pname = 'pages'; + referencesWarning = "\n\nBefore deleting: please update the content on the pages where this document is referenced, otherwise the links on those pages will break."; + if (relationCount === 1) { + pname = 'page'; + referencesWarning = "\n\nBefore deleting: please update the content on the page where this document is referenced, otherwise the links on that page will break."; + } + references = "\nWarning: doc is referenced in the text of "+relationCount +" "+pname+"."; + } + message = "Permanently delete this document and remove it from all pages where it is referenced?\n"+pages+references+"\n\nDeleting it here will permanently delete it from this page and all other pages where it is referenced."+referencesWarning; } else { message = "Permanently delete this document and remove it from this page?\n\nNotice: this document is only attached to this page, so deleting it won't affect any other pages."; }