Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LPS-25213 #3968

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,116 @@
/**
* Copyright (c) 2000-2012 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.
*/

package com.liferay.portal.kernel.search.facet;

import com.liferay.portal.kernel.json.JSONArray;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.search.BooleanClause;
import com.liferay.portal.kernel.search.Field;
import com.liferay.portal.kernel.search.SearchContext;
import com.liferay.portal.kernel.search.facet.config.FacetConfiguration;
import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
import com.liferay.portal.kernel.util.StringUtil;

import java.text.DateFormat;

import java.util.Calendar;

/**
* @author Raymond Augé
*/
public class ModifiedFacet extends RangeFacet {

public ModifiedFacet(SearchContext searchContext) {
super(searchContext);

setFieldName(Field.MODIFIED_DATE);
}

@Override
protected BooleanClause doGetFacetClause() {
SearchContext searchContext = getSearchContext();

FacetConfiguration facetConfiguration = getFacetConfiguration();

normalizeDates(searchContext, facetConfiguration);

return super.doGetFacetClause();
}

protected void normalizeDates(
SearchContext searchContext, FacetConfiguration facetConfiguration) {

Calendar now = Calendar.getInstance();

now.set(Calendar.SECOND, 0);
now.set(Calendar.MINUTE, 0);

Calendar pastHour = (Calendar)now.clone();
pastHour.set(Calendar.HOUR_OF_DAY, now.get(Calendar.HOUR_OF_DAY) - 1);

Calendar past24Hours = (Calendar)now.clone();
past24Hours.set(
Calendar.DAY_OF_YEAR, now.get(Calendar.DAY_OF_YEAR) - 1);

Calendar pastWeek = (Calendar)now.clone();
pastWeek.set(Calendar.DAY_OF_YEAR, now.get(Calendar.DAY_OF_YEAR) - 7);

Calendar pastMonth = (Calendar)now.clone();
pastMonth.set(Calendar.MONTH, now.get(Calendar.MONTH) - 1);

Calendar pastYear = (Calendar)now.clone();
pastYear.set(Calendar.YEAR, now.get(Calendar.YEAR) - 1);

now.set(Calendar.HOUR_OF_DAY, now.get(Calendar.HOUR_OF_DAY) + 1);

DateFormat dateFormat = DateFormatFactoryUtil.getSimpleDateFormat(
"yyyyMMddHHmmss");

JSONObject dataJSONObject = facetConfiguration.getData();

if (dataJSONObject.has("ranges")) {
JSONArray rangesJSONArray = dataJSONObject.getJSONArray("ranges");

for (int i = 0; i < rangesJSONArray.length(); i++) {
JSONObject rangeObject = rangesJSONArray.getJSONObject(i);

String rangeString = rangeObject.getString("range");

rangeString = StringUtil.replace(
rangeString,
new String[] {
"past-hour",
"past-24-hours",
"past-week",
"past-month",
"past-year",
"*"
},
new String[] {
dateFormat.format(pastHour.getTime()),
dateFormat.format(past24Hours.getTime()),
dateFormat.format(pastWeek.getTime()),
dateFormat.format(pastMonth.getTime()),
dateFormat.format(pastYear.getTime()),
dateFormat.format(now.getTime())
}
);

rangeObject.put("range", rangeString);
}
}
}

}
Expand Up @@ -51,7 +51,9 @@ protected BooleanClause doGetFacetClause() {
if (isStatic() && dataJSONObject.has("ranges")) {
JSONArray rangesJSONArray = dataJSONObject.getJSONArray("ranges");

String rangeString = rangesJSONArray.getString(0);
JSONObject rangeObject = rangesJSONArray.getJSONObject(0);

String rangeString = rangeObject.getString("range");

String[] range = RangeParserUtil.parserRange(rangeString);

Expand Down
2 changes: 1 addition & 1 deletion portal-web/docroot/html/portlet/search/configuration.jsp
Expand Up @@ -42,7 +42,7 @@
</div>

<div class="advanced-configuration <%= (!advancedConfiguration ? "aui-helper-hidden" : "") %>" id="<portlet:namespace />advancedConfiguration">
<aui:input helpMessage="search-configuration-help" inputCssClass="search-configuration-text" name="preferences--searchConfiguration--" type="textarea" value="<%= searchConfiguration %>" />
<aui:input helpMessage="search-configuration-help" inputCssClass="search-configuration-text" name="preferences--searchConfiguration--" type="textarea" value="<%= JSONFactoryUtil.createJSONObject(searchConfiguration).toString(4) %>" />
</div>
</aui:fieldset>

Expand Down
7 changes: 4 additions & 3 deletions portal-web/docroot/html/portlet/search/css/main.css
Expand Up @@ -4,12 +4,12 @@
}

.menu-column .search-layout-content {
padding-left: 20em;
padding-left: 24em;

.menu {
margin-left: -20em;
margin-left: -24em;
position: relative;
width: 20em;
width: 24em;
}

.result {
Expand Down Expand Up @@ -174,6 +174,7 @@

.asset-entry-summary {
display: block;
margin-bottom: 1em;
}

.asset-entry-title {
Expand Down
151 changes: 56 additions & 95 deletions portal-web/docroot/html/portlet/search/facets/modified.jsp
Expand Up @@ -23,13 +23,19 @@ String fieldParamTo = ParamUtil.getString(request, facet.getFieldName() + "to");

Calendar cal = Calendar.getInstance(timeZone);

Date now = new Date();

cal.setTime(now);

DateFormat dateFormat = DateFormatFactoryUtil.getSimpleDateFormat("yyyyMMddHHmmss", timeZone);

String nowString = dateFormat.format(cal.getTime());

JSONArray rangeArray = dataJSONObject.getJSONArray("ranges");

String modifiedLabel = StringPool.BLANK;

int index = 0;

if (fieldParamSelection.equals("0")) {
modifiedLabel = LanguageUtil.get(pageContext, "any-time");
}
%>

<div class="<%= cssClass %>" data-facetFieldName="<%= facet.getFieldName() %>" id="<%= randomNamespace %>facet">
Expand All @@ -43,81 +49,60 @@ String nowString = dateFormat.format(cal.getTime());
<img alt="" src='<%= themeDisplay.getPathThemeImages() + "/common/time.png" %>' /><liferay-ui:message key="any-time" />
</aui:a>
</li>
<li class="facet-value<%= fieldParamSelection.equals("1") ? " current-term" : StringPool.BLANK %>">

<%
cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) - 1);

String taglibSetRange = renderResponse.getNamespace() + facet.getFieldName() + "setRange(1, '[" + dateFormat.format(cal.getTime()) + " TO " + nowString + "]');";
%>

<aui:a href="javascript:;" onClick="<%= taglibSetRange %>">
<liferay-ui:message key="past-hour" />
</aui:a>
</li>
<li class="facet-value<%= fieldParamSelection.equals("2") ? " current-term" : StringPool.BLANK %>">

<%
cal.setTime(now);

cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) - 1);

