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

declare(strict_types=1);

namespace Mautic\IntegrationsBundle\Controller;

use Mautic\CoreBundle\Controller\CommonController;
use Mautic\IntegrationsBundle\Exception\IntegrationNotFoundException;
use Mautic\IntegrationsBundle\Form\Type\IntegrationSyncSettingsObjectFieldMappingType;
use Mautic\IntegrationsBundle\Helper\ConfigIntegrationsHelper;
use Mautic\IntegrationsBundle\Helper\FieldFilterHelper;
use Mautic\IntegrationsBundle\Helper\FieldMergerHelper;
use Mautic\IntegrationsBundle\Integration\Interfaces\ConfigFormSyncInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class FieldPaginationController extends CommonController
{
    /**
     * @return Response
     */
    public function paginateAction(
        Request $request,
        FormFactoryInterface $formFactory,
        ConfigIntegrationsHelper $integrationsHelper,
        string $integration,
        string $object,
        int $page
    ) {
        // Check ACL
        if (!$this->security->isGranted('plugin:plugins:manage')) {
            return $this->accessDenied();
        }

        // Find the integration
        try {
            /** @var ConfigFormSyncInterface $integrationObject */
            $integrationObject        = $integrationsHelper->getIntegration($integration);
            $integrationConfiguration = $integrationObject->getIntegrationConfiguration();
        } catch (IntegrationNotFoundException) {
            return $this->notFound();
        }

        $keyword         = $request->get('keyword');
        $featureSettings = $integrationConfiguration->getFeatureSettings();
        $currentFields   = $this->getFields($request, $integrationObject, $featureSettings, $object);

        $fieldFilterHelper = new FieldFilterHelper($integrationObject);
        if ($keyword) {
            $fieldFilterHelper->filterFieldsByKeyword($object, $keyword, $page);
        } else {
            $fieldFilterHelper->filterFieldsByPage($object, $page);
        }

        // Create the form
        $form = $formFactory->create(
            IntegrationSyncSettingsObjectFieldMappingType::class,
            $currentFields,
            [
                'integrationFields' => $fieldFilterHelper->getFilteredFields(),
                'page'              => $page,
                'keyword'           => $keyword,
                'totalFieldCount'   => $fieldFilterHelper->getTotalFieldCount(),
                'object'            => $object,
                'integrationObject' => $integrationObject,
                'csrf_protection'   => false,
            ]
        );

        $html = $this->render(
            '@Integrations/Config/field_mapping.html.twig',
            [
                'form'        => $form->createView(),
                'integration' => $integration,
                'object'      => $object,
                'page'        => $page,
            ]
        )->getContent();

        $prefix   = "integration_config[featureSettings][sync][fieldMappings][$object]";
        $idPrefix = str_replace(['][', '[', ']'], '_', $prefix);
        if (str_ends_with($idPrefix, '_')) {
            $idPrefix = substr($idPrefix, 0, -1);
        }

        $formType = 'integration_sync_settings_object_field_mapping';
        $html     = preg_replace('/'.$formType.'\[(.*?)\]/', $prefix.'[$1]', $html);
        $html     = str_replace($formType, $idPrefix, $html);

        return new JsonResponse(
            [
                'success' => 1,
                'html'    => $html,
            ]
        );
    }

    private function getFields(Request $request, ConfigFormSyncInterface $integrationObject, array $featureSettings, string $object): array
    {
        $fields = $featureSettings['sync']['fieldMappings'] ?? [];

        if (!isset($fields[$object])) {
            $fields[$object] = [];
        }

        // Pull those changed from session
        $session       = $request->getSession();
        $sessionFields = $session->get(sprintf('%s-fields', $integrationObject->getName()), []);

        if (!isset($sessionFields[$object])) {
            return $fields[$object];
        }

        $fieldMerger = new FieldMergerHelper($integrationObject, $fields);
        $fieldMerger->mergeSyncFieldMapping($object, $sessionFields[$object]);

        return $fieldMerger->getFieldMappings()[$object];
    }
}