File size: 8,749 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php

declare(strict_types=1);

namespace Mautic\LeadBundle\Model;

use Doctrine\ORM\EntityManager;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\CoreBundle\Helper\ExportHelper;
use Mautic\CoreBundle\Helper\UserHelper;
use Mautic\CoreBundle\Model\AbstractCommonModel;
use Mautic\CoreBundle\Model\IteratorExportDataModel;
use Mautic\CoreBundle\Security\Permissions\CorePermissions;
use Mautic\CoreBundle\Translation\Translator;
use Mautic\EmailBundle\Helper\MailHelper;
use Mautic\LeadBundle\Entity\ContactExportScheduler;
use Mautic\LeadBundle\Entity\ContactExportSchedulerRepository;
use Mautic\UserBundle\Entity\User;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
 * @extends AbstractCommonModel<ContactExportScheduler>
 */
class ContactExportSchedulerModel extends AbstractCommonModel
{
    private const EXPORT_FILE_NAME_DATE_FORMAT = 'Y_m_d_H_i_s';

    public function __construct(
        private SessionInterface $session,
        private RequestStack $requestStack,
        private LeadModel $leadModel,
        private ExportHelper $exportHelper,
        private MailHelper $mailHelper,
        EntityManager $em,
        CorePermissions $security,
        EventDispatcherInterface $dispatcher,
        UrlGeneratorInterface $router,
        Translator $translator,
        UserHelper $userHelper,
        LoggerInterface $mauticLogger,
        CoreParametersHelper $coreParametersHelper
    ) {
        parent::__construct($em, $security, $dispatcher, $router, $translator, $userHelper, $mauticLogger, $coreParametersHelper);
    }

    public function getRepository(): ContactExportSchedulerRepository
    {
        /** @var ContactExportSchedulerRepository $repo */
        $repo = $this->em->getRepository(ContactExportScheduler::class);

        return $repo;
    }

    /**
     * @param array<mixed> $permissions
     *
     * @return array<string, mixed>
     */
    public function prepareData(array $permissions): array
    {
        $search     = $this->session->get('mautic.lead.filter', '');
        $orderBy    = $this->session->get('mautic.lead.orderby', 'l.last_active');
        $orderByDir = $this->session->get('mautic.lead.orderbydir', 'DESC');
        $indexMode  = $this->session->get('mautic.lead.indexmode', 'list');

        $anonymous = $this->translator->trans('mautic.lead.lead.searchcommand.isanonymous');

        /** @var Request $request */
        $request = $this->getRequest();

        $ids      = $request->get('ids');
        $fileType = $request->get('filetype', 'csv');

        $filter = ['string' => $search, 'force' => []];

        if (!empty($ids)) {
            $filter['force'] = [
                [
                    'column' => 'l.id',
                    'expr'   => 'in',
                    'value'  => json_decode($ids, true, 512, JSON_THROW_ON_ERROR),
                ],
            ];
        } else {
            if ('list' !== $indexMode || (!str_contains($search, $anonymous))) {
                // Remove anonymous leads unless requested to prevent clutter.
                $filter['force'] = [
                    [
                        'column' => 'l.dateIdentified',
                        'expr'   => 'isNotNull',
                    ],
                ];
            }

            if (!$permissions['lead:leads:viewother']) {
                // Show only owner's contacts.
                $filter['force'] = [
                    [
                        'column' => 'l.owner',
                        'expr'   => 'eq',
                    ],
                ];
            }
        }

        return [
            'start'          => 0,
            'limit'          => $this->coreParametersHelper->get('contact_export_batch_size', 1000),
            'filter'         => $filter,
            'orderBy'        => $orderBy,
            'orderByDir'     => $orderByDir,
            'withTotalCount' => true,
            'fileType'       => $fileType,
        ];
    }

