<commit_before><?php
/*
 * This file is part of the CampaignChain package.
 *
 * (c) CampaignChain, Inc. <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CampaignChain\Hook\DueBundle\EntityService;

use CampaignChain\CoreBundle\Entity\Hook;
use CampaignChain\CoreBundle\EntityService\HookServiceTriggerInterface;
use CampaignChain\Hook\DueBundle\Entity\Due;
use Doctrine\ORM\EntityManager;
use Doctrine\Common\Inflector\Inflector;
use Symfony\Component\DependencyInjection\ContainerInterface;

class DueService implements HookServiceTriggerInterface
{
    protected $em;
    protected $container;

    public function __construct(EntityManager $em, ContainerInterface $container)
    {
        $this->container = $container;
        $this->em = $em;
    }

//    public function newObject($entityClass, $entityId, $formData){
//        $due = new Due();
//        $due->setEntityId($entityId);
//        $due->setEntityClass($entityClass);
//        $due->setDate($formData['date']);
//
//        return $due;
//    }

    public function getHook($entity, $mode = Hook::MODE_DEFAULT){
        $hook = new Due();

        if(
            is_object($entity) &&
            $entity->getId() !== null
        ){
            if(
                $mode == Hook::MODE_DEFAULT &&
                // Operations have no direct relation to a campaign, so exclude them.
                strpos(get_class($entity), 'CoreBundle\Entity\Operation') === false &&
                // Don't process Campaigns here.
                strpos(get_class($entity), 'CoreBundle\Entity\Campaign') === false &&
                // Check if the Action's campaign has relative dates.
                $entity->getCampaign()->getHasRelativeDates()
            ){
                $interval = $entity->getCampaign()->getStartDate()->diff(
                    $entity->getStartDate()
                );
                $time = $entity->getStartDate()->format('H').':'.$entity->getStartDate()->format('i');
                $days = $interval->format("%a");
                if($time != '00:00'){
                    $days = ++$days;
                }
                $hook->setDays($days);
                $hook->setTime($time);
            }

            $hook->setStartDate($entity->getStartDate());
        }

        return $hook;
    }

    public function processHook($entity, $hook){
        // TODO: Remove this hack which fixes a validation issue.
        if(!$hook->getStartDate()){
            $now = new \DateTime('now', new \DateTimeZone($hook->getTimezone()));
            $hook->setStartDate($now);
        }

        if($hook->getDays()){
            $campaignStartDate = $entity->getCampaign()->getStartDate();
            $days = $hook->getDays();
            if($hook->getTime() != '00:00'){
                $days = $days-1;
            }
            $hookStartDate = $campaignStartDate->modify('+'.$days.' days');
            $hookStartDate = new \DateTime(
                $hookStartDate->format('Y-M-d').' '.$hook->getTime().':00'
            );

            $hook->setStartDate($hookStartDate);
        }

        // Update the dates of the entity.
        $entity->setStartDate($hook->getStartDate());
        $entity->setEndDate($hook->getEndDate());

        // If the entity is an Activity and it equals the Operation, then
        // - the same dates will be set for the Operation
        // - the same trigger Hook will be set for the Operation
        $class = get_class($entity);
        if(strpos($class, 'CoreBundle\Entity\Activity') !== false && $entity->getEqualsOperation() == true){<commit_msg>CampaignChain/campaignchain-ce#166 Mismatch between relative due time and time displayed in timeline for Activity<commit_after><?php
/*
 * This file is part of the CampaignChain package.
 *
 * (c) CampaignChain, Inc. <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CampaignChain\Hook\DueBundle\EntityService;

use CampaignChain\CoreBundle\Entity\Hook;
use CampaignChain\CoreBundle\EntityService\HookServiceTriggerInterface;
use CampaignChain\Hook\DueBundle\Entity\Due;
use Doctrine\ORM\EntityManager;
use Doctrine\Common\Inflector\Inflector;
use Symfony\Component\DependencyInjection\ContainerInterface;

class DueService implements HookServiceTriggerInterface
{
    protected $em;
    protected $container;

    public function __construct(EntityManager $em, ContainerInterface $container)
    {
        $this->container = $container;
        $this->em = $em;
    }

//    public function newObject($entityClass, $entityId, $formData){
//        $due = new Due();
//        $due->setEntityId($entityId);
//        $due->setEntityClass($entityClass);
//        $due->setDate($formData['date']);
//
//        return $due;
//    }

    public function getHook($entity, $mode = Hook::MODE_DEFAULT){
        $hook = new Due();

        if(
            is_object($entity) &&
            $entity->getId() !== null
        ){
            if(
                $mode == Hook::MODE_DEFAULT &&
                // Operations have no direct relation to a campaign, so exclude them.
                strpos(get_class($entity), 'CoreBundle\Entity\Operation') === false &&
                // Don't process Campaigns here.
                strpos(get_class($entity), 'CoreBundle\Entity\Campaign') === false &&
                // Check if the Action's campaign has relative dates.
                $entity->getCampaign()->getHasRelativeDates()
            ){
                $interval = $entity->getCampaign()->getStartDate()->diff(
                    $entity->getStartDate()
                );
                $time = $entity->getStartDate()->format('H').':'.$entity->getStartDate()->format('i');
                $days = $interval->format("%a");
                if($time != '00:00'){
                    $days = ++$days;
                }
                $hook->setDays($days);
                $hook->setTime($time);
            }

            $hook->setStartDate($entity->getStartDate());
        }

        return $hook;
    }

    public function processHook($entity, $hook){
        // TODO: Remove this hack which fixes a validation issue.
        if(!$hook->getStartDate()){
            $now = new \DateTime('now', new \DateTimeZone($hook->getTimezone()));
            $hook->setStartDate($now);
        }

        if($hook->getDays()){
            $campaignStartDate = $entity->getCampaign()->getStartDate();
            $days = $hook->getDays();
            if($hook->getTime() != '00:00'){
                $days = $days-1;
            }
            $hookStartDate = $campaignStartDate->modify('+'.$days.' days');
            $hookStartDate = new \DateTime(
                $hookStartDate->format('Y-M-d').' '.$hook->getTime().':00',
                new \DateTimeZone($hook->getTimezone())
            );

            $hook->setStartDate($hookStartDate);
        }

        // Update the dates of the entity.
        $entity->setStartDate($hook->getStartDate());
        $entity->setEndDate($hook->getEndDate());

        // If the entity is an Activity and it equals the Operation, then
        // - the same dates will be set for the Operation
        // - the same trigger Hook will be set for the Operation
        $class = get_class($entity);
        if(strpos($class, 'CoreBundle\Entity\Activity') !== false && $entity->getEqualsOperation() == true){
{'fraction_non_alphanumeric': 0.14473161033797216, 'fraction_numerical': 0.0033134526176275677, 'mean_word_length': 2.1745898190997055, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 84, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5081191', 'n_tokens_mistral': 2064, 'n_tokens_neox': 1980, 'n_words': 457}