Skip to content

Commit

Permalink
LUT-28234 : Add an archiving and a suppressing buttons action
Browse files Browse the repository at this point in the history
  • Loading branch information
TimotheeHrl authored and husel-t committed Dec 17, 2024
1 parent a214fb0 commit 895dd63
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public final class BlogDAO implements IBlogDAO

private static final String SQL_QUERY_SELECT_BLOG_BY_ID_TAG = " SELECT b.id_blog, b.version, b.content_label, b.creation_date, b.update_date, b.html_content, b.user_editor, b.user_creator, b.attached_portlet_id, b.edit_comment, b.description, b.shareable, b.url, b.is_archived a.id_tag FROM blog_tag_document a Inner join blog_blog b on (b.id_blog = a.id_blog) WHERE a.id_tag = ? ORDER BY priority";

private static final String SQL_QUERY_SELECT_ALL_BLOG = " SELECT DISTINCT a.id_blog, a.version, a.content_label, a.creation_date, a.update_date, a.html_content, a.user_editor, a.user_creator , a.attached_portlet_id, a.edit_comment , a.description, a.shareable, a.url a.is_archived FROM blog_blog a";
private static final String SQL_QUERY_SELECT_ALL_BLOG = " SELECT DISTINCT a.id_blog, a.version, a.content_label, a.creation_date, a.update_date, a.html_content, a.user_editor, a.user_creator , a.attached_portlet_id, a.edit_comment , a.description, a.shareable, a.url, a.is_archived FROM blog_blog a";
private static final String SQL_QUERY_SELECT_VERSION_NUMBER_BY_BLOG_ID_AND_CREATION_DATE = "SELECT version FROM blog_versions WHERE id_blog = ? ORDER BY ABS( update_date - ? );";

private static final String SQL_FILTER_WHERE_CLAUSE = " WHERE ";
Expand Down
139 changes: 136 additions & 3 deletions src/java/fr/paris/lutece/plugins/blog/web/BlogJspBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ public class BlogJspBean extends ManageBlogJspBean
protected static final String PARAMETER_TO_ARCHIVE = "to_archive";

protected static final String PARAMETER_SELECTED_BLOGS = "select_blog_id";
protected static final String PARAMETER_SELECTED_BLOG_ACTION = "select_blog_action";
protected static final String PARAMETER_SELECTED_BLOG_IDS_LIST = "selected_blogs_list";


// Properties for page titles
private static final String PROPERTY_PAGE_TITLE_MANAGE_BLOG = "blog.manage_blog.pageTitle";
Expand Down Expand Up @@ -244,7 +247,8 @@ public class BlogJspBean extends ManageBlogJspBean
private static final String ACTION_REMOVE_HISTORY_BLOG = "removeHistoryBlog";
private static final String ACTION_CONFIRM_REMOVE_HISTORY_BLOG = "confirmRemoveHistoryBlog";
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";
// Infos
private static final String INFO_BLOG_CREATED = "blog.info.blog.created";
private static final String INFO_BLOG_UPDATED = "blog.info.blog.updated";
Expand All @@ -256,6 +260,8 @@ public class BlogJspBean extends ManageBlogJspBean
private static final String ERROR_HISTORY_BLOG_CANT_REMOVE_ORIGINAL = "blog.error.history.blog.cantRemoveOriginal";
private static final String ERROR_HISTORY_BLOG_NOT_REMOVED = "blog.error.history.blog.notRemoved";

private static final String ERROR_ACTION_EXECUTION_FAILED = "blog.message.error.action.executionFailed";
private static final String ERROR_ACTION_NOT_FOUND = "blog.message.error.action.notFound";
// Filter Marks
protected static final String MARK_BLOG_FILTER_LIST = "blog_filter_list";
protected static final String MARK_BLOG_FILTER_NAME = "Nom";
Expand Down Expand Up @@ -1644,7 +1650,55 @@ private static boolean checkLockBlog( int nIdBlog, String strIdSession )
}


