File size: 4,726 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
<?php

namespace Mautic\CampaignBundle\Executioner\Helper;

use Mautic\CampaignBundle\Entity\Event;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\CoreBundle\Model\NotificationModel;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\UserBundle\Entity\User;
use Mautic\UserBundle\Model\UserModel;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Router;
use Symfony\Contracts\Translation\TranslatorInterface;

class NotificationHelper
{
    public function __construct(
        private UserModel $userModel,
        private NotificationModel $notificationModel,
        private TranslatorInterface $translator,
        private Router $router,
        private CoreParametersHelper $coreParametersHelper
    ) {
    }

    public function notifyOfFailure(Lead $contact, Event $event): void
    {
        $user = $this->getUser($contact, $event);
        if (!$user || !$user->getId()) {
            return;
        }

        $this->notificationModel->addNotification(
            $event->getCampaign()->getName().' / '.$event->getName(),
            'error',
            false,
            $this->translator->trans(
                'mautic.campaign.event.failed',
                [
                    '%contact%' => '<a href="'.$this->router->generate(
                        'mautic_contact_action',
                        ['objectAction' => 'view', 'objectId' => $contact->getId()]
                    ).'" data-toggle="ajax">'.$contact->getPrimaryIdentifier().'</a>',
                ]
            ),
            null,
            null,
            $user
        );
    }

    public function notifyOfUnpublish(Event $event): void
    {
        /**
         * Pass a fake lead so we can just get the campaign creator.
         */
        $user = $this->getUser(new Lead(), $event);

        if (!$user || !$user->getId()) {
            return;
        }

        $campaign = $event->getCampaign();

        $this->notificationModel->addNotification(
            $campaign->getName().' / '.$event->getName(),
            'error',
            false,
            $this->translator->trans(
                'mautic.campaign.event.failed.campaign.unpublished',
                [
                    '%campaign%' => '<a href="'.$this->router->generate(
                        'mautic_campaign_action',
                        [
                            'objectAction' => 'view',
                            'objectId'     => $campaign->getId(),
                        ],
                        UrlGeneratorInterface::ABSOLUTE_URL
                    ).'" data-toggle="ajax">'.$campaign->getName().'</a>',
                    '%event%' => $event->getName(),
                ]
            ),
            null,
            null,
            $user
        );

        $subject = $this->translator->trans(
            'mautic.campaign.event.campaign_unpublished.title',
            [
                '%title%' => $campaign->getName(),
            ]
        );

        $content = $this->translator->trans(
            'mautic.campaign.event.failed.campaign.unpublished',
            [
                '%campaign%' => '<a href="'.$this->router->generate(
                    'mautic_campaign_action',
                    [
                        'objectAction' => 'view',
                        'objectId'     => $campaign->getId(),
                    ],
                    UrlGeneratorInterface::ABSOLUTE_URL
                ).'" data-toggle="ajax">'.$campaign->getName().'</a>',
                '%event%' => $event->getName(),
            ]
        );

        $sendToAuthor = $this->coreParametersHelper->get('campaign_send_notification_to_author', 1);
        if ($sendToAuthor) {
            $this->userModel->emailUser($user, $subject, $content);
        } else {
            $emailAddresses =  array_map('trim', explode(',', $this->coreParametersHelper->get('campaign_notification_email_addresses')));
            $this->userModel->sendMailToEmailAddresses($emailAddresses, $subject, $content);
        }
    }

    /**
     * @return User|null
     */
    private function getUser(Lead $contact, Event $event)
    {
        // Default is to notify the contact owner
        if ($owner = $contact->getOwner()) {
            return $owner;
        }

        // If the contact doesn't have an owner, notify the one that created the campaign
        if ($campaignCreator = $event->getCampaign()->getCreatedBy()) {
            if ($owner = $this->userModel->getEntity($campaignCreator)) {
                return $owner;
            }
        }

        // If all else fails, notifiy a system admins
        return $this->userModel->getSystemAdministrator();
    }
}