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