1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MauticPlugin\MauticCrmBundle\Integration\Pipedrive\Export; |
4
|
|
|
|
5
|
|
|
use Mautic\LeadBundle\Entity\Company; |
6
|
|
|
use Mautic\PluginBundle\Entity\IntegrationEntity; |
7
|
|
|
use MauticPlugin\MauticCrmBundle\Entity\PipedriveOwner; |
8
|
|
|
use MauticPlugin\MauticCrmBundle\Integration\Pipedrive\AbstractPipedrive; |
9
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
10
|
|
|
|
11
|
|
|
class CompanyExport extends AbstractPipedrive |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @return bool |
15
|
|
|
*/ |
16
|
|
|
public function pushCompany(Company $company) |
17
|
|
|
{ |
18
|
|
|
if (!$this->getIntegration()->isCompanySupportEnabled()) { |
19
|
|
|
return false; //feature disabled |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
$mappedData = $this->getMappedCompanyData($company); |
23
|
|
|
$integrationEntity = $this->getCompanyIntegrationEntity(['internalEntityId' => $company->getId()]); |
24
|
|
|
|
25
|
|
|
if ($integrationEntity) { |
26
|
|
|
return $this->update($integrationEntity, $mappedData); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return $this->create($company, $mappedData); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return bool |
34
|
|
|
*/ |
35
|
|
|
public function create(Company $company, array $mappedData = []) |
36
|
|
|
{ |
37
|
|
|
if (!$this->getIntegration()->isCompanySupportEnabled()) { |
38
|
|
|
return false; //feature disabled |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$companyId = $company->getId(); |
42
|
|
|
|
43
|
|
|
$integrationEntity = $this->getCompanyIntegrationEntity(['internalEntityId' => $companyId]); |
44
|
|
|
|
45
|
|
|
if ($integrationEntity) { |
46
|
|
|
return false; // company has integration |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
try { |
50
|
|
|
$createdData = $this->getIntegration()->getApiHelper()->createCompany($mappedData); |
51
|
|
|
if (empty($createdData['id'])) { |
52
|
|
|
return false; |
53
|
|
|
} |
54
|
|
|
$integrationEntity = $this->createIntegrationCompanyEntity(new \DateTime(), $createdData['id'], $companyId); |
55
|
|
|
|
56
|
|
|
$this->em->persist($integrationEntity); |
57
|
|
|
$this->em->flush(); |
58
|
|
|
|
59
|
|
|
return true; |
60
|
|
|
} catch (\Exception $e) { |
61
|
|
|
$this->getIntegration()->logIntegrationError($e); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
public function update(IntegrationEntity $integrationEntity, array $mappedData = []) |
71
|
|
|
{ |
72
|
|
|
if (!$this->getIntegration()->isCompanySupportEnabled()) { |
73
|
|
|
return false; //feature disabled |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
try { |
77
|
|
|
$this->getIntegration()->getApiHelper()->updateCompany($mappedData, $integrationEntity->getIntegrationEntityId()); |
78
|
|
|
$integrationEntity->setLastSyncDate(new \DateTime()); |
79
|
|
|
|
80
|
|
|
$this->em->flush(); |
81
|
|
|
|
82
|
|
|
return true; |
83
|
|
|
} catch (\Exception $e) { |
84
|
|
|
$this->getIntegration()->logIntegrationError($e); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
public function delete(Company $company) |
94
|
|
|
{ |
95
|
|
|
$integrationEntity = $this->getCompanyIntegrationEntity(['internalEntityId' => $company->getId()]); |
96
|
|
|
|
97
|
|
|
if (!$integrationEntity) { |
98
|
|
|
return true; // company doesn't have integration |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (!$this->getIntegration()->isCompanySupportEnabled()) { |
102
|
|
|
//feature disabled |
103
|
|
|
$this->em->remove($integrationEntity); |
104
|
|
|
$this->em->flush(); |
105
|
|
|
|
106
|
|
|
return false; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
try { |
110
|
|
|
$this->getIntegration()->getApiHelper()->removeCompany($integrationEntity->getIntegrationEntityId()); |
111
|
|
|
|
112
|
|
|
$this->em->remove($integrationEntity); |
113
|
|
|
$this->em->flush(); |
114
|
|
|
|
115
|
|
|
return true; |
116
|
|
|
} catch (\Exception $e) { |
117
|
|
|
$this->getIntegration()->logIntegrationError($e); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
|
|
private function getMappedCompanyData(Company $company) |
127
|
|
|
{ |
128
|
|
|
$mappedData = []; |
129
|
|
|
|
130
|
|
|
if (empty($this->getIntegration()->getIntegrationSettings()->getFeatureSettings()['companyFields'])) { |
131
|
|
|
return []; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$companyFields = $this->getIntegration()->getIntegrationSettings()->getFeatureSettings()['companyFields']; |
135
|
|
|
|
136
|
|
|
$accessor = PropertyAccess::createPropertyAccessor(); |
137
|
|
|
|
138
|
|
|
foreach ($companyFields as $externalField => $internalField) { |
139
|
|
|
if (false !== strpos($internalField, $company::FIELD_ALIAS, 0) && method_exists($company, 'get'.ucfirst(substr($internalField, strlen($company::FIELD_ALIAS))))) { |
140
|
|
|
//for core company field |
141
|
|
|
$fieldName = substr($internalField, strlen($company::FIELD_ALIAS)); |
142
|
|
|
} else { |
143
|
|
|
//for custom company field |
144
|
|
|
$fieldName = $internalField; |
145
|
|
|
} |
146
|
|
|
$mappedData[$externalField] = $accessor->getValue($company, $fieldName); |
147
|
|
|
} |
148
|
|
|
$companyIntegrationOwnerId = $this->getCompanyIntegrationOwnerId($company); |
149
|
|
|
if ($companyIntegrationOwnerId) { |
150
|
|
|
$mappedData['owner_id'] = $companyIntegrationOwnerId; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $mappedData; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
private function getCompanyIntegrationOwnerId(Company $company) |
157
|
|
|
{ |
158
|
|
|
$mauticOwner = $company->getOwner(); |
159
|
|
|
|
160
|
|
|
if (!$mauticOwner) { |
|
|
|
|
161
|
|
|
return null; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$pipedriveOwner = $this->em->getRepository(PipedriveOwner::class)->findOneByEmail($mauticOwner->getEmail()); |
|
|
|
|
165
|
|
|
|
166
|
|
|
if (!$pipedriveOwner) { |
167
|
|
|
return null; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $pipedriveOwner->getOwnerId(); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|