<%
/**
 * 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.
 */
%>

<c:if test="<%= installPluginsURL != null %>">

	<%
	String taglibOnClick = "submitForm(document." + renderResponse.getNamespace() + "fm , '" + installPluginsURL.toString() +"');";
	%>

	<aui:button onClick="<%= taglibOnClick %>" value="install-more-portlets" />

	<br /><br />
</c:if>

<%
List<String> headerNames = new ArrayList<String>();

headerNames.add("portlet");
headerNames.add("active");

if (showReindexButton) {
	headerNames.add("search-index");
}

SearchContainer searchContainer = new SearchContainer(renderRequest, null, null, SearchContainer.DEFAULT_CUR_PARAM, SearchContainer.DEFAULT_DELTA, portletURL, headerNames, null);

List<Portlet> portlets = PortletLocalServiceUtil.getPortlets(company.getCompanyId(), false, false);

portlets = ListUtil.sort(portlets, new PortletTitleComparator(application, locale));

int total = portlets.size();

searchContainer.setTotal(total);

List<Portlet> results = ListUtil.subList(portlets, searchContainer.getStart(), searchContainer.getEnd());

searchContainer.setResults(results);

List resultRows = searchContainer.getResultRows();

for (int i = 0; i < results.size(); i++) {
	Portlet portlet = results.get(i);

	PluginPackage pluginPackage = portlet.getPluginPackage();
	PluginSetting pluginSetting = PluginSettingLocalServiceUtil.getPluginSetting(company.getCompanyId(), portlet.getPortletId(), Plugin.TYPE_PORTLET);

	ResultRow row = new ResultRow(portlet, portlet.getId(), i);

	PortletURL rowURL = renderResponse.createRenderURL();

	rowURL.setParameter("struts_action", "/enterprise_admin/edit_plugin");
	rowURL.setParameter("redirect", currentURL);

	if (pluginPackage != null) {
		rowURL.setParameter("moduleId", pluginPackage.getModuleId());
	}

	rowURL.setParameter("pluginId", portlet.getPortletId());
	rowURL.setParameter("pluginType", Plugin.TYPE_PORTLET);

	String title = PortalUtil.getPortletTitle(portlet, application, locale);

	rowURL.setParameter("title", title);

	// Name and description

	StringBundler sb = new StringBundler();

	String displayName = portlet.getDisplayName();

	if (showEditPluginHREF) {
		sb.append("<a href=\"");
		sb.append(rowURL.toString());
		sb.append("\">");
	}

	sb.append("<strong>");
	sb.append(title);
	sb.append("</strong>");

	if (showEditPluginHREF) {
		sb.append("</a>");
	}

	sb.append("<br />");
	sb.append(LanguageUtil.get(pageContext, "package"));
	sb.append(": ");

	if (pluginPackage == null) {
		sb.append(LanguageUtil.get(pageContext, "unknown"));
	}
	else {
		sb.append(pluginPackage.getName());

		if (pluginPackage.getContext() != null) {
			sb.append(" (");
			sb.append(pluginPackage.getContext());
			sb.append(")");
		}
	}

	if (Validator.isNotNull(displayName) && !title.equals(displayName)) {
		sb.append("<br />");
		sb.append(portlet.getDisplayName());
	}

	row.addText(sb.toString());

	// Active

	row.addText(LanguageUtil.get(pageContext, (portlet.isActive() ? "yes" : "no")));

	// Search index

	if (showReindexButton) {
		if (Validator.isNotNull(portlet.getIndexerClass())) {
			sb.setIndex(0);

			sb.append("<input onclick=\"");
			sb.append(renderResponse.getNamespace());
			sb.append("reindexPortlet('");
			sb.append(portlet.getPortletId());
			sb.append("');\" type=\"button\" value=\"");
			sb.append(LanguageUtil.get(pageContext, "reindex"));
			sb.append("\" />");

			row.addText(sb.toString());
		}
		else {
			row.addText(StringPool.BLANK);
		}
	}

	// Add result row

	resultRows.add(row);
}
%>

<liferay-ui:search-iterator searchContainer="<%= searchContainer %>" />

<c:if test="<%= showReindexButton %>">
	<aui:script>
		function <portlet:namespace />reindexPortlet(portletId) {
			document.<portlet:namespace />fm.<portlet:namespace />portletId.value = portletId;
			<portlet:namespace />saveServer('reindex');
		}
	</aui:script>
</c:if>