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.portal.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.ServletRequest;
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(PageContext pageContext) {
037                    _pageContext = pageContext;
038                    _writer = pageContext.getOut();
039    
040                    ServletRequest servletRequest = pageContext.getRequest();
041    
042                    Layout selLayout = (Layout)servletRequest.getAttribute(
043                            "edit_pages.jsp-selLayout");
044    
045                    _layoutTypeSettings = selLayout.getTypeSettingsProperties();
046    
047                    _customizationEnabled = true;
048    
049                    if (!SitesUtil.isLayoutUpdateable(selLayout)) {
050                            _customizationEnabled = false;
051                    }
052    
053                    if (selLayout.isLayoutPrototypeLinkActive()) {
054                            _customizationEnabled = false;
055                    }
056            }
057    
058            public String processColumn(String columnId) throws Exception {
059                    return processColumn(columnId, StringPool.BLANK);
060            }
061    
062            public String processColumn(String columnId, String classNames)
063                    throws Exception {
064    
065                    String customizableKey = CustomizedPages.namespaceColumnId(columnId);
066    
067                    boolean customizable = false;
068    
069                    if (_customizationEnabled) {
070                            customizable = GetterUtil.getBoolean(
071                                    _layoutTypeSettings.getProperty(
072                                            customizableKey, String.valueOf(false)));
073                    }
074    
075                    _writer.append("<div class=\"");
076                    _writer.append(classNames);
077                    _writer.append("\">");
078    
079                    _writer.append("<h1>");
080                    _writer.append(columnId);
081                    _writer.append("</h1>");
082    
083                    InputTag inputTag = new InputTag();
084    
085                    inputTag.setDisabled(!_customizationEnabled);
086                    inputTag.setLabel("customizable");
087                    inputTag.setName(
088                            "TypeSettingsProperties--".concat(customizableKey).concat("--"));
089                    inputTag.setPageContext(_pageContext);
090                    inputTag.setType("checkbox");
091                    inputTag.setValue(customizable);
092    
093                    int result = inputTag.doStartTag();
094    
095                    if (result == Tag.EVAL_BODY_INCLUDE) {
096                            inputTag.doEndTag();
097                    }
098    
099                    _writer.append("</div>");
100    
101                    return StringPool.BLANK;
102            }
103    
104            public String processMax() throws Exception {
105                    return StringPool.BLANK;
106            }
107    
108            public String processPortlet(String portletId) throws Exception {
109                    _writer.append("<div class=\"portlet\">");
110                    _writer.append(portletId);
111                    _writer.append("</div>");
112    
113                    return StringPool.BLANK;
114            }
115    
116            private boolean _customizationEnabled;
117            private UnicodeProperties _layoutTypeSettings;
118            private PageContext _pageContext;
119            private Writer _writer;
120    
121    }