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(getMessage())) {
051                            if (_trash) {
052                                    setMessage("move-to-the-recycle-bin");
053                            }
054                            else {
055                                    setMessage("delete");
056                            }
057                    }
058    
059                    String url = getUrl();
060    
061                    if (url.startsWith("javascript:if (confirm('")) {
062                            return super.getPage();
063                    }
064    
065                    if (url.startsWith("javascript:")) {
066                            url = url.substring(11);
067                    }
068    
069                    if (url.startsWith(Http.HTTP_WITH_SLASH) ||
070                            url.startsWith(Http.HTTPS_WITH_SLASH)) {
071    
072                            url = "submitForm(document.hrefFm, '".concat(
073                                    HttpUtil.encodeURL(url)).concat("');");
074                    }
075    
076                    if (url.startsWith("wsrp_rewrite?")) {
077                            url = StringUtil.replace(
078                                    url, "/wsrp_rewrite",
079                                    "&wsrp-extensions=encodeURL/wsrp_rewrite");
080                            url = "submitForm(document.hrefFm, '".concat(url).concat("');");
081                    }
082    
083                    if (!_trash) {
084                            StringBundler sb = new StringBundler(5);
085    
086                            sb.append("javascript:if (confirm('");
087    
088                            if (Validator.isNotNull(_confirmation)) {
089                                    sb.append(UnicodeLanguageUtil.get(request, _confirmation));
090                            }
091                            else {
092                                    String confirmation = "are-you-sure-you-want-to-delete-this";
093    
094                                    sb.append(UnicodeLanguageUtil.get(request, confirmation));
095                            }
096    
097                            sb.append("')) { ");
098                            sb.append(url);
099                            sb.append(" } else { self.focus(); }");
100    
101                            url = sb.toString();
102                    }
103                    else {
104                            url = "javascript:".concat(url);
105                    }
106    
107                    setUrl(url);
108    
109                    return super.getPage();
110            }
111    
112            private static final String _PAGE = "/html/taglib/ui/icon_delete/page.jsp";
113    
114            private String _confirmation;
115            private boolean _trash;
116    
117    }