    /**
     * @param array<mixed> $data
     */
    public function saveEntity(array $data): ContactExportScheduler
    {
        $contactExportScheduler = new ContactExportScheduler();
        $contactExportScheduler
            ->setUser($this->userHelper->getUser())
            ->setScheduledDateTime(new \DateTimeImmutable())
            ->setData($data);

        $this->em->persist($contactExportScheduler);
        $this->em->flush();

        return $contactExportScheduler;
    }

    public function processAndGetExportFilePath(ContactExportScheduler $contactExportScheduler): string
    {
        $data            = $contactExportScheduler->getData();
        $fileType        = $data['fileType'];
        $resultsCallback = fn ($contact) => $contact->getProfileFields();
        $iterator        = new IteratorExportDataModel(
            $this->leadModel,
            $contactExportScheduler->getData(),
            $resultsCallback,
            true
        );
        /** @var \DateTimeImmutable $scheduledDateTime */
        $scheduledDateTime = $contactExportScheduler->getScheduledDateTime();
        $fileName          = 'contacts_export_'.$scheduledDateTime->format(self::EXPORT_FILE_NAME_DATE_FORMAT);

        return $this->exportResultsAs($iterator, $fileType, $fileName);
    }

    public function getEmailMessageWithLink(string $filePath): string
    {
        $link = $this->router->generate(
            'mautic_contact_export_download',
            ['fileName' => basename($filePath)],
            UrlGeneratorInterface::ABSOLUTE_URL
        );

        return $this->translator->trans(
            'mautic.lead.export.email',
            ['%link%' => $link, '%label%' => basename($filePath)]
        );
    }

    public function sendEmail(ContactExportScheduler $contactExportScheduler, string $filePath): void
    {
        /** @var User $user */
        $user    = $contactExportScheduler->getUser();
        $message = $this->getEmailMessageWithLink($filePath);

        $this->mailHelper->setTo([$user->getEmail() => $user->getName()]);
        $this->mailHelper->setSubject(
            $this->translator->trans('mautic.lead.export.email_subject', ['%file_name%' => basename($filePath)])
        );
        $this->mailHelper->setBody($message);
        $this->mailHelper->parsePlainText($message);
        $this->mailHelper->send(true);
    }

    public function deleteEntity(ContactExportScheduler $contactExportScheduler): void
    {
        $this->em->remove($contactExportScheduler);
        $this->em->flush();
    }

    public function getExportFileToDownload(string $fileName): BinaryFileResponse
    {
        $filePath    = $this->coreParametersHelper->get('contact_export_dir').'/'.$fileName;
        $contentType = $this->getContactExportFileContentType($fileName);

        return new BinaryFileResponse(
            $filePath,
            Response::HTTP_OK,
            [
                'Content-Type'        => $contentType,
                'Content-Disposition' => 'attachment; filename="'.$fileName.'"',
                'Expires'             => 0,
                'Cache-Control'       => 'must-revalidate',
                'Pragma'              => 'public',
            ]
        );
    }

    private function getRequest(): ?Request
    {
        return $this->requestStack->getCurrentRequest();
    }

    private function exportResultsAs(IteratorExportDataModel $iterator, string $fileType, string $fileName): string
    {
        if (!in_array($fileType, $this->exportHelper->getSupportedExportTypes(), true)) {
            throw new BadRequestHttpException($this->translator->trans('mautic.error.invalid.export.type', ['%type%' => $fileType]));
        }

        $csvFilePath = $this->exportHelper
            ->exportDataIntoFile($iterator, $fileType, strtolower($fileName.'.'.$fileType));

        return $this->exportHelper->zipFile($csvFilePath, 'contacts_export.csv');
    }

    private function getContactExportFileContentType(string $fileName): string
    {
        $ext = pathinfo($fileName, PATHINFO_EXTENSION);

        if ('zip' === $ext) {
            return 'application/zip';
        }

        throw new BadRequestHttpException($this->translator->trans('mautic.error.invalid.specific.export.type', ['%type%' => $ext, '%expected_type%' => 'zip']));
    }
}