001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            public String processColumn(String columnId) throws Exception {
060                    return processColumn(columnId, StringPool.BLANK);
061            }
062    
063            public String processColumn(String columnId, String classNames)
064                    throws Exception {
065    
066                    String customizableKey = CustomizedPages.namespaceColumnId(columnId);
067    
068                    boolean customizable = false;
069    
070                    if (_customizationEnabled) {
071                            customizable = GetterUtil.getBoolean(
072                                    _layoutTypeSettings.getProperty(
073                                            customizableKey, String.valueOf(false)));
074                    }
075    
076                    _writer.append("<div class=\"");
077                    _writer.append(classNames);
078                    _writer.append("\">");
079    
080                    _writer.append("<h1>");
081                    _writer.append(columnId);
082                    _writer.append("</h1>");
083    
084                    InputTag inputTag = new InputTag();
085    
086                    inputTag.setDisabled(!_customizationEnabled);
087                    inputTag.setLabel("customizable");
088                    inputTag.setName(
089                            "TypeSettingsProperties--".concat(customizableKey).concat("--"));
090                    inputTag.setPageContext(_pageContext);
091                    inputTag.setType("checkbox");
092                    inputTag.setValue(customizable);
093    
094                    int result = inputTag.doStartTag();
095    
096                    if (result == Tag.EVAL_BODY_INCLUDE) {
097                            inputTag.doEndTag();
098                    }
099    
100                    _writer.append("</div>");
101    
102                    return StringPool.BLANK;
103            }
104    
105            public String processMax() throws Exception {
106                    return processMax(StringPool.BLANK);
107            }
108    
109            public String processMax(String classNames) throws Exception {
110                    return StringPool.BLANK;
111            }
112    
113            public String processPortlet(String portletId) throws Exception {
114                    _writer.append("<div class=\"portlet\">");
115                    _writer.append(portletId);
116                    _writer.append("</div>");
117    
118                    return StringPool.BLANK;
119            }
120    
121            private boolean _customizationEnabled;
122            private UnicodeProperties _layoutTypeSettings;
123            private PageContext _pageContext;
124            private HttpServletRequest _request;
125            private Writer _writer;
126    
127    }