🔖 Version 1.0.0-beta

This commit is contained in:
Marc Koch 2025-03-20 16:01:05 +01:00
parent c93a06972b
commit 460a811554
Signed by: marc.koch
GPG key ID: 12406554CFB028B9
26 changed files with 2480 additions and 771 deletions

View file

@ -1,17 +1,17 @@
<?php
declare(strict_types=1);
namespace Civi\Mailinglistsync;
use Civi\API\Exception\UnauthorizedException;
use Civi\Api4\CustomField;
use Civi\Core\Lock\NullLock;
use Civi\Mailinglistsync\Exceptions\MailinglistException;
use CRM_Mailinglistsync_ExtensionUtil as E;
use Civi\Api4\MockBasicEntity;
use Civi\Api4\LocationType;
use Dompdf\Exception;
abstract class BaseMailingList {
abstract class BaseMailingList
{
public const GROUP_MAILING_LIST = 'group';
@ -20,6 +20,7 @@ abstract class BaseMailingList {
public const CUSTOM_GROUP_NAME = 'BASE_Mailing_List';
protected const RELATED_CLASS = MockBasicEntity::class;
protected const RELATED_TYPE = 'base';
protected MailingListManager $mailingListManager;
@ -38,12 +39,13 @@ abstract class BaseMailingList {
*
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
*/
function __construct(int|array $entity = NULL) {
function __construct(int|array $entity = NULL)
{
$this->updateData = [];
if ($entity) {
$this->load($entity);
}
$this->mailingListManager = new MailingListManager();
$this->mailingListManager = MailingListManager::getInstance();
}
/**
@ -54,7 +56,8 @@ abstract class BaseMailingList {
* @return void
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
*/
protected function load(array|int $entity): void {
protected function load(array|int $entity): void
{
if (is_int($entity)) {
$id = $entity;
try {
@ -64,16 +67,16 @@ abstract class BaseMailingList {
->addWhere('id', '=', $id)
->execute()
->first();
$this->setEntity($entity);
}
catch (UnauthorizedException) {
if (!empty($entity)) {
$this->setEntity($entity);
}
} catch (UnauthorizedException) {
$type = static::RELATED_TYPE;
throw new MailinglistException(
"Could not get $type with id '$id' via API4 due to insufficient permissions",
MailinglistException::ERROR_CODE_PERMISSION_DENIED
);
}
catch (\Exception) {
} catch (\Exception) {
$type = static::RELATED_TYPE;
throw new MailinglistException(
"Could not get $type with id '$id' via API4",
@ -90,7 +93,8 @@ abstract class BaseMailingList {
* @throws UnauthorizedException
* @throws \CRM_Core_Exception
*/
static protected function getCustomFields(): array {
static protected function getCustomFields(): array
{
return CustomField::get()
->addSelect('*')
->addWhere('custom_group_id:name', '=', static::CUSTOM_GROUP_NAME)
@ -98,6 +102,34 @@ abstract class BaseMailingList {
->getArrayCopy();
}
/**
* Maps field names from forms (e.g. custom_12_287) to custom field names.
*
* @param $fields
* @return array
* @throws UnauthorizedException
* @throws \CRM_Core_Exception
*/
static public function translateCustomFields($fields): array
{
$customFields = self::getCustomFields();
$mapping = [];
foreach ($customFields as $customField) {
$fieldId = $customField['id'];
$fieldName = "custom_$fieldId";
$getCustomField = function () use ($fields, $fieldName) {
foreach ($fields as $key => $value) {
if (preg_match("/^$fieldName" . "_.*/", $key)) {
return ['field_name' => $key, 'value' => $value];
}
}
return NULL;
};
$mapping[$customField['name']] = $getCustomField();
}
return $mapping;
}
/**
* Validate e-mail address.
*
@ -105,10 +137,25 @@ abstract class BaseMailingList {
*
* @return bool
*/
static public function validateEmailAddress(string $emailAddress): bool {
static public function validateEmailAddress(string $emailAddress): bool
{
return (bool) filter_var($emailAddress, FILTER_VALIDATE_EMAIL);
}
/**
* Checks if the domain of the e-mail address is the same as the configured
* domain.
*
* @param string $emailAddress
*
* @return bool
*/
static public function validateEmailDomain(string $emailAddress): bool
{
$domain = explode('@', $emailAddress)[1];
return MailingListSettings::get('domain') === $domain;
}
/**
* Validate custom fields.
*
@ -121,7 +168,8 @@ abstract class BaseMailingList {
* @throws \Civi\API\Exception\UnauthorizedException
* @throws \Exception
*/
static public function validateCustomFields(array $fields, \CRM_Core_Form $form, array &$errors): bool {
static public function validateCustomFields(array $fields, \CRM_Core_Form $form, array &$errors): bool
{
$result = TRUE;
$customFields = self::getCustomFields();
$customValues = [];
@ -130,36 +178,27 @@ abstract class BaseMailingList {
if ($form instanceof \CRM_Group_Form_Edit) {
$entityId = $form->getEntityId();
$type = self::GROUP_MAILING_LIST;
}
elseif ($form instanceof \CRM_Event_Form_ManageEvent_EventInfo) {
} elseif ($form instanceof \CRM_Event_Form_ManageEvent_EventInfo) {
$entityId = $form->getEventID(); // It's things like this...
$type = self::EVENT_MAILING_LIST;
}
else {
} else {
throw new \Exception('Unknown form type');
}
// Translate custom field names
foreach ($customFields as $customField) {
$fieldId = $customField['id'];
$fieldName = "custom_$fieldId";
$getCustomField = function() use ($fields, $fieldName) {
foreach ($fields as $key => $value) {
if (preg_match("/^$fieldName" . "_.*/", $key)) {
return ['field_name' => $key, 'value' => $value];
}
}
return NULL;
};
$customValues[$customField['name']] = $getCustomField();
}
$customValues = self::translateCustomFields($fields);
// Validate e-mail address
if (!empty($customValues['E_mail_address'])) {
if (!empty($customValues['E_mail_address']['value'])) {
if (!self::validateEmailAddress($customValues['E_mail_address']['value'])) {
$errors[$customValues['E_mail_address']['field_name']] = E::ts('Invalid e-mail address');
$result = FALSE;
}
if (!self::validateEmailDomain($customValues['E_mail_address']['value'])) {
$errors[$customValues['E_mail_address']['field_name']] = E::ts(
'E-mail address does not match the configured domain');
$result = FALSE;
}
}
// E-mail address must be unique for groups and events
@ -206,7 +245,8 @@ abstract class BaseMailingList {
* @throws \CRM_Core_Exception
* @throws \Civi\API\Exception\UnauthorizedException
*/
static public function getByEmailAddress(string $emailAddress, int $excludeId = NULL): ?self {
static public function getByEmailAddress(string $emailAddress, int $excludeId = NULL): ?self
{
$result = static::RELATED_CLASS::get()
->addSelect('*')
->addSelect('custom.*')
@ -236,20 +276,45 @@ abstract class BaseMailingList {
protected abstract function setEntity(array $value): void;
/**
* Check if the group is a mailing list.
* Check if this mailing list is enabled
*
* @return bool
*/
public function isMailingList(): bool {
return (bool) $this->getEntity()[static::CUSTOM_GROUP_NAME . '.Enable_mailing_list'];
public function isEnabled(): bool
{
if (empty($this->getEntity())) {
return FALSE;
}
return (bool)$this->getEntity()[static::CUSTOM_GROUP_NAME . '.Enable_mailing_list'];
}
/**
* Check if the group is an AD mailing list.
* Check if this is a group.
*
* @return bool
*/
public function isADGroup(): bool {
public function isGroup(): bool
{
return $this::RELATED_CLASS === \Civi\Api4\Group::class;
}
/**
* Check if this is an event.
*
* @return bool
*/
public function isEvent(): bool
{
return $this::RELATED_CLASS === \Civi\Api4\Event::class;
}
/**
* Check if this is an AD mailing list.
*
* @return bool
*/
public function isADGroup(): bool
{
return FALSE;
}
@ -258,7 +323,8 @@ abstract class BaseMailingList {
*
* @param array $values
*/
protected function update(array $values): void {
protected function update(array $values): void
{
$this->updateData += $values;
}
@ -268,7 +334,8 @@ abstract class BaseMailingList {
* @return void
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
*/
public function save(): void {
public function save(): void
{
if (empty($this->updateData)) {
return;
}
@ -277,8 +344,7 @@ abstract class BaseMailingList {
->setValues($this->updateData)
->addWhere('id', '=', $this->getEntity()['id'])
->execute();
}
catch (\Exception $e) {
} catch (\Exception $e) {
$type = static::RELATED_TYPE;
throw new MailinglistException(
"Could not update $type with id '{$this->getEntity()['id']}'\n$e",
@ -292,7 +358,8 @@ abstract class BaseMailingList {
*
* @return string
*/
public function getEmailAddress(): string {
public function getEmailAddress(): string
{
return $this->getEntity()[static::CUSTOM_GROUP_NAME . '.E_mail_address'];
}
@ -301,7 +368,8 @@ abstract class BaseMailingList {
*
* @return string
*/
public function getTitle(): string {
public function getTitle(): string
{
return $this->getEntity()['title'];
}
@ -313,26 +381,24 @@ abstract class BaseMailingList {
* @return void
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
*/
public function updateEmailAddress(string $emailAddress): void {
public function updateEmailAddress(string $emailAddress): void
{
// Validate email address
if (ADGroupMailingList::validateEmailAddress($emailAddress)) {
if (!ADGroupMailingList::validateEmailAddress($emailAddress)) {
throw new MailinglistException(
E::ts("Invalid e-mail address '%1'", [1 => $emailAddress]),
"Invalid e-mail address '$emailAddress'",
MailinglistException::ERROR_CODE_INVALID_EMAIL_ADDRESS
);
}
try {
static::update([
static::CUSTOM_GROUP_NAME . '.E_mail_address' => $emailAddress,
]);
}
catch (MailinglistException) {
$type = static::RELATED_TYPE;
if (!ADGroupMailingList::validateEmailDomain($emailAddress)) {
throw new MailinglistException(
"Could not update e-mail address for $type with id '{$this->getEntity()['id']}'",
MailinglistException::ERROR_CODE_UPDATE_EMAIL_ADDRESS_FAILED
"E-mail address '$emailAddress' does not match the configured domain",
MailinglistException::ERROR_CODE_EMAIL_DOMAIN_MISMATCH
);
}
static::update([
static::CUSTOM_GROUP_NAME . '.E_mail_address' => $emailAddress,
]);
}
/**
@ -343,7 +409,8 @@ abstract class BaseMailingList {
*
* @return string
*/
public static function getLocationTypeName(): string {
public static function getLocationTypeName(): string
{
return static::LOCATION_TYPE;
}
@ -359,8 +426,9 @@ abstract class BaseMailingList {
*
* @return int
*/
public function getId(): int {
return (int) $this->getEntity()['id'];
public function getId(): int
{
return (int)$this->getEntity()['id'];
}
/**
@ -372,36 +440,46 @@ abstract class BaseMailingList {
* @param string|null $lastName
* @param string|null $email
* @param string|null $sid
* @param array|null $searchBy ['sid', 'email', 'names']
*
* @return array|null ['contact' => $contact, 'found_by' => $found_by]
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
*/
protected function findExistingContact(
protected static function findExistingContact(
string $firstName = NULL,
string $lastName = NULL,
string $email = NULL,
string $sid = NULL,
): ?array {
array $searchBy = NULL
): ?array
{
// Set default search parameters.
if (empty($searchBy)) {
$searchBy = ['sid', 'email', 'names'];
}
// Get the tags to restrict the search to.
$tags = \Civi::settings()->get(E::SHORT_NAME . '_ad_contact_tags');
// Prepare the get call for reuse.
$prepareGetEmail = function () use ($tags,) {
$prepareGetEmail = function () use ($tags) {
$selectFields = [
'id',
'first_name',
'last_name',
'email.email',
GroupMailingList::CUSTOM_GROUP_NAME . '.Active_Directory_SID'
'email.location_type_id.name',
'Active_Directory.SID',
];
$call = \Civi\Api4\Contact::get()
$call = \Civi\Api4\Contact::get()
->addSelect(...$selectFields)
->addJoin('Email AS email', 'LEFT', ['id', '=', 'email.contact_id']);
->addJoin('Email AS email', 'LEFT',
['id', '=', 'email.contact_id'])
->addGroupBy('id');
if ($tags) {
$call->addJoin('EntityTag AS entity_tag', 'LEFT',
$call->addJoin('EntityTag AS entity_tag', 'INNER',
['id', '=', 'entity_tag.entity_id'],
['entity_tag.entity_table', '=', 'civicrm_contact'],
['entity_tag.entity_table', '=', '"civicrm_contact"'],
['entity_tag.tag_id', 'IN', $tags]
);
}
@ -409,61 +487,60 @@ abstract class BaseMailingList {
};
// Try to find the contact by the SID.
try {
$contact = $prepareGetEmail()
->addWhere(GroupMailingList::CUSTOM_GROUP_NAME . '.Active_Directory_SID', '=', $sid)
->execute()
->getArrayCopy();
}
catch (\Exception $e) {
throw new MailinglistException(
"Could not get contact by SID '$sid': $e",
MailinglistException::ERROR_CODE_GET_CONTACT_FAILED,
);
}
if (count($contact) > 1) {
throw new MailinglistException(
"Multiple contacts with the same SID '$sid' found",
MailinglistException::ERROR_CODE_MULTIPLE_CONTACTS_FOUND
);
}
elseif (count($contact) === 1) {
return ['contact' => array_pop($contact), 'found_by' => 'sid'];
if (in_array('sid', $searchBy)) {
try {
$contact = $prepareGetEmail()
->addWhere('Active_Directory.SID', '=', $sid)
->execute()
->getArrayCopy();
} catch (\Exception $e) {
throw new MailinglistException(
"Could not get contact by SID '$sid': $e",
MailinglistException::ERROR_CODE_GET_CONTACT_FAILED,
);
}
if (count($contact) > 1) {
throw new MailinglistException(
"Multiple contacts with the same SID '$sid' found",
MailinglistException::ERROR_CODE_MULTIPLE_CONTACTS_FOUND
);
} elseif (count($contact) === 1) {
return ['contact' => array_pop($contact), 'found_by' => 'sid'];
}
}
// Try fo find the contact by the e-mail address.
try {
$contact = $prepareGetEmail()
->addWhere('email', '=', $email)
->execute()
->getArrayCopy();
}
catch (\Exception $e) {
throw new MailinglistException(
"Could not get contact by e-mail address '$email': $e",
MailinglistException::ERROR_CODE_GET_CONTACT_FAILED,
);
}
if (count($contact) > 1) {
throw new MailinglistException(
"Multiple contacts with the same e-mail address '$email' found",
MailinglistException::ERROR_CODE_MULTIPLE_CONTACTS_FOUND
);
}
elseif (count($contact) === 1) {
return ['contact' => array_pop($contact), 'found_by' => 'email'];
if (in_array('email', $searchBy)) {
try {
$contact = $prepareGetEmail()
->addWhere('email.email', '=', $email)
->execute()
->getArrayCopy();
} catch (\Exception $e) {
throw new MailinglistException(
"Could not get contact by e-mail address '$email': $e",
MailinglistException::ERROR_CODE_GET_CONTACT_FAILED,
);
}
if (count($contact) > 1) {
throw new MailinglistException(
"Multiple contacts with the same e-mail address '$email' found",
MailinglistException::ERROR_CODE_MULTIPLE_CONTACTS_FOUND
);
} elseif (count($contact) === 1) {
return ['contact' => array_pop($contact), 'found_by' => 'email'];
}
}
// Try to find the contact by the first and last name and only if the tags are set.
if ($tags) {
if (in_array('names', $searchBy) && $tags) {
try {
$contact = $prepareGetEmail()
->addWhere('first_name', '=', $firstName)
->addWhere('last_name', '=', $lastName)
->execute()
->getArrayCopy();
}
catch (\Exception $e) {
} catch (\Exception $e) {
throw new MailinglistException(
"Could not get contact by first and last name '$firstName $lastName': $e",
MailinglistException::ERROR_CODE_GET_CONTACT_FAILED,
@ -474,8 +551,7 @@ abstract class BaseMailingList {
"Multiple contacts with the same first and last name found",
MailinglistException::ERROR_CODE_MULTIPLE_CONTACTS_FOUND
);
}
elseif (count($contact) === 1) {
} elseif (count($contact) === 1) {
return ['contact' => array_pop($contact), 'found_by' => 'names'];
}
}
@ -483,4 +559,157 @@ abstract class BaseMailingList {
// If no contact was found, return NULL.
return NULL;
}
/**
* Is this mailing list externally available?
*
* @return bool
*/
function isExternallyAvailable(): bool
{
return (bool)$this->getEntity()[static::CUSTOM_GROUP_NAME . '.Can_be_reached_externally'];
}
/**
* Get the subject prefix.
*
* @return ?string
*/
function getSubjectPrefix(): ?string
{
return $this->getEntity()[static::CUSTOM_GROUP_NAME . '.Subject_prefix'];
}
/**
* Synchronize this mailing list with mlmmj.
*
* @return array
*
*/
function sync(): array
{
$result = [];
$updateValues = [];
$mlmmjApi = MailingListApi::getInstance();
try {
// Get mailinglist
$mailinglist = $mlmmjApi->getMailingList($this->getEmailAddress());
// Create mailinglist if it does not exist yet
if (empty($mailinglist)) {
$mlmmjApi->createMailingList($this->getEmailAddress(), [
'close_list' => 'yes',
'only_subscriber_can_post' => $this->isExternallyAvailable() ? 'no' : 'yes',
'subject_prefix' => $this->getSubjectPrefix() ?? '',
]);
$result['created'] = TRUE;
} // If the mailing list exists, check for updates
else {
$data = $mailinglist['_data'];
$externallyAvailable = $data['only_subscriber_can_post'] === 'no';
$subjectPrefix = $data['subject_prefix'];
// Identify changes
if ($externallyAvailable !== $this->isExternallyAvailable()) {
$updateValues['only_subscriber_can_post'] = $this->isExternallyAvailable() ? 'no' : 'yes';
}
if ($subjectPrefix !== ($this->getSubjectPrefix() ?? '')) {
$updateValues['subject_prefix'] = $this->getSubjectPrefix() ?? '';
}
// Update the mlmmj mailing list
if (!empty($updateValues)) {
$mlmmjApi->updateMailingList(
$this->getEmailAddress(),
$updateValues,
);
$result['updated'] = $updateValues;
}
}
// Compare recipients
$civicrmRecipients = $this->getRecipients();
$civicrmRecipientsEmails = array_map(function ($recipient) {
return $recipient->getEmail();
}, $civicrmRecipients);
$mlmmjRecipients = $mlmmjApi->getMailingListSubscribers($this->getEmailAddress());
// Add and remove recipients
$recipientsToAdd = array_diff($civicrmRecipientsEmails, $mlmmjRecipients);
$recipientsToRemove = array_diff($mlmmjRecipients, $civicrmRecipientsEmails);
// Add recipients
if (!empty($recipientsToAdd) || !empty($recipientsToRemove)) {
$mlmmjApi->updateSubscribers(
$this->getEmailAddress(),
$recipientsToAdd,
$recipientsToRemove,
);
$result['recipients'] = [
'added' => array_values($recipientsToAdd),
'removed' => array_values($recipientsToRemove),
];
}
// Create contact activities
$added = array_filter($civicrmRecipients, function ($recipient) use ($recipientsToAdd) {
return in_array($recipient->getEmail(), $recipientsToAdd);
});
$removed = array_filter($civicrmRecipients, function ($recipient) use ($recipientsToRemove) {
return in_array($recipient->getEmail(), $recipientsToRemove);
});
// Create activities for added contacts
foreach ($added as $recipient) {
/* @var MailingListRecipient $recipient */
try {
MailingListRecipient::createSyncActivity(
$recipient->getContactId(),
'Completed',
E::ts('Contact added to mailing list'),
E::ts("This contact has been added to the mailing list '%1'.",
[1 => $this->getEmailAddress()])
);
} catch (MailinglistException $e) {
\Civi::log(E::LONG_NAME)->error($e->getLogMessage());
}
}
// Create activities for removed contacts
foreach ($removed as $recipient) {
/* @var MailingListRecipient $recipient */
try {
MailingListRecipient::createSyncActivity(
$recipient->getContactId(),
'Completed',
E::ts('Contact removed from mailing list'),
E::ts("This contact has been removed from the mailing list '%1'.",
[1 => $this->getEmailAddress()])
);
} catch (MailinglistException $e) {
\Civi::log(E::LONG_NAME)->error($e->getLogMessage());
}
}
} catch (\Exception $e) {
\Civi::log(E::LONG_NAME)->error($e);
$result['is_error'] = 1;
$result['error'] = $e->getMessage();
}
return $result;
}
/**
* Delete the corresponding mailing list on mlmmj and the email address.
*
* @return void
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
*/
function delete(): void
{
$mlmmjApi = MailingListApi::getInstance();
$mlmmjApi->deleteMailingList($this->getEmailAddress());
}
}