Details
-
Type:
Feature Request
-
Status:
Contributed Solution
-
Priority:
Minor
-
Resolution: Unresolved
-
Affects Version/s: 6.1.1 CE GA2, 6.1.20 EE GA2, 6.2.0 CE M4
-
Fix Version/s: None
-
Component/s: Collaboration, Collaboration > Announcements
-
Labels:None
-
Liferay Contributor's Agreement:Accept
-
Similar Issues:
Description
The current announcements portlet does always show all announcements an user is allowed to see. This is controlled by the scopes.
I have a special case where I have two pages for two different groups. Both pages are showing the latest announcements and it works. In this case a subset of users are in group 1, others in group 2 and another subset in both groups. The problem is that the users which are part of both groups are seeing all announcements of both groups at both pages. I had to extend the portlet, so it shows just announcements for group 1 on page 1 and for group 2 on page two.
To solve this I added a new portlet preference and made the portlet instanceable. This would be a nice addition to the official announcements portlet, so here the steps I did:
- Make the announcements portlet instanceable
- Add <instanceable>true</instanceable> to the liferay-portlet.xml for the portlet <portlet-name>84</portlet-name> (in my case it's an extension)
- Delete https://github.com/liferay/liferay-portal/blob/master/portal-web/docroot/html/portlet/announcements/entry_scope.jspf#L62,L76
- Change https://github.com/liferay/liferay-portal/blob/master/portal-web/docroot/html/portlet/announcements/view_entry.jspf
- Add here https://github.com/liferay/liferay-portal/blob/master/portal-web/docroot/html/portlet/announcements/view_entry.jspf#L44
String selectedScope; try { selectedScope = GetterUtil.getString(preferences.getValue("selectedScope", "")); } catch (Exception e) { selectedScope = ""; } if (selectedScope.equals("") || (selectedScope.contains(scopeName))) { %> - Replace https://github.com/liferay/liferay-portal/blob/master/portal-web/docroot/html/portlet/announcements/view_entry.jspf#L63,L70
if (selectedScope.equals("")) { showScopeName = true; } %> <c:choose> <c:when test='<%= showScopeName %>'> <div class="<%= hiddenEntry ? "aui-helper-hidden" : "" %> entry-content entry-type-<%= entry.getType() %>"> <span class="entry-scope"><%= scopeName %></span> <%= entry.getContent() %> </div> </c:when> <c:otherwise> <div class="<%= hiddenEntry ? "aui-helper-hidden" : "" %> entry-content entry-type-<%= entry.getType() %>"> <%= entry.getContent() %> </div> </c:otherwise> </c:choose> </div> <% } %>
- Add here https://github.com/liferay/liferay-portal/blob/master/portal-web/docroot/html/portlet/announcements/view_entry.jspf#L44
- Change https://github.com/liferay/liferay-portal/blob/master/portal-web/docroot/html/portlet/announcements/configuration.jsp
- Add here https://github.com/liferay/liferay-portal/blob/master/portal-web/docroot/html/portlet/announcements/configuration.jsp#L18
<%@ page import="java.util.TreeSet" %>
- Add here https://github.com/liferay/liferay-portal/blob/master/portal-web/docroot/html/portlet/announcements/configuration.jsp#L48
<aui:fieldset> <% String selectedScope; try { selectedScope = GetterUtil.getString(preferences.getValue("selectedScope", "")); } catch (Exception e) { selectedScope = ""; } boolean defaultScope = GetterUtil.getBoolean(preferences.getValue("defaultScope", null), true); Set<String> allScopes = new TreeSet<String>(); List<Role> roles = RoleLocalServiceUtil.getRoles(PortalUtil.getCompanyId(renderRequest)); for (Role role : roles) { if (RolePermissionUtil.contains(permissionChecker, role.getRoleId(), ActionKeys.MANAGE_ANNOUNCEMENTS)) { allScopes.add(role.getTitle(locale)); } } List<UserGroup> userGroups = UserGroupLocalServiceUtil.getUserGroups(PortalUtil.getCompanyId(renderRequest)); for (UserGroup userGroup : userGroups) { if (UserGroupPermissionUtil.contains(permissionChecker, userGroup.getUserGroupId(), ActionKeys.MANAGE_ANNOUNCEMENTS)) { allScopes.add(userGroup.getName()); } } // Left list List<KeyValuePair> leftList = new ArrayList<KeyValuePair>(); for (String curScope : allScopes) { if (selectedScope.contains(curScope)) { leftList.add(new KeyValuePair(curScope, curScope)); } } // Right list List<KeyValuePair> rightList = new ArrayList<KeyValuePair>(); boolean containsComma = selectedScope.contains(","); %> <aui:select label="" name="preferences--defaultScope--" showEmptyOption="<%= true %>" onChange='<%= renderResponse.getNamespace() + "selectScope();" %>' > <aui:option label='<%= LanguageUtil.get(pageContext,"select-more-than-one") + "..." %>' selected="<%= containsComma %>" value="<%= false %>" /> <optgroup label="<liferay-ui:message key="scopes" />"> <% System.out.println("selected: " + selectedScope); for (String curScope : allScopes) { KeyValuePair tempKeyValuePair = new KeyValuePair(curScope, curScope); if (!leftList.contains(tempKeyValuePair)) { rightList.add(tempKeyValuePair); } %> <aui:option label="<%= curScope %>" selected="<%= (curScope.equals(selectedScope)) %>" value="<%= curScope %>" /> <% } %> </optgroup> </aui:select> <aui:input name="preferences--selectedScope--" type="hidden" /> <div class="<%= defaultScope ? "aui-helper-hidden" : "" %>" id="<portlet:namespace />scopesBoxes"> <liferay-ui:input-move-boxes leftBoxName="currentScopes" leftList="<%= leftList %>" leftReorder="true" leftTitle="selected" rightBoxName="availableScopes" rightList="<%= rightList %>" rightTitle="available" /> </div> </aui:fieldset> - Replace https://github.com/liferay/liferay-portal/blob/master/portal-web/docroot/html/portlet/announcements/configuration.jsp#L50
<aui:button onClick='<%= renderResponse.getNamespace() + "saveSelectBoxes();" %>' type="submit" />
- Add after this https://github.com/liferay/liferay-portal/blob/master/portal-web/docroot/html/portlet/announcements/configuration.jsp#L62
function <portlet:namespace />selectScope() { if (document.<portlet:namespace />fm.<portlet:namespace />defaultScope.value != "false") { if (document.<portlet:namespace />fm.<portlet:namespace />selectedScope) { document.<portlet:namespace />fm.<portlet:namespace />selectedScope.value = document.<portlet:namespace />fm.<portlet:namespace />defaultScope.value; } submitForm(document.<portlet:namespace />fm); } } Liferay.provide( window, '<portlet:namespace />saveSelectBoxes', function() { if (document.<portlet:namespace />fm.<portlet:namespace />selectedScope) { document.<portlet:namespace />fm.<portlet:namespace />selectedScope.value = Liferay.Util.listSelect(document.<portlet:namespace />fm.<portlet:namespace />currentScopes); } submitForm(document.<portlet:namespace />fm); }, ['liferay-util-list-fields'] ); Liferay.Util.toggleSelectBox('<portlet:namespace />defaultScope','false','<portlet:namespace />scopesBoxes');
- Add here https://github.com/liferay/liferay-portal/blob/master/portal-web/docroot/html/portlet/announcements/configuration.jsp#L18
It's not perfect, just my solution and some things are missing (e.g. in 4.2 I'm just adding Roles and UserGroups, see https://github.com/liferay/liferay-portal/blob/master/portal-web/docroot/html/portlet/announcements/entry_select_scope.jspf for comparison).
The code to select more than one scope is based on the asset publisher configuration.
Adding an announcements portlet to a page will now generate a new instance. When opening the configuration you will see a drop down menu. Here you can choose the scope(s) which should be shown in this instance. If nothing is choosen here the portlet will work as before and show all announcements for all scopes.

Thanks for your contribution David and James! I have shared this with our Liferay team and will keep you posted on any updates.