From 092f202a03623522fb4eb8b713b858ea51bbaedb Mon Sep 17 00:00:00 2001 From: Thusel Date: Mon, 6 Jan 2025 16:59:26 +0100 Subject: [PATCH] LUT-28236 : Delete action: adding a confirmation popup --- .../blog/resources/blog_messages.properties | 2 +- .../resources/blog_messages_fr.properties | 2 +- .../lutece/plugins/blog/web/BlogJspBean.java | 103 ++++++++++++++---- 3 files changed, 86 insertions(+), 21 deletions(-) diff --git a/src/java/fr/paris/lutece/plugins/blog/resources/blog_messages.properties b/src/java/fr/paris/lutece/plugins/blog/resources/blog_messages.properties index f72829a5..625aa8f4 100644 --- a/src/java/fr/paris/lutece/plugins/blog/resources/blog_messages.properties +++ b/src/java/fr/paris/lutece/plugins/blog/resources/blog_messages.properties @@ -178,7 +178,7 @@ diff_blog.latestVersion=Compare the latest versions message.confirmRemoveBlog=Are you sure that you want to delete this post? message.confirmRemoveHistoryBlog=Are you sure that you want to delete this version of the post? -message.errorDocumentIsPublished=This post is still published in the site, please unpublish it before delete it. +message.errorDocumentIsActive=This post is still active in the site, please archive it before deleting it. message.errorTagIsAffected=This Tag is still atached to a post, please detached it before delete it ! message.errorTagExist=This Tag already exist ! message.errorTagUpdatePosition=The tag position can not be updated ! diff --git a/src/java/fr/paris/lutece/plugins/blog/resources/blog_messages_fr.properties b/src/java/fr/paris/lutece/plugins/blog/resources/blog_messages_fr.properties index 12bd32ab..aa9230e5 100644 --- a/src/java/fr/paris/lutece/plugins/blog/resources/blog_messages_fr.properties +++ b/src/java/fr/paris/lutece/plugins/blog/resources/blog_messages_fr.properties @@ -343,7 +343,7 @@ message.mandatory.searchField=Le champs de recherche est obligatoire ! message.notNumericField=Le champs doit \u00e9tre au format num\u00e9rique ! message.confirmRemoveBlog=Voulez-vous supprimer ce billet ? message.confirmRemoveHistoryBlog=Voulez-vous supprimer cette version du billet ? -message.errorDocumentIsPublished=Ce billet est encore publi\u00e9 sur le site, pour le supprimer d\u00e9publiez-le ! +message.errorDocumentIsActive=Ce billet est encore actif sur le site, pour supprimer ce billet merci d'archiver le billet au pr\u00e9alable. message.errorTagIsAffected=Ce Tag est est attach\u00e9 \u00e0 un billet, pour le supprimer d\u00e9tachez le ! message.errorTagExist=Ce Tag existe d\u00e9j\a ! message.errorTagUpdatePosition=Impossible de mettre \u00e0 jour la position de ce tag ! diff --git a/src/java/fr/paris/lutece/plugins/blog/web/BlogJspBean.java b/src/java/fr/paris/lutece/plugins/blog/web/BlogJspBean.java index 0fab5f9b..561b7de3 100644 --- a/src/java/fr/paris/lutece/plugins/blog/web/BlogJspBean.java +++ b/src/java/fr/paris/lutece/plugins/blog/web/BlogJspBean.java @@ -49,8 +49,6 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; -import fr.paris.lutece.util.date.DateUtil; - import java.util.Locale; import java.util.Map; import java.util.stream.Collectors; @@ -74,8 +72,6 @@ import org.outerj.daisy.diff.html.TextNodeComparator; import org.outerj.daisy.diff.html.dom.DomTreeBuilder; import org.xml.sax.InputSource; -import java.text.MessageFormat; - import fr.paris.lutece.api.user.User; import fr.paris.lutece.plugins.blog.business.Blog; @@ -93,7 +89,6 @@ import fr.paris.lutece.plugins.blog.service.BlogSessionListner; import fr.paris.lutece.plugins.blog.service.docsearch.BlogSearchService; import fr.paris.lutece.plugins.blog.utils.BlogLock; -import fr.paris.lutece.plugins.blog.utils.BlogUtils; import fr.paris.lutece.portal.business.rbac.RBAC; import fr.paris.lutece.portal.business.user.AdminUser; import fr.paris.lutece.portal.service.admin.AccessDeniedException; @@ -182,7 +177,6 @@ public class BlogJspBean extends ManageBlogJspBean private static final String PROPERTY_PAGE_TITLE_PREVIEW_BLOG = "blog.preview_blog.pageTitle"; private static final String PROPERTY_PAGE_TITLE_DIFF_BLOG = "blog.diff_blog.pageTitle"; protected static final String PROPERTY_USE_UPLOAD_IMAGE_PLUGIN = "use_upload_image_plugin"; - protected static final String PROPERTY_BLOG_ARCHIVE = "blog.manage_blog_archives.labelActionArchive"; // Markers protected static final String MARK_BLOG_LIST = "blog_list"; @@ -213,7 +207,7 @@ public class BlogJspBean extends ManageBlogJspBean // Properties private static final String PROPERTY_DEFAULT_LIST_ITEM_PER_PAGE = "blog.listItems.itemsPerPage"; private static final String MESSAGE_CONFIRM_REMOVE_BLOG = "blog.message.confirmRemoveBlog"; - private static final String MESSAGE_ERROR_DOCUMENT_IS_PUBLISHED = "blog.message.errorDocumentIsPublished"; + private static final String MESSAGE_ERROR_DOCUMENT_IS_ACTIVE = "blog.message.errorDocumentIsActive"; private static final String MESSAGE_CONFIRM_REMOVE_HISTORY_BLOG = "blog.message.confirmRemoveHistoryBlog"; private static final String ACCESS_DENIED_MESSAGE = "portal.message.user.accessDenied"; private static final String MESSAGE_CONFIRM_ARCHIVE_BLOG = "blog.message.confirmArchiveBlog"; @@ -222,7 +216,6 @@ public class BlogJspBean extends ManageBlogJspBean private static final String MESSAGE_CONFIRM_UNARCHIVE_BLOG= "blog.message.confirmUnarchiveBlog"; private static final String MESSAGE_CONFIRM_REMOVE_MULTIPE_BLOGS = "blog.message.confirmRemoveMultipleBlogs"; - protected static final String MARK_BLOG_ACTION_LIST = "selection_action_list"; private static final String INFO_BLOG_UNARCHIVED = "blog.info.blog.blogUnarchived"; private static final String INFO_MULTIPLE_BLOGS_UNARCHIVED = "blog.info.blog.multipleBlogsUnarchived"; private static final String INFO_BLOG_ARCHIVED = "blog.info.blog.blogArchived"; @@ -256,8 +249,7 @@ public class BlogJspBean extends ManageBlogJspBean private static final String ACTION_UPDATE_ARCHIVE_MULTIPLE_BLOGS = "updateArchiveMultipleBlogs"; private static final String ACTION_REMOVE_MULTIPLE_BLOGS = "removeMultipleBlogs"; private static final String ACTION_EXECUTE_SELECTED_ACTION = "form_checkbox_action"; - private static final String ACTION_CONFIRM_ARCHIVE_BLOG = "confirmArchiveBlog"; - private static final String ACTION_ARCHIVE_BLOG = "archiveBlog"; + private static final String ACTION_CONFIRM_REMOVE_MULTIPLE_BLOGS = "confirmRemoveMultipleBlogs"; private static final String ACTION_CONFIRM_ARCHIVE_BLOGS = "confirmArchiveBlogs"; private static final String ACTION_CONFIRM_UNARCHIVE_BLOGS = "confirmUnarchiveBlogs"; @@ -267,6 +259,7 @@ public class BlogJspBean extends ManageBlogJspBean private static final String INFO_BLOG_REMOVED = "blog.info.blog.removed"; private static final String BLOG_LOCKED = "blog.message.blogLocked"; private static final String INFO_HISTORY_BLOG_REMOVED = "blog.info.history.blog.removed"; + private static final String INFO_MULTIPLE_BLOGS_REMOVED = "blog.info.blog.multipleBlogsRemoved"; // Errors private static final String ERROR_HISTORY_BLOG_CANT_REMOVE_ORIGINAL = "blog.error.history.blog.cantRemoveOriginal"; @@ -309,7 +302,6 @@ public class BlogJspBean extends ManageBlogJspBean protected String _strSortedAttributeName; protected Boolean _bIsAscSort; protected String [ ] _strTag; - private ReferenceList _listBlogActionsList; private List _listSelectedBlogIds = new ArrayList<>( ); @@ -620,12 +612,12 @@ public String doRemoveHistoryBlog( HttpServletRequest request ) } // Check if this blog is currently published List docPublication = BlogPublicationHome.getDocPublicationByIdDoc( nBlogId ); - if ( CollectionUtils.isNotEmpty( docPublication ) ) + if ( CollectionUtils.isNotEmpty( docPublication ) || BlogHome.findByPrimaryKey( nBlogId ).isArchived() ) { // Inform the user that the blog is currently published and redirect to the blog's history view UrlItem url = new UrlItem( getViewFullUrl( VIEW_HISTORY_BLOG ) ); url.addParameter( PARAMETER_ID_BLOG, nBlogId ); - String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_DOCUMENT_IS_PUBLISHED, url.getUrl( ), + String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_DOCUMENT_IS_ACTIVE, url.getUrl( ), AdminMessage.TYPE_STOP ); return redirect( request, strMessageUrl ); } @@ -953,9 +945,9 @@ public String doRemoveBlog( HttpServletRequest request ) List docPublication = BlogPublicationHome.getDocPublicationByIdDoc( nId ); - if ( CollectionUtils.isNotEmpty( docPublication ) ) + if ( CollectionUtils.isNotEmpty( docPublication ) || BlogHome.findByPrimaryKey( nId ).isArchived() ) { - String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_DOCUMENT_IS_PUBLISHED, AdminMessage.TYPE_STOP ); + String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_DOCUMENT_IS_ACTIVE, AdminMessage.TYPE_STOP ); return redirect( request, strMessageUrl ); } @@ -965,7 +957,8 @@ public String doRemoveBlog( HttpServletRequest request ) ExtendableResourceRemovalListenerService.doRemoveResourceExtentions( Blog.PROPERTY_RESOURCE_TYPE, String.valueOf( nId ) ); - addInfo( INFO_BLOG_REMOVED, getLocale( ) ); + HttpSession session = request.getSession(); + session.setAttribute( PARAMETER_INFO_MESSAGE, INFO_BLOG_REMOVED); } return redirectView( request, VIEW_MANAGE_BLOGS ); } @@ -1673,6 +1666,51 @@ private static boolean checkLockBlog( int nIdBlog, String strIdSession ) } + /** + * Display the confirmation message before one or multiple selected blog posts are deleted + * + * @param request + * The Http request + * @return the html code to confirm the action + * @throws AccessDeniedException + */ + @Action( ACTION_CONFIRM_REMOVE_MULTIPLE_BLOGS ) + public String getConfirmRemoveMultipleBlogs( HttpServletRequest request ) throws AccessDeniedException + { + // Check if the user has the permission to archive a blog + AdminUser adminUser = AdminUserService.getAdminUser( request ); + User user = AdminUserService.getAdminUser( request ); + if ( !RBACService.isAuthorized( Blog.PROPERTY_RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, Blog.PERMISSION_DELETE, user ) ) + { + String strMessage = I18nService.getLocalizedString( ACCESS_DENIED_MESSAGE, request.getLocale( ) ); + throw new AccessDeniedException( strMessage ); + } + + // Check if one of the blog selected is currently locked. Display a message and redirect the user if it's the case + if ( checkLockMultipleBlogs( request.getSession( ).getId( ) ) ) + { + UrlItem url = new UrlItem( getActionUrl( VIEW_MANAGE_BLOGS ) ); + String strMessageUrl = AdminMessageService.getMessageUrl( request, BLOG_LOCKED, url.getUrl( ), AdminMessage.TYPE_STOP ); + return redirect( request, strMessageUrl ); + } + + UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_MULTIPLE_BLOGS ) ); + url.addParameter( PARAMETER_SELECTED_BLOGS, _listSelectedBlogIds.stream( ).map( String::valueOf ).collect( Collectors.joining( "," ) ) ); + // Check if there's 1 or multiple posts to be removed, to adapt the content of the displayed message + String confirmationMessage = _listSelectedBlogIds.size( ) > 1 ? MESSAGE_CONFIRM_REMOVE_MULTIPE_BLOGS : MESSAGE_CONFIRM_REMOVE_BLOG; + if( _listSelectedBlogIds.size( ) > 1 ) + { + Object [ ] messageArgs = { + _listSelectedBlogIds.size( ) + }; + return redirect( request, AdminMessageService.getMessageUrl( request, confirmationMessage, messageArgs, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION )); + } + else + { + return redirect( request, AdminMessageService.getMessageUrl( request, confirmationMessage, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION )); + } + } + /** * Handles the manual delete of multiple blog posts * @@ -1693,7 +1731,7 @@ public String doRemoveMultipleBlog( HttpServletRequest request ) throws AccessDe throw new AccessDeniedException( strMessage ); } // Get a List of the selected posts' IDs, from the current session - _listSelectedBlogIds = getSelectedBlogPostsIds( request ); + _listSelectedBlogIds = (List) request.getSession( ).getAttribute( PARAMETER_SELECTED_BLOG_IDS_LIST ); // Check if any of the selected post is being modified by another user if ( checkLockMultipleBlogs( request.getSession( ).getId( ) ) ) @@ -1702,7 +1740,18 @@ public String doRemoveMultipleBlog( HttpServletRequest request ) throws AccessDe String strMessageUrl = AdminMessageService.getMessageUrl( request, BLOG_LOCKED, url.getUrl( ), AdminMessage.TYPE_STOP ); return redirect( request, strMessageUrl ); } + for ( int docPublicationId : _listSelectedBlogIds ) + { + if ( CollectionUtils.isNotEmpty( BlogPublicationHome.getDocPublicationByIdDoc( docPublicationId ) ) ) + { + String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_DOCUMENT_IS_ACTIVE, AdminMessage.TYPE_STOP ); + + return redirect( request, strMessageUrl ); + } + } removeMultipleBlogs( _listSelectedBlogIds ); + HttpSession session = request.getSession(); + session.setAttribute( PARAMETER_INFO_MESSAGE, getRemovedResultMessageKey( _listSelectedBlogIds )); return redirectView( request, VIEW_MANAGE_BLOGS ); } private void removeMultipleBlogs( List listBlogIds ) @@ -1877,6 +1926,22 @@ private String getArchivedResultMessageKey(Boolean toArchived, List lis } } } + /** + * Get the key of the message to display after the removing of multiple blog posts + * @param listBlogIds + * @return the key of the message to display + */ + private String getRemovedResultMessageKey(List listBlogIds) + { + if(listBlogIds.size() == 1) + { + return INFO_BLOG_REMOVED; + } + else + { + return INFO_MULTIPLE_BLOGS_REMOVED; + } + }; /** * Process a specific action on a selection of multiple blog post elements @@ -1917,7 +1982,7 @@ else if ( selectedActionId == 1 ) } else if ( selectedActionId == 2 ) { - return doRemoveMultipleBlog( request ); + return getConfirmRemoveMultipleBlogs( request ); } else { @@ -1975,7 +2040,7 @@ private List getSelectedBlogPostsIds( HttpServletRequest request ) */ private synchronized boolean checkLockMultipleBlogs( String strIdSession ) { - if(_listBlogActionsList == null || CollectionUtils.isEmpty( _listSelectedBlogIds )) + if( CollectionUtils.isEmpty( _listSelectedBlogIds )) { return false; }