<%
/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */
%>

<%
List portletsList = new ArrayList();
Set portletIdsSet = new HashSet();

List exportLayouts = new ArrayList();

if (selLayout != null) {
	exportLayouts.add(selLayout);
}
else {
	exportLayouts = LayoutLocalServiceUtil.getLayouts(selGroup.getGroupId(), privateLayout);
}

Iterator itr1 = exportLayouts.iterator();

while (itr1.hasNext()) {
	Layout curLayout = (Layout)itr1.next();

	if (curLayout.isTypePortlet()) {
		LayoutTypePortlet curLayoutTypePortlet = (LayoutTypePortlet)curLayout.getLayoutType();

		Iterator itr2 = curLayoutTypePortlet.getPortletIds().iterator();

		while (itr2.hasNext()) {
			Portlet curPortlet = PortletLocalServiceUtil.getPortletById(company.getCompanyId(), (String)itr2.next());

			if (curPortlet != null) {
				PortletDataHandler portletDataHandler = curPortlet.getPortletDataHandlerInstance();

				if ((portletDataHandler != null) && !portletIdsSet.contains(curPortlet.getRootPortletId())) {
					portletIdsSet.add(curPortlet.getRootPortletId());

					portletsList.add(curPortlet);
				}
			}
		}
	}
}

List<Portlet> alwaysExportablePortlets = LayoutExporter.getAlwaysExportablePortlets(company.getCompanyId());

for (Portlet alwaysExportablePortlet : alwaysExportablePortlets) {
	if (!portletIdsSet.contains(alwaysExportablePortlet.getRootPortletId())) {
		portletIdsSet.add(alwaysExportablePortlet.getRootPortletId());

		portletsList.add(alwaysExportablePortlet);
	}
}

portletsList = ListUtil.sort(portletsList, new PortletTitleComparator(application, locale));
%>