/**
* Handles the manual delete of multiple blog posts
*
* @param request
* The Http request
* @return the jsp URL to display the main blog posts' management view
* @throws AccessDeniedException
*/
@Action( ACTION_REMOVE_MULTIPLE_BLOGS )
public String doRemoveMultipleBlog( HttpServletRequest request ) throws AccessDeniedException
{
User user = AdminUserService.getAdminUser( request );
AdminUser adminUser = AdminUserService.getAdminUser( request );

if ( !RBACService.isAuthorized( Blog.PROPERTY_RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, Blog.PERMISSION_DELETE, user ) && !adminUser.isAdmin( ) )
{
String strMessage = I18nService.getLocalizedString( ACCESS_DENIED_MESSAGE, getLocale( ) );
throw new AccessDeniedException( strMessage );
}
// Get a List of the selected posts' IDs, from the current session
_listSelectedBlogIds = getSelectedBlogPostsIds( request );

// Check if any of the selected post is being modified by another user
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 );
}
removeMultipleBlogs( _listSelectedBlogIds );
return redirectView( request, VIEW_MANAGE_BLOGS );
}
private void removeMultipleBlogs( List<Integer> listBlogIds )
{
if( listBlogIds == null )
{
return;
}
for ( int blogId : listBlogIds )
{
BlogPublicationHome.getDocPublicationByIdDoc( blogId );
if ( BlogPublicationHome.getDocPublicationByIdDoc( blogId ) != null && BlogPublicationHome.getDocPublicationByIdDoc( blogId ).size( ) > 0 )
{
BlogPublicationHome.removeByBlogId( blogId );
}
BlogHome.removeVersions( blogId );
BlogHome.remove( blogId );
}
}
/**
* Handles the manual archiving of multiple blog posts
*
Expand All @@ -1659,9 +1713,10 @@ public String doArchiveMultipleBlog( HttpServletRequest request ) throws AccessD
User user = AdminUserService.getAdminUser( request );
if ( !RBACService.isAuthorized( Blog.PROPERTY_RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, Blog.PERMISSION_ARCHIVE, user ) )
{
String strMessage = I18nService.getLocalizedString( ACCESS_DENIED_MESSAGE, request.getLocale( ) );
String strMessage = I18nService.getLocalizedString( ACCESS_DENIED_MESSAGE, getLocale( ) );
throw new AccessDeniedException( strMessage );
}

// Get a List of the selected posts' IDs, from the current session
_listSelectedBlogIds = getSelectedBlogPostsIds( request );

Expand All @@ -1673,11 +1728,89 @@ public String doArchiveMultipleBlog( HttpServletRequest request ) throws AccessD
return redirect( request, strMessageUrl );
}
// Archive the selected blog posts
Boolean bArchive = Boolean.parseBoolean( request.getParameter( PARAMETER_TO_ARCHIVE ) );
if( request.getParameter( PARAMETER_TO_ARCHIVE ) != null)
{
Boolean bArchive = Boolean.parseBoolean( request.getParameter( PARAMETER_TO_ARCHIVE ) );
updateArchiveMultipleBlogs( _listSelectedBlogIds, bArchive );
} else
{
Boolean bArchive = Boolean.parseBoolean( request.getAttribute( PARAMETER_TO_ARCHIVE ).toString( ) );
updateArchiveMultipleBlogs( _listSelectedBlogIds, bArchive );
request.removeAttribute( PARAMETER_TO_ARCHIVE );
}
return redirectView( request, VIEW_MANAGE_BLOGS );
}

/**
* Process a specific action on a selection of multiple blog post elements
*
* @param request
* The Http request
* @return the URL to redirect to once the action is executed
* @throws AccessDeniedException
*/
@Action( ACTION_EXECUTE_SELECTED_ACTION )
public String doExecuteSelectedAction( HttpServletRequest request ) throws AccessDeniedException
{
Locale locale = getLocale( );

// Get the selected action
int selectedActionId = getSelectedAction( request );
// Get the IDs of the blog posts selected for the action
_listSelectedBlogIds = getSelectedBlogPostsIds( request );

// Check if the content retrieved is null. Redirect and display an error if it is the case
if ( selectedActionId == -1 || CollectionUtils.isEmpty( _listSelectedBlogIds ) )
{
addError( ERROR_ACTION_EXECUTION_FAILED, locale );
return redirectView( request, VIEW_MANAGE_BLOGS );
}

// Save the list of selected blogs in the current session's attributes
request.getSession( ).setAttribute( PARAMETER_SELECTED_BLOG_IDS_LIST, _listSelectedBlogIds );

// Execute the action selected by the user
if ( selectedActionId == 0 )
{
// add parameter to the request to_archive=true
request.setAttribute( PARAMETER_TO_ARCHIVE, Boolean.TRUE.toString( ) );
return doArchiveMultipleBlog( request );
}
else if ( selectedActionId == 1 )
{
// add parameter to the request to_archive=false
request.setAttribute( PARAMETER_TO_ARCHIVE, Boolean.FALSE.toString( ) );
return doArchiveMultipleBlog( request );
}
else if ( selectedActionId == 2 )
{
return doRemoveMultipleBlog( request );
}
else
{
addError( ERROR_ACTION_NOT_FOUND, locale );
return redirectView( request, VIEW_MANAGE_BLOGS );
}
}

/**
* Get the value of the action selected by the user
*
* @param request
* The Http request
* @return the ID of the selected action, or -1 if the value couldn't be parsed properly
*
*/
private int getSelectedAction( HttpServletRequest request )
{
// Retrieve the value of the selected action from the request
String strSelectedActionId = request.getParameter( PARAMETER_SELECTED_BLOG_ACTION );
if ( StringUtils.isNumeric( strSelectedActionId ) )
{
return Integer.parseInt( strSelectedActionId );
}
return -1;
}


