001/* 002 * Copyright 2010 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.web.workflow; 017 018import java.io.IOException; 019import java.util.ArrayList; 020import java.util.Iterator; 021import java.util.List; 022import java.util.Map; 023 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.cocoon.components.ContextHelper; 027 028import org.ametys.cms.repository.WorkflowAwareContent; 029import org.ametys.core.ui.mail.StandardMailBodyHelper.MailBodyBuilder; 030import org.ametys.core.user.User; 031import org.ametys.core.util.HttpUtils; 032import org.ametys.core.util.URIUtils; 033import org.ametys.runtime.config.Config; 034import org.ametys.runtime.i18n.I18nizableText; 035import org.ametys.web.WebHelper; 036import org.ametys.web.repository.content.WebContent; 037import org.ametys.web.repository.page.Page; 038import org.ametys.web.repository.site.Site; 039import org.ametys.web.repository.site.SiteManager; 040 041/** 042 * OS workflow function to send mail after an action is triggered. 043 */ 044public class SendMailFunction extends org.ametys.cms.workflow.SendMailFunction 045{ 046 private SiteManager _siteManager; 047 048 @Override 049 public void service(ServiceManager smanager) throws ServiceException 050 { 051 super.service(smanager); 052 _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE); 053 } 054 @Override 055 protected List<String> getSubjectI18nParams (User user, WorkflowAwareContent content, boolean escapeHTML) 056 { 057 List<String> params = super.getSubjectI18nParams(user, content, escapeHTML); // {0} 058 Site site = _getSite(content); 059 if (site != null) 060 { 061 params.add(escapeHTML ? org.ametys.core.util.StringUtils.escapeHTML(site.getTitle()) : site.getTitle()); // {1} 062 } 063 else 064 { 065 // fallback to the current site if no site is provided 066 String currentSiteName = WebHelper.getSiteName(ContextHelper.getRequest(_context)); 067 Site currentSite = _siteManager.getSite(currentSiteName); 068 params.add(escapeHTML ? org.ametys.core.util.StringUtils.escapeHTML(currentSite.getTitle()) : currentSite.getTitle()); // {1} 069 } 070 return params; 071 } 072 073 @Override 074 protected MailBodyBuilder getMailBody(String subjectI18nKey, String bodyI18nKey, User user, WorkflowAwareContent content, Map transientVars) throws IOException 075 { 076 String i18nKey = bodyI18nKey; 077 078 Site site = _getSite(content); 079 if (site == null) 080 { 081 i18nKey += "_NOSITE"; 082 } 083 else if (_getPage(content, site) == null) 084 { 085 // Orphan content 086 i18nKey += "_ORPHAN"; 087 } 088 089 return super.getMailBody(subjectI18nKey, i18nKey, user, content, transientVars); 090 } 091 092 @Override 093 protected List<String> getBodyI18nParams(User user, WorkflowAwareContent content) 094 { 095 List<String> params = new ArrayList<>(); 096 097 params.add(org.ametys.core.util.StringUtils.escapeHTML(user.getFullName())); // {0} 098 params.add(org.ametys.core.util.StringUtils.escapeHTML(_contentHelper.getTitle(content))); // {1} 099 100 Site site = _getSite(content); 101 if (site != null) 102 { 103 params.add(org.ametys.core.util.StringUtils.escapeHTML(site.getTitle())); // {2} 104 } 105 106 Page page = _getPage(content, site); 107 if (page != null) 108 { 109 params.add(org.ametys.core.util.StringUtils.escapeHTML(page.getTitle())); // {3} 110 } 111 112 params.add(_getContentUri(content, page, site)); // {2}, {3} or {4} depending on page and site value 113 114 return params; 115 } 116 117 /** 118 * Get the site name 119 * @param content The content 120 * @return the site name 121 */ 122 protected Site _getSite(WorkflowAwareContent content) 123 { 124 if (content instanceof WebContent) 125 { 126 return ((WebContent) content).getSite(); 127 } 128 129 return null; 130 } 131 132 /** 133 * Get the page referenced by this content 134 * @param content The content 135 * @param site the site 136 * @return the page or null. 137 */ 138 protected Page _getPage(WorkflowAwareContent content, Site site) 139 { 140 if (content instanceof WebContent) 141 { 142 Iterator<Page> pages = ((WebContent) content).getReferencingPages().iterator(); 143 if (pages.hasNext()) 144 { 145 return pages.next(); 146 } 147 } 148 return null; 149 } 150 151 /** 152 * Get the content uri 153 * @param content the content 154 * @param page the referencing page. Can be null. 155 * @param site The site 156 * @return the content uri 157 */ 158 protected String _getContentUri(WorkflowAwareContent content, Page page, Site site) 159 { 160 String siteName; 161 if (site != null) 162 { 163 siteName = site.getName(); 164 } 165 else 166 { 167 // fallback to the current site if no site is provided 168 siteName = WebHelper.getSiteName(ContextHelper.getRequest(_context)); 169 } 170 171 if (page != null) 172 { 173 return _getRequestUri() + "/" + siteName + "/index.html?uitool=uitool-page,id:%27" + URIUtils.encodeParameter(page.getId()) + "%27"; 174 } 175 else 176 { 177 return _contentHelper.getContentBOUrl(content, Map.of("siteName", siteName)); 178 } 179 } 180 181 /** 182 * Get the request URI. 183 * @return the full request URI. 184 */ 185 protected String _getRequestUri() 186 { 187 return HttpUtils.sanitize(Config.getInstance().getValue("cms.url")); 188 } 189 190 @Override 191 public I18nizableText getLabel() 192 { 193 return new I18nizableText("plugin.web", "PLUGINS_WEB_SEND_MAIL_FUNCTION_LABEL"); 194 } 195 196}