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.language.LanguageUtil;
020    import com.liferay.portal.kernel.util.CharPool;
021    import com.liferay.portal.kernel.util.JavaConstants;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.kernel.util.WebKeys;
027    import com.liferay.portal.model.User;
028    import com.liferay.portal.theme.ThemeDisplay;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.taglib.aui.base.BaseNavTag;
031    import com.liferay.taglib.util.TagResourceBundleUtil;
032    
033    import java.util.ResourceBundle;
034    
035    import javax.portlet.PortletResponse;
036    
037    import javax.servlet.http.HttpServletRequest;
038    import javax.servlet.jsp.JspException;
039    import javax.servlet.jsp.tagext.BodyTag;
040    
041    /**
042     * @author Eduardo Lundgren
043     * @author Bruno Basto
044     * @author Nathan Cavanaugh
045     * @author Julio Camarero
046     */
047    public class NavTag extends BaseNavTag implements BodyTag {
048    
049            @Override
050            public int doStartTag() throws JspException {
051                    NavBarTag navBarTag = (NavBarTag)findAncestorWithClass(
052                            this, NavBarTag.class);
053    
054                    if ((navBarTag != null) &&
055                            (!_calledCollapsibleSetter || getCollapsible())) {
056    
057                            setCollapsible(true);
058    
059                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
060                                    WebKeys.THEME_DISPLAY);
061    
062                            StringBundler sb = navBarTag.getResponsiveButtonsSB();
063    
064                            sb.append("<a class=\"btn navbar-btn navbar-toggle");
065    
066                            String cssClass = getCssClass();
067    
068                            if (Validator.isNotNull(cssClass)) {
069                                    String[] cssClassParts = StringUtil.split(
070                                            cssClass, CharPool.SPACE);
071    
072                                    for (int i = 0; i < cssClassParts.length; i++) {
073                                            sb.append(StringPool.SPACE);
074                                            sb.append(cssClassParts[i]);
075                                            sb.append("-btn");
076                                    }
077                            }
078    
079                            if (_hasSearchResults()) {
080                                    sb.append(" hide");
081                            }
082    
083                            sb.append("\" id=\"");
084                            sb.append(_getNamespacedId());
085                            sb.append("NavbarBtn\" ");
086                            sb.append("data-navId=\"");
087                            sb.append(_getNamespacedId());
088                            sb.append("\" tabindex=\"0\">");
089    
090                            String icon = getIcon();
091    
092                            if (Validator.isNull(icon)) {
093                                    sb.append("<i class=\"icon-reorder\"></i>");
094                            }
095                            else if (icon.equals("user") && themeDisplay.isSignedIn()) {
096                                    try {
097                                            sb.append("<img alt=\"");
098    
099                                            ResourceBundle resourceBundle =
100                                                    TagResourceBundleUtil.getResourceBundle(pageContext);
101    
102                                            sb.append(LanguageUtil.get(resourceBundle, "my-account"));
103    
104                                            sb.append("\" class=\"user-avatar-image\" ");
105                                            sb.append("src=\"");
106    
107                                            User user = themeDisplay.getUser();
108    
109                                            sb.append(user.getPortraitURL(themeDisplay));
110    
111                                            sb.append("\">");
112                                    }
113                                    catch (Exception e) {
114                                            throw new JspException(e);
115                                    }
116                            }
117                            else {
118                                    sb.append("<i class=\"icon-");
119                                    sb.append(icon);
120                                    sb.append("\"></i>");
121                            }
122    
123                            sb.append("</a>");
124                    }
125    
126                    return super.doStartTag();
127            }
128    
129            @Override
130            public void setCollapsible(boolean collapsible) {
131                    super.setCollapsible(collapsible);
132    
133                    _calledCollapsibleSetter = true;
134            }
135    
136            @Override
137            protected void cleanUp() {
138                    super.cleanUp();
139    
140                    _calledCollapsibleSetter = false;
141                    _namespacedId = null;
142            }
143    
144            @Override
145            protected int processStartTag() throws Exception {
146                    return EVAL_BODY_BUFFERED;
147            }
148    
149            @Override
150            protected void setAttributes(HttpServletRequest request) {
151                    super.setAttributes(request);
152    
153                    setNamespacedAttribute(request, "id", _getNamespacedId());
154            }
155    
156            private String _getNamespacedId() {
157                    if (Validator.isNotNull(_namespacedId)) {
158                            return _namespacedId;
159                    }
160    
161                    _namespacedId = getId();
162    
163                    HttpServletRequest request =
164                            (HttpServletRequest)pageContext.getRequest();
165    
166                    if (Validator.isNull(_namespacedId)) {
167                            _namespacedId = PortalUtil.getUniqueElementId(
168                                    request, StringPool.BLANK, AUIUtil.normalizeId("navTag"));
169                    }
170    
171                    PortletResponse portletResponse = (PortletResponse)request.getAttribute(
172                            JavaConstants.JAVAX_PORTLET_RESPONSE);
173    
174                    if ((portletResponse != null) && getUseNamespace()) {
175                            _namespacedId = portletResponse.getNamespace() + _namespacedId;
176                    }
177    
178                    return _namespacedId;
179            }
180    
181            private boolean _hasSearchResults() {
182                    SearchContainer<?> searchContainer = getSearchContainer();
183    
184                    if (searchContainer == null) {
185                            return false;
186                    }
187    
188                    DisplayTerms displayTerms = searchContainer.getDisplayTerms();
189    
190                    String keywords = displayTerms.getKeywords();
191    
192                    if (displayTerms.isAdvancedSearch() ||
193                            !keywords.equals(StringPool.BLANK)) {
194    
195                            return true;
196                    }
197    
198                    return false;
199            }
200    
201            private boolean _calledCollapsibleSetter;
202            private String _namespacedId;
203    
204    }