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