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.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
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 void setAttributes(HttpServletRequest request) {
073                    super.setAttributes(request);
074    
075                    setNamespacedAttribute(request, "id", _getNamespacedId());
076                    setNamespacedAttribute(request, "searchResults", _hasSearchResults());
077            }
078    
079            private String _getNamespacedId() {
080                    if (Validator.isNotNull(_namespacedId)) {
081                            return _namespacedId;
082                    }
083    
084                    _namespacedId = getId();
085    
086                    if (Validator.isNull(_namespacedId)) {
087                            _namespacedId = StringUtil.randomId();
088                    }
089    
090                    HttpServletRequest request =
091                            (HttpServletRequest)pageContext.getRequest();
092    
093                    PortletResponse portletResponse = (PortletResponse)request.getAttribute(
094                            JavaConstants.JAVAX_PORTLET_RESPONSE);
095    
096                    if (portletResponse != null) {
097                            _namespacedId = portletResponse.getNamespace() + _namespacedId;
098                    }
099    
100                    return _namespacedId;
101            }
102    
103            private boolean _hasSearchResults() {
104                    SearchContainer<?> searchContainer = getSearchContainer();
105    
106                    if (searchContainer == null) {
107                            return false;
108                    }
109    
110                    DisplayTerms displayTerms = searchContainer.getDisplayTerms();
111    
112                    String keywords = displayTerms.getKeywords();
113    
114                    if (displayTerms.isAdvancedSearch() ||
115                            !keywords.equals(StringPool.BLANK)) {
116    
117                            return true;
118                    }
119    
120                    return false;
121            }
122    
123            private String _namespacedId;
124    
125    }