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\MauticSocialBundle\Integration; |
||
13 | |||
14 | use Doctrine\ORM\EntityManager; |
||
15 | use Mautic\CoreBundle\Helper\CacheStorageHelper; |
||
16 | use Mautic\CoreBundle\Helper\EncryptionHelper; |
||
17 | use Mautic\CoreBundle\Helper\PathsHelper; |
||
18 | use Mautic\CoreBundle\Model\NotificationModel; |
||
19 | use Mautic\LeadBundle\Model\CompanyModel; |
||
20 | use Mautic\LeadBundle\Model\DoNotContact; |
||
21 | use Mautic\LeadBundle\Model\FieldModel; |
||
22 | use Mautic\LeadBundle\Model\LeadModel; |
||
23 | use Mautic\PluginBundle\Helper\IntegrationHelper; |
||
24 | use Mautic\PluginBundle\Integration\AbstractIntegration; |
||
25 | use Mautic\PluginBundle\Model\IntegrationEntityModel; |
||
26 | use Monolog\Logger; |
||
27 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
||
28 | use Symfony\Component\Form\FormBuilder; |
||
29 | use Symfony\Component\HttpFoundation\RequestStack; |
||
30 | use Symfony\Component\HttpFoundation\Session\Session; |
||
31 | use Symfony\Component\Routing\Router; |
||
32 | use Symfony\Component\Translation\TranslatorInterface; |
||
33 | |||
34 | abstract class SocialIntegration extends AbstractIntegration |
||
35 | { |
||
36 | protected $persistNewLead = false; |
||
37 | |||
38 | /** |
||
39 | * @var IntegrationHelper |
||
40 | */ |
||
41 | protected $integrationHelper; |
||
42 | |||
43 | public function __construct( |
||
44 | EventDispatcherInterface $eventDispatcher, |
||
45 | CacheStorageHelper $cacheStorageHelper, |
||
46 | EntityManager $entityManager, |
||
47 | Session $session, |
||
48 | RequestStack $requestStack, |
||
49 | Router $router, |
||
50 | TranslatorInterface $translator, |
||
51 | Logger $logger, |
||
52 | EncryptionHelper $encryptionHelper, |
||
53 | LeadModel $leadModel, |
||
54 | CompanyModel $companyModel, |
||
55 | PathsHelper $pathsHelper, |
||
56 | NotificationModel $notificationModel, |
||
57 | FieldModel $fieldModel, |
||
58 | IntegrationEntityModel $integrationEntityModel, |
||
59 | DoNotContact $doNotContact, |
||
60 | IntegrationHelper $integrationHelper |
||
61 | ) { |
||
62 | $this->integrationHelper = $integrationHelper; |
||
63 | |||
64 | parent::__construct( |
||
65 | $eventDispatcher, |
||
66 | $cacheStorageHelper, |
||
67 | $entityManager, |
||
68 | $session, |
||
69 | $requestStack, |
||
70 | $router, |
||
71 | $translator, |
||
72 | $logger, |
||
73 | $encryptionHelper, |
||
74 | $leadModel, |
||
75 | $companyModel, |
||
76 | $pathsHelper, |
||
77 | $notificationModel, |
||
78 | $fieldModel, |
||
79 | $integrationEntityModel, |
||
80 | $doNotContact |
||
81 | ); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param \Mautic\PluginBundle\Integration\Form|FormBuilder $builder |
||
86 | * @param array $data |
||
87 | * @param string $formArea |
||
88 | */ |
||
89 | public function appendToForm(&$builder, $data, $formArea) |
||
90 | { |
||
91 | if ('features' == $formArea) { |
||
92 | $name = strtolower($this->getName()); |
||
93 | $formType = $this->getFormType(); |
||
94 | if ($formType) { |
||
95 | $builder->add('shareButton', $formType, [ |
||
96 | 'label' => 'mautic.integration.form.sharebutton', |
||
97 | 'required' => false, |
||
98 | 'data' => (isset($data['shareButton'])) ? $data['shareButton'] : [], |
||
99 | ]); |
||
100 | } |
||
101 | } |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | * |
||
107 | * @param array $settings |
||
108 | * |
||
109 | * @return array |
||
110 | */ |
||
111 | public function getFormLeadFields($settings = []) |
||
112 | { |
||
113 | static $fields = []; |
||
114 | |||
115 | if (empty($fields)) { |
||
116 | $s = $this->getName(); |
||
117 | $available = $this->getAvailableLeadFields($settings); |
||
118 | if (empty($available) || !is_array($available)) { |
||
119 | return []; |
||
120 | } |
||
121 | //create social profile fields |
||
122 | $socialProfileUrls = $this->integrationHelper->getSocialProfileUrlRegex(); |
||
123 | |||
124 | foreach ($available as $field => $details) { |
||
125 | $label = (!empty($details['label'])) ? $details['label'] : false; |
||
126 | $fn = $this->matchFieldName($field); |
||
127 | switch ($details['type']) { |
||
128 | case 'string': |
||
129 | case 'boolean': |
||
130 | $fields[$fn] = (!$label) |
||
131 | ? $this->translator->transConditional("mautic.integration.common.{$fn}", "mautic.integration.{$s}.{$fn}") |
||
132 | : $label; |
||
133 | break; |
||
134 | case 'object': |
||
135 | if (isset($details['fields'])) { |
||
136 | foreach ($details['fields'] as $f) { |
||
137 | $fn = $this->matchFieldName($field, $f); |
||
138 | $fields[$fn] = (!$label) |
||
139 | ? $this->translator->transConditional("mautic.integration.common.{$fn}", "mautic.integration.{$s}.{$fn}") |
||
140 | : $label; |
||
141 | } |
||
142 | } else { |
||
143 | $fields[$field] = (!$label) |
||
144 | ? $this->translator->transConditional("mautic.integration.common.{$fn}", "mautic.integration.{$s}.{$fn}") |
||
145 | : $label; |
||
146 | } |
||
147 | break; |
||
148 | case 'array_object': |
||
149 | if ('urls' == $field || 'url' == $field) { |
||
150 | foreach ($socialProfileUrls as $p => $d) { |
||
151 | $fields["{$p}ProfileHandle"] = (!$label) |
||
152 | ? $this->translator->transConditional("mautic.integration.common.{$p}ProfileHandle", "mautic.integration.{$s}.{$p}ProfileHandle") |
||
153 | : $label; |
||
154 | } |
||
155 | foreach ($details['fields'] as $f) { |
||
156 | $fields["{$p}Urls"] = (!$label) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
157 | ? $this->translator->transConditional("mautic.integration.common.{$f}Urls", "mautic.integration.{$s}.{$f}Urls") |
||
158 | : $label; |
||
159 | } |
||
160 | } elseif (isset($details['fields'])) { |
||
161 | foreach ($details['fields'] as $f) { |
||
162 | $fn = $this->matchFieldName($field, $f); |
||
163 | $fields[$fn] = (!$label) |
||
164 | ? $this->translator->transConditional("mautic.integration.common.{$fn}", "mautic.integration.{$s}.{$fn}") |
||
165 | : $label; |
||
166 | } |
||
167 | } else { |
||
168 | $fields[$fn] = (!$label) |
||
169 | ? $this->translator->transConditional("mautic.integration.common.{$fn}", "mautic.integration.{$s}.{$fn}") |
||
170 | : $label; |
||
171 | } |
||
172 | break; |
||
173 | } |
||
174 | } |
||
175 | if ($this->sortFieldsAlphabetically()) { |
||
176 | uasort($fields, 'strnatcmp'); |
||
177 | } |
||
178 | } |
||
179 | |||
180 | return $fields; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @param array $settings |
||
185 | */ |
||
186 | public function getFormCompanyFields($settings = []) |
||
187 | { |
||
188 | $settings['feature_settings']['objects'] = ['Company']; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * {@inheritdoc} |
||
193 | */ |
||
194 | public function getAuthenticationType() |
||
195 | { |
||
196 | return 'oauth2'; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * {@inheritdoc} |
||
201 | */ |
||
202 | public function getRequiredKeyFields() |
||
203 | { |
||
204 | return [ |
||
205 | 'client_id' => 'mautic.integration.keyfield.clientid', |
||
206 | 'client_secret' => 'mautic.integration.keyfield.clientsecret', |
||
207 | ]; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Get the array key for clientId. |
||
212 | * |
||
213 | * @return string |
||
214 | */ |
||
215 | public function getClientIdKey() |
||
216 | { |
||
217 | return 'client_id'; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Get the array key for client secret. |
||
222 | * |
||
223 | * @return string |
||
224 | */ |
||
225 | public function getClientSecretKey() |
||
226 | { |
||
227 | return 'client_secret'; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * {@inheritdoc} |
||
232 | * |
||
233 | * @param string $data |
||
234 | * @param bool $postAuthorization |
||
235 | * |
||
236 | * @return mixed |
||
237 | */ |
||
238 | public function parseCallbackResponse($data, $postAuthorization = false) |
||
239 | { |
||
240 | if ($postAuthorization) { |
||
241 | return json_decode($data, true); |
||
242 | } else { |
||
243 | return json_decode($data); |
||
244 | } |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Returns notes specific to sections of the integration form (if applicable). |
||
249 | * |
||
250 | * @param $section |
||
251 | * |
||
252 | * @return string |
||
253 | */ |
||
254 | public function getFormNotes($section) |
||
255 | { |
||
256 | return ['', 'info']; |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Get the template for social profiles. |
||
261 | * |
||
262 | * @return string |
||
263 | */ |
||
264 | public function getSocialProfileTemplate() |
||
265 | { |
||
266 | return "MauticSocialBundle:Integration/{$this->getName()}/Profile:view.html.php"; |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * Get the access token from session or socialCache. |
||
271 | * |
||
272 | * @param $socialCache |
||
273 | * |
||
274 | * @return array|mixed|null |
||
275 | */ |
||
276 | protected function getContactAccessToken(&$socialCache) |
||
277 | { |
||
278 | if (!$this->session) { |
||
279 | return null; |
||
280 | } |
||
281 | |||
282 | if (!$this->session->isStarted()) { |
||
283 | return (isset($socialCache['accessToken'])) ? $this->decryptApiKeys($socialCache['accessToken']) : null; |
||
284 | } |
||
285 | |||
286 | $accessToken = $this->session->get($this->getName().'_tokenResponse', []); |
||
287 | if (!isset($accessToken[$this->getAuthTokenKey()])) { |
||
288 | if (isset($socialCache['accessToken'])) { |
||
289 | $accessToken = $this->decryptApiKeys($socialCache['accessToken']); |
||
290 | } else { |
||
291 | return null; |
||
292 | } |
||
293 | } else { |
||
294 | $this->session->remove($this->getName().'_tokenResponse'); |
||
295 | $socialCache['accessToken'] = $this->encryptApiKeys($accessToken); |
||
296 | |||
297 | $this->persistNewLead = true; |
||
298 | } |
||
299 | |||
300 | return $accessToken; |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Returns form type. |
||
305 | * |
||
306 | * @return string|null |
||
307 | */ |
||
308 | abstract public function getFormType(); |
||
309 | } |
||
310 |