Uploaded image for project: 'PUBLIC - Liferay Portal Community Edition'
  1. PUBLIC - Liferay Portal Community Edition
  2. LPS-32261

Add a scope filter to the announcements portlet

    Details

    • Liferay Contributor's Agreement:
      Accept

      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:

      1. Make the announcements portlet instanceable
        1. Add <instanceable>true</instanceable> to the liferay-portlet.xml for the portlet <portlet-name>84</portlet-name> (in my case it's an extension)
      2. Delete https://github.com/liferay/liferay-portal/blob/master/portal-web/docroot/html/portlet/announcements/entry_scope.jspf#L62,L76
      3. Change https://github.com/liferay/liferay-portal/blob/master/portal-web/docroot/html/portlet/announcements/view_entry.jspf
      4. 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');
          

      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.

        Attachments

        1. announcements improvement.png
          announcements improvement.png
          37 kB
        2. filter1.png
          filter1.png
          17 kB
        3. filter2.png
          filter2.png
          23 kB
        4. filter3.png
          filter3.png
          47 kB
        5. filter4.png
          filter4.png
          28 kB
        6. filter5.png
          filter5.png
          16 kB

          Activity

            People

            Assignee:
            support-lep@liferay.com SE Support
            Reporter:
            mod David Kubitza
            Votes:
            4 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated:

                Packages

                Version Package