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    import com.liferay.taglib.util.TagResourceBundleUtil;
026    
027    import java.util.ResourceBundle;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @author Shuyang Zhou
032     */
033    public class IconDeleteTag extends IconTag {
034    
035            public void setConfirmation(String confirmation) {
036                    _confirmation = confirmation;
037            }
038    
039            public void setTrash(boolean trash) {
040                    _trash = trash;
041            }
042    
043            @Override
044            protected String getPage() {
045                    if (FileAvailabilityUtil.isAvailable(servletContext, _PAGE)) {
046                            return _PAGE;
047                    }
048    
049                    String cssClass = GetterUtil.getString(getCssClass());
050    
051                    setCssClass(cssClass.concat(" item-remove"));
052    
053                    if (Validator.isNull(getMessage())) {
054                            if (_trash) {
055                                    setMessage("move-to-the-recycle-bin");
056                            }
057                            else {
058                                    setMessage("delete");
059                            }
060                    }
061    
062                    String url = getUrl();
063    
064                    if (url.startsWith("javascript:if (confirm('")) {
065                            return super.getPage();
066                    }
067    
068                    if (url.startsWith("javascript:")) {
069                            url = url.substring(11);
070                    }
071    
072                    if (url.startsWith(Http.HTTP_WITH_SLASH) ||
073                            url.startsWith(Http.HTTPS_WITH_SLASH)) {
074    
075                            url = "submitForm(document.hrefFm, '".concat(
076                                    HttpUtil.encodeURL(url)).concat("');");
077                    }
078    
079                    if (url.startsWith("wsrp_rewrite?")) {
080                            url = StringUtil.replace(
081                                    url, "/wsrp_rewrite",
082                                    "&wsrp-extensions=encodeURL/wsrp_rewrite");
083                            url = "submitForm(document.hrefFm, '".concat(url).concat("');");
084                    }
085    
086                    if (!_trash) {
087                            StringBundler sb = new StringBundler(5);
088    
089                            sb.append("javascript:if (confirm('");
090    
091                            ResourceBundle resourceBundle =
092                                    TagResourceBundleUtil.getResourceBundle(pageContext);
093    
094                            if (Validator.isNotNull(_confirmation)) {
095                                    sb.append(
096                                            UnicodeLanguageUtil.get(resourceBundle, _confirmation));
097                            }
098                            else {
099                                    String confirmation = "are-you-sure-you-want-to-delete-this";
100    
101                                    sb.append(
102                                            UnicodeLanguageUtil.get(resourceBundle, 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    }