File size: 11,748 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php

declare(strict_types=1);

namespace Mautic\IntegrationsBundle\Controller;

use Mautic\CoreBundle\Controller\AbstractFormController;
use Mautic\CoreBundle\Twig\Extension\FormExtension;
use Mautic\IntegrationsBundle\Event\ConfigAuthUrlEvent;
use Mautic\IntegrationsBundle\Event\ConfigSaveEvent;
use Mautic\IntegrationsBundle\Event\FormLoadEvent;
use Mautic\IntegrationsBundle\Event\KeysSaveEvent;
use Mautic\IntegrationsBundle\Exception\IntegrationNotFoundException;
use Mautic\IntegrationsBundle\Form\Type\IntegrationConfigType;
use Mautic\IntegrationsBundle\Helper\ConfigIntegrationsHelper;
use Mautic\IntegrationsBundle\Helper\FieldMergerHelper;
use Mautic\IntegrationsBundle\Helper\FieldValidationHelper;
use Mautic\IntegrationsBundle\Integration\BasicIntegration;
use Mautic\IntegrationsBundle\Integration\Interfaces\ConfigFormAuthInterface;
use Mautic\IntegrationsBundle\Integration\Interfaces\ConfigFormAuthorizeButtonInterface;
use Mautic\IntegrationsBundle\Integration\Interfaces\ConfigFormCallbackInterface;
use Mautic\IntegrationsBundle\Integration\Interfaces\ConfigFormFeatureSettingsInterface;
use Mautic\IntegrationsBundle\Integration\Interfaces\ConfigFormFeaturesInterface;
use Mautic\IntegrationsBundle\Integration\Interfaces\ConfigFormInterface;
use Mautic\IntegrationsBundle\Integration\Interfaces\ConfigFormNotesInterface;
use Mautic\IntegrationsBundle\Integration\Interfaces\ConfigFormSyncInterface;
use Mautic\IntegrationsBundle\IntegrationEvents;
use Mautic\PluginBundle\Entity\Integration;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;

class ConfigController extends AbstractFormController
{
    /**
     * @var BasicIntegration|ConfigFormInterface
     */
    private $integrationObject;

    /**
     * @var Integration
     */
    private $integrationConfiguration;

    /**
     * @return array|JsonResponse|RedirectResponse|Response
     */
    public function editAction(
        Request $request,
        ConfigIntegrationsHelper $integrationsHelper,
        EventDispatcherInterface $dispatcher,
        FieldValidationHelper $fieldValidator,
        FormFactoryInterface $formFactory,
        FormExtension $formExtension,
        string $integration
    ) {
        // Check ACL
        if (!$this->security->isGranted('plugin:plugins:manage')) {
            return $this->accessDenied();
        }

        try {
            $this->integrationObject        = $integrationsHelper->getIntegration($integration);
            $this->integrationConfiguration = $this->integrationObject->getIntegrationConfiguration();
        } catch (IntegrationNotFoundException) {
            return $this->notFound();
        }

        $event = new FormLoadEvent($this->integrationConfiguration);
        $dispatcher->dispatch($event, IntegrationEvents::INTEGRATION_CONFIG_FORM_LOAD);

        // Create the form
        $form = $this->getForm($formFactory);

        if (Request::METHOD_POST === $request->getMethod()) {
            return $this->submitForm($request, $integrationsHelper, $fieldValidator, $dispatcher, $formFactory, $formExtension, $form);
        }

        // Clear the session of previously stored fields in case it got stuck
        /** @var Session $session */
        $session = $request->getSession();
        $session->remove("$integration-fields");

        return $this->showForm($request, $form, $formExtension);
    }

    /**
     * @param FormInterface<mixed> $form
     */
    private function submitForm(
        Request $request,
        ConfigIntegrationsHelper $integrationsHelper,
        FieldValidationHelper $fieldValidator,
        EventDispatcherInterface $eventDispatcher,
        FormFactoryInterface $formFactory,
        FormExtension $formExtension,
        FormInterface $form
    ): JsonResponse|Response {
        if ($this->isFormCancelled($form)) {
            return $this->closeForm($request);
        }

        // Get the fields before the form binds partial data due to pagination
        $settings          = $this->integrationConfiguration->getFeatureSettings();
        $fieldMappings     = $settings['sync']['fieldMappings'] ?? [];
        $oldApiKeys        = $this->integrationConfiguration->getApiKeys();

        // Submit the form
        $form->handleRequest($request);

        $configEvent = new KeysSaveEvent($this->integrationConfiguration, $oldApiKeys);
        $this->dispatcher->dispatch($configEvent, IntegrationEvents::INTEGRATION_API_KEYS_BEFORE_SAVE);

        if ($this->integrationObject instanceof ConfigFormSyncInterface) {
            $integration   = $this->integrationObject->getName();
            $settings      = $this->integrationConfiguration->getFeatureSettings();
            $session       = $request->getSession();
            $updatedFields = $session->get("$integration-fields", []);

            $fieldMerger = new FieldMergerHelper($this->integrationObject, $fieldMappings);

            foreach ($updatedFields as $object => $fields) {
                $fieldMerger->mergeSyncFieldMapping($object, $fields);
            }

            $settings['sync']['fieldMappings'] = $fieldMerger->getFieldMappings();

            $fieldValidator->validateRequiredFields($form, $this->integrationObject, $settings['sync']['fieldMappings']);

            $this->integrationConfiguration->setFeatureSettings($settings);
        }

        // Dispatch event prior to saving the Integration. Bundles/plugins may need to modify some field values before save
        $configEvent = new ConfigSaveEvent($this->integrationConfiguration);
        $eventDispatcher->dispatch($configEvent, IntegrationEvents::INTEGRATION_CONFIG_BEFORE_SAVE);

        // Show the form if there are errors and the plugin is published or the authorized button was clicked
        $integrationDetailsPost = $request->request->get('integration_details') ?? [];
        $authorize              = !empty($integrationDetailsPost['in_auth']);
        if ($form->isSubmitted() && !$form->isValid() && ($this->integrationConfiguration->getIsPublished() || $authorize)) {
            return $this->showForm($request, $form, $formExtension);
        }

        // Save the integration configuration
        $integrationsHelper->saveIntegrationConfiguration($this->integrationConfiguration);

        // Dispatch after save event
        $eventDispatcher->dispatch($configEvent, IntegrationEvents::INTEGRATION_CONFIG_AFTER_SAVE);

        // Show the form if the apply button was clicked
        if ($this->isFormApplied($form)) {
            // Regenerate the form
            $this->resetFieldsInSession($request);
            $form = $this->getForm($formFactory);

            return $this->showForm($request, $form, $formExtension);
        }

        // Otherwise close the modal
        return $this->closeForm($request);
    }

