<%
/**
 * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
%>

<select name="<portlet:namespace />distributionScope" <%= submitOnChange ? "onChange=\"" + renderResponse.getNamespace() + "selectDistributionScope(this.value);\"" : "" %>>
	<option value=""></option>

	<c:if test="<%= permissionChecker.isOmniadmin() %>">
		<option <%= ((classNameId == 0) && (classPK == 0)) ? "selected" : "" %> value="0,0"><liferay-ui:message key="general" /></option>
	</c:if>

	<%
	List<Group> groups = GroupLocalServiceUtil.getUserGroups(user.getUserId());
	%>

	<c:if test="<%= groups.size() > 0 %>">
		<optgroup label="<liferay-ui:message key="communities" />">

			<%
			for (Group group : groups) {
				if (group.isCommunity() && GroupPermissionUtil.contains(permissionChecker, group.getGroupId(), ActionKeys.MANAGE_ANNOUNCEMENTS)) {
			%>

					<option <%= (classPK == group.getGroupId()) ? "selected" : "" %> value="<%= PortalUtil.getClassNameId(Group.class) %><%= StringPool.COMMA %><%= group.getGroupId() %>"><%= group.getName() %></option>

			<%
				}
			}
			%>

		</optgroup>
	</c:if>

	<%
	List<Organization> organizations = OrganizationLocalServiceUtil.getUserOrganizations(user.getUserId());
	%>

	<c:if test="<%= organizations.size() > 0 %>">
		<optgroup label="<liferay-ui:message key="organizations" />">

			<%
			for (Organization organization : organizations) {
				if (OrganizationPermissionUtil.contains(permissionChecker, organization.getOrganizationId(), ActionKeys.MANAGE_ANNOUNCEMENTS)) {
			%>

					<option <%= (classPK == organization.getOrganizationId()) ? "selected" : "" %> value="<%= PortalUtil.getClassNameId(Organization.class) %><%= StringPool.COMMA %><%= organization.getOrganizationId() %>"><%= HtmlUtil.escape(organization.getName()) %></option>

			<%
				}
			}
			%>

		</optgroup>
	</c:if>

	<%
	List<Role> roles = RoleLocalServiceUtil.getRoles(themeDisplay.getCompanyId());
	%>

	<c:if test="<%= roles.size() > 0 %>">
		<optgroup label="<liferay-ui:message key="roles" />">

			<%
			for (Role role : roles) {
				if (RolePermissionUtil.contains(permissionChecker, role.getRoleId(), ActionKeys.MANAGE_ANNOUNCEMENTS)) {
			%>

					<option <%= (classPK == role.getRoleId()) ? "selected" : "" %> value="<%= PortalUtil.getClassNameId(Role.class) %><%= StringPool.COMMA %><%= role.getRoleId() %>"><%= HtmlUtil.escape(role.getTitle(locale)) %></option>

			<%
				}
			}
			%>

		</optgroup>
	</c:if>

	<%
	List<UserGroup> userGroups = UserGroupLocalServiceUtil.getUserGroups(themeDisplay.getCompanyId());
	%>

	<c:if test="<%= userGroups.size() > 0 %>">
		<optgroup label="<liferay-ui:message key="user-groups" />">

			<%
			for (UserGroup userGroup : userGroups) {
				if (UserGroupPermissionUtil.contains(permissionChecker, userGroup.getUserGroupId(), ActionKeys.MANAGE_ANNOUNCEMENTS)) {
			%>

					<option <%= (classPK == userGroup.getUserGroupId()) ? "selected" : "" %> value="<%= PortalUtil.getClassNameId(UserGroup.class) %><%= StringPool.COMMA %><%= userGroup.getUserGroupId() %>"><%= userGroup.getName() %></option>

			<%
				}
			}
			%>

		</optgroup>
	</c:if>
</select>