001    /**
002     * Copyright (c) 2000-2013 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.taglib.ui;
016    
017    import com.liferay.portal.kernel.editor.EditorUtil;
018    import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
019    import com.liferay.portal.kernel.util.PropsKeys;
020    import com.liferay.portal.kernel.util.PropsUtil;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.kernel.util.WebKeys;
025    import com.liferay.portal.model.Portlet;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.taglib.util.IncludeTag;
028    
029    import java.util.Map;
030    
031    import javax.servlet.http.HttpServletRequest;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class InputEditorTag extends IncludeTag {
037    
038            public void setConfigParams(Map<String, String> configParams) {
039                    _configParams = configParams;
040            }
041    
042            public void setContentsLanguageId(String contentsLanguageId) {
043                    _contentsLanguageId = contentsLanguageId;
044            }
045    
046            public void setCssClass(String cssClass) {
047                    _cssClass = cssClass;
048            }
049    
050            public void setEditorImpl(String editorImpl) {
051                    _editorImpl = editorImpl;
052            }
053    
054            public void setFileBrowserParams(Map<String, String> fileBrowserParams) {
055                    _fileBrowserParams = fileBrowserParams;
056            }
057    
058            public void setHeight(String height) {
059                    _height = height;
060            }
061    
062            public void setInitMethod(String initMethod) {
063                    _initMethod = initMethod;
064            }
065    
066            public void setInlineEdit(boolean inlineEdit) {
067                    _inlineEdit = inlineEdit;
068            }
069    
070            public void setInlineEditSaveURL(String inlineEditSaveURL) {
071                    _inlineEditSaveURL = inlineEditSaveURL;
072            }
073    
074            public void setName(String name) {
075                    _name = name;
076            }
077    
078            public void setOnBlurMethod(String onBlurMethod) {
079                    _onBlurMethod = onBlurMethod;
080            }
081    
082            public void setOnChangeMethod(String onChangeMethod) {
083                    _onChangeMethod = onChangeMethod;
084            }
085    
086            public void setOnFocusMethod(String onFocusMethod) {
087                    _onFocusMethod = onFocusMethod;
088            }
089    
090            public void setResizable(boolean resizable) {
091                    _resizable = resizable;
092            }
093    
094            public void setSkipEditorLoading(boolean skipEditorLoading) {
095                    _skipEditorLoading = skipEditorLoading;
096            }
097    
098            public void setToolbarSet(String toolbarSet) {
099                    _toolbarSet = toolbarSet;
100            }
101    
102            public void setWidth(String width) {
103                    _width = width;
104            }
105    
106            @Override
107            protected void cleanUp() {
108                    _configParams = null;
109                    _contentsLanguageId = null;
110                    _cssClass = null;
111                    _editorImpl = null;
112                    _fileBrowserParams = null;
113                    _height = null;
114                    _initMethod = "initEditor";
115                    _inlineEdit = false;
116                    _inlineEditSaveURL = null;
117                    _name = "editor";
118                    _onChangeMethod = null;
119                    _onBlurMethod = null;
120                    _onFocusMethod = null;
121                    _page = null;
122                    _resizable = true;
123                    _skipEditorLoading = false;
124                    _toolbarSet = "liferay";
125                    _width = null;
126            }
127    
128            @Override
129            protected String getPage() {
130                    return _page;
131            }
132    
133            @Override
134            protected void setAttributes(HttpServletRequest request) {
135                    if (_contentsLanguageId == null) {
136                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
137                                    WebKeys.THEME_DISPLAY);
138    
139                            _contentsLanguageId = themeDisplay.getLanguageId();
140                    }
141    
142                    String cssClasses = "portlet ";
143    
144                    Portlet portlet = (Portlet)request.getAttribute(WebKeys.RENDER_PORTLET);
145    
146                    if (portlet != null) {
147                            cssClasses += portlet.getCssClassWrapper();
148                    }
149    
150                    String editorImpl = EditorUtil.getEditorValue(request, _editorImpl);
151    
152                    _page = "/html/js/editor/" + editorImpl + ".jsp";
153    
154                    if (Validator.equals(editorImpl, "ckeditor")) {
155                            String ckEditorVersion = PropsUtil.get(
156                                    PropsKeys.EDITOR_CKEDITOR_VERSION);
157    
158                            if (Validator.equals(ckEditorVersion, "latest")) {
159                                    float majorVersion = BrowserSnifferUtil.getMajorVersion(
160                                            request);
161    
162                                    boolean useLatestCkeditorVersion = true;
163    
164                                    if (BrowserSnifferUtil.isChrome(request)) {
165                                            useLatestCkeditorVersion = setCkeditorVersion(
166                                                    PropsUtil.get(
167                                                            PropsKeys.EDITOR_CKEDITOR_VERSION_LATEST_CHROME),
168                                                    majorVersion);
169                                    }
170                                    else if (BrowserSnifferUtil.isFirefox(request)) {
171                                            useLatestCkeditorVersion = setCkeditorVersion(
172                                                    PropsUtil.get(
173                                                            PropsKeys.EDITOR_CKEDITOR_VERSION_LATEST_FIREFOX),
174                                                    majorVersion);
175                                    }
176                                    else if (BrowserSnifferUtil.isIe(request)) {
177                                            useLatestCkeditorVersion = setCkeditorVersion(
178                                                    PropsUtil.get(
179                                                            PropsKeys.EDITOR_CKEDITOR_VERSION_LATEST_IE),
180                                                    majorVersion);
181                                    }
182                                    else if (BrowserSnifferUtil.isSafari(request)) {
183                                            useLatestCkeditorVersion = setCkeditorVersion(
184                                                    PropsUtil.get
185                                                            (PropsKeys.EDITOR_CKEDITOR_VERSION_LATEST_SAFARI),
186                                                    majorVersion);
187                                    }
188                                    else {
189                                            useLatestCkeditorVersion = false;
190                                    }
191    
192                                    if (useLatestCkeditorVersion) {
193                                            StringBundler sb = new StringBundler(5);
194    
195                                            sb.append("/html/js/editor/");
196                                            sb.append(editorImpl);
197                                            sb.append(StringPool.UNDERLINE);
198                                            sb.append(ckEditorVersion);
199                                            sb.append(".jsp");
200    
201                                            _page = sb.toString();
202                                    }
203                            }
204                    }
205    
206                    request.setAttribute(
207                            "liferay-ui:input-editor:configParams", _configParams);
208                    request.setAttribute(
209                            "liferay-ui:input-editor:contentsLanguageId", _contentsLanguageId);
210                    request.setAttribute("liferay-ui:input-editor:cssClass", _cssClass);
211                    request.setAttribute("liferay-ui:input-editor:cssClasses", cssClasses);
212                    request.setAttribute("liferay-ui:input-editor:editorImpl", editorImpl);
213                    request.setAttribute(
214                            "liferay-ui:input-editor:fileBrowserParams", _fileBrowserParams);
215    
216                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
217                            WebKeys.THEME_DISPLAY);
218    
219                    long groupId = themeDisplay.getDoAsGroupId();
220    
221                    if (groupId == 0) {
222                            groupId = themeDisplay.getScopeGroupId();
223                    }
224    
225                    request.setAttribute("liferay-ui:input-editor:groupId", groupId);
226                    request.setAttribute("liferay-ui:input-editor:height", _height);
227                    request.setAttribute("liferay-ui:input-editor:initMethod", _initMethod);
228                    request.setAttribute(
229                            "liferay-ui:input-editor:inlineEdit", String.valueOf(_inlineEdit));
230                    request.setAttribute(
231                            "liferay-ui:input-editor:inlineEditSaveURL", _inlineEditSaveURL);
232                    request.setAttribute("liferay-ui:input-editor:name", _name);
233                    request.setAttribute(
234                            "liferay-ui:input-editor:onBlurMethod", _onBlurMethod);
235                    request.setAttribute(
236                            "liferay-ui:input-editor:onChangeMethod", _onChangeMethod);
237                    request.setAttribute(
238                            "liferay-ui:input-editor:onFocusMethod", _onFocusMethod);
239                    request.setAttribute(
240                            "liferay-ui:input-editor:resizable", String.valueOf(_resizable));
241                    request.setAttribute(
242                            "liferay-ui:input-editor:skipEditorLoading",
243                            String.valueOf(_skipEditorLoading));
244                    request.setAttribute("liferay-ui:input-editor:toolbarSet", _toolbarSet);
245                    request.setAttribute("liferay-ui:input-editor:width", _width);
246            }
247    
248            private boolean setCkeditorVersion(
249                    String propertyValue, float majorVersion) {
250    
251                    if (Validator.isNotNull(propertyValue)) {
252                            float propertyFloatValue = Float.valueOf(propertyValue);
253    
254                            if (propertyFloatValue > majorVersion) {
255                                    return false;
256                            }
257                    }
258                    else {
259                            return false;
260                    }
261    
262                    return true;
263            }
264    
265            private Map<String, String> _configParams;
266            private String _contentsLanguageId;
267            private String _cssClass;
268            private String _editorImpl;
269            private Map<String, String> _fileBrowserParams;
270            private String _height;
271            private String _initMethod = "initEditor";
272            private boolean _inlineEdit;
273            private String _inlineEditSaveURL;
274            private String _name = "editor";
275            private String _onBlurMethod;
276            private String _onChangeMethod;
277            private String _onFocusMethod;
278            private String _page;
279            private boolean _resizable = true;
280            private boolean _skipEditorLoading;
281            private String _toolbarSet = "liferay";
282            private String _width;
283    
284    }