taglibSetRange = renderResponse.getNamespace() + facet.getFieldName() + "setRange(2, '[" + dateFormat.format(cal.getTime()) + " TO " + nowString + "]');";
%>
<%
for (int i = 0; i < rangeArray.length(); i++) {
JSONObject rangeObject = rangeArray.getJSONObject(i);

<aui:a href="javascript:;" onClick="<%= taglibSetRange %>">
<liferay-ui:message key="past-24-hours" />
</aui:a>
</li>
<li class="facet-value<%= fieldParamSelection.equals("3") ? " current-term" : StringPool.BLANK %>">

<%
cal.setTime(now);
String label = rangeObject.getString("label");
String range = rangeObject.getString("range");
TermCollector termCollector = facetCollector.getTermCollector(range);

cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) - 7);
index = (i + 1);

taglibSetRange = renderResponse.getNamespace() + facet.getFieldName() + "setRange(3, '[" + dateFormat.format(cal.getTime()) + " TO " + nowString + "]');";
%>
if (fieldParamSelection.equals(String.valueOf(index))) {
modifiedLabel = LanguageUtil.get(pageContext, label);
}

<aui:a href="javascript:;" onClick="<%= taglibSetRange %>">
<liferay-ui:message key="past-week" />
</aui:a>
</li>
<li class="facet-value<%= fieldParamSelection.equals("4") ? " current-term" : StringPool.BLANK %>">
String taglibSetRange = renderResponse.getNamespace() + facet.getFieldName() + "setRange(" + index + ", '" + range + "');";
%>

