001/* 002 * Copyright 2018 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.plugins.ugc.actions; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022 023import javax.mail.MessagingException; 024 025import org.apache.avalon.framework.parameters.Parameters; 026import org.apache.avalon.framework.service.ServiceException; 027import org.apache.avalon.framework.service.ServiceManager; 028import org.apache.cocoon.acting.ServiceableAction; 029import org.apache.cocoon.environment.ObjectModelHelper; 030import org.apache.cocoon.environment.Redirector; 031import org.apache.cocoon.environment.Request; 032import org.apache.cocoon.environment.SourceResolver; 033import org.apache.commons.lang.StringUtils; 034 035import org.ametys.cms.contenttype.ContentType; 036import org.ametys.cms.contenttype.ContentTypeExtensionPoint; 037import org.ametys.cms.contenttype.ContentTypesHelper; 038import org.ametys.cms.repository.Content; 039import org.ametys.cms.repository.WorkflowAwareContent; 040import org.ametys.core.cocoon.ActionResultGenerator; 041import org.ametys.core.user.CurrentUserProvider; 042import org.ametys.core.user.User; 043import org.ametys.core.user.UserIdentity; 044import org.ametys.core.user.UserManager; 045import org.ametys.core.util.I18nUtils; 046import org.ametys.core.util.JSONUtils; 047import org.ametys.core.util.URLEncoder; 048import org.ametys.core.util.mail.SendMailHelper; 049import org.ametys.plugins.repository.AmetysObjectResolver; 050import org.ametys.plugins.repository.data.holder.ModelAwareDataHolder; 051import org.ametys.runtime.config.Config; 052import org.ametys.runtime.i18n.I18nizableText; 053import org.ametys.web.content.FOContentCreationHelper; 054import org.ametys.web.repository.content.WebContent; 055import org.ametys.web.repository.page.ZoneItem; 056import org.ametys.web.repository.site.Site; 057 058import com.google.common.collect.ArrayListMultimap; 059import com.google.common.collect.Multimap; 060 061/** 062 * Create content action 063 */ 064public class ProposeContentAction extends ServiceableAction 065{ 066 private static final int _INITIAL_ACTION_ID = 1111; 067 068 /** Ametys object resolver. */ 069 protected AmetysObjectResolver _resolver; 070 071 /** The content type extension point */ 072 protected ContentTypeExtensionPoint _cTypeEP; 073 074 /** The content type helper */ 075 protected ContentTypesHelper _contentTypeHelper; 076 077 /** The JSON Utils */ 078 protected JSONUtils _jsonUtils; 079 080 /** The current user provider */ 081 protected CurrentUserProvider _currentUserProvider; 082 083 /** The I18n utils */ 084 protected I18nUtils _i18nUtils; 085 086 /** The user manager */ 087 protected UserManager _userManager; 088 089 /** Helper for FO content creation */ 090 protected FOContentCreationHelper _foContentCreationHelper; 091 092 @Override 093 public void service(ServiceManager serviceManager) throws ServiceException 094 { 095 super.service(serviceManager); 096 _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE); 097 _cTypeEP = (ContentTypeExtensionPoint) serviceManager.lookup(ContentTypeExtensionPoint.ROLE); 098 _contentTypeHelper = (ContentTypesHelper) serviceManager.lookup(ContentTypesHelper.ROLE); 099 _jsonUtils = (JSONUtils) serviceManager.lookup(JSONUtils.ROLE); 100 _currentUserProvider = (CurrentUserProvider) serviceManager.lookup(CurrentUserProvider.ROLE); 101 _i18nUtils = (I18nUtils) serviceManager.lookup(I18nUtils.ROLE); 102 _userManager = (UserManager) serviceManager.lookup(UserManager.ROLE); 103 _foContentCreationHelper = (FOContentCreationHelper) serviceManager.lookup(FOContentCreationHelper.ROLE); 104 } 105 106 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 107 { 108 Map<String, Object> result = new HashMap<>(); 109 110 Multimap<String, I18nizableText> errors = ArrayListMultimap.create(); 111 112 Request request = ObjectModelHelper.getRequest(objectModel); 113 114 String zoneId = request.getParameter("zoneId"); 115 ZoneItem zoneItem = _resolver.resolveById(zoneId); 116 117 ModelAwareDataHolder serviceParameters = zoneItem.getServiceParameters(); 118 String workflowName = serviceParameters.getValue("workflow"); 119 String contentTypeId = serviceParameters.getValue("content-type"); 120 UserIdentity adminUser = serviceParameters.getValue("user"); 121 122 String siteName = request.getParameter("site"); 123 String language = request.getParameter("lang"); 124 125 ContentType contentType = _cTypeEP.getExtension(contentTypeId); 126 Map<String, Object> values = _foContentCreationHelper.getAndValidateFormValues(request, contentType, "main", errors); 127 128 129 // If there are no errors 130 if (errors.isEmpty()) 131 { 132 String title = request.getParameter("title"); 133 134 result = _foContentCreationHelper.createAndEditContent(_INITIAL_ACTION_ID, contentTypeId, siteName, title, title, language, values, workflowName, "main"); 135 136 if (result.containsKey(Content.class.getName())) 137 { 138 result.put("success", true); 139 140 // Send mail 141 WorkflowAwareContent content = (WorkflowAwareContent) result.get(Content.class.getName()); 142 _sendMail(adminUser, content); 143 } 144 else 145 { 146 result.put("success", false); 147 } 148 } 149 else 150 { 151 result.put("success", false); 152 result.put("errors", _jsonUtils.convertObjectToJson(errors.asMap())); 153 } 154 155 request.setAttribute(ActionResultGenerator.MAP_REQUEST_ATTR, result); 156 return EMPTY_MAP; 157 } 158 159 /** 160 * Send mail to inform that a content is created 161 * @param adminUser the admin user 162 * @param content the created content 163 */ 164 protected void _sendMail(UserIdentity adminUser, WorkflowAwareContent content) 165 { 166 if (adminUser != null) 167 { 168 UserIdentity creatorIdentity = _currentUserProvider.getUser(); 169 User creator = _userManager.getUser(creatorIdentity); 170 171 Site site = _getSite(content); 172 173 I18nizableText i18nSubject = _getI18nSubject(site); 174 I18nizableText i18nBody = _getI18nBody(content, creator, site); 175 176 String subject = _i18nUtils.translate(i18nSubject); 177 String body = _i18nUtils.translate(i18nBody); 178 179 User user = _userManager.getUser(adminUser); 180 String email = user.getEmail(); 181 try 182 { 183 SendMailHelper.sendMail(subject, null, body, email, Config.getInstance().getValue("smtp.mail.from")); 184 } 185 catch (MessagingException e) 186 { 187 getLogger().warn("Could not send an e-mail for the content creation to " + email, e); 188 } 189 } 190 } 191 192 /** 193 * Get the i18n body 194 * @param content the content 195 * @param creator the creator 196 * @param site the site 197 * @return The i18n body 198 */ 199 protected I18nizableText _getI18nBody(WorkflowAwareContent content, User creator, Site site) 200 { 201 List<String> parametersBody = new ArrayList<>(); 202 parametersBody.add(content.getTitle()); 203 parametersBody.add(creator.getFullName()); 204 I18nizableText i18nBody = null; 205 if (site != null) 206 { 207 parametersBody.add(_getContentUri(content, site)); 208 parametersBody.add(site.getTitle()); 209 parametersBody.add(site.getUrl()); 210 i18nBody = new I18nizableText("plugin.ugc", "PLUGINS_UGC_SERVICE_FORM_CREATED_CONTENT_MAIL_BODY", parametersBody); 211 } 212 else 213 { 214 i18nBody = new I18nizableText("plugin.ugc", "PLUGINS_UGC_SERVICE_FORM_CREATED_CONTENT_MAIL_BODY_NO_SITE", parametersBody); 215 } 216 return i18nBody; 217 } 218 219 /** 220 * Get the i18n subject 221 * @param site the site 222 * @return The i18n subject 223 */ 224 protected I18nizableText _getI18nSubject(Site site) 225 { 226 I18nizableText i18nSubject = null; 227 if (site != null) 228 { 229 List<String> parametersSubject = new ArrayList<>(); 230 parametersSubject.add(site.getTitle()); 231 i18nSubject = new I18nizableText("plugin.ugc", "PLUGINS_UGC_SERVICE_FORM_CREATED_CONTENT_MAIL_SUBJECT", parametersSubject); 232 } 233 else 234 { 235 i18nSubject = new I18nizableText("plugin.ugc", "PLUGINS_UGC_SERVICE_FORM_CREATED_CONTENT_MAIL_SUBJECT_NO_SITE"); 236 } 237 return i18nSubject; 238 } 239 240 /** 241 * Get the site name 242 * @param content The content 243 * @return the site name 244 */ 245 protected Site _getSite(WorkflowAwareContent content) 246 { 247 if (content instanceof WebContent) 248 { 249 return ((WebContent) content).getSite(); 250 } 251 252 return null; 253 } 254 255 256 /** 257 * Get the content uri 258 * @param content the content 259 * @param site The site 260 * @return the content uri 261 */ 262 protected String _getContentUri(WorkflowAwareContent content, Site site) 263 { 264 return _getRequestUri() + "/" + site.getName() + "/index.html?uitool=uitool-content,id:%27" + URLEncoder.encodeParameter(content.getId()) + "%27"; 265 } 266 267 /** 268 * Get the request URI. 269 * @return the full request URI. 270 */ 271 protected String _getRequestUri() 272 { 273 return StringUtils.stripEnd(StringUtils.removeEndIgnoreCase(Config.getInstance().getValue("cms.url"), "index.html"), "/"); 274 } 275}