001    /**
002     * Copyright (c) 2000-2013 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.portlet.layoutconfiguration.util.velocity;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.UnicodeProperties;
020    import com.liferay.portal.model.CustomizedPages;
021    import com.liferay.portal.model.Layout;
022    import com.liferay.portlet.sites.util.SitesUtil;
023    import com.liferay.taglib.aui.InputTag;
024    
025    import java.io.Writer;
026    
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.jsp.PageContext;
029    import javax.servlet.jsp.tagext.Tag;
030    
031    /**
032     * @author Raymond Aug??
033     */
034    public class CustomizationSettingsProcessor implements ColumnProcessor {
035    
036            public CustomizationSettingsProcessor(
037                    HttpServletRequest request, PageContext pageContext, Writer writer) {
038    
039                    _pageContext = pageContext;
040                    _request = request;
041                    _writer = writer;
042    
043                    Layout selLayout = (Layout)_request.getAttribute(
044                            "edit_pages.jsp-selLayout");
045    
046                    _layoutTypeSettings = selLayout.getTypeSettingsProperties();
047    
048                    _customizationEnabled = true;
049    
050                    if (!SitesUtil.isLayoutUpdateable(selLayout)) {
051                            _customizationEnabled = false;
052                    }
053    
054                    if (selLayout.isLayoutPrototypeLinkActive()) {
055                            _customizationEnabled = false;
056                    }
057            }
058    
059            @Override
060            public String processColumn(String columnId) throws Exception {
061                    return processColumn(columnId, StringPool.BLANK);
062            }
063    
064            @Override
065            public String processColumn(String columnId, String classNames)
066                    throws Exception {
067    
068                    String customizableKey = CustomizedPages.namespaceColumnId(columnId);
069    
070                    boolean customizable = false;
071    
072                    if (_customizationEnabled) {
073                            customizable = GetterUtil.getBoolean(
074                                    _layoutTypeSettings.getProperty(
075                                            customizableKey, String.valueOf(false)));
076                    }
077    
078                    _writer.append("<div class=\"");
079                    _writer.append(classNames);
080                    _writer.append("\">");
081    
082                    _writer.append("<h1>");
083                    _writer.append(columnId);
084                    _writer.append("</h1>");
085    
086                    InputTag inputTag = new InputTag();
087    
088                    inputTag.setDisabled(!_customizationEnabled);
089                    inputTag.setLabel("customizable");
090                    inputTag.setName(
091                            "TypeSettingsProperties--".concat(customizableKey).concat("--"));
092                    inputTag.setPageContext(_pageContext);
093                    inputTag.setType("checkbox");
094                    inputTag.setValue(customizable);
095    
096                    int result = inputTag.doStartTag();
097    
098                    if (result == Tag.EVAL_BODY_INCLUDE) {
099                            inputTag.doEndTag();
100                    }
101    
102                    _writer.append("</div>");
103    
104                    return StringPool.BLANK;
105            }
106    
107            @Override
108            public String processMax() throws Exception {
109                    return processMax(StringPool.BLANK);
110            }
111    
112            @Override
113            public String processMax(String classNames) throws Exception {
114                    return StringPool.BLANK;
115            }
116    
117            @Override
118            public String processPortlet(String portletId) throws Exception {
119                    _writer.append("<div class=\"portlet\">");
120                    _writer.append(portletId);
121                    _writer.append("</div>");
122    
123                    return StringPool.BLANK;
124            }
125    
126            private boolean _customizationEnabled;
127            private UnicodeProperties _layoutTypeSettings;
128            private PageContext _pageContext;
129            private HttpServletRequest _request;
130            private Writer _writer;
131    
132    }