1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* @copyright 2018 Mautic, Inc. All rights reserved |
7
|
|
|
* @author Mautic, Inc. |
8
|
|
|
* |
9
|
|
|
* @link https://mautic.com |
10
|
|
|
* |
11
|
|
|
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Mautic\IntegrationsBundle\Form\Type; |
15
|
|
|
|
16
|
|
|
use Mautic\IntegrationsBundle\Exception\InvalidFormOptionException; |
17
|
|
|
use Mautic\IntegrationsBundle\Integration\Interfaces\ConfigFormSyncInterface; |
18
|
|
|
use Mautic\IntegrationsBundle\Mapping\MappedFieldInfoInterface; |
19
|
|
|
use Mautic\IntegrationsBundle\Sync\Exception\ObjectNotFoundException; |
20
|
|
|
use Mautic\IntegrationsBundle\Sync\SyncDataExchange\Helper\FieldHelper; |
21
|
|
|
use Symfony\Component\Form\AbstractType; |
22
|
|
|
use Symfony\Component\Form\Extension\Core\Type\HiddenType; |
23
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
24
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
25
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
26
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
27
|
|
|
|
28
|
|
|
class IntegrationSyncSettingsObjectFieldMappingType extends AbstractType |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var TranslatorInterface |
32
|
|
|
*/ |
33
|
|
|
private $translator; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var FieldHelper |
37
|
|
|
*/ |
38
|
|
|
private $fieldHelper; |
39
|
|
|
|
40
|
|
|
public function __construct(TranslatorInterface $translator, FieldHelper $fieldHelper) |
41
|
|
|
{ |
42
|
|
|
$this->translator = $translator; |
43
|
|
|
$this->fieldHelper = $fieldHelper; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @throws InvalidFormOptionException |
48
|
|
|
* @throws ObjectNotFoundException |
49
|
|
|
*/ |
50
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options): void |
51
|
|
|
{ |
52
|
|
|
$integrationFields = $options['integrationFields']; |
53
|
|
|
|
54
|
|
|
/** @var ConfigFormSyncInterface $integrationObject */ |
55
|
|
|
$integrationObject = $options['integrationObject']; |
56
|
|
|
if (!$integrationObject instanceof ConfigFormSyncInterface) { |
|
|
|
|
57
|
|
|
throw new InvalidFormOptionException('integrationObject must be an instance of ConfigFormSyncInterface'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$objectName = $options['object']; |
61
|
|
|
foreach ($integrationFields as $fieldName => $fieldInfo) { |
62
|
|
|
if (!$fieldInfo instanceof MappedFieldInfoInterface) { |
63
|
|
|
throw new InvalidFormOptionException('integrationFields must contain an instance of MappedFieldInfoInterface'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$attr = [ |
67
|
|
|
'label' => $fieldInfo->getLabel(), |
68
|
|
|
'mauticFields' => $this->getMauticFields($integrationObject, $objectName), |
69
|
|
|
'required' => $fieldInfo->showAsRequired(), |
70
|
|
|
'placeholder' => $this->translator->trans('mautic.integration.sync_mautic_field'), |
71
|
|
|
'object' => $objectName, |
72
|
|
|
'integration' => $integrationObject->getName(), |
73
|
|
|
'field' => $fieldInfo, |
74
|
|
|
]; |
75
|
|
|
|
76
|
|
|
if ($fieldInfo->hasTooltip()) { |
77
|
|
|
$attr['attr'] = [ |
78
|
|
|
'tooltip' => $fieldInfo->getTooltip(), |
79
|
|
|
'class' => 'form-control', |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$builder->add( |
84
|
|
|
$fieldName, |
85
|
|
|
IntegrationSyncSettingsObjectFieldType::class, |
86
|
|
|
$attr |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$builder->add( |
91
|
|
|
'filter-keyword', |
92
|
|
|
TextType::class, |
93
|
|
|
[ |
94
|
|
|
'label' => false, |
95
|
|
|
'mapped' => false, |
96
|
|
|
'data' => $options['keyword'], |
97
|
|
|
'attr' => [ |
98
|
|
|
'class' => 'form-control integration-keyword-filter', |
99
|
|
|
'placeholder' => $this->translator->trans('mautic.integration.sync_filter_fields'), |
100
|
|
|
'data-object' => $objectName, |
101
|
|
|
'data-integration' => $integrationObject->getName(), |
102
|
|
|
], |
103
|
|
|
] |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
$builder->add( |
107
|
|
|
'filter-totalFieldCount', |
108
|
|
|
HiddenType::class, |
109
|
|
|
[ |
110
|
|
|
'label' => false, |
111
|
|
|
'mapped' => false, |
112
|
|
|
'data' => $options['totalFieldCount'], |
113
|
|
|
] |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function configureOptions(OptionsResolver $resolver): void |
118
|
|
|
{ |
119
|
|
|
$resolver->setRequired( |
120
|
|
|
[ |
121
|
|
|
'integrationFields', |
122
|
|
|
'page', |
123
|
|
|
'keyword', |
124
|
|
|
'totalFieldCount', |
125
|
|
|
'object', |
126
|
|
|
'integrationObject', |
127
|
|
|
] |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @throws ObjectNotFoundException |
133
|
|
|
*/ |
134
|
|
|
private function getMauticFields(ConfigFormSyncInterface $integrationObject, string $objectName): array |
135
|
|
|
{ |
136
|
|
|
$mappedObjects = $integrationObject->getSyncMappedObjects(); |
137
|
|
|
if (!isset($mappedObjects[$objectName])) { |
138
|
|
|
throw new ObjectNotFoundException($objectName); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$mauticObject = $mappedObjects[$objectName]; |
142
|
|
|
|
143
|
|
|
return $this->fieldHelper->getSyncFields($mauticObject); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|