/**
Expand Down
58 changes: 44 additions & 14 deletions webapp/WEB-INF/templates/admin/plugins/blog/manage_blogs.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,32 @@
<@messages infos=infos />
<@box>
<@boxBody>
<@tform id='form_checkbox_action' name='form_checkbox_action' method='post' class='p-0' action='jsp/admin/plugins/blog/ManageBlogs.jsp'>
<@input type='hidden' id='action' name='action' value='form_checkbox_action' />
<#assign idx=0 />
<#if permission_manage_archive_blog || permission_manage_delete_blog>
<@div class='d-flex flex-row'>
<@inputGroup class='p-2'>
<@select id='select_blog_action' name='select_blog_action' title='#i18n{blog.manage_blogs.selectActionToApply}' class='p-2'>
<#if permission_manage_archive_blog>
<option value=1 >#i18n{blog.manage_blogs.labelUnarchive}</option>
<option value=0 selected>#i18n{blog.manage_blogs.labelArchive}</option>
</#if>
<#if permission_manage_delete_blog>
<option value=2>#i18n{blog.manage_blogs.delete}</option>
</#if>
</@select>
<@inputGroupItem type='btn'>
<@button type='submit' id='button_apply_selected_action' name='button_apply_selected_action' title='#i18n{blog.manage_blogs.applyToSelection}' buttonIcon='check' hideTitle=['all'] disabled=true />
</@inputGroupItem>
</@inputGroup>
</@div>
</#if>
<@table headBody=true >
<@tr>
<@th><input id="select_all_blogs_id" type="checkbox"> </input></@th>
<#if permission_manage_archive_blog || permission_manage_delete_blog>
<@th><@checkBox name='select_all_blogs_id' id='select_all_blogs_id' /></@th>
</#if>
<@th>#i18n{blog.manage_blogs.columnContentLabel} <@sort jsp_url="jsp/admin/plugins/blog/ManageBlogs.jsp" attribute="contentLabel" /></@th>
<@th>#i18n{blog.manage_blogs.columnCreationDate} <@sort jsp_url="jsp/admin/plugins/blog/ManageBlogs.jsp" attribute="creationDate" /></@th>
<@th>#i18n{blog.manage_blogs.columnUpdateDate} <@sort jsp_url="jsp/admin/plugins/blog/ManageBlogs.jsp" attribute="updateDate" /></@th>
Expand All @@ -78,9 +100,11 @@
<@tableHeadBodySeparator />
<#list blog_list as blog>
<@tr>
<@td>
<@checkBox id='selected_blog_${blog.id}' name='select_blog_id' value='${blog.id}' />
</@td>
<#if permission_manage_archive_blog || permission_manage_delete_blog>
<@td>
<@checkBox id='selected_blog_${blog.id}' name='select_blog_id' value='${blog.id}' />
</@td>
</#if>
<@td>
<@div class='d-flex justify-content-between align-items-center mb-0 w-100'>
<@div class='mb-1'>
Expand Down Expand Up @@ -202,6 +226,7 @@
<#assign idx=idx + 1/>
</#list>
</@table>
</@tform>
</@boxBody>
</@box>
<@paginationAdmin paginator=paginator combo=1 />
Expand Down Expand Up @@ -275,30 +300,35 @@

// When the "Check all" / "Uncheck all" element is clicked, check / uncheck all the blog posts' checkboxes
document.getElementById('select_all_blogs_id').addEventListener('click', function () {
let selectionButton = document.getElementById('select_blog_action')
let checkBoxes = document.querySelectorAll('input[name="select_blog_id"]')
var selectionButton = document.getElementById('select_blog_action')
var checkBoxes = document.querySelectorAll('input[name="select_blog_id"]')

// Check / uncheck the available checkboxes
checkBoxes.forEach(function (checkBox) {
checkBox.checked = this.checked
}, this);
manageActionButtonVisibility();
});

// Enable / disabled the selection of an action when a blog post's checkbox is checked
document.querySelectorAll('input[name="select_blog_id"]').forEach(function (checkbox) {
checkbox.addEventListener('change', function () {
let selectionButton = document.getElementById('select_blog_action')
let selectedCheckBoxes = document.querySelectorAll('input[name="select_blog_id"]:checked')
manageActionButtonVisibility();
})
});

function manageActionButtonVisibility() {
var selectionButton = document.getElementById('select_blog_action');
var selectedCheckBoxes = document.querySelectorAll('input[name="select_blog_id"]:checked');
if (selectedCheckBoxes.length > 0) {
// Enable the selection and execution of an action
selectionButton.removeAttribute('disabled')
btnApplyActionOnSelection.removeAttribute('disabled')
selectionButton.removeAttribute('disabled');
btnApplyActionOnSelection.removeAttribute('disabled');
}
else {
// Disable the selection and execution of an action
selectionButton.setAttribute('disabled', 'disabled')
btnApplyActionOnSelection.setAttribute('disabled', 'disabled')
selectionButton.setAttribute('disabled', 'disabled');
btnApplyActionOnSelection.setAttribute('disabled', 'disabled');
}
})
});
}
</script>

0 comments on commit 895dd63

Please sign in to comment.