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