001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.taglib.aui;
016    
017    import com.liferay.portal.kernel.dao.search.DisplayTerms;
018    import com.liferay.portal.kernel.dao.search.SearchContainer;
019    import com.liferay.portal.kernel.util.JavaConstants;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.util.PortalUtil;
024    import com.liferay.taglib.aui.base.BaseNavBarSearchTag;
025    
026    import javax.portlet.PortletResponse;
027    
028    import javax.servlet.http.HttpServletRequest;
029    import javax.servlet.jsp.JspException;
030    
031    /**
032     * @author Eduardo Lundgren
033     * @author Bruno Basto
034     * @author Nathan Cavanaugh
035     * @author Julio Camarero
036     */
037    public class NavBarSearchTag extends BaseNavBarSearchTag {
038    
039            @Override
040            public int doStartTag() throws JspException {
041                    NavBarTag navBarTag = (NavBarTag)findAncestorWithClass(
042                            this, NavBarTag.class);
043    
044                    if (navBarTag != null) {
045                            StringBundler sb = navBarTag.getResponsiveButtonsSB();
046    
047                            sb.append("<a class=\"btn navbar-btn navbar-toggle");
048    
049                            if (_hasSearchResults()) {
050                                    sb.append(" hide");
051                            }
052    
053                            sb.append("\" id=\"");
054                            sb.append(_getNamespacedId());
055                            sb.append("NavbarBtn\" data-navId=\"");
056                            sb.append(_getNamespacedId());
057                            sb.append("\" tabindex=\"0\">");
058                            sb.append("<i class=\"icon-search\"></i></a>");
059                    }
060    
061                    return super.doStartTag();
062            }
063    
064            @Override
065            protected void cleanUp() {
066                    super.cleanUp();
067    
068                    _namespacedId = null;
069            }
070    
071            @Override
072            protected String getEndPage() {
073                    if (Validator.isNotNull(getMarkupView())) {
074                            return "/html/taglib/aui/nav_bar_search/" + getMarkupView() +
075                                    "/end.jsp";
076                    }
077    
078                    return "/html/taglib/aui/nav_bar_search/end.jsp";
079            }
080    
081            protected String getMarkupView() {
082                    String markupView = StringPool.BLANK;
083    
084                    NavBarTag navBarTag = (NavBarTag)findAncestorWithClass(
085                            this, NavBarTag.class);
086    
087                    if (navBarTag != null) {
088                            markupView = navBarTag.getMarkupView();
089                    }
090    
091                    return markupView;
092            }
093    
094            @Override
095            protected String getStartPage() {
096                    if (Validator.isNotNull(getMarkupView())) {
097                            return "/html/taglib/aui/nav_bar_search/" + getMarkupView() +
098                                    "/start.jsp";
099                    }
100    
101                    return "/html/taglib/aui/nav_bar_search/start.jsp";
102            }
103    
104            @Override
105            protected void setAttributes(HttpServletRequest request) {
106                    super.setAttributes(request);
107    
108                    setNamespacedAttribute(request, "id", _getNamespacedId());
109                    setNamespacedAttribute(request, "searchResults", _hasSearchResults());
110            }
111    
112            private String _getNamespacedId() {
113                    if (Validator.isNotNull(_namespacedId)) {
114                            return _namespacedId;
115                    }
116    
117                    _namespacedId = getId();
118    
119                    HttpServletRequest request =
120                            (HttpServletRequest)pageContext.getRequest();
121    
122                    if (Validator.isNull(_namespacedId)) {
123                            _namespacedId = PortalUtil.getUniqueElementId(
124                                    request, StringPool.BLANK, AUIUtil.normalizeId("navBar"));
125                    }
126    
127                    PortletResponse portletResponse = (PortletResponse)request.getAttribute(
128                            JavaConstants.JAVAX_PORTLET_RESPONSE);
129    
130                    if (portletResponse != null) {
131                            _namespacedId = portletResponse.getNamespace() + _namespacedId;
132                    }
133    
134                    return _namespacedId;
135            }
136    
137            private boolean _hasSearchResults() {
138                    SearchContainer<?> searchContainer = getSearchContainer();
139    
140                    if (searchContainer == null) {
141                            return false;
142                    }
143    
144                    DisplayTerms displayTerms = searchContainer.getDisplayTerms();
145    
146                    String keywords = displayTerms.getKeywords();
147    
148                    if (displayTerms.isAdvancedSearch() ||
149                            !keywords.equals(StringPool.BLANK)) {
150    
151                            return true;
152                    }
153    
154                    return false;
155            }
156    
157            private String _namespacedId;
158    
159    }