001/*
002 *  Copyright 2020 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.repository.comment;
017
018import java.util.Arrays;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.components.ContextHelper;
025import org.apache.cocoon.environment.Request;
026import org.apache.commons.lang.StringUtils;
027
028import org.ametys.cms.repository.Content;
029import org.ametys.web.cache.PageHelper;
030import org.ametys.web.repository.content.WebContent;
031import org.ametys.web.repository.page.Page;
032import org.ametys.web.repository.site.Site;
033
034/**
035 * Comments DAO for web content
036 *
037 */
038public class CommentsDAO extends org.ametys.cms.repository.comment.CommentsDAO
039{
040    /** The page helper */
041    protected PageHelper _pageHelper;
042
043    @Override
044    public void service(ServiceManager smanager) throws ServiceException
045    {
046        super.service(smanager);
047        _pageHelper = (PageHelper) smanager.lookup(PageHelper.ROLE);
048    }
049    
050    @Override
051    public boolean isValidatedByDefault(Content content)
052    {
053        if (content instanceof WebContent)
054        {
055            Site site = ((WebContent) content).getSite();
056            String validation = site.getValue("site-contents-comments-postvalidation");
057            
058            if (StringUtils.isNotEmpty(validation) && validation.equals("post"))
059            {
060                return true;
061            }
062            else if (StringUtils.isNotEmpty(validation) && validation.equals("pre"))
063            {
064                return false;
065            }
066            else // global
067            {
068                return super.isValidatedByDefault(content);
069            }
070        }
071        
072        return super.isValidatedByDefault(content);
073    }
074    
075    @Override
076    public boolean isCaptchaRequired(Content content, Map<String, Object> formValues)
077    {
078        Request request = ContextHelper.getRequest(_context);
079        String pageId = request.getParameter("pageId");
080        if (StringUtils.isEmpty(pageId))
081        {
082            pageId = (String) formValues.get("pageId");
083        }
084        
085        if (StringUtils.isNotEmpty(pageId))
086        {
087            Page page = _resolver.resolveById(pageId);
088            return _pageHelper.isCaptchaRequired(page);
089        }
090        
091        // Global
092        return super.isCaptchaRequired(content, formValues);
093    }
094    
095    @Override
096    protected List<String> getUserPopulationsContexts(Request request, Map<String, Object> contextualParameters)
097    {
098        String siteName = (String) contextualParameters.get("siteName");
099        return Arrays.asList("/sites/" + siteName, "/sites-fo/" + siteName);
100    }
101}