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