    /**
     * @return FormInterface<mixed>
     */
    private function getForm(FormFactoryInterface $formFactory)
    {
        return $formFactory->create(
            $this->integrationObject->getConfigFormName() ?: IntegrationConfigType::class,
            $this->integrationConfiguration,
            [
                'action'      => $this->generateUrl('mautic_integration_config', ['integration' => $this->integrationObject->getName()]),
                'integration' => $this->integrationObject->getName(),
            ]
        );
    }

    /**
     * @param FormInterface<mixed> $form
     */
    private function showForm(Request $request, FormInterface $form, FormExtension $formExtension): Response
    {
        $integrationObject = $this->integrationObject;
        $formView          = $form->createView();

        $showFeaturesTab = $integrationObject instanceof ConfigFormFeaturesInterface
            || $integrationObject instanceof ConfigFormSyncInterface
            || $integrationObject instanceof ConfigFormFeatureSettingsInterface;

        $hasFeatureErrors = (
            $integrationObject instanceof ConfigFormFeatureSettingsInterface
            && $formExtension->containsErrors($formView['featureSettings']['integration'])
        ) || (
            isset($formView['featureSettings']['sync']['integration'])
            && $formExtension->containsErrors($formView['featureSettings']['sync']['integration'])
        );

        $hasAuthErrors = $integrationObject instanceof ConfigFormAuthInterface && $formExtension->containsErrors($formView['apiKeys']);

        $useSyncFeatures = $integrationObject instanceof ConfigFormSyncInterface;

        $useFeatureSettings = $integrationObject instanceof ConfigFormFeatureSettingsInterface;

        $useAuthorizationUrl = $integrationObject instanceof ConfigFormAuthorizeButtonInterface;

        $callbackUrl = $integrationObject instanceof ConfigFormCallbackInterface ?
            $integrationObject->getRedirectUri()
            : false;

        $useConfigFormNotes = $integrationObject instanceof ConfigFormNotesInterface;

        return $this->delegateView(
            [
                'viewParameters' => [
                    'integrationObject'   => $integrationObject,
                    'form'                => $formView,
                    'activeTab'           => $request->get('activeTab'),
                    'showFeaturesTab'     => $showFeaturesTab,
                    'hasFeatureErrors'    => $hasFeatureErrors,
                    'hasAuthErrors'       => $hasAuthErrors,
                    'useSyncFeatures'     => $useSyncFeatures,
                    'useFeatureSettings'  => $useFeatureSettings,
                    'useAuthorizationUrl' => $useAuthorizationUrl,
                    'callbackUrl'         => $callbackUrl,
                    'useConfigFormNotes'  => $useConfigFormNotes,
                ],
                'contentTemplate' => $integrationObject->getConfigFormContentTemplate()
                    ?: '@Integrations/Config/form.html.twig',
                'passthroughVars' => [
                    'activeLink'    => '#mautic_plugin_index',
                    'mauticContent' => 'integrationsConfig',
                    'route'         => false,
                ],
            ]
        );
    }

    private function closeForm(Request $request): JsonResponse
    {
        $this->resetFieldsInSession($request);

        $response = [
            'closeModal'    => 1,
            'enabled'       => $this->integrationConfiguration->getIsPublished(),
            'name'          => $this->integrationConfiguration->getName(),
            'mauticContent' => 'integrationsConfig',
            'flashes'       => $this->getFlashContent(),
        ];

        if ($this->integrationObject instanceof ConfigFormAuthorizeButtonInterface) {
            // Dispatch event to allow listeners to extract information and/or manipulate the URL
            $authUrl      = $this->integrationObject->getAuthorizationUrl();
            $authUrlEvent = new ConfigAuthUrlEvent($this->integrationConfiguration, $authUrl);
            $this->dispatcher->dispatch($authUrlEvent, IntegrationEvents::INTEGRATION_CONFIG_ON_GENERATE_AUTH_URL);

            $response['authUrl'] = $authUrlEvent->getAuthUrl();
        }

        return new JsonResponse($response);
    }

    private function resetFieldsInSession(Request $request): void
    {
        $session = $request->getSession();
        $session->remove("{$this->integrationObject->getName()}-fields");
    }
}