From 382062c22249fcbf8ae7c6e72f3bd9ca8d3522c6 Mon Sep 17 00:00:00 2001 From: bencroker Date: Tue, 26 Mar 2024 08:00:54 -0600 Subject: [PATCH] Mark changed email attribute as dirty --- CHANGELOG.md | 6 ++++++ src/elements/ContactElement.php | 30 ++++++++++++++++++++++++++++++ src/services/SyncService.php | 2 -- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d9bdd66f..17e7b037 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Release Notes for Campaign +## 2.13.1 - Unreleased + +### Fixed + +- Fixed a bug in which newly created contacts were not being indexed for searching if only an email address was the only field added ([#463](https://github.com/putyourlightson/craft-campaign/issues/463)). + ## 2.13.0 - 2024-03-25 ### Added diff --git a/src/elements/ContactElement.php b/src/elements/ContactElement.php index e8580191..bd4f9c38 100755 --- a/src/elements/ContactElement.php +++ b/src/elements/ContactElement.php @@ -391,6 +391,12 @@ protected static function defineDefaultTableAttributes(string $source): array */ public ?string $subscriptionStatus = null; + /** + * @var string|null The initial email value, if there was one. + * @see getDirtyAttributes() + */ + private ?string $savedEmail = null; + /** * @var null|FieldLayout Field layout * @see getFieldLayout() @@ -405,6 +411,16 @@ public function __toString(): string return $this->email ?? ''; } + /** + * @inheritdoc + */ + public function init(): void + { + parent::init(); + + $this->savedEmail = $this->email; + } + /** * @inheritdoc */ @@ -765,6 +781,20 @@ public function getHasRoundedThumb(): bool return true; } + /** + * @inheritdoc + */ + public function getDirtyAttributes(): array + { + $dirtyAttributes = parent::getDirtyAttributes(); + + if ($this->email !== $this->savedEmail && !in_array('email', $dirtyAttributes)) { + $dirtyAttributes[] = 'email'; + } + + return $dirtyAttributes; + } + /** * @inheritdoc * @since 2.0.0 diff --git a/src/services/SyncService.php b/src/services/SyncService.php index 77182ee6..99b73f91 100755 --- a/src/services/SyncService.php +++ b/src/services/SyncService.php @@ -191,12 +191,10 @@ public function syncUserMailingList(User $user, MailingListElement $mailingList) $contactMailingListRecord = new ContactMailingListRecord(); $contactMailingListRecord->contactId = $contact->id; $contactMailingListRecord->mailingListId = $mailingList->id; - $contactMailingListRecord->subscriptionStatus = 'subscribed'; $contactMailingListRecord->subscribed = new DateTime(); $contactMailingListRecord->sourceType = 'user'; $contactMailingListRecord->source = $user->id; - $contactMailingListRecord->save(); } // If user is not active and contact mailing list record exists then delete it elseif ($user->status != User::STATUS_ACTIVE && $contactMailingListRecord !== null) {