simplenews_mailjet_subscriptions__usingmailjetmodule.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. diff --git a/simplenews_mailjet_subscriptions.info.yml b/simplenews_mailjet_subscriptions.info.yml
  2. index 2512a39..d4b0440 100644
  3. --- a/simplenews_mailjet_subscriptions.info.yml
  4. +++ b/simplenews_mailjet_subscriptions.info.yml
  5. @@ -7,3 +7,4 @@ package: Mail
  6. configure: simplenews_mailjet_subscription.list
  7. dependencies:
  8. - simplenews:simplenews
  9. + - mailjet:mailjet
  10. diff --git a/simplenews_mailjet_subscriptions.module b/simplenews_mailjet_subscriptions.module
  11. index 09abe13..4b2ee58 100644
  12. --- a/simplenews_mailjet_subscriptions.module
  13. +++ b/simplenews_mailjet_subscriptions.module
  14. @@ -7,7 +7,7 @@
  15. use Drupal\Core\Routing\RouteMatchInterface;
  16. use Drupal\simplenews\Entity\Subscriber;
  17. -use Drupal\simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsUtilities;
  18. +// use Drupal\simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsUtilities;
  19. use Drupal\simplenews_mailjet_subscriptions\Entity\SimplenewsMailjetSubscriptionEntity;
  20. use Drupal\Component\Utility\Html;
  21. @@ -24,18 +24,18 @@ use Drupal\Component\Utility\Html;
  22. function simplenews_mailjet_subscriptions_simplenews_subscribe(Subscriber $subscriber, string $subscription) {
  23. $entities = SimplenewsMailjetSubscriptionEntity::loadMultiple();
  24. foreach ((array) $entities as $entity) {
  25. - $api_key = $entity->getApiKey();
  26. - $secret_key = $entity->getSecretkey();
  27. $listID = $entity->getID();
  28. $table = $entity->get('mapping_table');
  29. if (is_array($table)) {
  30. foreach ($table as $category) {
  31. if ($category['simplenews_news'] == $subscription) {
  32. - SimplenewsMailjetSubscriptionsUtilities::subscribeUser($subscriber, $subscription, $category, $api_key, $secret_key, $listID);
  33. + // SimplenewsMailjetSubscriptionsUtilities::subscribeUser($subscriber, $category);
  34. + \Drupal::service('simplenews_mailjet_subscriptions.utilities')->subscribeUser($listID, $subscriber, $category);
  35. }
  36. }
  37. }
  38. }
  39. + // \Drupal::service('simplenews_mailjet_subscriptions.utilities')->subscribeUser($subscriber, $subscription);
  40. }
  41. /**
  42. @@ -51,19 +51,19 @@ function simplenews_mailjet_subscriptions_simplenews_subscribe(Subscriber $subsc
  43. function simplenews_mailjet_subscriptions_simplenews_unsubscribe(Subscriber $subscriber, string $subscription) {
  44. $entities = SimplenewsMailjetSubscriptionEntity::loadMultiple();
  45. foreach ((array) $entities as $entity) {
  46. - $api_key = $entity->getApiKey();
  47. - $secret_key = $entity->getSecretkey();
  48. $listID = $entity->getID();
  49. $table = $entity->get('mapping_table');
  50. if (is_array($table)) {
  51. foreach ($table as $category) {
  52. if ($category['simplenews_news'] == $subscription) {
  53. // Unsubscribe.
  54. - SimplenewsMailjetSubscriptionsUtilities::unsubscribeUser($subscriber, $subscription, $category, $api_key, $secret_key, $listID);
  55. + // SimplenewsMailjetSubscriptionsUtilities::unsubscribeUser($subscriber, $category);
  56. + \Drupal::service('simplenews_mailjet_subscriptions.utilities')->unsubscribeUser($listID, $subscriber, $category);
  57. }
  58. }
  59. }
  60. }
  61. + // \Drupal::service('simplenews_mailjet_subscriptions.utilities')->unsubscribeUser($subscriber, $subscription);
  62. }
  63. /**
  64. diff --git a/simplenews_mailjet_subscriptions.services.yml b/simplenews_mailjet_subscriptions.services.yml
  65. new file mode 100644
  66. index 0000000..cdd4137
  67. --- /dev/null
  68. +++ b/simplenews_mailjet_subscriptions.services.yml
  69. @@ -0,0 +1,4 @@
  70. +services:
  71. + simplenews_mailjet_subscriptions.utilities:
  72. + class: Drupal\simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsUtilities
  73. + arguments: [ '@mailjet.handler' ]
  74. diff --git a/src/SimplenewsMailjetSubscriptionsUtilities.php b/src/SimplenewsMailjetSubscriptionsUtilities.php
  75. index 950ff8a..05601ca 100644
  76. --- a/src/SimplenewsMailjetSubscriptionsUtilities.php
  77. +++ b/src/SimplenewsMailjetSubscriptionsUtilities.php
  78. @@ -2,141 +2,79 @@
  79. namespace Drupal\simplenews_mailjet_subscriptions;
  80. +use Drupal\mailjet\MailjetHandlerInterface;
  81. +
  82. /**
  83. - * Provides a list of Simplenews Mailjet Subscription entities.
  84. + * Utilisties
  85. */
  86. class SimplenewsMailjetSubscriptionsUtilities {
  87. /**
  88. - * Check Subscriber.
  89. + * MailjetHandler service.
  90. + *
  91. + * @var \Drupal\mailjet\MailjetHandlerInterface
  92. */
  93. - private static function checkSubscriber($api_key, $secret_key, $encoded_mail) {
  94. -
  95. - // Check if contact exist
  96. - // verifica se o contacto ja tem opções.
  97. - $endpoint = 'https://api.mailjet.com/v3/REST/contactdata/' . $encoded_mail;
  98. - $ch = curl_init();
  99. - curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
  100. - curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  101. - curl_setopt($ch, CURLOPT_USERPWD, $api_key . ':' . $secret_key);
  102. - curl_setopt($ch, CURLOPT_URL, $endpoint);
  103. - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  104. - $response = curl_exec($ch);
  105. - curl_close($ch);
  106. - $json = json_decode($response, TRUE);
  107. + protected $mailjetHandler;
  108. - return $json;
  109. +/**
  110. + * SubscribeEmailForm constructor.
  111. + *
  112. + * @param \Drupal\mailjet\MailjetHandlerInterface $mailjetHandler
  113. + * The mailjet handler service.
  114. + */
  115. + public function __construct(MailjetHandlerInterface $mailjetHandler) {
  116. + $this->mailjetHandler = $mailjetHandler;
  117. }
  118. /**
  119. - * Update Subscriber.
  120. + * {@inheritDoc}
  121. */
  122. - private static function updateSubscriber($api_key, $secret_key, $listID, $subscriber_email, $contact_property, $newsletter_property_name, $action) {
  123. -
  124. - // Add contact to mailjet.
  125. - $endpoint = 'https://api.mailjet.com/v3/REST/contactslist/' . $listID . '/managecontact';
  126. - $message = [
  127. - "Properties" => [
  128. - $contact_property => $newsletter_property_name,
  129. - ],
  130. - "Action" => $action,
  131. - "Email" => $subscriber_email,
  132. - ];
  133. - $payload = json_encode($message);
  134. -
  135. - $ch = curl_init();
  136. - curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
  137. - curl_setopt($ch, CURLOPT_POST, 1);
  138. - curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  139. - curl_setopt($ch, CURLOPT_USERPWD, $api_key . ':' . $secret_key);
  140. - curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
  141. - curl_setopt($ch, CURLOPT_URL, $endpoint);
  142. - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  143. - $response = curl_exec($ch);
  144. - curl_close($ch);
  145. - $json = json_decode($response, TRUE);
  146. -
  147. - return $json;
  148. + public static function create(ContainerInterface $container) {
  149. + return new static(
  150. + $container->get('mailjet.handler')
  151. + );
  152. }
  153. /**
  154. * Subscribe User.
  155. */
  156. - public static function subscribeUser($subscriber, $subscription, $category, $api_key, $secret_key, $listID) {
  157. + public function subscribeUser($listID, $subscriber, $category) {
  158. $contact_property = $category['contact_property'];
  159. $newsletter_property_name = $category['newsletter_property_name'];
  160. $subscriber_email = $subscriber->getMail();
  161. - $encoded_mail = urlencode($subscriber_email);
  162. - $response = '';
  163. -
  164. - $contact_info = self::checkSubscriber($api_key, $secret_key, $encoded_mail);
  165. -
  166. - $options = $contact_info['Data'];
  167. -
  168. - // Se existe - acrescenta a opções.
  169. - if ($options[0]['Data']) {
  170. - foreach ($options[0]['Data'] as $property) {
  171. - if ($property["Name"] == $contact_property) {
  172. -
  173. - if (is_int(strpos($property['Value'], $newsletter_property_name))) {
  174. - $newsletter_property_name = $property['Value'];
  175. - }
  176. - else {
  177. - $newsletter_property_name = $property['Value'] . ';' . $newsletter_property_name;
  178. - }
  179. - }
  180. - }
  181. - }
  182. + $contact = [
  183. + 'Email' => $subscriber_email,
  184. + 'Properties' => [
  185. + $contact_property => $newsletter_property_name
  186. + ]
  187. + ];
  188. - $response = self::updateSubscriber($api_key, $secret_key, $listID, $subscriber_email, $contact_property, $newsletter_property_name, 'addnoforce');
  189. - $contact_id = $response['Data'][0]['ContactID'];
  190. - \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 . '.');
  191. - return $response;
  192. + // $response = $this->updateSubscriber($listID, $contact, 'addnoforce');
  193. + $response = $this->mailjetHandler->syncMailjetContact($listID, $contact, 'addnoforce');
  194. + // $contact_id = $response['Data'][0]['ContactID'];
  195. + \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $subscriber_email . ' subscribed to the ' . $listID . ' list .');
  196. }
  197. /**
  198. * Unsubscribe User.
  199. */
  200. - public static function unsubscribeUser($subscriber, $subscription, $category, $api_key, $secret_key, $listID) {
  201. + public function unsubscribeUser($listID, $subscriber, $category) {
  202. $contact_property = $category['contact_property'];
  203. $newsletter_property_name = $category['newsletter_property_name'];
  204. $subscriber_email = $subscriber->getMail();
  205. - $encoded_mail = urlencode($subscriber_email);
  206. - $response = '';
  207. -
  208. - $contact_info = self::checkSubscriber($api_key, $secret_key, $encoded_mail);
  209. - $contact_id = $contact_info['ContactID'];
  210. - $options = $contact_info['Data'];
  211. -
  212. - if ($options[0]['Data']) {
  213. - foreach ($options[0]['Data'] as $property) {
  214. - if ($property["Name"] == $contact_property) {
  215. - if (substr_count($property['Value'], ';') == 0) {
  216. - $newsletter_property_name = '';
  217. - $response = self::updateSubscriber($api_key, $secret_key, $listID, $subscriber_email, $contact_property, $newsletter_property_name, 'remove');
  218. - $contact_id = $response['Data'][0]['ContactID'];
  219. - \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $contact_id . ' unsubscribed to the list ' . $listID . '.');
  220. - }
  221. - else {
  222. - $pos = strpos($property['Value'], $newsletter_property_name);
  223. - \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 . '.');
  224. - if ($pos === 0) {
  225. - $newsletter_property_name = str_replace($newsletter_property_name . ';', '', $property['Value']);
  226. - }
  227. - else {
  228. - $newsletter_property_name = str_replace(';' . $newsletter_property_name, '', $property['Value']);
  229. - }
  230. - $response = self::updateSubscriber($api_key, $secret_key, $listID, $subscriber_email, $contact_property, $newsletter_property_name, 'addnoforce');
  231. - $contact_id = $response['Data'][0]['ContactID'];
  232. - \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 . '.');
  233. -
  234. - }
  235. - }
  236. - }
  237. - }
  238. + $contact = [
  239. + 'Email' => $subscriber_email,
  240. + 'Properties' => [
  241. + $contact_property => $newsletter_property_name
  242. + ]
  243. + ];
  244. - return $response;
  245. + // $response = $this->updateSubscriber($subscriber_email, $newsletter_property_name, 'remove');
  246. + $response = $this->mailjetHandler->syncMailjetContact($listID, $contact, 'remove');
  247. + // $contact_id = $response['Data'][0]['ContactID'];
  248. + \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $subscriber_email . ' removed from the ' . $listID . ' list ');
  249. + // return $response;
  250. }
  251. }