File size: 5,535 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php

namespace Mautic\CoreBundle\Model;

use Mautic\CoreBundle\Entity\TranslationEntityInterface;
use Mautic\CoreBundle\Entity\VariantEntityInterface;

trait VariantModelTrait
{
    /**
     * @var bool
     */
    protected $inConversion = false;

    /**
     * Converts a variant to the main item and the original main item a variant.
     */
    public function convertVariant(VariantEntityInterface $entity): void
    {
        // let saveEntities() know it does not need to set variant start dates
        $this->inConversion = true;

        [$parent, $children] = $entity->getVariants();

        $save = [];

        // set this email as the parent for the original parent and children
        if ($parent) {
            if ($parent->getId() != $entity->getId()) {
                if (method_exists($parent, 'setIsPublished')) {
                    $parent->setIsPublished(false);
                }

                $entity->addVariantChild($parent);
                $parent->setVariantParent($entity);
            }

            $parent->setVariantStartDate(null);
            $parent->setVariantSentCount(0);

            foreach ($children as $child) {
                // capture child before it's removed from collection
                $save[] = $child;

                $parent->removeVariantChild($child);
            }
        }

        if (count($save)) {
            foreach ($save as $child) {
                if ($child->getId() != $entity->getId()) {
                    if (method_exists($child, 'setIsPublished')) {
                        $child->setIsPublished(false);
                    }

                    $entity->addVariantChild($child);
                    $child->setVariantParent($entity);
                } else {
                    $child->removeVariantParent();
                }

                $child->setVariantSentCount(0);
                $child->setVariantStartDate(null);
            }
        }

        $save[] = $parent;
        $save[] = $entity;

        // save the entities
        $this->saveEntities($save, false);
    }

    /**
     * Prepare a variant for saving.
     *
     * @param array $resetVariantCounterMethods ['setVariantHits', 'setVariantSends', ...]
     */
    protected function preVariantSaveEntity(VariantEntityInterface $entity, array $resetVariantCounterMethods = [], \DateTime $variantStartDate = null): bool
    {
        $isVariant = $entity->isVariant();

        if (!$isVariant && $entity instanceof TranslationEntityInterface) {
            // Translations could be assigned to a variant and thus applicable to be reset
            if ($translationParent = $entity->getTranslationParent()) {
                $isVariant = $translationParent->isVariant(); /** @phpstan-ignore-line @todo for M6, extend the TranslationEntityInterface from The VariantEntityInterface */
            }
        }

        if ($isVariant) {
            // Reset the variant hit and start date if there are any changes and if this is an A/B test
            // Do it here in addition to postVariantSave() so that it's available to the event listeners
            $changes = $entity->getChanges();

            // If unpublished and wasn't changed from published - don't reset
            if (!$entity->isPublished(false) && (!isset($changes['isPublished']))) {
                return false;
            }

            // Reset the variant
            if (!empty($changes) && empty($this->inConversion)) {
                if (method_exists($entity, 'setVariantStartDate')) {
                    $entity->setVariantStartDate($variantStartDate);
                }

                // Reset counters
                foreach ($resetVariantCounterMethods as $method) {
                    if (method_exists($entity, $method)) {
                        $entity->$method(0);
                    }
                }

                return true;
            }
        }

        return false;
    }

    /**
     * Run post saving a variant aware entity.
     *
     * @param bool  $resetVariants
     * @param array $relatedIds
     */
    protected function postVariantSaveEntity(VariantEntityInterface $entity, $resetVariants = false, $relatedIds = [], \DateTime $variantStartDate = null)
    {
        // If parent, add this entity as a child of the parent so that it populates the list in the tab (due to Doctrine hanging on to entities in memory)
        if ($parent = $entity->getVariantParent()) {
            $parent->addVariantChild($entity);
        }

        // Reset associated variants if applicable due to changes
        if ($resetVariants && empty($this->inConversion)) {
            $this->resetVariants($entity, $relatedIds, $variantStartDate);
        }
    }

    protected function resetVariants($entity, $relatedIds = null, \DateTime $variantStartDate = null)
    {
        $repo = $this->getRepository();

        if (method_exists($repo, 'resetVariants')) {
            if (null == $relatedIds) {
                $relatedIds = $entity->getRelatedEntityIds();
            }

            if (!in_array($entity->getId(), $relatedIds)) {
                $relatedIds[] = $entity->getId();
            }

            if (null === $variantStartDate) {
                $variantStartDate = new \DateTime();
            }

            // Ensure UTC since we're saving directly to the DB
            $variantStartDate->setTimezone(new \DateTimeZone('UTC'));
            $repo->resetVariants($relatedIds, $variantStartDate->format('Y-m-d H:i:s'));
        }
    }
}