<%
cal.setTime(now);
<li class="facet-value<%= fieldParamSelection.equals(String.valueOf(index)) ? " current-term" : StringPool.BLANK %>">
<aui:a href="javascript:;" onClick="<%= taglibSetRange %>">
<liferay-ui:message key="<%= label %>" />
</aui:a>

cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
<c:if test="<%= termCollector != null %>">
<span class="frequency">(<%= termCollector.getFrequency() %>)</span>
</c:if>
</li>

taglibSetRange = renderResponse.getNamespace() + facet.getFieldName() + "setRange(4, '[" + dateFormat.format(cal.getTime()) + " TO " + nowString + "]');";
%>
<%
}
%>

<aui:a href="javascript:;" onClick="<%= taglibSetRange %>">
<liferay-ui:message key="past-month" />
</aui:a>
</li>
<li class="facet-value<%= fieldParamSelection.equals("5") ? " current-term" : StringPool.BLANK %>">
<li class="facet-value<%= fieldParamSelection.equals(String.valueOf(index + 1)) ? " current-term" : StringPool.BLANK %>">

<%
cal.setTime(now);
TermCollector termCollector = null;

cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) - 1);
if (fieldParamSelection.equals(String.valueOf(index + 1))) {
modifiedLabel = LanguageUtil.get(pageContext, "custom-range");

taglibSetRange = renderResponse.getNamespace() + facet.getFieldName() + "setRange(5, '[" + dateFormat.format(cal.getTime()) + " TO " + nowString + "]');";
termCollector = facetCollector.getTermCollector(fieldParam);
}
%>

<aui:a href="javascript:;" onClick="<%= taglibSetRange %>">
<liferay-ui:message key="past-year" />
</aui:a>
</li>
<li class="facet-value<%= fieldParamSelection.equals("6") ? " current-term" : StringPool.BLANK %>">
<aui:a href="javascript:;" onClick='<%= renderResponse.getNamespace() + facet.getFieldName() + "customRange();" %>'>
<aui:a href="javascript:;" cssClass='<%= randomNamespace + "custom-range-toggle" %>'>
<liferay-ui:message key="custom-range" />&hellip;
</aui:a>

<c:if test="<%= termCollector != null %>">
<span class="frequency">(<%= termCollector.getFrequency() %>)</span>
</c:if>
</li>

<div class="<%= !fieldParamSelection.equals("6") ? "aui-helper-hidden" : StringPool.BLANK %> modified-custom-range" id="<%= randomNamespace %>custom-range">
<div class="<%= !fieldParamSelection.equals(String.valueOf(index + 1)) ? "aui-helper-hidden" : StringPool.BLANK %> modified-custom-range" id="<%= randomNamespace %>custom-range">
<div id="<%= randomNamespace %>custom-range-from">
<aui:input label="from" name='<%= facet.getFieldName() + "from" %>' size="14" />
</div>
Expand All @@ -126,7 +111,7 @@ String nowString = dateFormat.format(cal.getTime());
<aui:input label="to" name='<%= facet.getFieldName() + "to" %>' size="14" />
</div>

