simplenews_mailjet_subscriptions__usingmailjetmodule.patch 25 KB

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