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.user.User; 030import org.ametys.core.util.HttpUtils; 031import org.ametys.core.util.URIUtils; 032import org.ametys.runtime.config.Config; 033import org.ametys.runtime.i18n.I18nizableText; 034import org.ametys.web.WebHelper; 035import org.ametys.web.repository.content.WebContent; 036import org.ametys.web.repository.page.Page; 037import org.ametys.web.repository.site.Site; 038import org.ametys.web.repository.site.SiteManager; 039 040/** 041 * OS workflow function to send mail after an action is triggered. 042 */ 043public class SendMailFunction extends org.ametys.cms.workflow.SendMailFunction 044{ 045 private SiteManager _siteManager; 046 047 @Override 048 public void service(ServiceManager smanager) throws ServiceException 049 { 050 super.service(smanager); 051 _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE); 052 } 053 @Override 054 protected List<String> getSubjectI18nParams (User user, WorkflowAwareContent content) 055 { 056 List<String> params = super.getSubjectI18nParams(user, content); // {0} 057 Site site = _getSite(content); 058 if (site != null) 059 { 060 params.add(site.getTitle()); // {1} 061 } 062 else 063 { 064 // fallback to the current site if no site is provided 065 String currentSiteName = WebHelper.getSiteName(ContextHelper.getRequest(_context)); 066 params.add(_siteManager.getSite(currentSiteName).getTitle()); // {1} 067 } 068 return params; 069 } 070 071 @Override 072 protected String getMailBody(String subjectI18nKey, String bodyI18nKey, User user, WorkflowAwareContent content, Map transientVars) throws IOException 073 { 074 String i18nKey = bodyI18nKey; 075 076 Site site = _getSite(content); 077 if (site == null) 078 { 079 i18nKey += "_NOSITE"; 080 } 081 else if (_getPage(content, site) == null) 082 { 083 // Orphan content 084 i18nKey += "_ORPHAN"; 085 } 086 087 return super.getMailBody(subjectI18nKey, i18nKey, user, content, transientVars); 088 } 089 090 @Override 091 protected List<String> getBodyI18nParams(User user, WorkflowAwareContent content) 092 { 093 List<String> params = new ArrayList<>(); 094 095 params.add(user.getFullName()); // {0} 096 params.add(_contentHelper.getTitle(content)); // {1} 097 098 Site site = _getSite(content); 099 if (site != null) 100 { 101 params.add(site.getTitle()); // {2} 102 } 103 104 Page page = _getPage(content, site); 105 if (page != null) 106 { 107 params.add(page.getTitle()); // {3} 108 } 109 110 params.add(_getContentUri(content, page, site)); // {2}, {3} or {4} depending on page and site value 111 112 return params; 113 } 114 115 /** 116 * Get the site name 117 * @param content The content 118 * @return the site name 119 */ 120 protected Site _getSite(WorkflowAwareContent content) 121 { 122 if (content instanceof WebContent) 123 { 124 return ((WebContent) content).getSite(); 125 } 126 127 return null; 128 } 129 130 /** 131 * Get the page referenced by this content 132 * @param content The content 133 * @param site the site 134 * @return the page or null. 135 */ 136 protected Page _getPage(WorkflowAwareContent content, Site site) 137 { 138 if (content instanceof WebContent) 139 { 140 Iterator<Page> pages = ((WebContent) content).getReferencingPages().iterator(); 141 if (pages.hasNext()) 142 { 143 return pages.next(); 144 } 145 } 146 return null; 147 } 148 149 /** 150 * Get the content uri 151 * @param content the content 152 * @param page the referencing page. Can be null. 153 * @param site The site 154 * @return the content uri 155 */ 156 protected String _getContentUri(WorkflowAwareContent content, Page page, Site site) 157 { 158 String siteName; 159 if (site != null) 160 { 161 siteName = site.getName(); 162 } 163 else 164 { 165 // fallback to the current site if no site is provided 166 siteName = WebHelper.getSiteName(ContextHelper.getRequest(_context)); 167 } 168 169 if (page != null) 170 { 171 return _getRequestUri() + "/" + siteName + "/index.html?uitool=uitool-page,id:%27" + URIUtils.encodeParameter(page.getId()) + "%27"; 172 } 173 else 174 { 175 return _contentHelper.getContentBOUrl(content, Map.of("siteName", siteName)); 176 } 177 } 178 179 /** 180 * Get the request URI. 181 * @return the full request URI. 182 */ 183 protected String _getRequestUri() 184 { 185 return HttpUtils.sanitize(Config.getInstance().getValue("cms.url")); 186 } 187 188 @Override 189 public I18nizableText getLabel() 190 { 191 return new I18nizableText("plugin.web", "PLUGINS_WEB_SEND_MAIL_FUNCTION_LABEL"); 192 } 193 194}