<aui:button onClick='<%= renderResponse.getNamespace() + facet.getFieldName() + "searchCustomRange(6);" %>' value="search" />
<aui:button onClick='<%= renderResponse.getNamespace() + facet.getFieldName() + "searchCustomRange(" + (index + 1) + ");" %>' value="search" />
</div>
</ul>
</aui:field-wrapper>
Expand All @@ -141,33 +126,9 @@ String nowString = dateFormat.format(cal.getTime());
<aui:script use="liferay-token-list">

<%
String modifiedLabel = StringPool.BLANK;

if (fieldParamSelection.equals("0")) {
modifiedLabel = LanguageUtil.get(pageContext, "any-time");
}
else if (fieldParamSelection.equals("1")) {
modifiedLabel = LanguageUtil.get(pageContext, "past-hour");
}
else if (fieldParamSelection.equals("2")) {
modifiedLabel = LanguageUtil.get(pageContext, "past-24-hours");
}
else if (fieldParamSelection.equals("3")) {
modifiedLabel = LanguageUtil.get(pageContext, "past-week");
}
else if (fieldParamSelection.equals("4")) {
modifiedLabel = LanguageUtil.get(pageContext, "past-month");
}
else if (fieldParamSelection.equals("5")) {
modifiedLabel = LanguageUtil.get(pageContext, "past-year");
}
else if (fieldParamSelection.equals("6")) {
modifiedLabel = LanguageUtil.get(pageContext, "custom-range");
}

String tokenLabel = modifiedLabel;

if (fieldParamSelection.equals("6")) {
if (fieldParamSelection.equals(String.valueOf(index + 1))) {
String fromDateLabel = fieldParamFrom;
String toDateLabel = fieldParamTo;

Expand Down Expand Up @@ -198,15 +159,6 @@ String nowString = dateFormat.format(cal.getTime());
['aui-base']
);

Liferay.provide(
window,
'<portlet:namespace /><%= facet.getFieldName() %>customRange',
function() {
A.one('#<%= randomNamespace + "custom-range" %>').toggle();
},
['aui-base']
);

Liferay.provide(
window,
'<portlet:namespace /><%= facet.getFieldName() %>searchCustomRange',
Expand Down Expand Up @@ -287,4 +239,13 @@ String nowString = dateFormat.format(cal.getTime());
trigger: '#<portlet:namespace /><%= facet.getFieldName() %>to'
}
).render('#<%= randomNamespace %>custom-range-to');

A.one('.<%= randomNamespace %>custom-range-toggle').on(
'click',
function(event) {
event.halt();

A.one('#<%= randomNamespace + "custom-range" %>').toggle();
}
);
</aui:script>
2 changes: 1 addition & 1 deletion portal-web/docroot/html/portlet/search/init.jsp
Expand Up @@ -107,7 +107,7 @@ if (!advancedConfiguration && Validator.isNull(searchConfiguration)) {
}

if (displayModifiedRangeFacet) {
sb.append("{className: 'com.liferay.portal.kernel.search.facet.RangeFacet', data: {frequencyThreshold: 1, ranges: [{label:'modified', range:'[19700101000000 TO *]'}]}, displayStyle: 'modified', fieldName: 'modified', label: 'modified', order: 'OrderHitsDesc', static: false, weight: 1.1}");
sb.append("{className: 'com.liferay.portal.kernel.search.facet.ModifiedFacet', data: {frequencyThreshold: 0, ranges: [{label:'past-hour', range:'[past-hour TO *]'},{label:'past-24-hours', range:'[past-24-hours TO *]'},{label:'past-week', range:'[past-week TO *]'},{label:'past-month', range:'[past-month TO *]'},{label:'past-year', range:'[past-year TO *]'}]}, displayStyle: 'modified', fieldName: 'modified', label: 'modified', order: 'OrderHitsDesc', static: false, weight: 1.1}");
}

sb.append("]}");
Expand Down