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