simplenews_mailjet_subscriptions__usingmailjetmodule.patch 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. diff --git a/README.txt b/README.txt
  2. index cee6e1a..b488e91 100644
  3. --- a/README.txt
  4. +++ b/README.txt
  5. @@ -15,6 +15,7 @@ REQUIREMENTS
  6. This module requires the following modules:
  7. * Simplenews (https://www.drupal.org/project/simplenews)
  8. + * Mailet (https://www.drupal.org/project/Mailjet)
  9. INSTALLATION
  10. ------------
  11. diff --git a/simplenews_mailjet_subscriptions.info.yml b/simplenews_mailjet_subscriptions.info.yml
  12. index 2512a39..d4b0440 100644
  13. --- a/simplenews_mailjet_subscriptions.info.yml
  14. +++ b/simplenews_mailjet_subscriptions.info.yml
  15. @@ -7,3 +7,4 @@ package: Mail
  16. configure: simplenews_mailjet_subscription.list
  17. dependencies:
  18. - simplenews:simplenews
  19. + - mailjet:mailjet
  20. diff --git a/simplenews_mailjet_subscriptions.module b/simplenews_mailjet_subscriptions.module
  21. index 09abe13..4b2ee58 100644
  22. --- a/simplenews_mailjet_subscriptions.module
  23. +++ b/simplenews_mailjet_subscriptions.module
  24. @@ -7,7 +7,7 @@
  25. use Drupal\Core\Routing\RouteMatchInterface;
  26. use Drupal\simplenews\Entity\Subscriber;
  27. -use Drupal\simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsUtilities;
  28. +// use Drupal\simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsUtilities;
  29. use Drupal\simplenews_mailjet_subscriptions\Entity\SimplenewsMailjetSubscriptionEntity;
  30. use Drupal\Component\Utility\Html;
  31. @@ -24,18 +24,18 @@ use Drupal\Component\Utility\Html;
  32. function simplenews_mailjet_subscriptions_simplenews_subscribe(Subscriber $subscriber, string $subscription) {
  33. $entities = SimplenewsMailjetSubscriptionEntity::loadMultiple();
  34. foreach ((array) $entities as $entity) {
  35. - $api_key = $entity->getApiKey();
  36. - $secret_key = $entity->getSecretkey();
  37. $listID = $entity->getID();
  38. $table = $entity->get('mapping_table');
  39. if (is_array($table)) {
  40. foreach ($table as $category) {
  41. if ($category['simplenews_news'] == $subscription) {
  42. - SimplenewsMailjetSubscriptionsUtilities::subscribeUser($subscriber, $subscription, $category, $api_key, $secret_key, $listID);
  43. + // SimplenewsMailjetSubscriptionsUtilities::subscribeUser($subscriber, $category);
  44. + \Drupal::service('simplenews_mailjet_subscriptions.utilities')->subscribeUser($listID, $subscriber, $category);
  45. }
  46. }
  47. }
  48. }
  49. + // \Drupal::service('simplenews_mailjet_subscriptions.utilities')->subscribeUser($subscriber, $subscription);
  50. }
  51. /**
  52. @@ -51,19 +51,19 @@ function simplenews_mailjet_subscriptions_simplenews_subscribe(Subscriber $subsc
  53. function simplenews_mailjet_subscriptions_simplenews_unsubscribe(Subscriber $subscriber, string $subscription) {
  54. $entities = SimplenewsMailjetSubscriptionEntity::loadMultiple();
  55. foreach ((array) $entities as $entity) {
  56. - $api_key = $entity->getApiKey();
  57. - $secret_key = $entity->getSecretkey();
  58. $listID = $entity->getID();
  59. $table = $entity->get('mapping_table');
  60. if (is_array($table)) {
  61. foreach ($table as $category) {
  62. if ($category['simplenews_news'] == $subscription) {
  63. // Unsubscribe.
  64. - SimplenewsMailjetSubscriptionsUtilities::unsubscribeUser($subscriber, $subscription, $category, $api_key, $secret_key, $listID);
  65. + // SimplenewsMailjetSubscriptionsUtilities::unsubscribeUser($subscriber, $category);
  66. + \Drupal::service('simplenews_mailjet_subscriptions.utilities')->unsubscribeUser($listID, $subscriber, $category);
  67. }
  68. }
  69. }
  70. }
  71. + // \Drupal::service('simplenews_mailjet_subscriptions.utilities')->unsubscribeUser($subscriber, $subscription);
  72. }
  73. /**
  74. diff --git a/simplenews_mailjet_subscriptions.services.yml b/simplenews_mailjet_subscriptions.services.yml
  75. new file mode 100644
  76. index 0000000..cee6a81
  77. --- /dev/null
  78. +++ b/simplenews_mailjet_subscriptions.services.yml
  79. @@ -0,0 +1,7 @@
  80. +services:
  81. + simplenews_mailjet_subscriptions.handler:
  82. + class: Drupal\simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsMailjetHandler
  83. + arguments: ['@mailjet.client_factory']
  84. + simplenews_mailjet_subscriptions.utilities:
  85. + class: Drupal\simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsUtilities
  86. + arguments: [ '@simplenews_mailjet_subscriptions.handler' ]
  87. diff --git a/src/Entity/SimplenewsMailjetSubscriptionEntity.php b/src/Entity/SimplenewsMailjetSubscriptionEntity.php
  88. index 27b7737..34c8df5 100644
  89. --- a/src/Entity/SimplenewsMailjetSubscriptionEntity.php
  90. +++ b/src/Entity/SimplenewsMailjetSubscriptionEntity.php
  91. @@ -30,8 +30,6 @@ use Drupal\Core\Config\Entity\ConfigEntityBase;
  92. * config_export = {
  93. * "id",
  94. * "title",
  95. - * "api_key",
  96. - * "secret_key",
  97. * "mapping_table"
  98. * }
  99. * )
  100. @@ -94,53 +92,53 @@ class SimplenewsMailjetSubscriptionEntity extends ConfigEntityBase implements Si
  101. return $this->get('id');
  102. }
  103. - /**
  104. - * Returns the entity api key.
  105. - *
  106. - * @return string
  107. - * The entity api key.
  108. - */
  109. - public function getApiKey() {
  110. - return $this->get('api_key');
  111. - }
  112. -
  113. - /**
  114. - * Sets the entity api key.
  115. - *
  116. - * @param string $api_key
  117. - * Api key.
  118. - *
  119. - * @return $this
  120. - * The Simplenews Mailjet Subscription entity.
  121. - */
  122. - public function setApiKey($api_key) {
  123. - $this->set('api_key', $api_key);
  124. - return $this;
  125. - }
  126. -
  127. - /**
  128. - * Returns the entity secret key.
  129. - *
  130. - * @return string
  131. - * The entity secret key.
  132. - */
  133. - public function getSecretkey() {
  134. - return $this->get('secret_key');
  135. - }
  136. -
  137. - /**
  138. - * Sets the entity secret key.
  139. - *
  140. - * @param string $secret_key
  141. - * Secret key.
  142. - *
  143. - * @return $this
  144. - * The Simplenews Mailjet Subscription entity.
  145. - */
  146. - public function setSecretkey($secret_key) {
  147. - $this->set('secret_key', $secret_key);
  148. - return $this;
  149. - }
  150. + // /**
  151. + // * Returns the entity api key.
  152. + // *
  153. + // * @return string
  154. + // * The entity api key.
  155. + // */
  156. + // public function getApiKey() {
  157. + // return $this->get('api_key');
  158. + // }
  159. +
  160. + // /**
  161. + // * Sets the entity api key.
  162. + // *
  163. + // * @param string $api_key
  164. + // * Api key.
  165. + // *
  166. + // * @return $this
  167. + // * The Simplenews Mailjet Subscription entity.
  168. + // */
  169. + // public function setApiKey($api_key) {
  170. + // $this->set('api_key', $api_key);
  171. + // return $this;
  172. + // }
  173. +
  174. + // /**
  175. + // * Returns the entity secret key.
  176. + // *
  177. + // * @return string
  178. + // * The entity secret key.
  179. + // */
  180. + // public function getSecretkey() {
  181. + // return $this->get('secret_key');
  182. + // }
  183. +
  184. + // /**
  185. + // * Sets the entity secret key.
  186. + // *
  187. + // * @param string $secret_key
  188. + // * Secret key.
  189. + // *
  190. + // * @return $this
  191. + // * The Simplenews Mailjet Subscription entity.
  192. + // */
  193. + // public function setSecretkey($secret_key) {
  194. + // $this->set('secret_key', $secret_key);
  195. + // return $this;
  196. + // }
  197. /**
  198. * Sets the entity mapping table.
  199. diff --git a/src/Form/SimplenewsMailjetSubscriptionAddForm.php b/src/Form/SimplenewsMailjetSubscriptionAddForm.php
  200. index 836bee3..bc70090 100644
  201. --- a/src/Form/SimplenewsMailjetSubscriptionAddForm.php
  202. +++ b/src/Form/SimplenewsMailjetSubscriptionAddForm.php
  203. @@ -64,20 +64,20 @@ class SimplenewsMailjetSubscriptionAddForm extends EntityForm {
  204. 'exists' => [$this, 'exist'],
  205. ],
  206. ];
  207. - $form['api_key'] = [
  208. - '#type' => 'textfield',
  209. - '#title' => $this->t('API Key'),
  210. - '#maxlength' => 32,
  211. - '#description' => $this->t('Mailjet Account API Key'),
  212. - '#required' => TRUE,
  213. - ];
  214. - $form['secret_key'] = [
  215. - '#type' => 'password',
  216. - '#title' => $this->t('Secret Key'),
  217. - '#maxlength' => 32,
  218. - '#description' => $this->t('Mailjet Account Secret Key'),
  219. - '#required' => TRUE,
  220. - ];
  221. + // $form['api_key'] = [
  222. + // '#type' => 'textfield',
  223. + // '#title' => $this->t('API Key'),
  224. + // '#maxlength' => 32,
  225. + // '#description' => $this->t('Mailjet Account API Key'),
  226. + // '#required' => TRUE,
  227. + // ];
  228. + // $form['secret_key'] = [
  229. + // '#type' => 'password',
  230. + // '#title' => $this->t('Secret Key'),
  231. + // '#maxlength' => 32,
  232. + // '#description' => $this->t('Mailjet Account Secret Key'),
  233. + // '#required' => TRUE,
  234. + // ];
  235. return $form;
  236. }
  237. diff --git a/src/Form/SimplenewsMailjetSubscriptionEditForm.php b/src/Form/SimplenewsMailjetSubscriptionEditForm.php
  238. index 98342b5..f67ba96 100644
  239. --- a/src/Form/SimplenewsMailjetSubscriptionEditForm.php
  240. +++ b/src/Form/SimplenewsMailjetSubscriptionEditForm.php
  241. @@ -76,20 +76,20 @@ class SimplenewsMailjetSubscriptionEditForm extends EntityForm {
  242. '#help' => $this->t('Configuration title'),
  243. '#required' => TRUE,
  244. ];
  245. - $form['api_key'] = [
  246. - '#type' => 'textfield',
  247. - '#title' => $this->t('API Key'),
  248. - '#maxlength' => 32,
  249. - '#default_value' => $this->entity->getApiKey(),
  250. - '#help' => $this->t('Mailjet Account API Key'),
  251. - '#required' => TRUE,
  252. - '#disabled' => !$this->entity->isNew(),
  253. - ];
  254. -
  255. - if ($this->entity->getApiKey() && $this->entity->getSecretkey()) {
  256. + // $form['api_key'] = [
  257. + // '#type' => 'textfield',
  258. + // '#title' => $this->t('API Key'),
  259. + // '#maxlength' => 32,
  260. + // '#default_value' => $this->entity->getApiKey(),
  261. + // '#help' => $this->t('Mailjet Account API Key'),
  262. + // '#required' => TRUE,
  263. + // '#disabled' => !$this->entity->isNew(),
  264. + // ];
  265. +
  266. + // if ($this->entity->getApiKey() && $this->entity->getSecretkey()) {
  267. $this->constructTable($form, $form_state);
  268. - }
  269. + // }
  270. $form['add_subscription'] = [
  271. '#type' => 'submit',
  272. diff --git a/src/SimplenewsMailjetSubscriptionsInterface.php b/src/SimplenewsMailjetSubscriptionsInterface.php
  273. index c727d26..37c082d 100644
  274. --- a/src/SimplenewsMailjetSubscriptionsInterface.php
  275. +++ b/src/SimplenewsMailjetSubscriptionsInterface.php
  276. @@ -36,43 +36,43 @@ interface SimplenewsMailjetSubscriptionsInterface extends ConfigEntityInterface
  277. */
  278. public function getId();
  279. - /**
  280. - * Returns the entity api key.
  281. - *
  282. - * @return string
  283. - * The entity api key.
  284. - */
  285. - public function getApiKey();
  286. + // /**
  287. + // * Returns the entity api key.
  288. + // *
  289. + // * @return string
  290. + // * The entity api key.
  291. + // */
  292. + // public function getApiKey();
  293. - /**
  294. - * Sets the entity api key.
  295. - *
  296. - * @param string $api_key
  297. - * Api key.
  298. - *
  299. - * @return $this
  300. - * The Simplenews Mailjet Subscription entity.
  301. - */
  302. - public function setApiKey($api_key);
  303. + // /**
  304. + // * Sets the entity api key.
  305. + // *
  306. + // * @param string $api_key
  307. + // * Api key.
  308. + // *
  309. + // * @return $this
  310. + // * The Simplenews Mailjet Subscription entity.
  311. + // */
  312. + // public function setApiKey($api_key);
  313. - /**
  314. - * Returns the entity secret key.
  315. - *
  316. - * @return string
  317. - * The entity secret key.
  318. - */
  319. - public function getSecretkey();
  320. + // /**
  321. + // * Returns the entity secret key.
  322. + // *
  323. + // * @return string
  324. + // * The entity secret key.
  325. + // */
  326. + // public function getSecretkey();
  327. - /**
  328. - * Sets the entity secret key.
  329. - *
  330. - * @param string $secret_key
  331. - * Secret key.
  332. - *
  333. - * @return $this
  334. - * The Simplenews Mailjet Subscription entity.
  335. - */
  336. - public function setSecretkey($secret_key);
  337. + // /**
  338. + // * Sets the entity secret key.
  339. + // *
  340. + // * @param string $secret_key
  341. + // * Secret key.
  342. + // *
  343. + // * @return $this
  344. + // * The Simplenews Mailjet Subscription entity.
  345. + // */
  346. + // public function setSecretkey($secret_key);
  347. /**
  348. * Sets the entity mapping table.
  349. diff --git a/src/SimplenewsMailjetSubscriptionsMailjetHandler.php b/src/SimplenewsMailjetSubscriptionsMailjetHandler.php
  350. new file mode 100644
  351. index 0000000..a68cbf1
  352. --- /dev/null
  353. +++ b/src/SimplenewsMailjetSubscriptionsMailjetHandler.php
  354. @@ -0,0 +1,47 @@
  355. +<?php
  356. +
  357. +namespace Drupal\simplenews_mailjet_subscriptions;
  358. +
  359. +use Mailjet\Resources;
  360. +use Drupal\mailjet\MailjetHandler;
  361. +
  362. +/**
  363. + * Implement SimplenewsMailjetSubscriptionsMailjetHandler interface.
  364. + */
  365. +class SimplenewsMailjetSubscriptionsMailjetHandler extends MailjetHandler implements SimplenewsMailjetSubscriptionsMailjetHandlerInterface{
  366. +
  367. + /**
  368. + * {@inheritdoc}
  369. + *
  370. + * @return array|null
  371. + * Return array with result or null.
  372. + */
  373. + public function createMailjetContact($email): ?array {
  374. +
  375. + $response = $this->mailjetClient->post(Resources::$Contact, ["body" => ['Email' => $email]]);
  376. +
  377. + if ($response->success()) {
  378. + return $response->getData();
  379. + }
  380. +
  381. + return NULL;
  382. + }
  383. +
  384. + /**
  385. + * {@inheritdoc}
  386. + *
  387. + * @return array|null
  388. + * Return array with result or null.
  389. + */
  390. + public function getMailjetContactByEmail($email): ?array {
  391. +
  392. + $response = $this->mailjetClient->get(Resources::$Contact, ['id' => $email]);
  393. +
  394. + if ($response->success()) {
  395. + return $response->getData();
  396. + }
  397. +
  398. + return NULL;
  399. + }
  400. +
  401. +}
  402. \ No newline at end of file
  403. diff --git a/src/SimplenewsMailjetSubscriptionsMailjetHandlerInterface.php b/src/SimplenewsMailjetSubscriptionsMailjetHandlerInterface.php
  404. new file mode 100644
  405. index 0000000..b6484d7
  406. --- /dev/null
  407. +++ b/src/SimplenewsMailjetSubscriptionsMailjetHandlerInterface.php
  408. @@ -0,0 +1,34 @@
  409. +<?php
  410. +
  411. +namespace Drupal\simplenews_mailjet_subscriptions;
  412. +
  413. +use Drupal\mailjet\MailjetHandlerInterface;
  414. +
  415. +/**
  416. + * Interface for mailjet handler.
  417. + */
  418. +interface SimplenewsMailjetSubscriptionsMailjetHandlerInterface extends MailjetHandlerInterface {
  419. +
  420. + /**
  421. + * create contact.
  422. + *
  423. + * @param string $email
  424. + * email of the contact
  425. + *
  426. + * @return array|null
  427. + * Return array with the result or null.
  428. + */
  429. + public function createMailjetContact($email): ?array;
  430. +
  431. + /**
  432. + * Get contact by email.
  433. + *
  434. + * @param string $email
  435. + * email of the contact
  436. + *
  437. + * @return array|null
  438. + * Return array with the result or null.
  439. + */
  440. + public function getMailjetContactByEmail($email): ?array;
  441. +
  442. +}
  443. \ No newline at end of file
  444. diff --git a/src/SimplenewsMailjetSubscriptionsUtilities.php b/src/SimplenewsMailjetSubscriptionsUtilities.php
  445. index 950ff8a..82e15b5 100644
  446. --- a/src/SimplenewsMailjetSubscriptionsUtilities.php
  447. +++ b/src/SimplenewsMailjetSubscriptionsUtilities.php
  448. @@ -2,141 +2,122 @@
  449. namespace Drupal\simplenews_mailjet_subscriptions;
  450. +// use Drupal\mailjet\MailjetHandlerInterface;
  451. +use Drupal\simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsMailjetHandlerInterface;
  452. +
  453. /**
  454. - * Provides a list of Simplenews Mailjet Subscription entities.
  455. + * Utilisties
  456. */
  457. class SimplenewsMailjetSubscriptionsUtilities {
  458. /**
  459. - * Check Subscriber.
  460. + * MailjetHandler service.
  461. + *
  462. + * @var \Drupal\Simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsMailjetHandlerInterface
  463. + */
  464. + protected $mailjetHandler;
  465. +
  466. +/**
  467. + * SubscribeEmailForm constructor.
  468. + *
  469. + * @param \Drupal\Simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsMailjetHandlerInterface $mailjetHandler
  470. + * The mailjet handler service.
  471. */
  472. - private static function checkSubscriber($api_key, $secret_key, $encoded_mail) {
  473. -
  474. - // Check if contact exist
  475. - // verifica se o contacto ja tem opções.
  476. - $endpoint = 'https://api.mailjet.com/v3/REST/contactdata/' . $encoded_mail;
  477. - $ch = curl_init();
  478. - curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
  479. - curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  480. - curl_setopt($ch, CURLOPT_USERPWD, $api_key . ':' . $secret_key);
  481. - curl_setopt($ch, CURLOPT_URL, $endpoint);
  482. - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  483. - $response = curl_exec($ch);
  484. - curl_close($ch);
  485. - $json = json_decode($response, TRUE);
  486. -
  487. - return $json;
  488. + public function __construct(SimplenewsMailjetSubscriptionsMailjetHandlerInterface $mailjetHandler) {
  489. + $this->mailjetHandler = $mailjetHandler;
  490. }
  491. /**
  492. - * Update Subscriber.
  493. + * {@inheritDoc}
  494. */
  495. - private static function updateSubscriber($api_key, $secret_key, $listID, $subscriber_email, $contact_property, $newsletter_property_name, $action) {
  496. -
  497. - // Add contact to mailjet.
  498. - $endpoint = 'https://api.mailjet.com/v3/REST/contactslist/' . $listID . '/managecontact';
  499. - $message = [
  500. - "Properties" => [
  501. - $contact_property => $newsletter_property_name,
  502. - ],
  503. - "Action" => $action,
  504. - "Email" => $subscriber_email,
  505. - ];
  506. - $payload = json_encode($message);
  507. -
  508. - $ch = curl_init();
  509. - curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
  510. - curl_setopt($ch, CURLOPT_POST, 1);
  511. - curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  512. - curl_setopt($ch, CURLOPT_USERPWD, $api_key . ':' . $secret_key);
  513. - curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
  514. - curl_setopt($ch, CURLOPT_URL, $endpoint);
  515. - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  516. - $response = curl_exec($ch);
  517. - curl_close($ch);
  518. - $json = json_decode($response, TRUE);
  519. -
  520. - return $json;
  521. + public static function create(ContainerInterface $container) {
  522. + return new static(
  523. + $container->get('simplenews_mailjet_subscriptions.handler')
  524. + );
  525. + }
  526. +
  527. + public function checkSubscriber($encoded_mail) {
  528. + $response = $this->mailjetHandler->getMailjetContactByEmail($encoded_mail);
  529. + if ($response) {
  530. + // contact alredy exists
  531. + $contact_id = $response[0]['ID'];
  532. + return $contact_id;
  533. + }
  534. +
  535. + return null;
  536. + }
  537. +
  538. + public function createSubscriber($email) {
  539. + $response = $this->mailjetHandler->createMailjetContact($email);
  540. + if ($response) {
  541. + // contact alredy exists
  542. + $contact_id = $response[0]['ID'];
  543. + return $contact_id;
  544. + }
  545. +
  546. + return null;
  547. }
  548. /**
  549. * Subscribe User.
  550. */
  551. - public static function subscribeUser($subscriber, $subscription, $category, $api_key, $secret_key, $listID) {
  552. + public function subscribeUser($listID, $subscriber, $category) {
  553. $contact_property = $category['contact_property'];
  554. $newsletter_property_name = $category['newsletter_property_name'];
  555. $subscriber_email = $subscriber->getMail();
  556. $encoded_mail = urlencode($subscriber_email);
  557. - $response = '';
  558. -
  559. - $contact_info = self::checkSubscriber($api_key, $secret_key, $encoded_mail);
  560. -
  561. - $options = $contact_info['Data'];
  562. - // Se existe - acrescenta a opções.
  563. - if ($options[0]['Data']) {
  564. - foreach ($options[0]['Data'] as $property) {
  565. - if ($property["Name"] == $contact_property) {
  566. + $contactid = $this->checkSubscriber($encoded_mail);
  567. + if (!$contactid) {
  568. + // subscriber does not exists yet, create it
  569. + $contactid = $this->createSubscriber($subscriber_email);
  570. + }
  571. - if (is_int(strpos($property['Value'], $newsletter_property_name))) {
  572. - $newsletter_property_name = $property['Value'];
  573. - }
  574. - else {
  575. - $newsletter_property_name = $property['Value'] . ';' . $newsletter_property_name;
  576. - }
  577. - }
  578. + if ($contactid) {
  579. + $contact = [
  580. + 'Email' => $subscriber_email,
  581. + // 'Properties' => [
  582. + // $contact_property => $newsletter_property_name
  583. + // ]
  584. + ];
  585. +
  586. + $response = $this->mailjetHandler->syncMailjetContact($listID, $contact, 'addnoforce');
  587. + if ($response) {
  588. + // $contact_id = $response['Data'][0]['ContactID'];
  589. + \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $subscriber_email . ' subscribed to the ' . $listID . ' list.');
  590. + }else{
  591. + \Drupal::logger('simplenews_mailjet_subscriptions')->warning('error while subscrining ' . $subscriber_email . ' to the ' . $listID . ' list.');
  592. }
  593. +
  594. + } else {
  595. + \Drupal::logger('simplenews_mailjet_subscriptions')->warning($subscriber_email . ' does not exists and can not be created on mailjet.');
  596. }
  597. - $response = self::updateSubscriber($api_key, $secret_key, $listID, $subscriber_email, $contact_property, $newsletter_property_name, 'addnoforce');
  598. - $contact_id = $response['Data'][0]['ContactID'];
  599. - \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $contact_id . ' subscribed to the ' . $contact_property . ' properties with the ' . $newsletter_property_name . ' values in the list ' . $listID . '.');
  600. - return $response;
  601. +
  602. }
  603. /**
  604. * Unsubscribe User.
  605. */
  606. - public static function unsubscribeUser($subscriber, $subscription, $category, $api_key, $secret_key, $listID) {
  607. + public function unsubscribeUser($listID, $subscriber, $category) {
  608. $contact_property = $category['contact_property'];
  609. $newsletter_property_name = $category['newsletter_property_name'];
  610. $subscriber_email = $subscriber->getMail();
  611. - $encoded_mail = urlencode($subscriber_email);
  612. - $response = '';
  613. -
  614. - $contact_info = self::checkSubscriber($api_key, $secret_key, $encoded_mail);
  615. - $contact_id = $contact_info['ContactID'];
  616. - $options = $contact_info['Data'];
  617. -
  618. - if ($options[0]['Data']) {
  619. - foreach ($options[0]['Data'] as $property) {
  620. - if ($property["Name"] == $contact_property) {
  621. - if (substr_count($property['Value'], ';') == 0) {
  622. - $newsletter_property_name = '';
  623. - $response = self::updateSubscriber($api_key, $secret_key, $listID, $subscriber_email, $contact_property, $newsletter_property_name, 'remove');
  624. - $contact_id = $response['Data'][0]['ContactID'];
  625. - \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $contact_id . ' unsubscribed to the list ' . $listID . '.');
  626. - }
  627. - else {
  628. - $pos = strpos($property['Value'], $newsletter_property_name);
  629. - \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $contact_id . ' unsubscribed to the ' . $contact_property . ' properties with the ' . $newsletter_property_name . ' values in the list ' . $listID . '.');
  630. - if ($pos === 0) {
  631. - $newsletter_property_name = str_replace($newsletter_property_name . ';', '', $property['Value']);
  632. - }
  633. - else {
  634. - $newsletter_property_name = str_replace(';' . $newsletter_property_name, '', $property['Value']);
  635. - }
  636. - $response = self::updateSubscriber($api_key, $secret_key, $listID, $subscriber_email, $contact_property, $newsletter_property_name, 'addnoforce');
  637. - $contact_id = $response['Data'][0]['ContactID'];
  638. - \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $contact_id . 'remains subscribed to the ' . $contact_property . ' properties with the ' . $newsletter_property_name . ' values in the list ' . $listID . '.');
  639. -
  640. - }
  641. - }
  642. - }
  643. - }
  644. + $contact = [
  645. + 'Email' => $subscriber_email,
  646. + // 'Properties' => [
  647. + // $contact_property => $newsletter_property_name
  648. + // ]
  649. + ];
  650. - return $response;
  651. + $response = $this->mailjetHandler->syncMailjetContact($listID, $contact, 'remove');
  652. + if ($response) {
  653. + // $contact_id = $response['Data'][0]['ContactID'];
  654. + \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $subscriber_email . ' removed from the ' . $listID . ' list.');
  655. + }else{
  656. + \Drupal::logger('simplenews_mailjet_subscriptions')->warning('error while removing ' . $subscriber_email . ' from the ' . $listID . ' list.');
  657. + }
  658. }
  659. }