🔖 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,4 +1,5 @@
<?php
declare(strict_types=1);
namespace Civi\Mailinglistsync;
@ -30,7 +31,7 @@ class GroupMailingList extends BaseMailingList {
* Create a new GroupMailingList.
*
* @param string $title
* @param string $description
* @param ?string $description
* @param string $email
* @param string|null $sid
*
@ -39,22 +40,28 @@ class GroupMailingList extends BaseMailingList {
*/
public static function createGroup(
string $title,
string $description,
string $email,
string $description = NULL,
string $sid = NULL
): self {
try {
$request = \Civi\Api4\Group::create()
->addValue('title', $title)
->addValue('description', $description)
->addValue(static::CUSTOM_GROUP_NAME . '.Email', $email)
->addValue(static::CUSTOM_GROUP_NAME . '.E_mail_address', $email)
->addValue(static::CUSTOM_GROUP_NAME . '.Enable_mailing_list', 1)
->addValue('is_active', 1);
// If the group is an AD group, add the AD SID
if (!empty($values['sid'])) {
$request->addValue(static::CUSTOM_GROUP_NAME . '.SID', $values['sid']);
// Add the description if it's not empty
if (!empty($description)) {
$request->addValue('description', $description);
}
return new self($request->execute()->getArrayCopy()['id']);
// If the group is an AD group, add the AD SID
if (!empty($sid)) {
$request->addValue(static::CUSTOM_GROUP_NAME . '.Active_Directory_SID', $sid);
}
$group = $request->execute()->first();
return empty($sid) ? new self($group): new ADGroupMailingList($group);
}
catch (\Exception $e) {
throw new MailinglistException(
@ -78,7 +85,7 @@ class GroupMailingList extends BaseMailingList {
* @return bool
*/
public function isADGroup(): bool {
return !empty($this->group[static::CUSTOM_GROUP_NAME . '.Active_Directory_UUID']);
return !empty($this->group[static::CUSTOM_GROUP_NAME . '.Active_Directory_SID']);
}
/**
@ -91,11 +98,12 @@ class GroupMailingList extends BaseMailingList {
try {
$recipientData = Contact::get()
->addSelect('id', 'first_name', 'last_name', 'email.email', 'Active_Directory.SID')
->addJoin('Email AS email', 'INNER', ['id', '=', 'email.contact_id'])
->addJoin('GroupContact AS group_contact', 'INNER', ['id', '=', 'group_contact.contact_id'])
->addJoin('LocationType AS location_type', 'INNER', ['email.location_type_id', '=', 'location_type.id'])
->addWhere('group_contact.group_id', '=', $this->group['id'])
->addWhere('location_type.name', '=', static::LOCATION_TYPE)
->addJoin('Email AS email', 'LEFT', ['id', '=', 'email.contact_id'])
->addJoin('GroupContact AS group_contact', 'INNER',
['id', '=', 'group_contact.contact_id'],
['group_contact.group_id', '=', $this->group['id']],
['group_contact.status', '=', '"Added"'],
)
->addGroupBy('id')
->execute()
->getArrayCopy();
@ -109,13 +117,20 @@ class GroupMailingList extends BaseMailingList {
$recipients = [];
foreach ($recipientData as $recipient) {
$recipients[$recipient['email.email']] = new MailingListRecipient(
contact_id: $recipient['id'],
first_name: $recipient['first_name'],
last_name: $recipient['last_name'],
email: $recipient['email.email'],
sid: $recipient['Active_Directory.SID'],
);
try {
$recipients[$recipient['email.email']] = new MailingListRecipient(
sid: $recipient['Active_Directory.SID'],
contact_id: $recipient['id'],
first_name: $recipient['first_name'],
last_name: $recipient['last_name'],
email: $recipient['email.email'],
);
} catch (\Exception $e) {
throw new MailinglistException(
"Could not create recipient object for contact with id '{$recipient['id']}': {$e->getMessage()}",
MailinglistException::ERROR_CODE_GET_RECIPIENTS_FAILED
);
}
}
return $recipients;
@ -139,7 +154,7 @@ class GroupMailingList extends BaseMailingList {
* @return string
*/
public function getGroupDescription(): string {
return $this->group['description'];
return $this->group['description'] ?? '';
}
/**
@ -148,10 +163,86 @@ class GroupMailingList extends BaseMailingList {
* @param string $description
*
* @return void
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
*/
public function updateGroupDescription(string $description): void {
$this->update(['description' => $description]);
}
/**
* Add a contact to this mailing lists related group.
*
* @param int $contactId
*
* @return void
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
*/
protected function addContactToGroup(int $contactId): void {
try {
\Civi\Api4\GroupContact::create()
->addValue('contact_id', $contactId)
->addValue('group_id', $this->group['id'])
->execute();
}
catch (\Exception $e) {
if ($e->getMessage() === 'DB Error: already exists') {
$this->reAddContactToGroup($contactId);
}
else {
throw new MailinglistException(
"Could not add contact with id '$contactId' to group with id '{$this->group['id']}': $e",
MailinglistException::ERROR_CODE_ADD_CONTACT_TO_GROUP_FAILED
);
}
}
}
/**
* Remove a contact from this mailing lists related group.
*
* @param int $contactId
*
* @return void
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
*/
protected function removeContactFromGroup(int $contactId): void {
try {
\Civi\Api4\GroupContact::update()
->addWhere('contact_id', '=', $contactId)
->addWhere('group_id', '=', $this->group['id'])
->addValue('status', 'Removed')
->execute();
}
catch (\Exception $e) {
throw new MailinglistException(
"Could not remove contact with id '$contactId' from group with id '{$this->group['id']}'\n$e",
MailinglistException::ERROR_CODE_REMOVE_CONTACT_FROM_GROUP_FAILED
);
}
}
/**
* If a group_contact entry already exists, but is marked as removed,
* re-add the contact to the group.
*
* @param int $contactId
*
* @return void
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
*/
private function reAddContactToGroup(int $contactId): void {
try {
\Civi\Api4\GroupContact::update()
->addWhere('contact_id', '=', $contactId)
->addWhere('group_id', '=', $this->group['id'])
->addValue('status', 'Added')
->execute();
}
catch (\Exception $e) {
throw new MailinglistException(
"Could not re-add contact with id '$contactId' to group with id '{$this->group['id']}': $e",
MailinglistException::ERROR_CODE_ADD_CONTACT_TO_GROUP_FAILED
);
}
}
}