1 | <?php |
||
2 | |||
3 | /* |
||
4 | * @copyright 2014 Mautic Contributors. All rights reserved |
||
5 | * @author Mautic |
||
6 | * |
||
7 | * @link http://mautic.org |
||
8 | * |
||
9 | * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
||
10 | */ |
||
11 | |||
12 | namespace MauticPlugin\MauticEmailMarketingBundle\Integration; |
||
13 | |||
14 | use MauticPlugin\MauticEmailMarketingBundle\Form\Type\ConstantContactType; |
||
15 | |||
16 | /** |
||
17 | * Class ConstantContactIntegration. |
||
18 | */ |
||
19 | class ConstantContactIntegration extends EmailAbstractIntegration |
||
20 | { |
||
21 | /** |
||
22 | * {@inheritdoc} |
||
23 | * |
||
24 | * @return string |
||
25 | */ |
||
26 | public function getName() |
||
27 | { |
||
28 | return 'ConstantContact'; |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * @return string |
||
33 | */ |
||
34 | public function getDisplayName() |
||
35 | { |
||
36 | return 'Constant Contact'; |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | public function getAuthenticationType() |
||
43 | { |
||
44 | return 'oauth2'; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Get the URL required to obtain an oauth2 access token. |
||
49 | * |
||
50 | * @return string |
||
51 | */ |
||
52 | public function getAccessTokenUrl() |
||
53 | { |
||
54 | return 'https://oauth2.constantcontact.com/oauth2/oauth/token'; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Get the authentication/login URL for oauth2 access. |
||
59 | * |
||
60 | * @return string |
||
61 | */ |
||
62 | public function getAuthenticationUrl() |
||
63 | { |
||
64 | return 'https://oauth2.constantcontact.com/oauth2/oauth/siteowner/authorize'; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Retrieves and stores tokens returned from oAuthLogin. |
||
69 | * |
||
70 | * @param array $settings |
||
71 | * @param array $parameters |
||
72 | * |
||
73 | * @return bool|string false if no error; otherwise the error string |
||
74 | */ |
||
75 | public function authCallback($settings = [], $parameters = []) |
||
76 | { |
||
77 | // Constanct Contact doesn't like POST |
||
78 | $settings['method'] = 'GET'; |
||
79 | |||
80 | return parent::authCallback($settings, $parameters); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @return array |
||
85 | */ |
||
86 | public function getAvailableLeadFields($settings = []) |
||
87 | { |
||
88 | if (!$this->isAuthorized()) { |
||
89 | return []; |
||
90 | } |
||
91 | |||
92 | $fields = [ |
||
93 | 'email', |
||
94 | 'prefix_name', |
||
95 | 'first_name', |
||
96 | 'last_name', |
||
97 | 'company_name', |
||
98 | 'job_title', |
||
99 | 'address_line1', |
||
100 | 'address_line2', |
||
101 | 'address_city', |
||
102 | 'address_state', |
||
103 | 'address_country_code', |
||
104 | 'address_postal_code', |
||
105 | 'cell_phone', |
||
106 | 'fax', |
||
107 | 'work_phone', |
||
108 | 'home_phone', |
||
109 | ]; |
||
110 | |||
111 | $leadFields = []; |
||
112 | foreach ($fields as $f) { |
||
113 | $leadFields[$f] = [ |
||
114 | 'label' => $this->translator->trans('mautic.constantcontact.field.'.$f), |
||
115 | 'type' => 'string', |
||
116 | 'required' => ('email' == $f) ? true : false, |
||
117 | ]; |
||
118 | } |
||
119 | |||
120 | $c = 1; |
||
121 | while ($c <= 15) { |
||
122 | $leadFields['customfield_'.$c] = [ |
||
123 | 'label' => $this->translator->trans('mautic.constantcontact.customfield.'.$f), |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
124 | 'type' => 'string', |
||
125 | 'required' => false, |
||
126 | ]; |
||
127 | ++$c; |
||
128 | } |
||
129 | |||
130 | return $leadFields; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @param $lead |
||
135 | */ |
||
136 | public function pushLead($lead, $config = []) |
||
137 | { |
||
138 | $config = $this->mergeConfigToFeatureSettings($config); |
||
139 | |||
140 | $mappedData = $this->populateLeadData($lead, $config); |
||
141 | |||
142 | if (empty($mappedData)) { |
||
143 | return false; |
||
144 | } elseif (empty($mappedData['email'])) { |
||
145 | return false; |
||
146 | } elseif (!isset($config['list_settings'])) { |
||
147 | return false; |
||
148 | } |
||
149 | |||
150 | try { |
||
151 | if ($this->isAuthorized()) { |
||
152 | $email = $mappedData['email']; |
||
153 | unset($mappedData['email']); |
||
154 | |||
155 | $addresses = []; |
||
156 | $customfields = []; |
||
157 | foreach ($mappedData as $k => $v) { |
||
158 | if (0 === strpos($v, 'address_')) { |
||
159 | $addresses[str_replace('address_', '', $k)] = $v; |
||
160 | unset($mappedData[$k]); |
||
161 | } elseif (0 === strpos($v, 'customfield_')) { |
||
162 | $key = str_replace('customfield_', 'CustomField', $k); |
||
163 | $customfields[] = [ |
||
164 | 'name' => $key, |
||
165 | 'value' => $v, |
||
166 | ]; |
||
167 | unset($mappedData[$k]); |
||
168 | } |
||
169 | } |
||
170 | |||
171 | if (!empty($addresses)) { |
||
172 | $addresses['address_type'] = 'PERSONAL'; |
||
173 | $mappedData['addresses'] = $addresses; |
||
174 | } |
||
175 | |||
176 | if (!empty($customfields)) { |
||
177 | $mappedData['custom_fields'] = $customfields; |
||
178 | } |
||
179 | |||
180 | $options = []; |
||
181 | $options['action_by'] = (!empty($config['list_settings']['sendWelcome'])) ? 'ACTION_BY_VISITOR' : 'ACTION_BY_OWNER'; |
||
182 | $listId = $config['list_settings']['list']; |
||
183 | |||
184 | $this->getApiHelper()->subscribeLead($email, $listId, $mappedData, $options); |
||
185 | |||
186 | return true; |
||
187 | } |
||
188 | } catch (\Exception $e) { |
||
189 | $this->logIntegrationError($e); |
||
190 | } |
||
191 | |||
192 | return false; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * {@inheritdoc} |
||
197 | * |
||
198 | * @return string|null |
||
199 | */ |
||
200 | public function getFormType() |
||
201 | { |
||
202 | return ConstantContactType::class; |
||
203 | } |
||
204 | } |
||
205 |