linkit upgrad. Mailet update, removed 2 obsolet patch, repatched simplenews_mailet_subscrition
This commit is contained in:
		@@ -251,9 +251,7 @@
 | 
			
		||||
              "Integrate Address with Search API https://www.drupal.org/project/address/issues/2812659": "https://www.drupal.org/files/issues/2020-11-02/integrate-address-searchapi-2812659-56.patch"
 | 
			
		||||
            },
 | 
			
		||||
            "drupal/mailjet": {
 | 
			
		||||
              "DrupalmailjetControllerMailjetRegisterController::redirectRegister() does not exist https://www.drupal.org/project/mailjet/issues/3450474": "https://www.drupal.org/files/issues/2024-05-29/mailjet-15615025-5.patch",
 | 
			
		||||
              "Error: Class MailjetTools/MailjetApi not found in Drupal/mailjet/Form/MailjetApiForm->submitForm() https://www.drupal.org/project/mailjet/issues/3464788":"https://www.drupal.org/files/issues/2024-08-20/mailjetApiForm_use_mailjettools_mailjetapi.patch",
 | 
			
		||||
              "Error: Class MailjetTools/UsStates not found https://www.drupal.org/project/mailjet/issues/3469059":"https://www.drupal.org/files/issues/2024-08-20/mailjet_settingsform_usstates.patch"
 | 
			
		||||
              "DrupalmailjetControllerMailjetRegisterController::redirectRegister() does not exist https://www.drupal.org/project/mailjet/issues/3450474": "https://www.drupal.org/files/issues/2024-05-29/mailjet-15615025-5.patch"
 | 
			
		||||
            },
 | 
			
		||||
            "drupal/pwa": {
 | 
			
		||||
              "Manifest doesn't have a maskable icon https://www.drupal.org/project/pwa/issues/3165433": "https://www.drupal.org/files/issues/2024-03-10/manifest-doesnt-have-a-maskable-icon-error-3165433-20.patch",
 | 
			
		||||
@@ -267,7 +265,7 @@
 | 
			
		||||
              "Define void return type for setUp() and tearDown() methods https://gitlab.com/weitzman/drupal-test-traits/-/issues/78":"./patches/Definevoidreturntype2.patch"
 | 
			
		||||
            },
 | 
			
		||||
            "drupal/simplenews_mailjet_subscriptions": {
 | 
			
		||||
              "using mailjet module":"./patches/simplenews_mailjet_subscriptions__usingmailjetmodule#3.patch"
 | 
			
		||||
              "using mailjet module https://www.drupal.org/project/simplenews_mailjet_subscriptions/issues/3318780":"./patches/simplenews_mailjet_subscriptions__usingmailjetmodule#4.patch"
 | 
			
		||||
            },
 | 
			
		||||
            "drupal/graphql": {
 | 
			
		||||
              "invalid translation language https://github.com/drupal-graphql/graphql/pull/1176": "./patches/graphql-fix-invalid-translation-language-dev.patch"
 | 
			
		||||
@@ -308,6 +306,10 @@
 | 
			
		||||
          },
 | 
			
		||||
          "drupal/advancedqueue": {
 | 
			
		||||
            "Issue with configuration schema advancedqueue.backend.[id] https://www.drupal.org/project/advancedqueue/issues/3469869": "https://git.drupalcode.org/project/advancedqueue/-/commit/a8864851cdfa40c9e38a3f5f64885baf1937375e.patch"
 | 
			
		||||
          },
 | 
			
		||||
          "drupal/mailjet": {
 | 
			
		||||
            "Error: Class MailjetTools/MailjetApi not found in Drupal/mailjet/Form/MailjetApiForm->submitForm() https://www.drupal.org/project/mailjet/issues/3464788":"https://www.drupal.org/files/issues/2024-08-20/mailjetApiForm_use_mailjettools_mailjetapi.patch",
 | 
			
		||||
              "Error: Class MailjetTools/UsStates not found https://www.drupal.org/project/mailjet/issues/3469059":"https://www.drupal.org/files/issues/2024-08-20/mailjet_settingsform_usstates.patch"
 | 
			
		||||
          }
 | 
			
		||||
        },
 | 
			
		||||
        "drupal-scaffold": {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										75
									
								
								composer.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										75
									
								
								composer.lock
									
									
									
										generated
									
									
									
								
							@@ -4,7 +4,7 @@
 | 
			
		||||
        "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
 | 
			
		||||
        "This file is @generated automatically"
 | 
			
		||||
    ],
 | 
			
		||||
    "content-hash": "e77925a1be45f78777e4bb0814575446",
 | 
			
		||||
    "content-hash": "445d8e41c8bdc4ff207e8f7c23dfa6f4",
 | 
			
		||||
    "packages": [
 | 
			
		||||
        {
 | 
			
		||||
            "name": "algolia/places",
 | 
			
		||||
@@ -8812,30 +8812,26 @@
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
            "name": "drupal/linkit",
 | 
			
		||||
            "version": "6.1.6",
 | 
			
		||||
            "version": "7.0.10",
 | 
			
		||||
            "source": {
 | 
			
		||||
                "type": "git",
 | 
			
		||||
                "url": "https://git.drupalcode.org/project/linkit.git",
 | 
			
		||||
                "reference": "6.1.6"
 | 
			
		||||
                "reference": "7.0.10"
 | 
			
		||||
            },
 | 
			
		||||
            "dist": {
 | 
			
		||||
                "type": "zip",
 | 
			
		||||
                "url": "https://ftp.drupal.org/files/projects/linkit-6.1.6.zip",
 | 
			
		||||
                "reference": "6.1.6",
 | 
			
		||||
                "shasum": "113d8e569cfb92f8d7feb0a2f7713953c0715e92"
 | 
			
		||||
                "url": "https://ftp.drupal.org/files/projects/linkit-7.0.10.zip",
 | 
			
		||||
                "reference": "7.0.10",
 | 
			
		||||
                "shasum": "10a7d4bce0038e887e597911b60919496c640f41"
 | 
			
		||||
            },
 | 
			
		||||
            "require": {
 | 
			
		||||
                "drupal/core": "^10.1"
 | 
			
		||||
            },
 | 
			
		||||
            "require-dev": {
 | 
			
		||||
                "drupal/ckeditor": "*",
 | 
			
		||||
                "drupal/imce": "*"
 | 
			
		||||
                "drupal/core": "^10.1 || ^11"
 | 
			
		||||
            },
 | 
			
		||||
            "type": "drupal-module",
 | 
			
		||||
            "extra": {
 | 
			
		||||
                "drupal": {
 | 
			
		||||
                    "version": "6.1.6",
 | 
			
		||||
                    "datestamp": "1729189029",
 | 
			
		||||
                    "version": "7.0.10",
 | 
			
		||||
                    "datestamp": "1759925018",
 | 
			
		||||
                    "security-coverage": {
 | 
			
		||||
                        "status": "covered",
 | 
			
		||||
                        "message": "Covered by Drupal's security advisory policy"
 | 
			
		||||
@@ -9054,34 +9050,37 @@
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
            "name": "drupal/mailjet",
 | 
			
		||||
            "version": "4.0.0",
 | 
			
		||||
            "version": "4.0.2",
 | 
			
		||||
            "source": {
 | 
			
		||||
                "type": "git",
 | 
			
		||||
                "url": "https://git.drupalcode.org/project/Mailjet.git",
 | 
			
		||||
                "reference": "4.0.0"
 | 
			
		||||
                "reference": "4.0.2"
 | 
			
		||||
            },
 | 
			
		||||
            "dist": {
 | 
			
		||||
                "type": "zip",
 | 
			
		||||
                "url": "https://ftp.drupal.org/files/projects/mailjet-4.0.0.zip",
 | 
			
		||||
                "reference": "4.0.0",
 | 
			
		||||
                "shasum": "f1a8cf63bb1a77c9ea6038f32446e53be603bf69"
 | 
			
		||||
                "url": "https://ftp.drupal.org/files/projects/mailjet-4.0.2.zip",
 | 
			
		||||
                "reference": "4.0.2",
 | 
			
		||||
                "shasum": "2a4bedce7ce73beac181977acc063e30e31f287a"
 | 
			
		||||
            },
 | 
			
		||||
            "require": {
 | 
			
		||||
                "drupal/core": "^8.8 || ^9 || ^10",
 | 
			
		||||
                "drupal/core": "^9 || ^10 || ^11",
 | 
			
		||||
                "guzzlehttp/guzzle": "^7.0",
 | 
			
		||||
                "mailjet/mailjet-apiv3-php": "^1.5",
 | 
			
		||||
                "phpmailer/phpmailer": "^6.0.7"
 | 
			
		||||
            },
 | 
			
		||||
            "require-dev": {
 | 
			
		||||
                "drupal/coder": "^8.2",
 | 
			
		||||
                "drupal/commerce_order": "*",
 | 
			
		||||
                "drupal/message": "*",
 | 
			
		||||
                "drupal/state_machine": "*",
 | 
			
		||||
                "drupal/token": "*"
 | 
			
		||||
                "drupal/token": "*",
 | 
			
		||||
                "squizlabs/php_codesniffer": "^3.9.1"
 | 
			
		||||
            },
 | 
			
		||||
            "type": "drupal-module",
 | 
			
		||||
            "extra": {
 | 
			
		||||
                "drupal": {
 | 
			
		||||
                    "version": "4.0.0",
 | 
			
		||||
                    "datestamp": "1717934119",
 | 
			
		||||
                    "version": "4.0.2",
 | 
			
		||||
                    "datestamp": "1738321982",
 | 
			
		||||
                    "security-coverage": {
 | 
			
		||||
                        "status": "covered",
 | 
			
		||||
                        "message": "Covered by Drupal's security advisory policy"
 | 
			
		||||
@@ -9102,7 +9101,7 @@
 | 
			
		||||
                    "homepage": "https://www.drupal.org/user/289861"
 | 
			
		||||
                },
 | 
			
		||||
                {
 | 
			
		||||
                    "name": "Mailjet Plugins",
 | 
			
		||||
                    "name": "mailjet plugins",
 | 
			
		||||
                    "homepage": "https://www.drupal.org/user/1599668"
 | 
			
		||||
                },
 | 
			
		||||
                {
 | 
			
		||||
@@ -15136,16 +15135,16 @@
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
            "name": "mailjet/mailjet-apiv3-php",
 | 
			
		||||
            "version": "v1.6.3",
 | 
			
		||||
            "version": "v1.6.5",
 | 
			
		||||
            "source": {
 | 
			
		||||
                "type": "git",
 | 
			
		||||
                "url": "https://github.com/mailjet/mailjet-apiv3-php.git",
 | 
			
		||||
                "reference": "b10ac28c0c201bfd834e28a5a2c13f89d283aaa1"
 | 
			
		||||
                "reference": "c31917a8fcf5eb04363aa2af933ab560b0a934ea"
 | 
			
		||||
            },
 | 
			
		||||
            "dist": {
 | 
			
		||||
                "type": "zip",
 | 
			
		||||
                "url": "https://api.github.com/repos/mailjet/mailjet-apiv3-php/zipball/b10ac28c0c201bfd834e28a5a2c13f89d283aaa1",
 | 
			
		||||
                "reference": "b10ac28c0c201bfd834e28a5a2c13f89d283aaa1",
 | 
			
		||||
                "url": "https://api.github.com/repos/mailjet/mailjet-apiv3-php/zipball/c31917a8fcf5eb04363aa2af933ab560b0a934ea",
 | 
			
		||||
                "reference": "c31917a8fcf5eb04363aa2af933ab560b0a934ea",
 | 
			
		||||
                "shasum": ""
 | 
			
		||||
            },
 | 
			
		||||
            "require": {
 | 
			
		||||
@@ -15159,8 +15158,8 @@
 | 
			
		||||
                "mockery/mockery": "^1.4",
 | 
			
		||||
                "php-coveralls/php-coveralls": "^2.0",
 | 
			
		||||
                "phpcompatibility/php-compatibility": "*",
 | 
			
		||||
                "phpstan/phpstan": "^1.10",
 | 
			
		||||
                "phpunit/phpunit": "^8|^9",
 | 
			
		||||
                "phpstan/phpstan": "^2.0",
 | 
			
		||||
                "phpunit/phpunit": "^11.4.0",
 | 
			
		||||
                "squizlabs/php_codesniffer": "*"
 | 
			
		||||
            },
 | 
			
		||||
            "type": "library",
 | 
			
		||||
@@ -15191,9 +15190,9 @@
 | 
			
		||||
            ],
 | 
			
		||||
            "support": {
 | 
			
		||||
                "issues": "https://github.com/mailjet/mailjet-apiv3-php/issues",
 | 
			
		||||
                "source": "https://github.com/mailjet/mailjet-apiv3-php/tree/v1.6.3"
 | 
			
		||||
                "source": "https://github.com/mailjet/mailjet-apiv3-php/tree/v1.6.5"
 | 
			
		||||
            },
 | 
			
		||||
            "time": "2023-12-10T11:13:26+00:00"
 | 
			
		||||
            "time": "2025-04-19T16:48:36+00:00"
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
            "name": "masterminds/html5",
 | 
			
		||||
@@ -16232,16 +16231,16 @@
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
            "name": "phpmailer/phpmailer",
 | 
			
		||||
            "version": "v6.9.1",
 | 
			
		||||
            "version": "v6.11.1",
 | 
			
		||||
            "source": {
 | 
			
		||||
                "type": "git",
 | 
			
		||||
                "url": "https://github.com/PHPMailer/PHPMailer.git",
 | 
			
		||||
                "reference": "039de174cd9c17a8389754d3b877a2ed22743e18"
 | 
			
		||||
                "reference": "d9e3b36b47f04b497a0164c5a20f92acb4593284"
 | 
			
		||||
            },
 | 
			
		||||
            "dist": {
 | 
			
		||||
                "type": "zip",
 | 
			
		||||
                "url": "https://api.github.com/repos/PHPMailer/PHPMailer/zipball/039de174cd9c17a8389754d3b877a2ed22743e18",
 | 
			
		||||
                "reference": "039de174cd9c17a8389754d3b877a2ed22743e18",
 | 
			
		||||
                "url": "https://api.github.com/repos/PHPMailer/PHPMailer/zipball/d9e3b36b47f04b497a0164c5a20f92acb4593284",
 | 
			
		||||
                "reference": "d9e3b36b47f04b497a0164c5a20f92acb4593284",
 | 
			
		||||
                "shasum": ""
 | 
			
		||||
            },
 | 
			
		||||
            "require": {
 | 
			
		||||
@@ -16262,6 +16261,7 @@
 | 
			
		||||
            },
 | 
			
		||||
            "suggest": {
 | 
			
		||||
                "decomplexity/SendOauth2": "Adapter for using XOAUTH2 authentication",
 | 
			
		||||
                "ext-imap": "Needed to support advanced email address parsing according to RFC822",
 | 
			
		||||
                "ext-mbstring": "Needed to send email in multibyte encoding charset or decode encoded addresses",
 | 
			
		||||
                "ext-openssl": "Needed for secure SMTP sending and DKIM signing",
 | 
			
		||||
                "greew/oauth2-azure-provider": "Needed for Microsoft Azure XOAUTH2 authentication",
 | 
			
		||||
@@ -16301,7 +16301,7 @@
 | 
			
		||||
            "description": "PHPMailer is a full-featured email creation and transfer class for PHP",
 | 
			
		||||
            "support": {
 | 
			
		||||
                "issues": "https://github.com/PHPMailer/PHPMailer/issues",
 | 
			
		||||
                "source": "https://github.com/PHPMailer/PHPMailer/tree/v6.9.1"
 | 
			
		||||
                "source": "https://github.com/PHPMailer/PHPMailer/tree/v6.11.1"
 | 
			
		||||
            },
 | 
			
		||||
            "funding": [
 | 
			
		||||
                {
 | 
			
		||||
@@ -16309,7 +16309,7 @@
 | 
			
		||||
                    "type": "github"
 | 
			
		||||
                }
 | 
			
		||||
            ],
 | 
			
		||||
            "time": "2023-11-25T22:23:28+00:00"
 | 
			
		||||
            "time": "2025-09-30T11:54:53+00:00"
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
            "name": "phpoption/phpoption",
 | 
			
		||||
@@ -21155,7 +21155,6 @@
 | 
			
		||||
        "drupal/flag": 10,
 | 
			
		||||
        "drupal/flag_lists": 5,
 | 
			
		||||
        "drupal/inline_entity_form": 5,
 | 
			
		||||
        "drupal/linkit": 10,
 | 
			
		||||
        "drupal/maxlength": 10,
 | 
			
		||||
        "drupal/pathologic": 15,
 | 
			
		||||
        "drupal/pwa": 10,
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,794 @@
 | 
			
		||||
diff --git a/README.txt b/README.txt
 | 
			
		||||
index cee6e1a..b488e91 100644
 | 
			
		||||
--- a/README.txt
 | 
			
		||||
+++ b/README.txt
 | 
			
		||||
@@ -15,6 +15,7 @@ REQUIREMENTS
 | 
			
		||||
 This module requires the following modules:
 | 
			
		||||
 
 | 
			
		||||
  * Simplenews (https://www.drupal.org/project/simplenews)
 | 
			
		||||
+ * Mailet (https://www.drupal.org/project/Mailjet)
 | 
			
		||||
 
 | 
			
		||||
  INSTALLATION
 | 
			
		||||
 ------------
 | 
			
		||||
diff --git a/simplenews_mailjet_subscriptions.module b/simplenews_mailjet_subscriptions.module
 | 
			
		||||
index 09abe13..74214f6 100644
 | 
			
		||||
--- a/simplenews_mailjet_subscriptions.module
 | 
			
		||||
+++ b/simplenews_mailjet_subscriptions.module
 | 
			
		||||
@@ -7,7 +7,7 @@
 | 
			
		||||
 
 | 
			
		||||
 use Drupal\Core\Routing\RouteMatchInterface;
 | 
			
		||||
 use Drupal\simplenews\Entity\Subscriber;
 | 
			
		||||
-use Drupal\simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsUtilities;
 | 
			
		||||
+// use Drupal\simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsUtilities;
 | 
			
		||||
 use Drupal\simplenews_mailjet_subscriptions\Entity\SimplenewsMailjetSubscriptionEntity;
 | 
			
		||||
 use Drupal\Component\Utility\Html;
 | 
			
		||||
 
 | 
			
		||||
@@ -24,18 +24,18 @@ use Drupal\Component\Utility\Html;
 | 
			
		||||
 function simplenews_mailjet_subscriptions_simplenews_subscribe(Subscriber $subscriber, string $subscription) {
 | 
			
		||||
   $entities = SimplenewsMailjetSubscriptionEntity::loadMultiple();
 | 
			
		||||
   foreach ((array) $entities as $entity) {
 | 
			
		||||
-    $api_key = $entity->getApiKey();
 | 
			
		||||
-    $secret_key = $entity->getSecretkey();
 | 
			
		||||
     $listID = $entity->getID();
 | 
			
		||||
     $table = $entity->get('mapping_table');
 | 
			
		||||
     if (is_array($table)) {
 | 
			
		||||
       foreach ($table as $category) {
 | 
			
		||||
         if ($category['simplenews_news'] == $subscription) {
 | 
			
		||||
-          SimplenewsMailjetSubscriptionsUtilities::subscribeUser($subscriber, $subscription, $category, $api_key, $secret_key, $listID);
 | 
			
		||||
+          // SimplenewsMailjetSubscriptionsUtilities::subscribeUser($subscriber, $category);
 | 
			
		||||
+          \Drupal::service('simplenews_mailjet_subscriptions.utilities')->subscribeUser($listID, $subscriber, $category);
 | 
			
		||||
         }
 | 
			
		||||
       }
 | 
			
		||||
     }
 | 
			
		||||
   }
 | 
			
		||||
+  // \Drupal::service('simplenews_mailjet_subscriptions.utilities')->subscribeUser($subscriber, $subscription);
 | 
			
		||||
 }
 | 
			
		||||
 
 | 
			
		||||
 /**
 | 
			
		||||
@@ -51,19 +51,74 @@ function simplenews_mailjet_subscriptions_simplenews_subscribe(Subscriber $subsc
 | 
			
		||||
 function simplenews_mailjet_subscriptions_simplenews_unsubscribe(Subscriber $subscriber, string $subscription) {
 | 
			
		||||
   $entities = SimplenewsMailjetSubscriptionEntity::loadMultiple();
 | 
			
		||||
   foreach ((array) $entities as $entity) {
 | 
			
		||||
-    $api_key = $entity->getApiKey();
 | 
			
		||||
-    $secret_key = $entity->getSecretkey();
 | 
			
		||||
     $listID = $entity->getID();
 | 
			
		||||
     $table = $entity->get('mapping_table');
 | 
			
		||||
     if (is_array($table)) {
 | 
			
		||||
       foreach ($table as $category) {
 | 
			
		||||
         if ($category['simplenews_news'] == $subscription) {
 | 
			
		||||
           // Unsubscribe.
 | 
			
		||||
-          SimplenewsMailjetSubscriptionsUtilities::unsubscribeUser($subscriber, $subscription, $category, $api_key, $secret_key, $listID);
 | 
			
		||||
+          // SimplenewsMailjetSubscriptionsUtilities::unsubscribeUser($subscriber, $category);
 | 
			
		||||
+          \Drupal::service('simplenews_mailjet_subscriptions.utilities')->unsubscribeUser($listID, $subscriber, $category);
 | 
			
		||||
         }
 | 
			
		||||
       }
 | 
			
		||||
     }
 | 
			
		||||
   }
 | 
			
		||||
+  // \Drupal::service('simplenews_mailjet_subscriptions.utilities')->unsubscribeUser($subscriber, $subscription);
 | 
			
		||||
+}
 | 
			
		||||
+
 | 
			
		||||
+
 | 
			
		||||
+/**
 | 
			
		||||
+ * Act after a subscriber is updated.
 | 
			
		||||
+ *
 | 
			
		||||
+ * @param \Drupal\simplenews\Entity\Subscriber $subscriber
 | 
			
		||||
+ *   The subscriber object including all subscriptions of this user.
 | 
			
		||||
+ *
 | 
			
		||||
+ * @ingroup subscriber
 | 
			
		||||
+ */
 | 
			
		||||
+function simplenews_mailjet_subscriptions_simplenews_subscriber_update(Subscriber $subscriber) {
 | 
			
		||||
+  $new_mail=$subscriber->getMail();
 | 
			
		||||
+  $original_mail = $subscriber->original->getMail();
 | 
			
		||||
+  if ($new_mail !== $original_mail) {
 | 
			
		||||
+    $new_subscriptions = $subscriber->getSubscribedNewsletterIds();
 | 
			
		||||
+    $original_subscriptions = $subscriber->original->getSubscribedNewsletterIds();
 | 
			
		||||
+    $entities = SimplenewsMailjetSubscriptionEntity::loadMultiple();
 | 
			
		||||
+    foreach ((array) $entities as $entity) {
 | 
			
		||||
+      $listID = $entity->getID();
 | 
			
		||||
+      $table = $entity->get('mapping_table');
 | 
			
		||||
+      if (is_array($table)) {
 | 
			
		||||
+        foreach ($table as $category) {
 | 
			
		||||
+          if ( in_array($category['simplenews_news'], $original_subscriptions) ) {
 | 
			
		||||
+            \Drupal::service('simplenews_mailjet_subscriptions.utilities')->unsubscribeUser($listID, $subscriber->original, $category);
 | 
			
		||||
+          }
 | 
			
		||||
+          if ( in_array($category['simplenews_news'], $new_subscriptions) ) {
 | 
			
		||||
+            \Drupal::service('simplenews_mailjet_subscriptions.utilities')->subscribeUser($listID, $subscriber, $category);
 | 
			
		||||
+          }
 | 
			
		||||
+        }
 | 
			
		||||
+      }
 | 
			
		||||
+    }
 | 
			
		||||
+  }
 | 
			
		||||
+}
 | 
			
		||||
+
 | 
			
		||||
+
 | 
			
		||||
+/**
 | 
			
		||||
+ * Act after a subscriber has been deleted.
 | 
			
		||||
+ *
 | 
			
		||||
+ * @param \Drupal\simplenews\Entity\Subscriber $subscriber
 | 
			
		||||
+ *   The subscriber object including all subscriptions of this user.
 | 
			
		||||
+ *
 | 
			
		||||
+ * @ingroup subscriber
 | 
			
		||||
+ */
 | 
			
		||||
+function simplenews_mailjet_subscriptions_simplenews_subscriber_delete(Subscriber $subscriber) {
 | 
			
		||||
+  $entities = SimplenewsMailjetSubscriptionEntity::loadMultiple();
 | 
			
		||||
+  foreach ((array) $entities as $entity) {
 | 
			
		||||
+    $listID = $entity->getID();
 | 
			
		||||
+    $table = $entity->get('mapping_table');
 | 
			
		||||
+    if (is_array($table)) {
 | 
			
		||||
+      foreach ($table as $category) {
 | 
			
		||||
+        \Drupal::service('simplenews_mailjet_subscriptions.utilities')->unsubscribeUser($listID, $subscriber, $category);
 | 
			
		||||
+      }
 | 
			
		||||
+    }
 | 
			
		||||
+  }
 | 
			
		||||
 }
 | 
			
		||||
 
 | 
			
		||||
 /**
 | 
			
		||||
diff --git a/simplenews_mailjet_subscriptions.services.yml b/simplenews_mailjet_subscriptions.services.yml
 | 
			
		||||
new file mode 100644
 | 
			
		||||
index 0000000..cee6a81
 | 
			
		||||
--- /dev/null
 | 
			
		||||
+++ b/simplenews_mailjet_subscriptions.services.yml
 | 
			
		||||
@@ -0,0 +1,7 @@
 | 
			
		||||
+services:
 | 
			
		||||
+  simplenews_mailjet_subscriptions.handler:
 | 
			
		||||
+    class: Drupal\simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsMailjetHandler
 | 
			
		||||
+    arguments: ['@mailjet.factory']
 | 
			
		||||
+  simplenews_mailjet_subscriptions.utilities:
 | 
			
		||||
+    class: Drupal\simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsUtilities
 | 
			
		||||
+    arguments: [ '@simplenews_mailjet_subscriptions.handler' ]
 | 
			
		||||
diff --git a/src/Entity/SimplenewsMailjetSubscriptionEntity.php b/src/Entity/SimplenewsMailjetSubscriptionEntity.php
 | 
			
		||||
index 27b7737..34c8df5 100644
 | 
			
		||||
--- a/src/Entity/SimplenewsMailjetSubscriptionEntity.php
 | 
			
		||||
+++ b/src/Entity/SimplenewsMailjetSubscriptionEntity.php
 | 
			
		||||
@@ -30,8 +30,6 @@ use Drupal\Core\Config\Entity\ConfigEntityBase;
 | 
			
		||||
  *   config_export = {
 | 
			
		||||
  *     "id",
 | 
			
		||||
  *     "title",
 | 
			
		||||
- *     "api_key",
 | 
			
		||||
- *     "secret_key",
 | 
			
		||||
  *     "mapping_table"
 | 
			
		||||
  *   }
 | 
			
		||||
  * )
 | 
			
		||||
@@ -94,53 +92,53 @@ class SimplenewsMailjetSubscriptionEntity extends ConfigEntityBase implements Si
 | 
			
		||||
     return $this->get('id');
 | 
			
		||||
   }
 | 
			
		||||
 
 | 
			
		||||
-  /**
 | 
			
		||||
-   * Returns the entity api key.
 | 
			
		||||
-   *
 | 
			
		||||
-   * @return string
 | 
			
		||||
-   *   The entity api key.
 | 
			
		||||
-   */
 | 
			
		||||
-  public function getApiKey() {
 | 
			
		||||
-    return $this->get('api_key');
 | 
			
		||||
-  }
 | 
			
		||||
-
 | 
			
		||||
-  /**
 | 
			
		||||
-   * Sets the entity api key.
 | 
			
		||||
-   *
 | 
			
		||||
-   * @param string $api_key
 | 
			
		||||
-   *   Api key.
 | 
			
		||||
-   *
 | 
			
		||||
-   * @return $this
 | 
			
		||||
-   *   The Simplenews Mailjet Subscription entity.
 | 
			
		||||
-   */
 | 
			
		||||
-  public function setApiKey($api_key) {
 | 
			
		||||
-    $this->set('api_key', $api_key);
 | 
			
		||||
-    return $this;
 | 
			
		||||
-  }
 | 
			
		||||
-
 | 
			
		||||
-  /**
 | 
			
		||||
-   * Returns the entity secret key.
 | 
			
		||||
-   *
 | 
			
		||||
-   * @return string
 | 
			
		||||
-   *   The entity secret key.
 | 
			
		||||
-   */
 | 
			
		||||
-  public function getSecretkey() {
 | 
			
		||||
-    return $this->get('secret_key');
 | 
			
		||||
-  }
 | 
			
		||||
-
 | 
			
		||||
-  /**
 | 
			
		||||
-   * Sets the entity secret key.
 | 
			
		||||
-   *
 | 
			
		||||
-   * @param string $secret_key
 | 
			
		||||
-   *   Secret key.
 | 
			
		||||
-   *
 | 
			
		||||
-   * @return $this
 | 
			
		||||
-   *   The Simplenews Mailjet Subscription entity.
 | 
			
		||||
-   */
 | 
			
		||||
-  public function setSecretkey($secret_key) {
 | 
			
		||||
-    $this->set('secret_key', $secret_key);
 | 
			
		||||
-    return $this;
 | 
			
		||||
-  }
 | 
			
		||||
+  // /**
 | 
			
		||||
+  //  * Returns the entity api key.
 | 
			
		||||
+  //  *
 | 
			
		||||
+  //  * @return string
 | 
			
		||||
+  //  *   The entity api key.
 | 
			
		||||
+  //  */
 | 
			
		||||
+  // public function getApiKey() {
 | 
			
		||||
+  //   return $this->get('api_key');
 | 
			
		||||
+  // }
 | 
			
		||||
+
 | 
			
		||||
+  // /**
 | 
			
		||||
+  //  * Sets the entity api key.
 | 
			
		||||
+  //  *
 | 
			
		||||
+  //  * @param string $api_key
 | 
			
		||||
+  //  *   Api key.
 | 
			
		||||
+  //  *
 | 
			
		||||
+  //  * @return $this
 | 
			
		||||
+  //  *   The Simplenews Mailjet Subscription entity.
 | 
			
		||||
+  //  */
 | 
			
		||||
+  // public function setApiKey($api_key) {
 | 
			
		||||
+  //   $this->set('api_key', $api_key);
 | 
			
		||||
+  //   return $this;
 | 
			
		||||
+  // }
 | 
			
		||||
+
 | 
			
		||||
+  // /**
 | 
			
		||||
+  //  * Returns the entity secret key.
 | 
			
		||||
+  //  *
 | 
			
		||||
+  //  * @return string
 | 
			
		||||
+  //  *   The entity secret key.
 | 
			
		||||
+  //  */
 | 
			
		||||
+  // public function getSecretkey() {
 | 
			
		||||
+  //   return $this->get('secret_key');
 | 
			
		||||
+  // }
 | 
			
		||||
+
 | 
			
		||||
+  // /**
 | 
			
		||||
+  //  * Sets the entity secret key.
 | 
			
		||||
+  //  *
 | 
			
		||||
+  //  * @param string $secret_key
 | 
			
		||||
+  //  *   Secret key.
 | 
			
		||||
+  //  *
 | 
			
		||||
+  //  * @return $this
 | 
			
		||||
+  //  *   The Simplenews Mailjet Subscription entity.
 | 
			
		||||
+  //  */
 | 
			
		||||
+  // public function setSecretkey($secret_key) {
 | 
			
		||||
+  //   $this->set('secret_key', $secret_key);
 | 
			
		||||
+  //   return $this;
 | 
			
		||||
+  // }
 | 
			
		||||
 
 | 
			
		||||
   /**
 | 
			
		||||
    * Sets the entity mapping table.
 | 
			
		||||
diff --git a/src/Form/SimplenewsMailjetSubscriptionAddForm.php b/src/Form/SimplenewsMailjetSubscriptionAddForm.php
 | 
			
		||||
index 836bee3..bc70090 100644
 | 
			
		||||
--- a/src/Form/SimplenewsMailjetSubscriptionAddForm.php
 | 
			
		||||
+++ b/src/Form/SimplenewsMailjetSubscriptionAddForm.php
 | 
			
		||||
@@ -64,20 +64,20 @@ class SimplenewsMailjetSubscriptionAddForm extends EntityForm {
 | 
			
		||||
         'exists' => [$this, 'exist'],
 | 
			
		||||
       ],
 | 
			
		||||
     ];
 | 
			
		||||
-    $form['api_key'] = [
 | 
			
		||||
-      '#type' => 'textfield',
 | 
			
		||||
-      '#title' => $this->t('API Key'),
 | 
			
		||||
-      '#maxlength' => 32,
 | 
			
		||||
-      '#description' => $this->t('Mailjet Account API Key'),
 | 
			
		||||
-      '#required' => TRUE,
 | 
			
		||||
-    ];
 | 
			
		||||
-    $form['secret_key'] = [
 | 
			
		||||
-      '#type' => 'password',
 | 
			
		||||
-      '#title' => $this->t('Secret Key'),
 | 
			
		||||
-      '#maxlength' => 32,
 | 
			
		||||
-      '#description' => $this->t('Mailjet Account Secret Key'),
 | 
			
		||||
-      '#required' => TRUE,
 | 
			
		||||
-    ];
 | 
			
		||||
+    // $form['api_key'] = [
 | 
			
		||||
+    //   '#type' => 'textfield',
 | 
			
		||||
+    //   '#title' => $this->t('API Key'),
 | 
			
		||||
+    //   '#maxlength' => 32,
 | 
			
		||||
+    //   '#description' => $this->t('Mailjet Account API Key'),
 | 
			
		||||
+    //   '#required' => TRUE,
 | 
			
		||||
+    // ];
 | 
			
		||||
+    // $form['secret_key'] = [
 | 
			
		||||
+    //   '#type' => 'password',
 | 
			
		||||
+    //   '#title' => $this->t('Secret Key'),
 | 
			
		||||
+    //   '#maxlength' => 32,
 | 
			
		||||
+    //   '#description' => $this->t('Mailjet Account Secret Key'),
 | 
			
		||||
+    //   '#required' => TRUE,
 | 
			
		||||
+    // ];
 | 
			
		||||
 
 | 
			
		||||
     return $form;
 | 
			
		||||
   }
 | 
			
		||||
diff --git a/src/Form/SimplenewsMailjetSubscriptionEditForm.php b/src/Form/SimplenewsMailjetSubscriptionEditForm.php
 | 
			
		||||
index 98342b5..f67ba96 100644
 | 
			
		||||
--- a/src/Form/SimplenewsMailjetSubscriptionEditForm.php
 | 
			
		||||
+++ b/src/Form/SimplenewsMailjetSubscriptionEditForm.php
 | 
			
		||||
@@ -76,20 +76,20 @@ class SimplenewsMailjetSubscriptionEditForm extends EntityForm {
 | 
			
		||||
       '#help' => $this->t('Configuration title'),
 | 
			
		||||
       '#required' => TRUE,
 | 
			
		||||
     ];
 | 
			
		||||
-    $form['api_key'] = [
 | 
			
		||||
-      '#type' => 'textfield',
 | 
			
		||||
-      '#title' => $this->t('API Key'),
 | 
			
		||||
-      '#maxlength' => 32,
 | 
			
		||||
-      '#default_value' => $this->entity->getApiKey(),
 | 
			
		||||
-      '#help' => $this->t('Mailjet Account API Key'),
 | 
			
		||||
-      '#required' => TRUE,
 | 
			
		||||
-      '#disabled' => !$this->entity->isNew(),
 | 
			
		||||
-    ];
 | 
			
		||||
-
 | 
			
		||||
-    if ($this->entity->getApiKey() && $this->entity->getSecretkey()) {
 | 
			
		||||
+    // $form['api_key'] = [
 | 
			
		||||
+    //   '#type' => 'textfield',
 | 
			
		||||
+    //   '#title' => $this->t('API Key'),
 | 
			
		||||
+    //   '#maxlength' => 32,
 | 
			
		||||
+    //   '#default_value' => $this->entity->getApiKey(),
 | 
			
		||||
+    //   '#help' => $this->t('Mailjet Account API Key'),
 | 
			
		||||
+    //   '#required' => TRUE,
 | 
			
		||||
+    //   '#disabled' => !$this->entity->isNew(),
 | 
			
		||||
+    // ];
 | 
			
		||||
+
 | 
			
		||||
+    // if ($this->entity->getApiKey() && $this->entity->getSecretkey()) {
 | 
			
		||||
       $this->constructTable($form, $form_state);
 | 
			
		||||
 
 | 
			
		||||
-    }
 | 
			
		||||
+    // }
 | 
			
		||||
 
 | 
			
		||||
     $form['add_subscription'] = [
 | 
			
		||||
       '#type' => 'submit',
 | 
			
		||||
diff --git a/src/Plugin/RulesAction/UnsubscribeFromList.php b/src/Plugin/RulesAction/UnsubscribeFromList.php
 | 
			
		||||
new file mode 100644
 | 
			
		||||
index 0000000..3f546ef
 | 
			
		||||
--- /dev/null
 | 
			
		||||
+++ b/src/Plugin/RulesAction/UnsubscribeFromList.php
 | 
			
		||||
@@ -0,0 +1,51 @@
 | 
			
		||||
+<?php
 | 
			
		||||
+
 | 
			
		||||
+namespace Drupal\simplenews_mailjet_subscriptions\Plugin\RulesAction;
 | 
			
		||||
+
 | 
			
		||||
+use Drupal\rules\Core\RulesActionBase;
 | 
			
		||||
+use Drupal\simplenews\SubscriberInterface;
 | 
			
		||||
+
 | 
			
		||||
+/**
 | 
			
		||||
+ * Provides a 'Unsubscribe' action.
 | 
			
		||||
+ *
 | 
			
		||||
+ * @RulesAction(
 | 
			
		||||
+ *   id = "rules_simplenews_mailjet_subscriptions_unsubscribe_from_list",
 | 
			
		||||
+ *   label = @Translation("Unsubscribe from newsletter, giving mailjet listID and email"),
 | 
			
		||||
+ *   category = @Translation("Simplenews Mailjet Subscriptions"),
 | 
			
		||||
+ *   context_definitions = {
 | 
			
		||||
+ *     "listID" = @ContextDefinition("integer",
 | 
			
		||||
+ *       label = @Translation("Mailjet list ID"),
 | 
			
		||||
+ *       description = @Translation("Specifies the mailjet list ID from which simplenews list will be found and email will be unsubscribed from.")
 | 
			
		||||
+ *     ),
 | 
			
		||||
+ *     "email" = @ContextDefinition("string",
 | 
			
		||||
+ *       label = @Translation("Subscriber email"),
 | 
			
		||||
+ *       description = @Translation("Specifies the email to unsubscribe.")
 | 
			
		||||
+ *     )
 | 
			
		||||
+ *   }
 | 
			
		||||
+ * )
 | 
			
		||||
+ */
 | 
			
		||||
+class UnsubscribeFromList extends RulesActionBase {
 | 
			
		||||
+
 | 
			
		||||
+  /**
 | 
			
		||||
+   * Deletes the Entity.
 | 
			
		||||
+   *
 | 
			
		||||
+   * @param integer listID
 | 
			
		||||
+   *    The mailjet list to unsubcsribe from.
 | 
			
		||||
+   *
 | 
			
		||||
+   * @param string email
 | 
			
		||||
+   *    The email to unsubcribe.
 | 
			
		||||
+   */
 | 
			
		||||
+  protected function doExecute(integer $listID, string $email) {
 | 
			
		||||
+    $l = $listID;
 | 
			
		||||
+    $e = $email;
 | 
			
		||||
+    \Drupal::logger('simplenews_mailjet_subscriptions')->notice(print_r($listID, true));
 | 
			
		||||
+    // \Drupal::logger('simplenews_mailjet_subscriptions')->notice("Unsubscribe webhook triggered for listID: " . $listID . ", and email: " . $email . ".");
 | 
			
		||||
+
 | 
			
		||||
+    // find corresponding simplenews list from mailjet listID
 | 
			
		||||
+
 | 
			
		||||
+    // get subscriber entity
 | 
			
		||||
+
 | 
			
		||||
+    // unsubscribe subscriber from simplenews list
 | 
			
		||||
+  }
 | 
			
		||||
+
 | 
			
		||||
+}
 | 
			
		||||
\ No newline at end of file
 | 
			
		||||
diff --git a/src/SimplenewsMailjetSubscriptionsInterface.php b/src/SimplenewsMailjetSubscriptionsInterface.php
 | 
			
		||||
index c727d26..37c082d 100644
 | 
			
		||||
--- a/src/SimplenewsMailjetSubscriptionsInterface.php
 | 
			
		||||
+++ b/src/SimplenewsMailjetSubscriptionsInterface.php
 | 
			
		||||
@@ -36,43 +36,43 @@ interface SimplenewsMailjetSubscriptionsInterface extends ConfigEntityInterface
 | 
			
		||||
    */
 | 
			
		||||
   public function getId();
 | 
			
		||||
 
 | 
			
		||||
-  /**
 | 
			
		||||
-   * Returns the entity api key.
 | 
			
		||||
-   *
 | 
			
		||||
-   * @return string
 | 
			
		||||
-   *   The entity api key.
 | 
			
		||||
-   */
 | 
			
		||||
-  public function getApiKey();
 | 
			
		||||
+  // /**
 | 
			
		||||
+  //  * Returns the entity api key.
 | 
			
		||||
+  //  *
 | 
			
		||||
+  //  * @return string
 | 
			
		||||
+  //  *   The entity api key.
 | 
			
		||||
+  //  */
 | 
			
		||||
+  // public function getApiKey();
 | 
			
		||||
 
 | 
			
		||||
-  /**
 | 
			
		||||
-   * Sets the entity api key.
 | 
			
		||||
-   *
 | 
			
		||||
-   * @param string $api_key
 | 
			
		||||
-   *   Api key.
 | 
			
		||||
-   *
 | 
			
		||||
-   * @return $this
 | 
			
		||||
-   *   The Simplenews Mailjet Subscription entity.
 | 
			
		||||
-   */
 | 
			
		||||
-  public function setApiKey($api_key);
 | 
			
		||||
+  // /**
 | 
			
		||||
+  //  * Sets the entity api key.
 | 
			
		||||
+  //  *
 | 
			
		||||
+  //  * @param string $api_key
 | 
			
		||||
+  //  *   Api key.
 | 
			
		||||
+  //  *
 | 
			
		||||
+  //  * @return $this
 | 
			
		||||
+  //  *   The Simplenews Mailjet Subscription entity.
 | 
			
		||||
+  //  */
 | 
			
		||||
+  // public function setApiKey($api_key);
 | 
			
		||||
 
 | 
			
		||||
-  /**
 | 
			
		||||
-   * Returns the entity secret key.
 | 
			
		||||
-   *
 | 
			
		||||
-   * @return string
 | 
			
		||||
-   *   The entity secret key.
 | 
			
		||||
-   */
 | 
			
		||||
-  public function getSecretkey();
 | 
			
		||||
+  // /**
 | 
			
		||||
+  //  * Returns the entity secret key.
 | 
			
		||||
+  //  *
 | 
			
		||||
+  //  * @return string
 | 
			
		||||
+  //  *   The entity secret key.
 | 
			
		||||
+  //  */
 | 
			
		||||
+  // public function getSecretkey();
 | 
			
		||||
 
 | 
			
		||||
-  /**
 | 
			
		||||
-   * Sets the entity secret key.
 | 
			
		||||
-   *
 | 
			
		||||
-   * @param string $secret_key
 | 
			
		||||
-   *   Secret key.
 | 
			
		||||
-   *
 | 
			
		||||
-   * @return $this
 | 
			
		||||
-   *   The Simplenews Mailjet Subscription entity.
 | 
			
		||||
-   */
 | 
			
		||||
-  public function setSecretkey($secret_key);
 | 
			
		||||
+  // /**
 | 
			
		||||
+  //  * Sets the entity secret key.
 | 
			
		||||
+  //  *
 | 
			
		||||
+  //  * @param string $secret_key
 | 
			
		||||
+  //  *   Secret key.
 | 
			
		||||
+  //  *
 | 
			
		||||
+  //  * @return $this
 | 
			
		||||
+  //  *   The Simplenews Mailjet Subscription entity.
 | 
			
		||||
+  //  */
 | 
			
		||||
+  // public function setSecretkey($secret_key);
 | 
			
		||||
 
 | 
			
		||||
   /**
 | 
			
		||||
    * Sets the entity mapping table.
 | 
			
		||||
diff --git a/src/SimplenewsMailjetSubscriptionsMailjetHandler.php b/src/SimplenewsMailjetSubscriptionsMailjetHandler.php
 | 
			
		||||
new file mode 100644
 | 
			
		||||
index 0000000..a68cbf1
 | 
			
		||||
--- /dev/null
 | 
			
		||||
+++ b/src/SimplenewsMailjetSubscriptionsMailjetHandler.php
 | 
			
		||||
@@ -0,0 +1,47 @@
 | 
			
		||||
+<?php
 | 
			
		||||
+
 | 
			
		||||
+namespace Drupal\simplenews_mailjet_subscriptions;
 | 
			
		||||
+
 | 
			
		||||
+use Mailjet\Resources;
 | 
			
		||||
+use Drupal\mailjet\MailjetHandler;
 | 
			
		||||
+
 | 
			
		||||
+/**
 | 
			
		||||
+ * Implement SimplenewsMailjetSubscriptionsMailjetHandler interface.
 | 
			
		||||
+ */
 | 
			
		||||
+class SimplenewsMailjetSubscriptionsMailjetHandler extends MailjetHandler implements SimplenewsMailjetSubscriptionsMailjetHandlerInterface{
 | 
			
		||||
+
 | 
			
		||||
+  /**
 | 
			
		||||
+   * {@inheritdoc}
 | 
			
		||||
+   *
 | 
			
		||||
+   * @return array|null
 | 
			
		||||
+   *   Return array with result or null.
 | 
			
		||||
+   */
 | 
			
		||||
+  public function createMailjetContact($email): ?array {
 | 
			
		||||
+    
 | 
			
		||||
+    $response = $this->mailjetClient->post(Resources::$Contact, ["body" => ['Email' => $email]]);
 | 
			
		||||
+
 | 
			
		||||
+    if ($response->success()) {
 | 
			
		||||
+      return $response->getData();
 | 
			
		||||
+    }
 | 
			
		||||
+
 | 
			
		||||
+    return NULL;
 | 
			
		||||
+  }
 | 
			
		||||
+
 | 
			
		||||
+  /**
 | 
			
		||||
+   * {@inheritdoc}
 | 
			
		||||
+   *
 | 
			
		||||
+   * @return array|null
 | 
			
		||||
+   *   Return array with result or null.
 | 
			
		||||
+   */
 | 
			
		||||
+  public function getMailjetContactByEmail($email): ?array {
 | 
			
		||||
+    
 | 
			
		||||
+    $response = $this->mailjetClient->get(Resources::$Contact, ['id' => $email]);
 | 
			
		||||
+
 | 
			
		||||
+    if ($response->success()) {
 | 
			
		||||
+      return $response->getData();
 | 
			
		||||
+    }
 | 
			
		||||
+
 | 
			
		||||
+    return NULL;
 | 
			
		||||
+  }
 | 
			
		||||
+
 | 
			
		||||
+}
 | 
			
		||||
\ No newline at end of file
 | 
			
		||||
diff --git a/src/SimplenewsMailjetSubscriptionsMailjetHandlerInterface.php b/src/SimplenewsMailjetSubscriptionsMailjetHandlerInterface.php
 | 
			
		||||
new file mode 100644
 | 
			
		||||
index 0000000..b6484d7
 | 
			
		||||
--- /dev/null
 | 
			
		||||
+++ b/src/SimplenewsMailjetSubscriptionsMailjetHandlerInterface.php
 | 
			
		||||
@@ -0,0 +1,34 @@
 | 
			
		||||
+<?php
 | 
			
		||||
+
 | 
			
		||||
+namespace Drupal\simplenews_mailjet_subscriptions;
 | 
			
		||||
+
 | 
			
		||||
+use Drupal\mailjet\MailjetHandlerInterface;
 | 
			
		||||
+
 | 
			
		||||
+/**
 | 
			
		||||
+ * Interface for mailjet handler.
 | 
			
		||||
+ */
 | 
			
		||||
+interface SimplenewsMailjetSubscriptionsMailjetHandlerInterface extends MailjetHandlerInterface {
 | 
			
		||||
+
 | 
			
		||||
+  /**
 | 
			
		||||
+   * create contact.
 | 
			
		||||
+   *
 | 
			
		||||
+   * @param string $email
 | 
			
		||||
+   *   email of the contact
 | 
			
		||||
+   *
 | 
			
		||||
+   * @return array|null
 | 
			
		||||
+   *   Return array with the result or null.
 | 
			
		||||
+   */
 | 
			
		||||
+  public function createMailjetContact($email): ?array;
 | 
			
		||||
+
 | 
			
		||||
+  /**
 | 
			
		||||
+   * Get contact by email.
 | 
			
		||||
+   *
 | 
			
		||||
+   * @param string $email
 | 
			
		||||
+   *   email of the contact
 | 
			
		||||
+   *
 | 
			
		||||
+   * @return array|null
 | 
			
		||||
+   *   Return array with the result or null.
 | 
			
		||||
+   */
 | 
			
		||||
+  public function getMailjetContactByEmail($email): ?array;
 | 
			
		||||
+
 | 
			
		||||
+}
 | 
			
		||||
\ No newline at end of file
 | 
			
		||||
diff --git a/src/SimplenewsMailjetSubscriptionsUtilities.php b/src/SimplenewsMailjetSubscriptionsUtilities.php
 | 
			
		||||
index 950ff8a..5cb351d 100644
 | 
			
		||||
--- a/src/SimplenewsMailjetSubscriptionsUtilities.php
 | 
			
		||||
+++ b/src/SimplenewsMailjetSubscriptionsUtilities.php
 | 
			
		||||
@@ -2,141 +2,122 @@
 | 
			
		||||
 
 | 
			
		||||
 namespace Drupal\simplenews_mailjet_subscriptions;
 | 
			
		||||
 
 | 
			
		||||
+// use Drupal\mailjet\MailjetHandlerInterface;
 | 
			
		||||
+use Drupal\simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsMailjetHandlerInterface;
 | 
			
		||||
+
 | 
			
		||||
 /**
 | 
			
		||||
- * Provides a list of Simplenews Mailjet Subscription entities.
 | 
			
		||||
+ * Utilisties
 | 
			
		||||
  */
 | 
			
		||||
 class SimplenewsMailjetSubscriptionsUtilities {
 | 
			
		||||
 
 | 
			
		||||
   /**
 | 
			
		||||
-   * Check Subscriber.
 | 
			
		||||
+   * MailjetHandler service.
 | 
			
		||||
+   *
 | 
			
		||||
+   * @var \Drupal\Simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsMailjetHandlerInterface
 | 
			
		||||
+   */
 | 
			
		||||
+  protected $mailjetHandler;
 | 
			
		||||
+
 | 
			
		||||
+/**
 | 
			
		||||
+   * SubscribeEmailForm constructor.
 | 
			
		||||
+   *
 | 
			
		||||
+   * @param \Drupal\Simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsMailjetHandlerInterface $mailjetHandler
 | 
			
		||||
+   *   The mailjet handler service.
 | 
			
		||||
    */
 | 
			
		||||
-  private static function checkSubscriber($api_key, $secret_key, $encoded_mail) {
 | 
			
		||||
-
 | 
			
		||||
-    // Check if contact exist
 | 
			
		||||
-    // verifica se o contacto ja tem opções.
 | 
			
		||||
-    $endpoint = 'https://api.mailjet.com/v3/REST/contactdata/' . $encoded_mail;
 | 
			
		||||
-    $ch = curl_init();
 | 
			
		||||
-    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
 | 
			
		||||
-    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
 | 
			
		||||
-    curl_setopt($ch, CURLOPT_USERPWD, $api_key . ':' . $secret_key);
 | 
			
		||||
-    curl_setopt($ch, CURLOPT_URL, $endpoint);
 | 
			
		||||
-    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 | 
			
		||||
-    $response = curl_exec($ch);
 | 
			
		||||
-    curl_close($ch);
 | 
			
		||||
-    $json = json_decode($response, TRUE);
 | 
			
		||||
-
 | 
			
		||||
-    return $json;
 | 
			
		||||
+  public function __construct(SimplenewsMailjetSubscriptionsMailjetHandlerInterface $mailjetHandler) {
 | 
			
		||||
+    $this->mailjetHandler = $mailjetHandler;
 | 
			
		||||
   }
 | 
			
		||||
 
 | 
			
		||||
   /**
 | 
			
		||||
-   * Update Subscriber.
 | 
			
		||||
+   * {@inheritDoc}
 | 
			
		||||
    */
 | 
			
		||||
-  private static function updateSubscriber($api_key, $secret_key, $listID, $subscriber_email, $contact_property, $newsletter_property_name, $action) {
 | 
			
		||||
-
 | 
			
		||||
-    // Add contact to mailjet.
 | 
			
		||||
-    $endpoint = 'https://api.mailjet.com/v3/REST/contactslist/' . $listID . '/managecontact';
 | 
			
		||||
-    $message = [
 | 
			
		||||
-      "Properties" => [
 | 
			
		||||
-        $contact_property => $newsletter_property_name,
 | 
			
		||||
-      ],
 | 
			
		||||
-      "Action" => $action,
 | 
			
		||||
-      "Email" => $subscriber_email,
 | 
			
		||||
-    ];
 | 
			
		||||
-    $payload = json_encode($message);
 | 
			
		||||
-
 | 
			
		||||
-    $ch = curl_init();
 | 
			
		||||
-    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
 | 
			
		||||
-    curl_setopt($ch, CURLOPT_POST, 1);
 | 
			
		||||
-    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
 | 
			
		||||
-    curl_setopt($ch, CURLOPT_USERPWD, $api_key . ':' . $secret_key);
 | 
			
		||||
-    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
 | 
			
		||||
-    curl_setopt($ch, CURLOPT_URL, $endpoint);
 | 
			
		||||
-    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 | 
			
		||||
-    $response = curl_exec($ch);
 | 
			
		||||
-    curl_close($ch);
 | 
			
		||||
-    $json = json_decode($response, TRUE);
 | 
			
		||||
-
 | 
			
		||||
-    return $json;
 | 
			
		||||
+  public static function create(ContainerInterface $container) {
 | 
			
		||||
+    return new static(
 | 
			
		||||
+      $container->get('simplenews_mailjet_subscriptions.handler')
 | 
			
		||||
+    );
 | 
			
		||||
+  }
 | 
			
		||||
+
 | 
			
		||||
+  public function checkSubscriber($encoded_mail) {
 | 
			
		||||
+    $response = $this->mailjetHandler->getMailjetContactByEmail($encoded_mail);
 | 
			
		||||
+    if ($response) {
 | 
			
		||||
+      // contact alredy exists
 | 
			
		||||
+      $contact_id = $response[0]['ID'];
 | 
			
		||||
+      return $contact_id;
 | 
			
		||||
+    }
 | 
			
		||||
+
 | 
			
		||||
+    return null;
 | 
			
		||||
+  }
 | 
			
		||||
+
 | 
			
		||||
+  public function createSubscriber($email) {
 | 
			
		||||
+    $response = $this->mailjetHandler->createMailjetContact($email);
 | 
			
		||||
+    if ($response) {
 | 
			
		||||
+      // contact alredy exists
 | 
			
		||||
+      $contact_id = $response[0]['ID'];
 | 
			
		||||
+      return $contact_id;
 | 
			
		||||
+    }
 | 
			
		||||
+
 | 
			
		||||
+    return null;
 | 
			
		||||
   }
 | 
			
		||||
 
 | 
			
		||||
   /**
 | 
			
		||||
    * Subscribe User.
 | 
			
		||||
    */
 | 
			
		||||
-  public static function subscribeUser($subscriber, $subscription, $category, $api_key, $secret_key, $listID) {
 | 
			
		||||
+  public function subscribeUser($listID, $subscriber, $category) {
 | 
			
		||||
 
 | 
			
		||||
-    $contact_property = $category['contact_property'];
 | 
			
		||||
-    $newsletter_property_name = $category['newsletter_property_name'];
 | 
			
		||||
     $subscriber_email = $subscriber->getMail();
 | 
			
		||||
-    $encoded_mail = urlencode($subscriber_email);
 | 
			
		||||
-    $response = '';
 | 
			
		||||
 
 | 
			
		||||
-    $contact_info = self::checkSubscriber($api_key, $secret_key, $encoded_mail);
 | 
			
		||||
+    // hook is sometimes called with empty subscriber
 | 
			
		||||
+    if (!$subscriber_email) return;
 | 
			
		||||
 
 | 
			
		||||
-    $options = $contact_info['Data'];
 | 
			
		||||
+    $encoded_mail = urlencode($subscriber_email);
 | 
			
		||||
 
 | 
			
		||||
-    // Se existe - acrescenta a opções.
 | 
			
		||||
-    if ($options[0]['Data']) {
 | 
			
		||||
-      foreach ($options[0]['Data'] as $property) {
 | 
			
		||||
-        if ($property["Name"] == $contact_property) {
 | 
			
		||||
+    $contactid = $this->checkSubscriber($encoded_mail);
 | 
			
		||||
+    if (!$contactid) {
 | 
			
		||||
+      // subscriber does not exists yet, create it
 | 
			
		||||
+      $contactid = $this->createSubscriber($subscriber_email);
 | 
			
		||||
+    }
 | 
			
		||||
 
 | 
			
		||||
-          if (is_int(strpos($property['Value'], $newsletter_property_name))) {
 | 
			
		||||
-            $newsletter_property_name = $property['Value'];
 | 
			
		||||
-          }
 | 
			
		||||
-          else {
 | 
			
		||||
-            $newsletter_property_name = $property['Value'] . ';' . $newsletter_property_name;
 | 
			
		||||
-          }
 | 
			
		||||
-        }
 | 
			
		||||
+    if ($contactid) {
 | 
			
		||||
+      $contact = [
 | 
			
		||||
+        'Email' => $subscriber_email,
 | 
			
		||||
+        // 'Properties' => [
 | 
			
		||||
+        //   $contact_property => $newsletter_property_name
 | 
			
		||||
+        // ]
 | 
			
		||||
+      ];
 | 
			
		||||
+
 | 
			
		||||
+      $response = $this->mailjetHandler->syncMailjetContact($listID, $contact, 'addnoforce');
 | 
			
		||||
+      if ($response) {
 | 
			
		||||
+        // $contact_id = $response['Data'][0]['ContactID'];
 | 
			
		||||
+        \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $subscriber_email . ' subscribed to the ' . $listID . ' list.');
 | 
			
		||||
+      }else{
 | 
			
		||||
+        \Drupal::logger('simplenews_mailjet_subscriptions')->warning('error while subscrining ' . $subscriber_email . ' to the ' . $listID . ' list.');
 | 
			
		||||
       }
 | 
			
		||||
+
 | 
			
		||||
+    } else {
 | 
			
		||||
+      \Drupal::logger('simplenews_mailjet_subscriptions')->warning($subscriber_email . ' does not exists and can not be created on mailjet.');
 | 
			
		||||
     }
 | 
			
		||||
 
 | 
			
		||||
-    $response = self::updateSubscriber($api_key, $secret_key, $listID, $subscriber_email, $contact_property, $newsletter_property_name, 'addnoforce');
 | 
			
		||||
-    $contact_id = $response['Data'][0]['ContactID'];
 | 
			
		||||
-    \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 . '.');
 | 
			
		||||
-    return $response;
 | 
			
		||||
+
 | 
			
		||||
   }
 | 
			
		||||
 
 | 
			
		||||
   /**
 | 
			
		||||
    * Unsubscribe User.
 | 
			
		||||
    */
 | 
			
		||||
-  public static function unsubscribeUser($subscriber, $subscription, $category, $api_key, $secret_key, $listID) {
 | 
			
		||||
-    $contact_property = $category['contact_property'];
 | 
			
		||||
-    $newsletter_property_name = $category['newsletter_property_name'];
 | 
			
		||||
+  public function unsubscribeUser($listID, $subscriber, $category) {
 | 
			
		||||
     $subscriber_email = $subscriber->getMail();
 | 
			
		||||
-    $encoded_mail = urlencode($subscriber_email);
 | 
			
		||||
-    $response = '';
 | 
			
		||||
-
 | 
			
		||||
-    $contact_info = self::checkSubscriber($api_key, $secret_key, $encoded_mail);
 | 
			
		||||
-    $contact_id = $contact_info['ContactID'];
 | 
			
		||||
-    $options = $contact_info['Data'];
 | 
			
		||||
-
 | 
			
		||||
-    if ($options[0]['Data']) {
 | 
			
		||||
-      foreach ($options[0]['Data'] as $property) {
 | 
			
		||||
-        if ($property["Name"] == $contact_property) {
 | 
			
		||||
-          if (substr_count($property['Value'], ';') == 0) {
 | 
			
		||||
-            $newsletter_property_name = '';
 | 
			
		||||
-            $response = self::updateSubscriber($api_key, $secret_key, $listID, $subscriber_email, $contact_property, $newsletter_property_name, 'remove');
 | 
			
		||||
-            $contact_id = $response['Data'][0]['ContactID'];
 | 
			
		||||
-            \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $contact_id . ' unsubscribed to the list ' . $listID . '.');
 | 
			
		||||
-          }
 | 
			
		||||
-          else {
 | 
			
		||||
-            $pos = strpos($property['Value'], $newsletter_property_name);
 | 
			
		||||
-            \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 . '.');
 | 
			
		||||
-            if ($pos === 0) {
 | 
			
		||||
-              $newsletter_property_name = str_replace($newsletter_property_name . ';', '', $property['Value']);
 | 
			
		||||
-            }
 | 
			
		||||
-            else {
 | 
			
		||||
-              $newsletter_property_name = str_replace(';' . $newsletter_property_name, '', $property['Value']);
 | 
			
		||||
-            }
 | 
			
		||||
-            $response = self::updateSubscriber($api_key, $secret_key, $listID, $subscriber_email, $contact_property, $newsletter_property_name, 'addnoforce');
 | 
			
		||||
-            $contact_id = $response['Data'][0]['ContactID'];
 | 
			
		||||
-            \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 . '.');
 | 
			
		||||
-
 | 
			
		||||
-          }
 | 
			
		||||
-        }
 | 
			
		||||
-      }
 | 
			
		||||
-    }
 | 
			
		||||
+    $contact = [
 | 
			
		||||
+      'Email' => $subscriber_email,
 | 
			
		||||
+      // 'Properties' => [
 | 
			
		||||
+      //   $contact_property => $newsletter_property_name
 | 
			
		||||
+      // ]
 | 
			
		||||
+    ];
 | 
			
		||||
 
 | 
			
		||||
-    return $response;
 | 
			
		||||
+    $response = $this->mailjetHandler->syncMailjetContact($listID, $contact, 'remove');
 | 
			
		||||
+    if ($response) {
 | 
			
		||||
+      // $contact_id = $response['Data'][0]['ContactID'];
 | 
			
		||||
+      \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $subscriber_email . ' removed from the ' . $listID . ' list.');
 | 
			
		||||
+    }else{
 | 
			
		||||
+      \Drupal::logger('simplenews_mailjet_subscriptions')->warning('error while removing ' . $subscriber_email . ' from the ' . $listID . ' list.');
 | 
			
		||||
+    }
 | 
			
		||||
   }
 | 
			
		||||
 
 | 
			
		||||
 }
 | 
			
		||||
 Submodule web/profiles/d8-starterkit-profile updated: 5667e1b15a...b1619b75ec
									
								
							
		Reference in New Issue
	
	Block a user