simplenews_mailjet_subscriptions__usingmailjetmodule.patch 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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/Plugin/RulesAction/UnsubscribeFromList.php b/src/Plugin/RulesAction/UnsubscribeFromList.php
  294. new file mode 100644
  295. index 0000000..b880ed2
  296. --- /dev/null
  297. +++ b/src/Plugin/RulesAction/UnsubscribeFromList.php
  298. @@ -0,0 +1,49 @@
  299. +<?php
  300. +
  301. +namespace Drupal\simplenews_mailjet_subscriptions\Plugin\RulesAction;
  302. +
  303. +use Drupal\rules\Core\RulesActionBase;
  304. +use Drupal\simplenews\SubscriberInterface;
  305. +
  306. +/**
  307. + * Provides a 'Unsubscribe' action.
  308. + *
  309. + * @RulesAction(
  310. + * id = "rules_simplenews_mailjet_subscriptions_unsubscribe_from_list",
  311. + * label = @Translation("Unsubscribe from newsletter, giving mailjet listID and email"),
  312. + * category = @Translation("Simplenews Mailjet Subscriptions"),
  313. + * context_definitions = {
  314. + * "listID" = @ContextDefinition("integer",
  315. + * label = @Translation("Mailjet list ID"),
  316. + * description = @Translation("Specifies the mailjet list ID from which simplenews list will be found and email will be unsubscribed from.")
  317. + * ),
  318. + * "email" = @ContextDefinition("string",
  319. + * label = @Translation("Subscriber email"),
  320. + * description = @Translation("Specifies the email to unsubscribe.")
  321. + * )
  322. + * }
  323. + * )
  324. + */
  325. +class UnsubscribeFromList extends RulesActionBase {
  326. +
  327. + /**
  328. + * Deletes the Entity.
  329. + *
  330. + * @param integer listID
  331. + * The mailjet list to unsubcsribe from.
  332. + *
  333. + * @param string email
  334. + * The email to unsubcribe.
  335. + */
  336. + protected function doExecute(integer $listID, string $email) {
  337. + $l = $listID;
  338. + $e = $email;
  339. + \Drupal::logger('simplenews_mailjet_subscriptions')->notice("Unsubscribe webhook triggered for listID: " . $listID . ", and email: " . $email . ".");
  340. + // find corresponding simplenews list from mailjet listID
  341. +
  342. + // get subscriber entity
  343. +
  344. + // unsubscribe subscriber from simplenews list
  345. + }
  346. +
  347. +}
  348. \ No newline at end of file
  349. diff --git a/src/SimplenewsMailjetSubscriptionsInterface.php b/src/SimplenewsMailjetSubscriptionsInterface.php
  350. index c727d26..37c082d 100644
  351. --- a/src/SimplenewsMailjetSubscriptionsInterface.php
  352. +++ b/src/SimplenewsMailjetSubscriptionsInterface.php
  353. @@ -36,43 +36,43 @@ interface SimplenewsMailjetSubscriptionsInterface extends ConfigEntityInterface
  354. */
  355. public function getId();
  356. - /**
  357. - * Returns the entity api key.
  358. - *
  359. - * @return string
  360. - * The entity api key.
  361. - */
  362. - public function getApiKey();
  363. + // /**
  364. + // * Returns the entity api key.
  365. + // *
  366. + // * @return string
  367. + // * The entity api key.
  368. + // */
  369. + // public function getApiKey();
  370. - /**
  371. - * Sets the entity api key.
  372. - *
  373. - * @param string $api_key
  374. - * Api key.
  375. - *
  376. - * @return $this
  377. - * The Simplenews Mailjet Subscription entity.
  378. - */
  379. - public function setApiKey($api_key);
  380. + // /**
  381. + // * Sets the entity api key.
  382. + // *
  383. + // * @param string $api_key
  384. + // * Api key.
  385. + // *
  386. + // * @return $this
  387. + // * The Simplenews Mailjet Subscription entity.
  388. + // */
  389. + // public function setApiKey($api_key);
  390. - /**
  391. - * Returns the entity secret key.
  392. - *
  393. - * @return string
  394. - * The entity secret key.
  395. - */
  396. - public function getSecretkey();
  397. + // /**
  398. + // * Returns the entity secret key.
  399. + // *
  400. + // * @return string
  401. + // * The entity secret key.
  402. + // */
  403. + // public function getSecretkey();
  404. - /**
  405. - * Sets the entity secret key.
  406. - *
  407. - * @param string $secret_key
  408. - * Secret key.
  409. - *
  410. - * @return $this
  411. - * The Simplenews Mailjet Subscription entity.
  412. - */
  413. - public function setSecretkey($secret_key);
  414. + // /**
  415. + // * Sets the entity secret key.
  416. + // *
  417. + // * @param string $secret_key
  418. + // * Secret key.
  419. + // *
  420. + // * @return $this
  421. + // * The Simplenews Mailjet Subscription entity.
  422. + // */
  423. + // public function setSecretkey($secret_key);
  424. /**
  425. * Sets the entity mapping table.
  426. diff --git a/src/SimplenewsMailjetSubscriptionsMailjetHandler.php b/src/SimplenewsMailjetSubscriptionsMailjetHandler.php
  427. new file mode 100644
  428. index 0000000..a68cbf1
  429. --- /dev/null
  430. +++ b/src/SimplenewsMailjetSubscriptionsMailjetHandler.php
  431. @@ -0,0 +1,47 @@
  432. +<?php
  433. +
  434. +namespace Drupal\simplenews_mailjet_subscriptions;
  435. +
  436. +use Mailjet\Resources;
  437. +use Drupal\mailjet\MailjetHandler;
  438. +
  439. +/**
  440. + * Implement SimplenewsMailjetSubscriptionsMailjetHandler interface.
  441. + */
  442. +class SimplenewsMailjetSubscriptionsMailjetHandler extends MailjetHandler implements SimplenewsMailjetSubscriptionsMailjetHandlerInterface{
  443. +
  444. + /**
  445. + * {@inheritdoc}
  446. + *
  447. + * @return array|null
  448. + * Return array with result or null.
  449. + */
  450. + public function createMailjetContact($email): ?array {
  451. +
  452. + $response = $this->mailjetClient->post(Resources::$Contact, ["body" => ['Email' => $email]]);
  453. +
  454. + if ($response->success()) {
  455. + return $response->getData();
  456. + }
  457. +
  458. + return NULL;
  459. + }
  460. +
  461. + /**
  462. + * {@inheritdoc}
  463. + *
  464. + * @return array|null
  465. + * Return array with result or null.
  466. + */
  467. + public function getMailjetContactByEmail($email): ?array {
  468. +
  469. + $response = $this->mailjetClient->get(Resources::$Contact, ['id' => $email]);
  470. +
  471. + if ($response->success()) {
  472. + return $response->getData();
  473. + }
  474. +
  475. + return NULL;
  476. + }
  477. +
  478. +}
  479. \ No newline at end of file
  480. diff --git a/src/SimplenewsMailjetSubscriptionsMailjetHandlerInterface.php b/src/SimplenewsMailjetSubscriptionsMailjetHandlerInterface.php
  481. new file mode 100644
  482. index 0000000..b6484d7
  483. --- /dev/null
  484. +++ b/src/SimplenewsMailjetSubscriptionsMailjetHandlerInterface.php
  485. @@ -0,0 +1,34 @@
  486. +<?php
  487. +
  488. +namespace Drupal\simplenews_mailjet_subscriptions;
  489. +
  490. +use Drupal\mailjet\MailjetHandlerInterface;
  491. +
  492. +/**
  493. + * Interface for mailjet handler.
  494. + */
  495. +interface SimplenewsMailjetSubscriptionsMailjetHandlerInterface extends MailjetHandlerInterface {
  496. +
  497. + /**
  498. + * create contact.
  499. + *
  500. + * @param string $email
  501. + * email of the contact
  502. + *
  503. + * @return array|null
  504. + * Return array with the result or null.
  505. + */
  506. + public function createMailjetContact($email): ?array;
  507. +
  508. + /**
  509. + * Get contact by email.
  510. + *
  511. + * @param string $email
  512. + * email of the contact
  513. + *
  514. + * @return array|null
  515. + * Return array with the result or null.
  516. + */
  517. + public function getMailjetContactByEmail($email): ?array;
  518. +
  519. +}
  520. \ No newline at end of file
  521. diff --git a/src/SimplenewsMailjetSubscriptionsUtilities.php b/src/SimplenewsMailjetSubscriptionsUtilities.php
  522. index 950ff8a..82e15b5 100644
  523. --- a/src/SimplenewsMailjetSubscriptionsUtilities.php
  524. +++ b/src/SimplenewsMailjetSubscriptionsUtilities.php
  525. @@ -2,141 +2,122 @@
  526. namespace Drupal\simplenews_mailjet_subscriptions;
  527. +// use Drupal\mailjet\MailjetHandlerInterface;
  528. +use Drupal\simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsMailjetHandlerInterface;
  529. +
  530. /**
  531. - * Provides a list of Simplenews Mailjet Subscription entities.
  532. + * Utilisties
  533. */
  534. class SimplenewsMailjetSubscriptionsUtilities {
  535. /**
  536. - * Check Subscriber.
  537. + * MailjetHandler service.
  538. + *
  539. + * @var \Drupal\Simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsMailjetHandlerInterface
  540. + */
  541. + protected $mailjetHandler;
  542. +
  543. +/**
  544. + * SubscribeEmailForm constructor.
  545. + *
  546. + * @param \Drupal\Simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsMailjetHandlerInterface $mailjetHandler
  547. + * The mailjet handler service.
  548. */
  549. - private static function checkSubscriber($api_key, $secret_key, $encoded_mail) {
  550. -
  551. - // Check if contact exist
  552. - // verifica se o contacto ja tem opções.
  553. - $endpoint = 'https://api.mailjet.com/v3/REST/contactdata/' . $encoded_mail;
  554. - $ch = curl_init();
  555. - curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
  556. - curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  557. - curl_setopt($ch, CURLOPT_USERPWD, $api_key . ':' . $secret_key);
  558. - curl_setopt($ch, CURLOPT_URL, $endpoint);
  559. - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  560. - $response = curl_exec($ch);
  561. - curl_close($ch);
  562. - $json = json_decode($response, TRUE);
  563. -
  564. - return $json;
  565. + public function __construct(SimplenewsMailjetSubscriptionsMailjetHandlerInterface $mailjetHandler) {
  566. + $this->mailjetHandler = $mailjetHandler;
  567. }
  568. /**
  569. - * Update Subscriber.
  570. + * {@inheritDoc}
  571. */
  572. - private static function updateSubscriber($api_key, $secret_key, $listID, $subscriber_email, $contact_property, $newsletter_property_name, $action) {
  573. -
  574. - // Add contact to mailjet.
  575. - $endpoint = 'https://api.mailjet.com/v3/REST/contactslist/' . $listID . '/managecontact';
  576. - $message = [
  577. - "Properties" => [
  578. - $contact_property => $newsletter_property_name,
  579. - ],
  580. - "Action" => $action,
  581. - "Email" => $subscriber_email,
  582. - ];
  583. - $payload = json_encode($message);
  584. -
  585. - $ch = curl_init();
  586. - curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
  587. - curl_setopt($ch, CURLOPT_POST, 1);
  588. - curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  589. - curl_setopt($ch, CURLOPT_USERPWD, $api_key . ':' . $secret_key);
  590. - curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
  591. - curl_setopt($ch, CURLOPT_URL, $endpoint);
  592. - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  593. - $response = curl_exec($ch);
  594. - curl_close($ch);
  595. - $json = json_decode($response, TRUE);
  596. -
  597. - return $json;
  598. + public static function create(ContainerInterface $container) {
  599. + return new static(
  600. + $container->get('simplenews_mailjet_subscriptions.handler')
  601. + );
  602. + }
  603. +
  604. + public function checkSubscriber($encoded_mail) {
  605. + $response = $this->mailjetHandler->getMailjetContactByEmail($encoded_mail);
  606. + if ($response) {
  607. + // contact alredy exists
  608. + $contact_id = $response[0]['ID'];
  609. + return $contact_id;
  610. + }
  611. +
  612. + return null;
  613. + }
  614. +
  615. + public function createSubscriber($email) {
  616. + $response = $this->mailjetHandler->createMailjetContact($email);
  617. + if ($response) {
  618. + // contact alredy exists
  619. + $contact_id = $response[0]['ID'];
  620. + return $contact_id;
  621. + }
  622. +
  623. + return null;
  624. }
  625. /**
  626. * Subscribe User.
  627. */
  628. - public static function subscribeUser($subscriber, $subscription, $category, $api_key, $secret_key, $listID) {
  629. + public function subscribeUser($listID, $subscriber, $category) {
  630. $contact_property = $category['contact_property'];
  631. $newsletter_property_name = $category['newsletter_property_name'];
  632. $subscriber_email = $subscriber->getMail();
  633. $encoded_mail = urlencode($subscriber_email);
  634. - $response = '';
  635. -
  636. - $contact_info = self::checkSubscriber($api_key, $secret_key, $encoded_mail);
  637. -
  638. - $options = $contact_info['Data'];
  639. - // Se existe - acrescenta a opções.
  640. - if ($options[0]['Data']) {
  641. - foreach ($options[0]['Data'] as $property) {
  642. - if ($property["Name"] == $contact_property) {
  643. + $contactid = $this->checkSubscriber($encoded_mail);
  644. + if (!$contactid) {
  645. + // subscriber does not exists yet, create it
  646. + $contactid = $this->createSubscriber($subscriber_email);
  647. + }
  648. - if (is_int(strpos($property['Value'], $newsletter_property_name))) {
  649. - $newsletter_property_name = $property['Value'];
  650. - }
  651. - else {
  652. - $newsletter_property_name = $property['Value'] . ';' . $newsletter_property_name;
  653. - }
  654. - }
  655. + if ($contactid) {
  656. + $contact = [
  657. + 'Email' => $subscriber_email,
  658. + // 'Properties' => [
  659. + // $contact_property => $newsletter_property_name
  660. + // ]
  661. + ];
  662. +
  663. + $response = $this->mailjetHandler->syncMailjetContact($listID, $contact, 'addnoforce');
  664. + if ($response) {
  665. + // $contact_id = $response['Data'][0]['ContactID'];
  666. + \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $subscriber_email . ' subscribed to the ' . $listID . ' list.');
  667. + }else{
  668. + \Drupal::logger('simplenews_mailjet_subscriptions')->warning('error while subscrining ' . $subscriber_email . ' to the ' . $listID . ' list.');
  669. }
  670. +
  671. + } else {
  672. + \Drupal::logger('simplenews_mailjet_subscriptions')->warning($subscriber_email . ' does not exists and can not be created on mailjet.');
  673. }
  674. - $response = self::updateSubscriber($api_key, $secret_key, $listID, $subscriber_email, $contact_property, $newsletter_property_name, 'addnoforce');
  675. - $contact_id = $response['Data'][0]['ContactID'];
  676. - \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 . '.');
  677. - return $response;
  678. +
  679. }
  680. /**
  681. * Unsubscribe User.
  682. */
  683. - public static function unsubscribeUser($subscriber, $subscription, $category, $api_key, $secret_key, $listID) {
  684. + public function unsubscribeUser($listID, $subscriber, $category) {
  685. $contact_property = $category['contact_property'];
  686. $newsletter_property_name = $category['newsletter_property_name'];
  687. $subscriber_email = $subscriber->getMail();
  688. - $encoded_mail = urlencode($subscriber_email);
  689. - $response = '';
  690. -
  691. - $contact_info = self::checkSubscriber($api_key, $secret_key, $encoded_mail);
  692. - $contact_id = $contact_info['ContactID'];
  693. - $options = $contact_info['Data'];
  694. -
  695. - if ($options[0]['Data']) {
  696. - foreach ($options[0]['Data'] as $property) {
  697. - if ($property["Name"] == $contact_property) {
  698. - if (substr_count($property['Value'], ';') == 0) {
  699. - $newsletter_property_name = '';
  700. - $response = self::updateSubscriber($api_key, $secret_key, $listID, $subscriber_email, $contact_property, $newsletter_property_name, 'remove');
  701. - $contact_id = $response['Data'][0]['ContactID'];
  702. - \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $contact_id . ' unsubscribed to the list ' . $listID . '.');
  703. - }
  704. - else {
  705. - $pos = strpos($property['Value'], $newsletter_property_name);
  706. - \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 . '.');
  707. - if ($pos === 0) {
  708. - $newsletter_property_name = str_replace($newsletter_property_name . ';', '', $property['Value']);
  709. - }
  710. - else {
  711. - $newsletter_property_name = str_replace(';' . $newsletter_property_name, '', $property['Value']);
  712. - }
  713. - $response = self::updateSubscriber($api_key, $secret_key, $listID, $subscriber_email, $contact_property, $newsletter_property_name, 'addnoforce');
  714. - $contact_id = $response['Data'][0]['ContactID'];
  715. - \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 . '.');
  716. -
  717. - }
  718. - }
  719. - }
  720. - }
  721. + $contact = [
  722. + 'Email' => $subscriber_email,
  723. + // 'Properties' => [
  724. + // $contact_property => $newsletter_property_name
  725. + // ]
  726. + ];
  727. - return $response;
  728. + $response = $this->mailjetHandler->syncMailjetContact($listID, $contact, 'remove');
  729. + if ($response) {
  730. + // $contact_id = $response['Data'][0]['ContactID'];
  731. + \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $subscriber_email . ' removed from the ' . $listID . ' list.');
  732. + }else{
  733. + \Drupal::logger('simplenews_mailjet_subscriptions')->warning('error while removing ' . $subscriber_email . ' from the ' . $listID . ' list.');
  734. + }
  735. }
  736. }