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.portal.layoutconfiguration.util.velocity;
016    
017    import com.liferay.portal.kernel.model.CustomizedPages;
018    import com.liferay.portal.kernel.model.Layout;
019    import com.liferay.portal.kernel.model.LayoutConstants;
020    import com.liferay.portal.kernel.portlet.PortletProvider;
021    import com.liferay.portal.kernel.portlet.PortletProviderUtil;
022    import com.liferay.portal.kernel.service.LayoutLocalServiceUtil;
023    import com.liferay.portal.kernel.servlet.JSPSupportServlet;
024    import com.liferay.portal.kernel.util.ClassLoaderUtil;
025    import com.liferay.portal.kernel.util.GetterUtil;
026    import com.liferay.portal.kernel.util.ParamUtil;
027    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
028    import com.liferay.portal.kernel.util.StringPool;
029    import com.liferay.portal.kernel.util.UnicodeProperties;
030    import com.liferay.sites.kernel.util.SitesUtil;
031    import com.liferay.taglib.aui.InputTag;
032    
033    import java.io.Writer;
034    
035    import java.util.Map;
036    
037    import javax.servlet.http.HttpServletRequest;
038    import javax.servlet.http.HttpServletResponse;
039    import javax.servlet.jsp.JspFactory;
040    import javax.servlet.jsp.PageContext;
041    import javax.servlet.jsp.tagext.Tag;
042    
043    /**
044     * @author Raymond Aug??
045     * @author Oliver Teichmann
046     */
047    public class CustomizationSettingsProcessor implements ColumnProcessor {
048    
049            public CustomizationSettingsProcessor(
050                    HttpServletRequest request, HttpServletResponse response) {
051    
052                    JspFactory jspFactory = JspFactory.getDefaultFactory();
053    
054                    ClassLoader contextClassLoader =
055                            ClassLoaderUtil.getContextClassLoader();
056    
057                    try {
058                            ClassLoaderUtil.setContextClassLoader(
059                                    PortalClassLoaderUtil.getClassLoader());
060    
061                            _pageContext = jspFactory.getPageContext(
062                                    new JSPSupportServlet(request.getServletContext()), request,
063                                    response, null, false, 0, false);
064                    }
065                    finally {
066                            ClassLoaderUtil.setContextClassLoader(contextClassLoader);
067                    }
068    
069                    _writer = _pageContext.getOut();
070    
071                    Layout selLayout = null;
072    
073                    long selPlid = ParamUtil.getLong(
074                            request, "selPlid", LayoutConstants.DEFAULT_PLID);
075    
076                    if (selPlid != LayoutConstants.DEFAULT_PLID) {
077                            selLayout = LayoutLocalServiceUtil.fetchLayout(selPlid);
078                    }
079    
080                    _layoutTypeSettings = selLayout.getTypeSettingsProperties();
081    
082                    if (!SitesUtil.isLayoutUpdateable(selLayout) ||
083                            selLayout.isLayoutPrototypeLinkActive()) {
084    
085                            _customizationEnabled = false;
086                    }
087                    else {
088                            _customizationEnabled = true;
089                    }
090            }
091    
092            @Override
093            public String processColumn(String columnId) throws Exception {
094                    return processColumn(columnId, StringPool.BLANK);
095            }
096    
097            @Override
098            public String processColumn(String columnId, String classNames)
099                    throws Exception {
100    
101                    String customizableKey = CustomizedPages.namespaceColumnId(columnId);
102    
103                    boolean customizable = false;
104    
105                    if (_customizationEnabled) {
106                            customizable = GetterUtil.getBoolean(
107                                    _layoutTypeSettings.getProperty(
108                                            customizableKey, String.valueOf(false)));
109                    }
110    
111                    _writer.append("<div class=\"");
112                    _writer.append(classNames);
113                    _writer.append("\">");
114    
115                    _writer.append("<h1>");
116                    _writer.append(columnId);
117                    _writer.append("</h1>");
118    
119                    InputTag inputTag = new InputTag();
120    
121                    inputTag.setDisabled(!_customizationEnabled);
122                    inputTag.setDynamicAttribute(
123                            StringPool.BLANK, "labelOff", "not-customizable");
124                    inputTag.setDynamicAttribute(
125                            StringPool.BLANK, "labelOn", "customizable");
126                    inputTag.setLabel(StringPool.BLANK);
127                    inputTag.setName(
128                            "TypeSettingsProperties--".concat(customizableKey).concat("--"));
129                    inputTag.setPageContext(_pageContext);
130                    inputTag.setType("toggle-switch");
131                    inputTag.setValue(customizable);
132    
133                    int result = inputTag.doStartTag();
134    
135                    if (result == Tag.EVAL_BODY_INCLUDE) {
136                            inputTag.doEndTag();
137                    }
138    
139                    _writer.append("</div>");
140    
141                    return StringPool.BLANK;
142            }
143    
144            @Override
145            public String processMax() throws Exception {
146                    return StringPool.BLANK;
147            }
148    
149            @Override
150            public String processPortlet(String portletId) throws Exception {
151                    _writer.append("<div class=\"portlet\">");
152                    _writer.append(portletId);
153                    _writer.append("</div>");
154    
155                    return StringPool.BLANK;
156            }
157    
158            @Override
159            public String processPortlet(
160                            String portletId, Map<String, ?> defaultSettingsMap)
161                    throws Exception {
162    
163                    return processPortlet(portletId);
164            }
165    
166            @Override
167            public String processPortlet(
168                            String portletProviderClassName,
169                            PortletProvider.Action portletProviderAction)
170                    throws Exception {
171    
172                    String portletId = PortletProviderUtil.getPortletId(
173                            portletProviderClassName, portletProviderAction);
174    
175                    return processPortlet(portletId);
176            }
177    
178            private final boolean _customizationEnabled;
179            private final UnicodeProperties _layoutTypeSettings;
180            private final PageContext _pageContext;
181            private final Writer _writer;
182    
183    }