simplenews_mailjet_subscriptions__usingmailjetmodule.patch 26 KB

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