simplenews_mailjet_subscriptions__usingmailjetmodule#2.patch 27 KB

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