Skip to content

Commit

Permalink
Mark changed email attribute as dirty
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Mar 26, 2024
1 parent 30d15b5 commit 382062c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
30 changes: 30 additions & 0 deletions src/elements/ContactElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -405,6 +411,16 @@ public function __toString(): string
return $this->email ?? '';
}

/**
* @inheritdoc
*/
public function init(): void
{
parent::init();

$this->savedEmail = $this->email;
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/services/SyncService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 382062c

Please sign in to comment.