simplenews_mailjet_subscriptions__usingmailjetmodule.patch 24 KB

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