001/* 002 * Copyright 2022 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.content; 017 018import java.util.Map; 019 020import org.apache.avalon.framework.service.ServiceException; 021import org.apache.avalon.framework.service.ServiceManager; 022import org.apache.cocoon.components.ContextHelper; 023import org.apache.cocoon.environment.Request; 024import org.apache.commons.lang3.StringUtils; 025 026import org.ametys.cms.repository.Content; 027import org.ametys.core.util.URIUtils; 028import org.ametys.web.WebHelper; 029import org.ametys.web.contenttype.SkinContentViewHelper; 030import org.ametys.web.contenttype.SkinContentViewHelper.SkinContentView; 031 032/** 033 * Helper for {@link Content} 034 * 035 */ 036public class ContentHelper extends org.ametys.cms.content.ContentHelper 037{ 038 private SkinContentViewHelper _skinContentViewHelper; 039 040 @Override 041 public void service(ServiceManager smanager) throws ServiceException 042 { 043 super.service(smanager); 044 _skinContentViewHelper = (SkinContentViewHelper) smanager.lookup(SkinContentViewHelper.ROLE); 045 } 046 047 @Override 048 public Map<String, String> getContentViewUrlParameters(Content content, String viewName, String format, Map<String, String> additionalParams) 049 { 050 Map<String, String> params = super.getContentViewUrlParameters(content, viewName, format, additionalParams); 051 052 SkinContentView contentViewFromSkin = _skinContentViewHelper.getContentViewFromSkin(viewName, content); 053 if (contentViewFromSkin != null) 054 { 055 params.put("viewName", contentViewFromSkin.modelViewName()); 056 params.put(SkinContentViewHelper.REQUEST_PARAM_RENDERING_VIEW_NAME, contentViewFromSkin.name()); 057 } 058 059 return params; 060 } 061 062 @Override 063 public String getContentBOUrl(Content content, Map<String, Object> contextualParameters) 064 { 065 String siteName = _getSiteName(content, contextualParameters); 066 if (StringUtils.isNotBlank(siteName)) 067 { 068 return _baseURL + "/" + siteName + "/index.html?uitool=uitool-content,id:%27" + URIUtils.encodeParameter(content.getId()) + "%27"; 069 } 070 071 return super.getContentBOUrl(content, contextualParameters); 072 } 073 074 private String _getSiteName(Content content, Map<String, Object> contextualParameters) 075 { 076 String siteName = (String) contextualParameters.get("siteName"); 077 // First test if there is a given site in the contextual parameters 078 if (StringUtils.isNotBlank(siteName)) 079 { 080 return siteName; 081 } 082 083 // Then check if the content is a site aware ametys object 084 // And finally in the request 085 Request request = ContextHelper.getRequest(_context); 086 return WebHelper.getSiteName(request, content); 087 } 088}