001    /**
002     * Copyright (c) 2000-2013 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            @Override
036            protected String getPage() {
037                    if (FileAvailabilityUtil.isAvailable(getServletContext(), _PAGE)) {
038                            return _PAGE;
039                    }
040    
041                    setImage("delete");
042    
043                    String url = getUrl();
044    
045                    if (url.startsWith("javascript:if (confirm('")) {
046                            return super.getPage();
047                    }
048    
049                    if (url.startsWith("javascript:")) {
050                            url = url.substring(11);
051                    }
052    
053                    if (url.startsWith(Http.HTTP_WITH_SLASH) ||
054                            url.startsWith(Http.HTTPS_WITH_SLASH)) {
055    
056                            url =
057                                    "submitForm(document.hrefFm, '".concat(
058                                            HttpUtil.encodeURL(url)).concat("');");
059                    }
060    
061                    if (url.startsWith("wsrp_rewrite?")) {
062                            url = StringUtil.replace(
063                                    url, "/wsrp_rewrite",
064                                    "&wsrp-extensions=encodeURL/wsrp_rewrite");
065                            url = "submitForm(document.hrefFm, '".concat(url).concat("');");
066                    }
067    
068                    StringBundler sb = new StringBundler(5);
069    
070                    sb.append("javascript:if (confirm('");
071    
072                    if (Validator.isNotNull(_confirmation)) {
073                            sb.append(UnicodeLanguageUtil.get(pageContext, _confirmation));
074                    }
075                    else {
076                            sb.append(
077                                    UnicodeLanguageUtil.get(
078                                            pageContext, "are-you-sure-you-want-to-delete-this"));
079                    }
080    
081                    sb.append("')) { ");
082                    sb.append(url);
083                    sb.append(" } else { self.focus(); }");
084    
085                    url = sb.toString();
086    
087                    setUrl(url);
088    
089                    return super.getPage();
090            }
091    
092            private static final String _PAGE = "/html/taglib/ui/icon_delete/page.jsp";
093    
094            private String _confirmation;
095    
096    }