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.taglib.ui;
016    
017    import com.liferay.portal.kernel.language.UnicodeLanguageUtil;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.Http;
020    import com.liferay.portal.kernel.util.HttpUtil;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.taglib.FileAvailabilityUtil;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     * @author Shuyang Zhou
029     */
030    public class IconDeleteTag extends IconTag {
031    
032            public void setConfirmation(String confirmation) {
033                    _confirmation = confirmation;
034            }
035    
036            public void setTrash(boolean trash) {
037                    _trash = trash;
038            }
039    
040            @Override
041            protected String getPage() {
042                    if (FileAvailabilityUtil.isAvailable(servletContext, _PAGE)) {
043                            return _PAGE;
044                    }
045    
046                    String cssClass = GetterUtil.getString(getCssClass());
047    
048                    setCssClass(cssClass.concat(" item-remove"));
049    
050                    if (Validator.isNull(getImage())) {
051                            if (_trash) {
052                                    setIconCssClass("icon-trash");
053                            }
054                            else {
055                                    setIconCssClass("icon-remove");
056    
057                                    if (Validator.isNull(getMessage())) {
058                                            setMessage("delete");
059                                    }
060                            }
061                    }
062    
063                    if (_trash && Validator.isNull(getMessage())) {
064                            setMessage("move-to-the-recycle-bin");
065                    }
066    
067                    String url = getUrl();
068    
069                    if (url.startsWith("javascript:if (confirm('")) {
070                            return super.getPage();
071                    }
072    
073                    if (url.startsWith("javascript:")) {
074                            url = url.substring(11);
075                    }
076    
077                    if (url.startsWith(Http.HTTP_WITH_SLASH) ||
078                            url.startsWith(Http.HTTPS_WITH_SLASH)) {
079    
080                            url = "submitForm(document.hrefFm, '".concat(
081                                    HttpUtil.encodeURL(url)).concat("');");
082                    }
083    
084                    if (url.startsWith("wsrp_rewrite?")) {
085                            url = StringUtil.replace(
086                                    url, "/wsrp_rewrite",
087                                    "&wsrp-extensions=encodeURL/wsrp_rewrite");
088                            url = "submitForm(document.hrefFm, '".concat(url).concat("');");
089                    }
090    
091                    if (!_trash) {
092                            StringBundler sb = new StringBundler(5);
093    
094                            sb.append("javascript:if (confirm('");
095    
096                            if (Validator.isNotNull(_confirmation)) {
097                                    sb.append(UnicodeLanguageUtil.get(request, _confirmation));
098                            }
099                            else {
100                                    String confirmation = "are-you-sure-you-want-to-delete-this";
101    
102                                    sb.append(UnicodeLanguageUtil.get(request, confirmation));
103                            }
104    
105                            sb.append("')) { ");
106                            sb.append(url);
107                            sb.append(" } else { self.focus(); }");
108    
109                            url = sb.toString();
110                    }
111                    else {
112                            url = "javascript:".concat(url);
113                    }
114    
115                    setUrl(url);
116    
117                    return super.getPage();
118            }
119    
120            private static final String _PAGE = "/html/taglib/ui/icon_delete/page.jsp";
121    
122            private String _confirmation;
123            private boolean _trash;
124    
125    }