001
014
015 package com.liferay.portal.theme;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.util.HtmlUtil;
019 import com.liferay.portal.kernel.util.StringUtil;
020 import com.liferay.portal.kernel.util.Validator;
021 import com.liferay.portal.kernel.util.WebKeys;
022 import com.liferay.portal.model.Layout;
023 import com.liferay.portal.util.PortalUtil;
024
025 import java.io.Serializable;
026
027 import java.lang.reflect.Method;
028
029 import java.util.ArrayList;
030 import java.util.List;
031 import java.util.Map;
032
033 import javax.servlet.http.HttpServletRequest;
034
035
043 public class NavItem implements Serializable {
044
045
060 public static List<NavItem> fromLayouts(
061 HttpServletRequest request, List<Layout> layouts,
062 Map<String, Object> contextObjects) {
063
064 if (layouts == null) {
065 return null;
066 }
067
068 List<NavItem> navItems = new ArrayList<>(layouts.size());
069
070 for (Layout layout : layouts) {
071 navItems.add(new NavItem(request, layout, contextObjects));
072 }
073
074 return navItems;
075 }
076
077 public NavItem(
078 HttpServletRequest request, Layout layout,
079 Map<String, Object> contextObjects) {
080
081 _request = request;
082 _themeDisplay = (ThemeDisplay)request.getAttribute(
083 WebKeys.THEME_DISPLAY);
084 _layout = layout;
085 _contextObjects = contextObjects;
086 }
087
088
096 public List<NavItem> getChildren() throws Exception {
097 if (_children == null) {
098 List<Layout> layouts = _layout.getChildren(
099 _themeDisplay.getPermissionChecker());
100
101 _children = fromLayouts(_request, layouts, _contextObjects);
102 }
103
104 return _children;
105 }
106
107
112 public Layout getLayout() {
113 return _layout;
114 }
115
116
121 public long getLayoutId() {
122 return _layout.getLayoutId();
123 }
124
125
130 public String getName() {
131 return HtmlUtil.escape(getUnescapedName());
132 }
133
134
141 public String getRegularFullURL() throws Exception {
142 String portalURL = PortalUtil.getPortalURL(_request);
143
144 String regularURL = getRegularURL();
145
146 if (StringUtil.startsWith(regularURL, portalURL) ||
147 Validator.isUrl(regularURL)) {
148
149 return regularURL;
150 }
151 else {
152 return portalURL.concat(regularURL);
153 }
154 }
155
156
162 public String getRegularURL() throws Exception {
163 return _layout.getRegularURL(_request);
164 }
165
166 public String getResetLayoutURL() throws Exception {
167 return _layout.getResetLayoutURL(_request);
168 }
169
170 public String getResetMaxStateURL() throws Exception {
171 return _layout.getResetMaxStateURL(_request);
172 }
173
174
179 public String getTarget() {
180 return _layout.getTarget();
181 }
182
183
190 public String getTitle() {
191 return _layout.getTitle(_themeDisplay.getLocale());
192 }
193
194
201 public String getUnescapedName() {
202 return _layout.getName(_themeDisplay.getLocale());
203 }
204
205
213 public String getURL() throws Exception {
214 return HtmlUtil.escapeHREF(getRegularFullURL());
215 }
216
217
225 public boolean hasChildren() throws Exception {
226 List<NavItem> children = getChildren();
227
228 if (!children.isEmpty()) {
229 return true;
230 }
231 else {
232 return false;
233 }
234 }
235
236 public void icon() throws Exception {
237 Object velocityTaglib = _contextObjects.get("theme");
238
239 Method method = (Method)_contextObjects.get(
240 "velocityTaglib_layoutIcon");
241
242 method.invoke(velocityTaglib, _layout);
243 }
244
245 public boolean isChildSelected() throws PortalException {
246 return _layout.isChildSelected(
247 _themeDisplay.isTilesSelectable(), _themeDisplay.getLayout());
248 }
249
250 public boolean isSelected() throws Exception {
251 return _layout.isSelected(
252 _themeDisplay.isTilesSelectable(), _themeDisplay.getLayout(),
253 _themeDisplay.getLayout().getAncestorPlid());
254 }
255
256 private List<NavItem> _children;
257 private final Map<String, Object> _contextObjects;
258 private final Layout _layout;
259 private final HttpServletRequest _request;
260 private final ThemeDisplay _themeDisplay;
261
262 }