<div>
	<ul class="lfr-tree lfr-component">
		<li class="tree-item" <%= (proposalId > 0) ? "style=\"display: none;\"" : "" %>>
			<aui:input checked="<%= true %>" disabled="<%= true %>" inlineLabel="right" name="pages" type="checkbox" />
		</li>

		<li class="tree-item">
			<aui:input checked="<%= true %>" disabled="<%= true %>" inlineLabel="right" name="portlets" type="checkbox" />

			<ul>
				<li class="tree-item">
					<aui:input inlineLabel="right" label="setup" name="<%= PortletDataHandlerKeys.PORTLET_SETUP %>" type="checkbox" value="<%= true %>" />
				</li>
				<li class="tree-item">
					<aui:input inlineLabel="right" label="user-preferences" name="<%= PortletDataHandlerKeys.PORTLET_USER_PREFERENCES %>" type="checkbox" value="<%= false %>" />
				</li>
				<li id="<portlet:namespace />deleteDataOption" class="tree-item" <%= (!selGroup.isStagingGroup() || !localPublishing) ? "style=\"display: none;\"" : "" %>>
					<aui:input helpMessage="delete-portlet-data-help" inlineLabel="right" label="delete-portlet-data-before-importing" name="<%= PortletDataHandlerKeys.DELETE_PORTLET_DATA %>" type="checkbox" />
				</li>
				<li class="tree-item">
					<aui:input name="<%= PortletDataHandlerKeys.PORTLET_DATA_CONTROL_DEFAULT %>" type="hidden" value="false" />

					<%
					String taglibOnChange = renderResponse.getNamespace() + "toggleChildren(this, '" + renderResponse.getNamespace() + "portletDataList');" ;
					%>

					<aui:input checked="<%= true %>" inlineLabel="right" label="data" name="<%= PortletDataHandlerKeys.PORTLET_DATA %>" type="checkbox" onchange="<%= taglibOnChange %>" />

					<div id="<portlet:namespace />portletDataList">
						<ul>
							<li class="tree-item">
								<liferay-ui:message key="range" />:

								<ul>
									<li>
										<aui:input id="rangeAll" label="all" name="range" onClick='<%= renderResponse.getNamespace() + "hideDateRange()" %>' type="radio" value="all" />
									</li>
									<li>
										<aui:input checked="<%= true %>" inlineLabel="right" label="from-last-publish-date" name="range" onClick='<%= renderResponse.getNamespace() + "hideDateRange()" %>' type="radio" value="fromLastPublishDate" />
									</li>
									<li>
										<aui:input helpMessage="export-date-range-help" id="rangeDateRange" label="date-range" name="range" onClick='<%= renderResponse.getNamespace() + "showDateRange()" %>' type="radio" value="dateRange" />

										<%
										Calendar today = CalendarFactoryUtil.getCalendar(timeZone, locale);

										Calendar yesterday = CalendarFactoryUtil.getCalendar(timeZone, locale);

										yesterday.add(Calendar.DATE, -1);
										%>

										<ul class="aui-helper-hidden" id="<portlet:namespace />startEndDate">
											<li>
												<table class="lfr-table">
												<tr>
													<td>
														<liferay-ui:message key="start-date" />
													</td>
													<td>
														<liferay-ui:input-date
															dayParam="startDateDay"
															dayValue="<%= yesterday.get(Calendar.DATE) %>"
															disabled="<%= false %>"
															firstDayOfWeek="<%= yesterday.getFirstDayOfWeek() - 1 %>"
															monthParam="startDateMonth"
															monthValue="<%= yesterday.get(Calendar.MONTH) %>"
															yearParam="startDateYear"
															yearValue="<%= yesterday.get(Calendar.YEAR) %>"
															yearRangeStart="<%= yesterday.get(Calendar.YEAR) - 100 %>"
															yearRangeEnd="<%= yesterday.get(Calendar.YEAR) %>"
														/>

														&nbsp;

														<liferay-ui:input-time
															amPmParam='<%= "startDateAmPm" %>'
															amPmValue="<%= yesterday.get(Calendar.AM_PM) %>"
															disabled="<%= false %>"
															hourParam='<%= "startDateHour" %>'
															hourValue="<%= yesterday.get(Calendar.HOUR) %>"
															minuteParam='<%= "startDateMinute" %>'
															minuteValue="<%= yesterday.get(Calendar.MINUTE) %>"
															minuteInterval="<%= 1 %>"
														/>
													</td>
												</tr>
												<tr>
													<td>
														<liferay-ui:message key="end-date" />
													</td>
													<td>
														<liferay-ui:input-date
															dayParam="endDateDay"
															dayValue="<%= today.get(Calendar.DATE) %>"
															disabled="<%= false %>"
															firstDayOfWeek="<%= today.getFirstDayOfWeek() - 1 %>"
															monthParam="endDateMonth"
															monthValue="<%= today.get(Calendar.MONTH) %>"
															yearParam="endDateYear"
															yearValue="<%= today.get(Calendar.YEAR) %>"
															yearRangeStart="<%= today.get(Calendar.YEAR) - 100 %>"
															yearRangeEnd="<%= today.get(Calendar.YEAR) %>"
														/>

														&nbsp;

														<liferay-ui:input-time
															amPmParam='<%= "endDateAmPm" %>'
															amPmValue="<%= today.get(Calendar.AM_PM) %>"
															disabled="<%= false %>"
															hourParam='<%= "endDateHour" %>'
															hourValue="<%= today.get(Calendar.HOUR) %>"
															minuteParam='<%= "endDateMinute" %>'
															minuteValue="<%= today.get(Calendar.MINUTE) %>"
															minuteInterval="<%= 1 %>"
														/>
													</td>
												</tr>
												</table>
											</li>
										</ul>
									</li>
									<li>
										<aui:input cssClass="input-container" id="rangeLast" inlineField="<%= true %>" label="last" name="range" onClick='<%= renderResponse.getNamespace() + "hideDateRange()" %>' type="radio" value="last" />

										<aui:select label="" name="last">
											<aui:option value="12"><%= LanguageUtil.format(pageContext, "x-hours", "12") %></aui:option>
											<aui:option value="24"><%= LanguageUtil.format(pageContext, "x-hours", "24") %></aui:option>
											<aui:option value="48"><%= LanguageUtil.format(pageContext, "x-hours", "48") %></aui:option>
											<aui:option value="168"><%= LanguageUtil.format(pageContext, "x-days", "7") %></aui:option>
										</aui:select>
									</li>
								</ul>
							</li>
						</ul>
						<ul>

							<%
							itr1 = portletsList.iterator();

							while (itr1.hasNext()) {
								Portlet curPortlet = (Portlet)itr1.next();

								PortletDataHandler portletDataHandler = curPortlet.getPortletDataHandlerInstance();

								try {
									PortletDataHandlerControl[] controls = portletDataHandler.getExportControls();

									if (controls != null) {
										String rootPortletId = curPortlet.getRootPortletId();

										boolean checked = portletDataHandler.isPublishToLiveByDefault();

										boolean showRenderControls = true;

										if (!liveGroup.isStagedPortlet(curPortlet.getRootPortletId())) {
											showRenderControls = false;
										}

										request.setAttribute("render_controls.jsp-controls", controls);
										request.setAttribute("render_controls.jsp-portletDisabled", !checked);
							%>

										<li class="tree-item">

											<%
											taglibOnChange = renderResponse.getNamespace() + "toggleChildren(this, '" + renderResponse.getNamespace() + PortletDataHandlerKeys.PORTLET_DATA + StringPool.UNDERLINE + rootPortletId + "Controls');";
											%>

											<aui:input inlineLabel="right" label="<%= PortalUtil.getPortletTitle(curPortlet, application, locale) %>" name="<%= PortletDataHandlerKeys.PORTLET_DATA + StringPool.UNDERLINE + rootPortletId %>" onchange="<%= taglibOnChange %>" type="checkbox" value="<%= checked %>" />

											<ul id="<portlet:namespace /><%= PortletDataHandlerKeys.PORTLET_DATA %>_<%= rootPortletId %>Controls">
												<li class="<%= renderResponse.getNamespace() %>handler-control">
													<aui:input disabled="<%= !checked %>" inlineLabel="right" label="setup "name="<%= PortletDataHandlerKeys.PORTLET_SETUP + StringPool.UNDERLINE + rootPortletId %>" type="checkbox" value="<%= true %>" />
												</li>

												<c:if test="<%= showRenderControls %>">
													<liferay-util:include page="/html/portlet/communities/render_controls.jsp" />
												</c:if>
											</ul>
										</li>

							<%
									}
								}
								catch (PortletDataException pde) {
								%>

									<li class="tree-item">
										<div class="portlet-msg-error"><%= PortalUtil.getPortletTitle(curPortlet, application, locale) %>: <%= LanguageUtil.get(pageContext, "error-initializing-import-controls") %></div>
									</li>

							<%
								}
							}
							%>

						</ul>
					</div>
				</li>
			</ul>
		</li>

		<li class="tree-item">

			<%
			taglibOnChange = renderResponse.getNamespace() + "toggleChildren(this, '" + renderResponse.getNamespace() + "permissionsUl');";
			%>

			<aui:input helpMessage="export-import-permissions-help" inlineLabel="right" label="permissions" name="<%= PortletDataHandlerKeys.PERMISSIONS %>" onchange="<%= taglibOnChange %>" type="checkbox" />


			<ul id="<portlet:namespace />permissionsUl">
				<c:choose>
					<c:when test="<%= (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) || (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) %>">
						<li class="tree-item">
							<aui:input cssClass="disabled" disabled="<%= true %>" inlineLabel="right" name="permissionsAssignedToRoles" type="checkbox" value="<%= true %>" />
						</li>
					</c:when>
					<c:otherwise>
						<li class="tree-item">
							<aui:input cssClass="disabled" disabled="<%= true %>" inlineLabel="right" name="permissionsAssignedToOrganizationsUserGroupsRolesAndCommunities" type="checkbox" value="<%= true %>" />
						</li>
						<li class="tree-item">
							<aui:input cssClass="disabled" disabled="<%= true %>" helpMessage="export-import-permissions-assigned-to-users-help" inlineLabel="right" label="permissions-assigned-to-users" name="<%= PortletDataHandlerKeys.USER_PERMISSIONS %>" type="checkbox" />
						</li>
					</c:otherwise>
				</c:choose>
			</ul>
		</li>

		<li class="tree-item">
			<aui:input helpMessage="export-import-categories-help" inlineLabel="right" label="categories" name="<%= PortletDataHandlerKeys.CATEGORIES %>" type="checkbox" value="<%= false %>" />
		</li>
	</ul>
</div>

<aui:script>
	Liferay.provide(
		window,
		'<portlet:namespace />hideDateRange',
		function() {
			var A = AUI();

			A.one('#<portlet:namespace />startEndDate').hide();
		},
		['aui-base']
	);

	Liferay.provide(
		window,
		'<portlet:namespace />showDateRange',
		function() {
			var A = AUI();

			A.one('#<portlet:namespace />startEndDate').show();
		},
		['aui-base']
	);

	Liferay.provide(
		window,
		'<portlet:namespace />toggleChildren',
		function(checkbox, parentDivId) {
			var A = AUI();

			var parentDiv = A.one('#' + parentDivId);

			var enabled = checkbox.checked;

			if (parentDiv) {
				parentDiv.all('input').each(
					function(item, index, collection) {
						var disabled = !enabled;

						if (enabled && item.hasClass('disabled')) {
							disabled = true;
						}

						item.set('disabled', disabled);
					}
				);
			}
		},
		['aui-base']
	);
</aui:script>