🔖 Version 1.0.0-beta
This commit is contained in:
parent
c93a06972b
commit
460a811554
26 changed files with 2480 additions and 771 deletions
|
@ -25,6 +25,12 @@ class CRM_Mailinglistsync_Form_MailingListSettings extends CRM_Core_Form {
|
||||||
label: E::ts('Enable logging of synchronization'),
|
label: E::ts('Enable logging of synchronization'),
|
||||||
extra: ['class' => 'huge'],
|
extra: ['class' => 'huge'],
|
||||||
);
|
);
|
||||||
|
$this->add(
|
||||||
|
'text',
|
||||||
|
E::SHORT_NAME . '_domain',
|
||||||
|
label: E::ts('Domain for mailing list email addresses'),
|
||||||
|
extra: ['class' => 'huge'],
|
||||||
|
);
|
||||||
$this->add(
|
$this->add(
|
||||||
'text',
|
'text',
|
||||||
E::SHORT_NAME . '_mlmmj_host',
|
E::SHORT_NAME . '_mlmmj_host',
|
||||||
|
|
|
@ -32,7 +32,7 @@ class CRM_Mailinglistsync_Upgrader extends \CRM_Extension_Upgrader_Base {
|
||||||
// $this->ctx->log->info(E::ts('Created %1 LocationType', [1 => $location['name']]));
|
// $this->ctx->log->info(E::ts('Created %1 LocationType', [1 => $location['name']]));
|
||||||
}
|
}
|
||||||
catch (\CRM_Core_Exception $e) {
|
catch (\CRM_Core_Exception $e) {
|
||||||
if ($e->getMessage() == 'DB Error: already exists') {
|
if ($e->getMessage() === 'DB Error: already exists') {
|
||||||
// $this->ctx->log->info(E::ts('LocationType %1 already exists', [1 => $location['name']]));
|
// $this->ctx->log->info(E::ts('LocationType %1 already exists', [1 => $location['name']]));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -40,6 +40,28 @@ class CRM_Mailinglistsync_Upgrader extends \CRM_Extension_Upgrader_Base {
|
||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create activity types
|
||||||
|
$activityTypes = require_once E::path() . '/resources/activity_types.php';
|
||||||
|
foreach ($activityTypes as $activity) {
|
||||||
|
try {
|
||||||
|
\Civi\Api4\OptionValue::create(FALSE)
|
||||||
|
->addValue('option_group_id.name', 'activity_type')
|
||||||
|
->addValue('label', $activity['label'])
|
||||||
|
->addValue('name', $activity['name'])
|
||||||
|
->addValue('description', $activity['description'])
|
||||||
|
->execute();
|
||||||
|
// $this->ctx->log->info(E::ts('Created %1 ActivityType', [1 => $activity['name']]));
|
||||||
|
}
|
||||||
|
catch (\CRM_Core_Exception $e) {
|
||||||
|
if ($e->getMessage() === 'DB Error: already exists') {
|
||||||
|
// $this->ctx->log->info(E::ts('ActivityType %1 already exists', [1 => $activity['name']]));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// $this->ctx->log->err(E::ts('Failed to create %1 ActivityType', [1 => $activity['name']]));
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -84,6 +106,16 @@ class CRM_Mailinglistsync_Upgrader extends \CRM_Extension_Upgrader_Base {
|
||||||
->execute();
|
->execute();
|
||||||
// $this->ctx->log->info(E::ts('Enabled %1 CustomGroup', [1 => $customGroup]));
|
// $this->ctx->log->info(E::ts('Enabled %1 CustomGroup', [1 => $customGroup]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create scheduled job
|
||||||
|
\Civi\Api4\Job::create(FALSE)
|
||||||
|
->addValue('run_frequency', 'Hourly')
|
||||||
|
->addValue('name', 'Mlmmjsync')
|
||||||
|
->addValue('api_entity', 'Mailinglistsync')
|
||||||
|
->addValue('api_action', 'Mlmmjsync')
|
||||||
|
->addValue('description', E::ts('Runs the synchronization between CiviCRM and mlmmj.'))
|
||||||
|
->addValue('is_active', TRUE)
|
||||||
|
->execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -100,6 +132,11 @@ class CRM_Mailinglistsync_Upgrader extends \CRM_Extension_Upgrader_Base {
|
||||||
->execute();
|
->execute();
|
||||||
// $this->ctx->log->info(E::ts('Disabled %1 CustomGroup', [1 => $customGroup]));
|
// $this->ctx->log->info(E::ts('Disabled %1 CustomGroup', [1 => $customGroup]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Delete scheduled job
|
||||||
|
\Civi\Api4\Job::delete(FALSE)
|
||||||
|
->addWhere('name', '=', 'Mlmmjsync')
|
||||||
|
->execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -108,12 +145,10 @@ class CRM_Mailinglistsync_Upgrader extends \CRM_Extension_Upgrader_Base {
|
||||||
* @return TRUE on success
|
* @return TRUE on success
|
||||||
* @throws CRM_Core_Exception
|
* @throws CRM_Core_Exception
|
||||||
*/
|
*/
|
||||||
// public function upgrade_4200(): bool {
|
public function upgrade_4200(): bool {
|
||||||
// $this->ctx->log->info('Applying update 4200');
|
$this->install();
|
||||||
// CRM_Core_DAO::executeQuery('UPDATE foo SET bar = "whiz"');
|
return TRUE;
|
||||||
// CRM_Core_DAO::executeQuery('DELETE FROM bang WHERE willy = wonka(2)');
|
}
|
||||||
// return TRUE;
|
|
||||||
// }
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Example: Run an external SQL script.
|
* Example: Run an external SQL script.
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
<?php /** @noinspection SpellCheckingInspection */
|
<?php /** @noinspection SpellCheckingInspection */
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Civi\Mailinglistsync;
|
namespace Civi\Mailinglistsync;
|
||||||
|
|
||||||
|
@ -7,6 +8,14 @@ use Civi\Mailinglistsync\Exceptions\MailinglistException;
|
||||||
|
|
||||||
class ADGroupMailingList extends GroupMailingList {
|
class ADGroupMailingList extends GroupMailingList {
|
||||||
|
|
||||||
|
const RECIPIENTS_ATTRIBUTE_MAP = [
|
||||||
|
// Map recipient attribute names to method names (get/update)
|
||||||
|
'sid' => 'Sid',
|
||||||
|
'email' => 'Email',
|
||||||
|
'first_name' => 'FirstName',
|
||||||
|
'last_name' => 'LastName',
|
||||||
|
];
|
||||||
|
|
||||||
public const LOCATION_TYPE = 'AD_Mailing_List_Address';
|
public const LOCATION_TYPE = 'AD_Mailing_List_Address';
|
||||||
|
|
||||||
protected string $sid;
|
protected string $sid;
|
||||||
|
@ -24,15 +33,13 @@ class ADGroupMailingList extends GroupMailingList {
|
||||||
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
||||||
*/
|
*/
|
||||||
public static function getBySID(string $sid): ?self {
|
public static function getBySID(string $sid): ?self {
|
||||||
$id = static::RELATED_CLASS::get()
|
$result = static::RELATED_CLASS::get()
|
||||||
->addSelect('id')
|
->addSelect('id')
|
||||||
->addWhere(static::CUSTOM_GROUP_NAME . '.Active_Directory_SID', '=', $sid)
|
->addWhere(static::CUSTOM_GROUP_NAME . '.Active_Directory_SID', '=', $sid)
|
||||||
->execute()
|
->execute()
|
||||||
->first()['id'];
|
->first();
|
||||||
|
|
||||||
$groups = \CRM_Contact_BAO_Group::getGroups(['id' => $id]);
|
return $result ? new self($result['id']) : NULL;
|
||||||
$groupId = array_pop($groups);
|
|
||||||
return $groupId ? new self($groupId) : NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -45,42 +52,192 @@ class ADGroupMailingList extends GroupMailingList {
|
||||||
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
||||||
*/
|
*/
|
||||||
public function syncRecipients(array $recipients): array {
|
public function syncRecipients(array $recipients): array {
|
||||||
|
|
||||||
$adGroupMembers = $this->getRecipients();
|
$adGroupMembers = $this->getRecipients();
|
||||||
|
|
||||||
// Add new recipients
|
// Add new recipients
|
||||||
$recipientsAdded = [];
|
$recipientsAdded = [];
|
||||||
foreach ($recipients as $recipient) {
|
foreach ($recipients as $recipient) {
|
||||||
if (empty($adGroupMembers[$recipient['sid']] ?? NULL)) {
|
if (empty($adGroupMembers[$recipient['sid']] ?? NULL)) {
|
||||||
$recipientsAdded[$recipient['sid']] = $this->addRecipient($recipient);
|
$contactId = NULL;
|
||||||
|
$recipientResult = $recipient + [
|
||||||
|
'is_error' => 0,
|
||||||
|
];
|
||||||
|
|
||||||
|
// Check if the contact already exists
|
||||||
|
$found = $this::findExistingContact(
|
||||||
|
firstName: $recipient['first_name'],
|
||||||
|
lastName: $recipient['last_name'],
|
||||||
|
email: $recipient['email'],
|
||||||
|
sid: $recipient['sid'],
|
||||||
|
);
|
||||||
|
|
||||||
|
// If the contac does not exist, add it
|
||||||
|
if ($found === NULL) {
|
||||||
|
try {
|
||||||
|
$newContact = MailingListRecipient::createContact(
|
||||||
|
email: $recipient['email'],
|
||||||
|
sid: $recipient['sid'],
|
||||||
|
locationType: static::LOCATION_TYPE,
|
||||||
|
firstName: $recipient['first_name'] ?? '',
|
||||||
|
lastName: $recipient['last_name'] ?? '',
|
||||||
|
);
|
||||||
|
|
||||||
|
$contactId = $newContact->getContactId();
|
||||||
|
$recipientResult['contact_id'] = $contactId;
|
||||||
|
$recipientResult['contact_created'] = TRUE;
|
||||||
|
|
||||||
|
// Create contact activity
|
||||||
|
try {
|
||||||
|
MailingListRecipient::createSyncActivity(
|
||||||
|
$contactId,
|
||||||
|
'Completed',
|
||||||
|
E::ts('Contact created by AD group synchronization'),
|
||||||
|
E::ts("This contact has been created by the AD group synchronization process for the group '%1'",
|
||||||
|
[1 => $this->getTitle()])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
catch (MailinglistException $e) {
|
||||||
|
$error = $e->getLogMessage();
|
||||||
|
\Civi::log(E::LONG_NAME)->error($error);
|
||||||
|
$recipientResult['is_error'] = 1;
|
||||||
|
$recipientResult['activity_error_message'] = $error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (MailinglistException $e) {
|
||||||
|
$recipientResult['is_error'] = 1;
|
||||||
|
$recipientResult['error_message'] = $e->getLogMessage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the contact exists, use the existing contact
|
||||||
|
else {
|
||||||
|
$contactId = $found['contact']['id'];
|
||||||
|
$recipientResult['contact_id'] = $contactId;
|
||||||
|
$resipientResult['found_by'] = $found['found_by'];
|
||||||
|
$recipientResult['contact_created'] = FALSE;
|
||||||
|
|
||||||
|
// If the contact was not found by SID, add the SID to the contact
|
||||||
|
if ($found['found_by'] !== 'sid') {
|
||||||
|
try {
|
||||||
|
$contact = new MailingListRecipient(contact_id: $contactId);
|
||||||
|
$contact->updateSid($recipient['sid']);
|
||||||
|
$contact->save();
|
||||||
|
|
||||||
|
$recipientResult['updated'] = [
|
||||||
|
'sid' => [
|
||||||
|
'is_error' => 0,
|
||||||
|
'old' => $found['contact']['sid'],
|
||||||
|
'new' => $recipient['sid'],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
catch (MailinglistException $e) {
|
||||||
|
$recipientResult['is_error'] = 1;
|
||||||
|
$recipientResult['error_message'] = $e->getLogMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create contact activity
|
||||||
|
try {
|
||||||
|
MailingListRecipient::createSyncActivity(
|
||||||
|
$contactId,
|
||||||
|
'Completed',
|
||||||
|
E::ts('Contact updated by AD contact synchronization'),
|
||||||
|
E::ts("This contact has been updated by the AD contact synchronization process")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
catch (MailinglistException $e) {
|
||||||
|
$error = $e->getLogMessage();
|
||||||
|
\Civi::log(E::LONG_NAME)->error($error);
|
||||||
|
$recipientResult['is_error'] = 1;
|
||||||
|
$recipientResult['activity_error_message'] = $error;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip adding the contact to the group if it already is a member
|
||||||
|
if (in_array(
|
||||||
|
$contactId,
|
||||||
|
array_column($adGroupMembers, 'contactId')
|
||||||
|
)) {
|
||||||
|
$recipientsAdded['recipients'][] = $recipientResult;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the contact to the mailing list
|
||||||
|
if ($contactId !== NULL) {
|
||||||
|
$recipientResult += $this->addRecipient($contactId);
|
||||||
|
|
||||||
|
// Create contact activity
|
||||||
|
try {
|
||||||
|
MailingListRecipient::createSyncActivity(
|
||||||
|
$contactId,
|
||||||
|
'Completed',
|
||||||
|
E::ts('Contact added to group'),
|
||||||
|
E::ts("This contact has been added to the group '%1' by AD group synchronization.",
|
||||||
|
[1 => $this->getTitle()])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
catch (MailinglistException $e) {
|
||||||
|
$error = $e->getLogMessage();
|
||||||
|
\Civi::log(E::LONG_NAME)->error($error);
|
||||||
|
$recipientResult['is_error'] = 1;
|
||||||
|
$recipientResult['activity_error_message'] = $error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$recipientResult['is_error'] = 1;
|
||||||
|
$recipientResult['error_message'] = 'Contact ID is NULL';
|
||||||
|
}
|
||||||
|
$recipientsAdded['recipients'][] = $recipientResult;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$recipientsAdded['count'] = count($recipientsAdded['recipients'] ?? []);
|
||||||
|
$recipientsAdded['error_count'] = count(
|
||||||
|
array_filter($recipientsAdded['recipients'] ?? [],
|
||||||
|
fn($recipient) => (bool) $recipient['is_error'])
|
||||||
|
);
|
||||||
|
|
||||||
// Remove recipients that are no longer in the list
|
// Remove recipients that are no longer in the list
|
||||||
$recipientsRemoved = [];
|
$recipientsRemoved = [];
|
||||||
foreach ($adGroupMembers as $adGroupMember) {
|
foreach ($adGroupMembers as $adGroupMember) {
|
||||||
/* @var \Civi\Mailinglistsync\MailingListRecipient $adGroupMember */
|
/* @var \Civi\Mailinglistsync\MailingListRecipient $adGroupMember */
|
||||||
if (!in_array($adGroupMember->getSid(), array_column($recipients, 'sid'))) {
|
if (!in_array($adGroupMember->getSid(), array_column($recipients, 'sid'))) {
|
||||||
$recipientsRemoved[$adGroupMember->getSid()] = $this->removeRecipient($adGroupMember);
|
$recipientsRemoved['recipients'][] = $this->removeRecipient($adGroupMember);
|
||||||
|
|
||||||
|
// Create contact activity
|
||||||
|
try {
|
||||||
|
MailingListRecipient::createSyncActivity(
|
||||||
|
$adGroupMember->getContactId(),
|
||||||
|
'Completed',
|
||||||
|
E::ts('Contact removed from group'),
|
||||||
|
E::ts("This contact has been removed from the group '%1' by AD group synchronization.",
|
||||||
|
[1 => $this->getTitle()])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
catch (MailinglistException $e) {
|
||||||
|
$error = $e->getLogMessage();
|
||||||
|
\Civi::log(E::LONG_NAME)->error($error);
|
||||||
|
$recipientResult['is_error'] = 1;
|
||||||
|
$recipientResult['activity_error_message'] = $error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
$recipientsRemoved['count'] = count($recipientsRemoved['recipients'] ?? []);
|
||||||
|
$recipientsRemoved['error_count'] = count(
|
||||||
|
array_filter($recipientsRemoved['recipients'] ?? [],
|
||||||
|
fn($recipient) => (bool) $recipient['is_error'])
|
||||||
|
);
|
||||||
|
|
||||||
// Update recipients
|
// Update recipients
|
||||||
$recipientsUpdated = [];
|
$recipientsUpdated = [];
|
||||||
|
|
||||||
$recipientAttributesMap = [
|
|
||||||
// Map recipient attribute names to method names (get/set)
|
|
||||||
'first_name' => 'FirstName',
|
|
||||||
'last_name' => 'LastName',
|
|
||||||
'email' => 'Email',
|
|
||||||
];
|
|
||||||
|
|
||||||
foreach ($recipients as $recipient) {
|
foreach ($recipients as $recipient) {
|
||||||
|
$recipientUpdated = $recipient;
|
||||||
$changed = FALSE;
|
$changed = FALSE;
|
||||||
|
|
||||||
// Find the group member by SID
|
// Find the group member by SID
|
||||||
$adGroupMemberArray = array_filter(
|
$adGroupMemberArray = array_filter(
|
||||||
$adGroupMembers,
|
$this->getRecipients(),
|
||||||
fn($adGroupMember) => $adGroupMember->getSid() === $recipient['sid']
|
fn($adGroupMember) => $adGroupMember->getSid() === $recipient['sid']
|
||||||
);
|
);
|
||||||
$count = count($adGroupMemberArray);
|
$count = count($adGroupMemberArray);
|
||||||
|
@ -88,54 +245,208 @@ class ADGroupMailingList extends GroupMailingList {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
elseif ($count > 1) {
|
elseif ($count > 1) {
|
||||||
throw new MailinglistException(
|
$recipientUpdated += [
|
||||||
"Multiple recipients with the same SID",
|
'is_error' => 1,
|
||||||
MailinglistException::ERROR_CODE_MULTIPLE_RECIPIENTS
|
'error_message' => "Multiple recipients found with SID '{$recipient['sid']}'",
|
||||||
);
|
];
|
||||||
}
|
}
|
||||||
/* @var \Civi\Mailinglistsync\MailingListRecipient $adGroupMember */
|
/* @var \Civi\Mailinglistsync\MailingListRecipient $adGroupMember */
|
||||||
$adGroupMember = array_pop($adGroupMemberArray);
|
$adGroupMember = array_pop($adGroupMemberArray);
|
||||||
|
|
||||||
|
// Create new email with AD_Mailing_List_Address location type if necessary
|
||||||
|
if (!in_array(static::LOCATION_TYPE, $adGroupMember->getEmailLocationTypes())) {
|
||||||
|
try {
|
||||||
|
$adGroupMember->createEmail($recipient['email'], static::LOCATION_TYPE);
|
||||||
|
$recipientUpdated['new_email_address_created'] = [
|
||||||
|
'is_error' => 0,
|
||||||
|
'email' => $recipient['email'],
|
||||||
|
'location_type' => static::LOCATION_TYPE,
|
||||||
|
];
|
||||||
|
$changed = TRUE;
|
||||||
|
}
|
||||||
|
catch (MailinglistException $e) {
|
||||||
|
$recipientUpdated['new_email_address_created']['is_error'] = 1;
|
||||||
|
$recipientUpdated['new_email_address_created']['error_message'] = $e->getLogMessage();
|
||||||
|
}
|
||||||
|
$recipientUpdated['is_error'] = $recipientUpdated['is_error'] ||
|
||||||
|
(int) $recipientUpdated['new_email_address_created']['is_error'];
|
||||||
|
}
|
||||||
|
|
||||||
// Update attributes if they have changed
|
// Update attributes if they have changed
|
||||||
foreach ($recipientAttributesMap as $attrName => $methodName) {
|
$recipientAttributes = self::RECIPIENTS_ATTRIBUTE_MAP;
|
||||||
|
unset($recipientAttributes['sid']);
|
||||||
|
foreach (self::RECIPIENTS_ATTRIBUTE_MAP as $attrName => $methodName) {
|
||||||
$changed = self::updateRecipient(
|
$changed = self::updateRecipient(
|
||||||
$adGroupMember,
|
|
||||||
$recipient,
|
$recipient,
|
||||||
$attrName,
|
$attrName,
|
||||||
fn() => $adGroupMember->{"get$methodName"}(),
|
fn() => $adGroupMember->{"get$methodName"}(),
|
||||||
fn($value) => $adGroupMember->{"set$methodName"}($value),
|
fn($value) => $adGroupMember->{"update$methodName"}($value),
|
||||||
$recipientsUpdated,
|
$recipientUpdated,
|
||||||
) || $changed;
|
) || $changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply changes
|
|
||||||
if ($changed) {
|
if ($changed) {
|
||||||
|
// Apply changes
|
||||||
|
try {
|
||||||
$adGroupMember->save();
|
$adGroupMember->save();
|
||||||
}
|
}
|
||||||
|
catch (MailinglistException $e) {
|
||||||
|
$recipientUpdated['is_error'] = 1;
|
||||||
|
$recipientUpdated['error_message'] = $e->getLogMessage();
|
||||||
|
}
|
||||||
|
$recipientUpdated['is_error'] = (int) ($recipientUpdated['is_error'] ||
|
||||||
|
!empty(array_filter(
|
||||||
|
$recipientUpdated['updated'] ?? [],
|
||||||
|
fn($update) => (bool) $update['is_error']
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Count errors
|
if ($changed) {
|
||||||
$errorCount = count(array_filter(
|
$recipientsUpdated['recipients'][] = $recipientUpdated;
|
||||||
$recipientsAdded,
|
}
|
||||||
fn($recipient) => $recipient['is_error']
|
}
|
||||||
));
|
|
||||||
$errorCount += count(array_filter(
|
$recipientsUpdated['count'] = count($recipientsUpdated['recipients'] ?? []);
|
||||||
$recipientsRemoved,
|
$recipientsUpdated['error_count'] = count(
|
||||||
fn($recipient) => $recipient['is_error']
|
array_filter($recipientsUpdated['recipients'] ?? [],
|
||||||
));
|
fn($recipient) => (bool) $recipient['is_error'])
|
||||||
$errorCount += count(array_filter(
|
);
|
||||||
$recipientsUpdated,
|
|
||||||
fn($recipient) => array_filter($recipient, fn($attr) => $attr['is_error'])
|
// Count results
|
||||||
));
|
$resultCount = $recipientsAdded['count']
|
||||||
|
+ $recipientsRemoved['count']
|
||||||
|
+ $recipientsUpdated['count'];
|
||||||
|
$errorCount = $recipientsAdded['error_count']
|
||||||
|
+ $recipientsRemoved['error_count']
|
||||||
|
+ $recipientsUpdated['error_count'];
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
'count' => $resultCount,
|
||||||
'error_count' => $errorCount,
|
'error_count' => $errorCount,
|
||||||
'recipients_added' => $recipientsAdded,
|
'is_error' => (int) ($errorCount > 0),
|
||||||
'recipients_removed' => $recipientsRemoved,
|
'added' => $recipientsAdded,
|
||||||
'recipients_updated' => $recipientsUpdated,
|
'removed' => $recipientsRemoved,
|
||||||
|
'updated' => $recipientsUpdated,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates contacts with values coming from the AD.
|
||||||
|
*
|
||||||
|
* @param $recipients
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
* @throws \Civi\Mailinglistsync\Exceptions\ContactSyncException
|
||||||
|
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
static function syncContacts($recipients
|
||||||
|
): array {
|
||||||
|
$results = [];
|
||||||
|
|
||||||
|
foreach ($recipients as $contact) {
|
||||||
|
$result = [
|
||||||
|
'sid' => $contact['sid'] ?? 'unknown',
|
||||||
|
'first_name' => $contact['first_name'] ?? 'unknown',
|
||||||
|
'last_name' => $contact['last_name'] ?? 'unknown',
|
||||||
|
'email' => $contact['email'] ?? 'unknown',
|
||||||
|
];
|
||||||
|
|
||||||
|
// Check for required fields
|
||||||
|
foreach (self::RECIPIENTS_ATTRIBUTE_MAP as $attrName => $_) {
|
||||||
|
$missing = [];
|
||||||
|
if (empty($contact[$attrName])) {
|
||||||
|
$missing[] = $attrName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!empty($missing)) {
|
||||||
|
$result += [
|
||||||
|
'is_error' => 1,
|
||||||
|
'error_message' => E::ts('Missing required attributes: %1', [1 => implode(', ', $missing)]),
|
||||||
|
];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to find an existing contact
|
||||||
|
try {
|
||||||
|
$contactSearch = self::findExistingContact(
|
||||||
|
firstName: $contact['first_name'],
|
||||||
|
lastName: $contact['last_name'],
|
||||||
|
email: $contact['email'],
|
||||||
|
sid: $contact['sid'],
|
||||||
|
searchBy: ['sid'],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
catch (MailinglistException $e) {
|
||||||
|
$result += [
|
||||||
|
'is_error' => 1,
|
||||||
|
'error_message' => $e->getLogMessage(),
|
||||||
|
];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ($contactSearch === NULL) {
|
||||||
|
$result += [
|
||||||
|
'is_error' => 1,
|
||||||
|
'error_message' => 'No contact found for SID %1',
|
||||||
|
[1 => $contact['sid']],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update Contact
|
||||||
|
else {
|
||||||
|
$recipient = new MailingListRecipient(
|
||||||
|
sid: $contact['sid'],
|
||||||
|
contact_id: $contactSearch['contact']['id'],
|
||||||
|
first_name: $contactSearch['contact']['first_name'],
|
||||||
|
last_name: $contactSearch['contact']['last_name'],
|
||||||
|
email: $contactSearch['contact']['email.email'],
|
||||||
|
);
|
||||||
|
|
||||||
|
// Update attributes if they have changed;
|
||||||
|
$changed = FALSE;
|
||||||
|
$recipientAttributes = self::RECIPIENTS_ATTRIBUTE_MAP;
|
||||||
|
unset($recipientAttributes['sid']);
|
||||||
|
foreach (self::RECIPIENTS_ATTRIBUTE_MAP as $attrName => $methodName) {
|
||||||
|
$changed = self::updateRecipient(
|
||||||
|
$contact,
|
||||||
|
$attrName,
|
||||||
|
fn() => $recipient->{"get$methodName"}(),
|
||||||
|
fn($value) => $recipient->{"update$methodName"}($value),
|
||||||
|
$result,
|
||||||
|
) || $changed;
|
||||||
|
}
|
||||||
|
if ($changed) {
|
||||||
|
// Apply changes
|
||||||
|
try {
|
||||||
|
$recipient->save();
|
||||||
|
$result += [
|
||||||
|
'is_error' => 0,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
catch (MailinglistException $e) {
|
||||||
|
$result += [
|
||||||
|
'is_error' => 1,
|
||||||
|
'error_message' => $e->getLogMessage(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Count errors in updated attributes
|
||||||
|
$result['error_count'] = count(
|
||||||
|
array_filter($result['updated'] ?? [],
|
||||||
|
fn($update) => (bool) $update['is_error'])
|
||||||
|
);
|
||||||
|
$result['is_error'] = (int) (($result['is_error'] ?? FALSE) || $result['error_count'] > 0);
|
||||||
|
}
|
||||||
|
if ((($changed ?? FALSE) === TRUE) || $result['is_error']) {
|
||||||
|
$results['updated'][] = $result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$results['count'] = count($results['updated'] ?? []);
|
||||||
|
$results['error_count'] = array_sum(array_column($results['updated'] ?? [], 'error_count'));
|
||||||
|
return ($results['count'] || $results['error_count']) ? $results : [];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a list of recipients indexed by SID.
|
* Get a list of recipients indexed by SID.
|
||||||
*
|
*
|
||||||
|
@ -143,47 +454,86 @@ class ADGroupMailingList extends GroupMailingList {
|
||||||
* @return array
|
* @return array
|
||||||
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
||||||
*/
|
*/
|
||||||
public function getRecipients(): array {
|
public
|
||||||
|
function getRecipients(): array {
|
||||||
$recipients = parent::getRecipients();
|
$recipients = parent::getRecipients();
|
||||||
return array_column($recipients, null, fn($recipient) => $recipient->getSid());
|
$recipientsBySid = [];
|
||||||
|
foreach ($recipients as $recipient) {
|
||||||
|
$recipientsBySid[$recipient->getSid()] = $recipient;
|
||||||
|
}
|
||||||
|
return $recipientsBySid;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function addRecipient(array $recipient): array {
|
/**
|
||||||
$result = ['recipient' => $recipient];
|
* Add a recipient to the group related to this mailing list.
|
||||||
|
*
|
||||||
|
* @param int $contactId
|
||||||
|
*
|
||||||
|
* @return array|array[]
|
||||||
|
*/
|
||||||
|
private
|
||||||
|
function addRecipient(int $contactId
|
||||||
|
): array {
|
||||||
|
$result = [];
|
||||||
|
// Add the contact to the group
|
||||||
try {
|
try {
|
||||||
// Try to find an existing contact
|
$this->addContactToGroup($contactId);
|
||||||
$contactSearch = $this->findExistingContact(
|
$result['added'] = TRUE;
|
||||||
firstName: $recipient['first_name'],
|
|
||||||
lastName: $recipient['last_name'],
|
|
||||||
email: $recipient['email'],
|
|
||||||
);
|
|
||||||
if ($contactSearch !== NULL) {
|
|
||||||
$result['is_error'] = FALSE;
|
|
||||||
$result['recipient']['contact_id'] = $contactSearch['contact']['id'];
|
|
||||||
$result['recipient']['found_by'] = $contactSearch['found_by'];
|
|
||||||
$result['recipient']['contact_created'] = FALSE;
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a new contact if none was found
|
|
||||||
$newRecipient = MailingListRecipient::createContact(
|
|
||||||
firstName: $recipient['first_name'],
|
|
||||||
lastName: $recipient['last_name'],
|
|
||||||
email: $recipient['email'],
|
|
||||||
sid: $recipient['sid'],
|
|
||||||
);
|
|
||||||
$result['is_error'] = FALSE;
|
|
||||||
$result['recipient']['contact_id'] = $newRecipient->getContactId();
|
|
||||||
$result['recipient']['contact_created'] = TRUE;
|
|
||||||
return $result;
|
|
||||||
}
|
}
|
||||||
catch (MailinglistException $e) {
|
catch (MailinglistException $e) {
|
||||||
$error = $e->getLogMessage();
|
$error = $e->getLogMessage();
|
||||||
\Civi::log(E::LONG_NAME)->error($error);
|
\Civi::log(E::LONG_NAME)->error($error);
|
||||||
$result['is_error'] = TRUE;
|
$result['is_error'] = 1;
|
||||||
|
$result['group_error_message'] = $error;
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove a recipient from the group related to this mailing list.
|
||||||
|
*
|
||||||
|
* @param \Civi\Mailinglistsync\MailingListRecipient $recipient
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
function removeRecipient(MailingListRecipient $recipient
|
||||||
|
): array {
|
||||||
|
$result = [
|
||||||
|
'sid' => $recipient->getSid(),
|
||||||
|
'contact_id' => $recipient->getContactId(),
|
||||||
|
'first_name' => $recipient->getFirstName(),
|
||||||
|
'last_name' => $recipient->getLastName(),
|
||||||
|
'email' => $recipient->getEmail(),
|
||||||
|
];
|
||||||
|
try {
|
||||||
|
$this->removeContactFromGroup($recipient->getContactId());
|
||||||
|
$result['is_error'] = 0;
|
||||||
|
|
||||||
|
// Create contact activity
|
||||||
|
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->getTitle()])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
catch (MailinglistException $e) {
|
||||||
|
$error = $e->getLogMessage();
|
||||||
|
\Civi::log(E::LONG_NAME)->error($error);
|
||||||
|
$result['is_error'] = 1;
|
||||||
|
$result['activity_error_message'] = $error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (MailinglistException $e) {
|
||||||
|
$error = $e->getLogMessage();
|
||||||
|
\Civi::log(E::LONG_NAME)->error($error);
|
||||||
|
$result['is_error'] = 1;
|
||||||
$result['error_message'] = $error;
|
$result['error_message'] = $error;
|
||||||
$result['recipient']['contact_created'] = FALSE;
|
}
|
||||||
|
finally {
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -192,7 +542,6 @@ class ADGroupMailingList extends GroupMailingList {
|
||||||
* Helper function to update recipient data dynamically.
|
* Helper function to update recipient data dynamically.
|
||||||
* OMG, is this DRY!
|
* OMG, is this DRY!
|
||||||
*
|
*
|
||||||
* @param \Civi\Mailinglistsync\MailingListRecipient $old
|
|
||||||
* @param array $new
|
* @param array $new
|
||||||
* @param string $attributeName
|
* @param string $attributeName
|
||||||
* @param callable $getter
|
* @param callable $getter
|
||||||
|
@ -201,8 +550,8 @@ class ADGroupMailingList extends GroupMailingList {
|
||||||
*
|
*
|
||||||
* @return bool TRUE if the recipient was updated, FALSE otherwise
|
* @return bool TRUE if the recipient was updated, FALSE otherwise
|
||||||
*/
|
*/
|
||||||
private static function updateRecipient(
|
private
|
||||||
MailingListRecipient $old,
|
static function updateRecipient(
|
||||||
array $new,
|
array $new,
|
||||||
string $attributeName,
|
string $attributeName,
|
||||||
callable $getter,
|
callable $getter,
|
||||||
|
@ -210,7 +559,7 @@ class ADGroupMailingList extends GroupMailingList {
|
||||||
array &$changes,
|
array &$changes,
|
||||||
): bool {
|
): bool {
|
||||||
$updated = FALSE;
|
$updated = FALSE;
|
||||||
if ($new[$attributeName] !== $getter()) {
|
if (strtoupper($new[$attributeName]) !== strtoupper($getter())) {
|
||||||
$error = NULL;
|
$error = NULL;
|
||||||
$oldValue = $getter();
|
$oldValue = $getter();
|
||||||
try {
|
try {
|
||||||
|
@ -222,16 +571,17 @@ class ADGroupMailingList extends GroupMailingList {
|
||||||
$error = $e->getLogMessage();
|
$error = $e->getLogMessage();
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
$changes[$old['sid']][$attributeName] = [
|
$changes['updated'][$attributeName] = [
|
||||||
'is_error' => $error !== NULL,
|
'is_error' => (int) ($error !== NULL),
|
||||||
'old' => $oldValue,
|
'old' => $oldValue,
|
||||||
'new' => $new[$attributeName],
|
'new' => $new[$attributeName],
|
||||||
];
|
];
|
||||||
if ($error !== NULL) {
|
if ($error !== NULL) {
|
||||||
$changes[$old['sid']][$attributeName]['error'] = $error;
|
$changes['updated'][$attributeName]['error_message'] = $error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $updated;
|
return $updated;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,17 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Civi\Mailinglistsync;
|
namespace Civi\Mailinglistsync;
|
||||||
|
|
||||||
use Civi\API\Exception\UnauthorizedException;
|
use Civi\API\Exception\UnauthorizedException;
|
||||||
use Civi\Api4\CustomField;
|
use Civi\Api4\CustomField;
|
||||||
use Civi\Core\Lock\NullLock;
|
|
||||||
use Civi\Mailinglistsync\Exceptions\MailinglistException;
|
use Civi\Mailinglistsync\Exceptions\MailinglistException;
|
||||||
use CRM_Mailinglistsync_ExtensionUtil as E;
|
use CRM_Mailinglistsync_ExtensionUtil as E;
|
||||||
use Civi\Api4\MockBasicEntity;
|
use Civi\Api4\MockBasicEntity;
|
||||||
use Civi\Api4\LocationType;
|
|
||||||
use Dompdf\Exception;
|
|
||||||
|
|
||||||
abstract class BaseMailingList {
|
abstract class BaseMailingList
|
||||||
|
{
|
||||||
|
|
||||||
public const GROUP_MAILING_LIST = 'group';
|
public const GROUP_MAILING_LIST = 'group';
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@ abstract class BaseMailingList {
|
||||||
public const CUSTOM_GROUP_NAME = 'BASE_Mailing_List';
|
public const CUSTOM_GROUP_NAME = 'BASE_Mailing_List';
|
||||||
|
|
||||||
protected const RELATED_CLASS = MockBasicEntity::class;
|
protected const RELATED_CLASS = MockBasicEntity::class;
|
||||||
|
|
||||||
protected const RELATED_TYPE = 'base';
|
protected const RELATED_TYPE = 'base';
|
||||||
|
|
||||||
protected MailingListManager $mailingListManager;
|
protected MailingListManager $mailingListManager;
|
||||||
|
@ -38,12 +39,13 @@ abstract class BaseMailingList {
|
||||||
*
|
*
|
||||||
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
||||||
*/
|
*/
|
||||||
function __construct(int|array $entity = NULL) {
|
function __construct(int|array $entity = NULL)
|
||||||
|
{
|
||||||
$this->updateData = [];
|
$this->updateData = [];
|
||||||
if ($entity) {
|
if ($entity) {
|
||||||
$this->load($entity);
|
$this->load($entity);
|
||||||
}
|
}
|
||||||
$this->mailingListManager = new MailingListManager();
|
$this->mailingListManager = MailingListManager::getInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -54,7 +56,8 @@ abstract class BaseMailingList {
|
||||||
* @return void
|
* @return void
|
||||||
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
||||||
*/
|
*/
|
||||||
protected function load(array|int $entity): void {
|
protected function load(array|int $entity): void
|
||||||
|
{
|
||||||
if (is_int($entity)) {
|
if (is_int($entity)) {
|
||||||
$id = $entity;
|
$id = $entity;
|
||||||
try {
|
try {
|
||||||
|
@ -64,16 +67,16 @@ abstract class BaseMailingList {
|
||||||
->addWhere('id', '=', $id)
|
->addWhere('id', '=', $id)
|
||||||
->execute()
|
->execute()
|
||||||
->first();
|
->first();
|
||||||
|
if (!empty($entity)) {
|
||||||
$this->setEntity($entity);
|
$this->setEntity($entity);
|
||||||
}
|
}
|
||||||
catch (UnauthorizedException) {
|
} catch (UnauthorizedException) {
|
||||||
$type = static::RELATED_TYPE;
|
$type = static::RELATED_TYPE;
|
||||||
throw new MailinglistException(
|
throw new MailinglistException(
|
||||||
"Could not get $type with id '$id' via API4 due to insufficient permissions",
|
"Could not get $type with id '$id' via API4 due to insufficient permissions",
|
||||||
MailinglistException::ERROR_CODE_PERMISSION_DENIED
|
MailinglistException::ERROR_CODE_PERMISSION_DENIED
|
||||||
);
|
);
|
||||||
}
|
} catch (\Exception) {
|
||||||
catch (\Exception) {
|
|
||||||
$type = static::RELATED_TYPE;
|
$type = static::RELATED_TYPE;
|
||||||
throw new MailinglistException(
|
throw new MailinglistException(
|
||||||
"Could not get $type with id '$id' via API4",
|
"Could not get $type with id '$id' via API4",
|
||||||
|
@ -90,7 +93,8 @@ abstract class BaseMailingList {
|
||||||
* @throws UnauthorizedException
|
* @throws UnauthorizedException
|
||||||
* @throws \CRM_Core_Exception
|
* @throws \CRM_Core_Exception
|
||||||
*/
|
*/
|
||||||
static protected function getCustomFields(): array {
|
static protected function getCustomFields(): array
|
||||||
|
{
|
||||||
return CustomField::get()
|
return CustomField::get()
|
||||||
->addSelect('*')
|
->addSelect('*')
|
||||||
->addWhere('custom_group_id:name', '=', static::CUSTOM_GROUP_NAME)
|
->addWhere('custom_group_id:name', '=', static::CUSTOM_GROUP_NAME)
|
||||||
|
@ -98,6 +102,34 @@ abstract class BaseMailingList {
|
||||||
->getArrayCopy();
|
->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.
|
* Validate e-mail address.
|
||||||
*
|
*
|
||||||
|
@ -105,10 +137,25 @@ abstract class BaseMailingList {
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
static public function validateEmailAddress(string $emailAddress): bool {
|
static public function validateEmailAddress(string $emailAddress): bool
|
||||||
|
{
|
||||||
return (bool) filter_var($emailAddress, FILTER_VALIDATE_EMAIL);
|
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.
|
* Validate custom fields.
|
||||||
*
|
*
|
||||||
|
@ -121,7 +168,8 @@ abstract class BaseMailingList {
|
||||||
* @throws \Civi\API\Exception\UnauthorizedException
|
* @throws \Civi\API\Exception\UnauthorizedException
|
||||||
* @throws \Exception
|
* @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;
|
$result = TRUE;
|
||||||
$customFields = self::getCustomFields();
|
$customFields = self::getCustomFields();
|
||||||
$customValues = [];
|
$customValues = [];
|
||||||
|
@ -130,36 +178,27 @@ abstract class BaseMailingList {
|
||||||
if ($form instanceof \CRM_Group_Form_Edit) {
|
if ($form instanceof \CRM_Group_Form_Edit) {
|
||||||
$entityId = $form->getEntityId();
|
$entityId = $form->getEntityId();
|
||||||
$type = self::GROUP_MAILING_LIST;
|
$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...
|
$entityId = $form->getEventID(); // It's things like this...
|
||||||
$type = self::EVENT_MAILING_LIST;
|
$type = self::EVENT_MAILING_LIST;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
throw new \Exception('Unknown form type');
|
throw new \Exception('Unknown form type');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Translate custom field names
|
// Translate custom field names
|
||||||
foreach ($customFields as $customField) {
|
$customValues = self::translateCustomFields($fields);
|
||||||
$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();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate e-mail address
|
// 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'])) {
|
if (!self::validateEmailAddress($customValues['E_mail_address']['value'])) {
|
||||||
$errors[$customValues['E_mail_address']['field_name']] = E::ts('Invalid e-mail address');
|
$errors[$customValues['E_mail_address']['field_name']] = E::ts('Invalid e-mail address');
|
||||||
$result = FALSE;
|
$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
|
// E-mail address must be unique for groups and events
|
||||||
|
@ -206,7 +245,8 @@ abstract class BaseMailingList {
|
||||||
* @throws \CRM_Core_Exception
|
* @throws \CRM_Core_Exception
|
||||||
* @throws \Civi\API\Exception\UnauthorizedException
|
* @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()
|
$result = static::RELATED_CLASS::get()
|
||||||
->addSelect('*')
|
->addSelect('*')
|
||||||
->addSelect('custom.*')
|
->addSelect('custom.*')
|
||||||
|
@ -236,20 +276,45 @@ abstract class BaseMailingList {
|
||||||
protected abstract function setEntity(array $value): void;
|
protected abstract function setEntity(array $value): void;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if the group is a mailing list.
|
* Check if this mailing list is enabled
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isMailingList(): bool {
|
public function isEnabled(): bool
|
||||||
return (bool) $this->getEntity()[static::CUSTOM_GROUP_NAME . '.Enable_mailing_list'];
|
{
|
||||||
|
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
|
* @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;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -258,7 +323,8 @@ abstract class BaseMailingList {
|
||||||
*
|
*
|
||||||
* @param array $values
|
* @param array $values
|
||||||
*/
|
*/
|
||||||
protected function update(array $values): void {
|
protected function update(array $values): void
|
||||||
|
{
|
||||||
$this->updateData += $values;
|
$this->updateData += $values;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -268,7 +334,8 @@ abstract class BaseMailingList {
|
||||||
* @return void
|
* @return void
|
||||||
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
||||||
*/
|
*/
|
||||||
public function save(): void {
|
public function save(): void
|
||||||
|
{
|
||||||
if (empty($this->updateData)) {
|
if (empty($this->updateData)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -277,8 +344,7 @@ abstract class BaseMailingList {
|
||||||
->setValues($this->updateData)
|
->setValues($this->updateData)
|
||||||
->addWhere('id', '=', $this->getEntity()['id'])
|
->addWhere('id', '=', $this->getEntity()['id'])
|
||||||
->execute();
|
->execute();
|
||||||
}
|
} catch (\Exception $e) {
|
||||||
catch (\Exception $e) {
|
|
||||||
$type = static::RELATED_TYPE;
|
$type = static::RELATED_TYPE;
|
||||||
throw new MailinglistException(
|
throw new MailinglistException(
|
||||||
"Could not update $type with id '{$this->getEntity()['id']}'\n$e",
|
"Could not update $type with id '{$this->getEntity()['id']}'\n$e",
|
||||||
|
@ -292,7 +358,8 @@ abstract class BaseMailingList {
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getEmailAddress(): string {
|
public function getEmailAddress(): string
|
||||||
|
{
|
||||||
return $this->getEntity()[static::CUSTOM_GROUP_NAME . '.E_mail_address'];
|
return $this->getEntity()[static::CUSTOM_GROUP_NAME . '.E_mail_address'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -301,7 +368,8 @@ abstract class BaseMailingList {
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getTitle(): string {
|
public function getTitle(): string
|
||||||
|
{
|
||||||
return $this->getEntity()['title'];
|
return $this->getEntity()['title'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -313,27 +381,25 @@ abstract class BaseMailingList {
|
||||||
* @return void
|
* @return void
|
||||||
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
||||||
*/
|
*/
|
||||||
public function updateEmailAddress(string $emailAddress): void {
|
public function updateEmailAddress(string $emailAddress): void
|
||||||
|
{
|
||||||
// Validate email address
|
// Validate email address
|
||||||
if (ADGroupMailingList::validateEmailAddress($emailAddress)) {
|
if (!ADGroupMailingList::validateEmailAddress($emailAddress)) {
|
||||||
throw new MailinglistException(
|
throw new MailinglistException(
|
||||||
E::ts("Invalid e-mail address '%1'", [1 => $emailAddress]),
|
"Invalid e-mail address '$emailAddress'",
|
||||||
MailinglistException::ERROR_CODE_INVALID_EMAIL_ADDRESS
|
MailinglistException::ERROR_CODE_INVALID_EMAIL_ADDRESS
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
try {
|
if (!ADGroupMailingList::validateEmailDomain($emailAddress)) {
|
||||||
|
throw new MailinglistException(
|
||||||
|
"E-mail address '$emailAddress' does not match the configured domain",
|
||||||
|
MailinglistException::ERROR_CODE_EMAIL_DOMAIN_MISMATCH
|
||||||
|
);
|
||||||
|
}
|
||||||
static::update([
|
static::update([
|
||||||
static::CUSTOM_GROUP_NAME . '.E_mail_address' => $emailAddress,
|
static::CUSTOM_GROUP_NAME . '.E_mail_address' => $emailAddress,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
catch (MailinglistException) {
|
|
||||||
$type = static::RELATED_TYPE;
|
|
||||||
throw new MailinglistException(
|
|
||||||
"Could not update e-mail address for $type with id '{$this->getEntity()['id']}'",
|
|
||||||
MailinglistException::ERROR_CODE_UPDATE_EMAIL_ADDRESS_FAILED
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the location type name for the mailing list.
|
* Get the location type name for the mailing list.
|
||||||
|
@ -343,7 +409,8 @@ abstract class BaseMailingList {
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function getLocationTypeName(): string {
|
public static function getLocationTypeName(): string
|
||||||
|
{
|
||||||
return static::LOCATION_TYPE;
|
return static::LOCATION_TYPE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -359,8 +426,9 @@ abstract class BaseMailingList {
|
||||||
*
|
*
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function getId(): int {
|
public function getId(): int
|
||||||
return (int) $this->getEntity()['id'];
|
{
|
||||||
|
return (int)$this->getEntity()['id'];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -372,36 +440,46 @@ abstract class BaseMailingList {
|
||||||
* @param string|null $lastName
|
* @param string|null $lastName
|
||||||
* @param string|null $email
|
* @param string|null $email
|
||||||
* @param string|null $sid
|
* @param string|null $sid
|
||||||
|
* @param array|null $searchBy ['sid', 'email', 'names']
|
||||||
*
|
*
|
||||||
* @return array|null ['contact' => $contact, 'found_by' => $found_by]
|
* @return array|null ['contact' => $contact, 'found_by' => $found_by]
|
||||||
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
||||||
*/
|
*/
|
||||||
protected function findExistingContact(
|
protected static function findExistingContact(
|
||||||
string $firstName = NULL,
|
string $firstName = NULL,
|
||||||
string $lastName = NULL,
|
string $lastName = NULL,
|
||||||
string $email = NULL,
|
string $email = NULL,
|
||||||
string $sid = 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.
|
// Get the tags to restrict the search to.
|
||||||
$tags = \Civi::settings()->get(E::SHORT_NAME . '_ad_contact_tags');
|
$tags = \Civi::settings()->get(E::SHORT_NAME . '_ad_contact_tags');
|
||||||
|
|
||||||
// Prepare the get call for reuse.
|
// Prepare the get call for reuse.
|
||||||
$prepareGetEmail = function () use ($tags,) {
|
$prepareGetEmail = function () use ($tags) {
|
||||||
$selectFields = [
|
$selectFields = [
|
||||||
'id',
|
'id',
|
||||||
'first_name',
|
'first_name',
|
||||||
'last_name',
|
'last_name',
|
||||||
'email.email',
|
'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)
|
->addSelect(...$selectFields)
|
||||||
->addJoin('Email AS email', 'LEFT', ['id', '=', 'email.contact_id']);
|
->addJoin('Email AS email', 'LEFT',
|
||||||
|
['id', '=', 'email.contact_id'])
|
||||||
|
->addGroupBy('id');
|
||||||
if ($tags) {
|
if ($tags) {
|
||||||
$call->addJoin('EntityTag AS entity_tag', 'LEFT',
|
$call->addJoin('EntityTag AS entity_tag', 'INNER',
|
||||||
['id', '=', 'entity_tag.entity_id'],
|
['id', '=', 'entity_tag.entity_id'],
|
||||||
['entity_tag.entity_table', '=', 'civicrm_contact'],
|
['entity_tag.entity_table', '=', '"civicrm_contact"'],
|
||||||
['entity_tag.tag_id', 'IN', $tags]
|
['entity_tag.tag_id', 'IN', $tags]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -409,13 +487,13 @@ abstract class BaseMailingList {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Try to find the contact by the SID.
|
// Try to find the contact by the SID.
|
||||||
|
if (in_array('sid', $searchBy)) {
|
||||||
try {
|
try {
|
||||||
$contact = $prepareGetEmail()
|
$contact = $prepareGetEmail()
|
||||||
->addWhere(GroupMailingList::CUSTOM_GROUP_NAME . '.Active_Directory_SID', '=', $sid)
|
->addWhere('Active_Directory.SID', '=', $sid)
|
||||||
->execute()
|
->execute()
|
||||||
->getArrayCopy();
|
->getArrayCopy();
|
||||||
}
|
} catch (\Exception $e) {
|
||||||
catch (\Exception $e) {
|
|
||||||
throw new MailinglistException(
|
throw new MailinglistException(
|
||||||
"Could not get contact by SID '$sid': $e",
|
"Could not get contact by SID '$sid': $e",
|
||||||
MailinglistException::ERROR_CODE_GET_CONTACT_FAILED,
|
MailinglistException::ERROR_CODE_GET_CONTACT_FAILED,
|
||||||
|
@ -426,19 +504,19 @@ abstract class BaseMailingList {
|
||||||
"Multiple contacts with the same SID '$sid' found",
|
"Multiple contacts with the same SID '$sid' found",
|
||||||
MailinglistException::ERROR_CODE_MULTIPLE_CONTACTS_FOUND
|
MailinglistException::ERROR_CODE_MULTIPLE_CONTACTS_FOUND
|
||||||
);
|
);
|
||||||
}
|
} elseif (count($contact) === 1) {
|
||||||
elseif (count($contact) === 1) {
|
|
||||||
return ['contact' => array_pop($contact), 'found_by' => 'sid'];
|
return ['contact' => array_pop($contact), 'found_by' => 'sid'];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Try fo find the contact by the e-mail address.
|
// Try fo find the contact by the e-mail address.
|
||||||
|
if (in_array('email', $searchBy)) {
|
||||||
try {
|
try {
|
||||||
$contact = $prepareGetEmail()
|
$contact = $prepareGetEmail()
|
||||||
->addWhere('email', '=', $email)
|
->addWhere('email.email', '=', $email)
|
||||||
->execute()
|
->execute()
|
||||||
->getArrayCopy();
|
->getArrayCopy();
|
||||||
}
|
} catch (\Exception $e) {
|
||||||
catch (\Exception $e) {
|
|
||||||
throw new MailinglistException(
|
throw new MailinglistException(
|
||||||
"Could not get contact by e-mail address '$email': $e",
|
"Could not get contact by e-mail address '$email': $e",
|
||||||
MailinglistException::ERROR_CODE_GET_CONTACT_FAILED,
|
MailinglistException::ERROR_CODE_GET_CONTACT_FAILED,
|
||||||
|
@ -449,21 +527,20 @@ abstract class BaseMailingList {
|
||||||
"Multiple contacts with the same e-mail address '$email' found",
|
"Multiple contacts with the same e-mail address '$email' found",
|
||||||
MailinglistException::ERROR_CODE_MULTIPLE_CONTACTS_FOUND
|
MailinglistException::ERROR_CODE_MULTIPLE_CONTACTS_FOUND
|
||||||
);
|
);
|
||||||
}
|
} elseif (count($contact) === 1) {
|
||||||
elseif (count($contact) === 1) {
|
|
||||||
return ['contact' => array_pop($contact), 'found_by' => 'email'];
|
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.
|
// 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 {
|
try {
|
||||||
$contact = $prepareGetEmail()
|
$contact = $prepareGetEmail()
|
||||||
->addWhere('first_name', '=', $firstName)
|
->addWhere('first_name', '=', $firstName)
|
||||||
->addWhere('last_name', '=', $lastName)
|
->addWhere('last_name', '=', $lastName)
|
||||||
->execute()
|
->execute()
|
||||||
->getArrayCopy();
|
->getArrayCopy();
|
||||||
}
|
} catch (\Exception $e) {
|
||||||
catch (\Exception $e) {
|
|
||||||
throw new MailinglistException(
|
throw new MailinglistException(
|
||||||
"Could not get contact by first and last name '$firstName $lastName': $e",
|
"Could not get contact by first and last name '$firstName $lastName': $e",
|
||||||
MailinglistException::ERROR_CODE_GET_CONTACT_FAILED,
|
MailinglistException::ERROR_CODE_GET_CONTACT_FAILED,
|
||||||
|
@ -474,8 +551,7 @@ abstract class BaseMailingList {
|
||||||
"Multiple contacts with the same first and last name found",
|
"Multiple contacts with the same first and last name found",
|
||||||
MailinglistException::ERROR_CODE_MULTIPLE_CONTACTS_FOUND
|
MailinglistException::ERROR_CODE_MULTIPLE_CONTACTS_FOUND
|
||||||
);
|
);
|
||||||
}
|
} elseif (count($contact) === 1) {
|
||||||
elseif (count($contact) === 1) {
|
|
||||||
return ['contact' => array_pop($contact), 'found_by' => 'names'];
|
return ['contact' => array_pop($contact), 'found_by' => 'names'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -483,4 +559,157 @@ abstract class BaseMailingList {
|
||||||
// If no contact was found, return NULL.
|
// If no contact was found, return NULL.
|
||||||
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());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Civi\Mailinglistsync;
|
namespace Civi\Mailinglistsync;
|
||||||
|
|
||||||
|
use Civi\Api4\Contact;
|
||||||
|
use Civi\Mailinglistsync\Exceptions\MailinglistException;
|
||||||
use CRM_Mailinglistsync_ExtensionUtil as E;
|
use CRM_Mailinglistsync_ExtensionUtil as E;
|
||||||
use Civi\Api4\Event;
|
use Civi\Api4\Event;
|
||||||
|
|
||||||
|
@ -22,7 +25,7 @@ class EventMailingList extends BaseMailingList {
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function getEntity(): array {
|
protected function getEntity(): array {
|
||||||
return $this->event;
|
return $this->event ?? NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -37,20 +40,53 @@ class EventMailingList extends BaseMailingList {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getRecipients(): array {
|
public function getRecipients(): array {
|
||||||
// TODO: Implement getRecipients() method.
|
try {
|
||||||
|
$recipientData = Contact::get()
|
||||||
|
->addSelect('id', 'first_name', 'last_name', 'email.email', 'Active_Directory.SID')
|
||||||
|
->addJoin('Email AS email', 'LEFT', ['id', '=', 'email.contact_id'])
|
||||||
|
->addJoin('Participant AS participant', 'INNER',
|
||||||
|
['id', '=', 'participant.contact_id'],
|
||||||
|
['participant.event_id', '=', $this->event['id']],
|
||||||
|
['participant.status', 'IN', self::getEnabledParticipantStatus()],
|
||||||
|
)
|
||||||
|
->addGroupBy('id')
|
||||||
|
->execute()
|
||||||
|
->getArrayCopy();
|
||||||
|
}
|
||||||
|
catch (\Exception $e) {
|
||||||
|
throw new MailinglistException(
|
||||||
|
"Could not get recipients for event with id '{$this->event['id']}': {$e->getMessage()}",
|
||||||
|
MailinglistException::ERROR_CODE_GET_RECIPIENTS_FAILED
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$recipients = [];
|
||||||
|
foreach ($recipientData as $recipient) {
|
||||||
|
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']}'\n$e",
|
||||||
|
MailinglistException::ERROR_CODE_GET_RECIPIENTS_FAILED
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $recipients;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a list of participants status that are enabled for the mailing list.
|
* Get a list of participants status that are enabled for the mailing list.
|
||||||
*
|
*
|
||||||
* @return array
|
* @return ?array
|
||||||
*/
|
*/
|
||||||
public static function getEnabledParticipantStatus(): array {
|
public static function getEnabledParticipantStatus(): ?array {
|
||||||
return MailingListSettings::get(E::SHORT_NAME . '_participant_status');
|
return MailingListSettings::get(E::SHORT_NAME . '_participant_status');
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function create(array $values): BaseMailingList {
|
|
||||||
// TODO: Implement create() method.
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
14
Civi/Mailinglistsync/Exceptions/ContactSyncException.php
Normal file
14
Civi/Mailinglistsync/Exceptions/ContactSyncException.php
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Civi\Mailinglistsync\Exceptions;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A simple custom error indicating a problem with the synchronization of
|
||||||
|
* contacts.
|
||||||
|
*/
|
||||||
|
class ContactSyncException extends BaseException {
|
||||||
|
|
||||||
|
public const ERROR_CODE_MISSING_RECIPIENT_ATTRIBUTE = 'missing_recipient_attribute';
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -9,12 +9,14 @@ namespace Civi\Mailinglistsync\Exceptions;
|
||||||
class MailinglistException extends BaseException {
|
class MailinglistException extends BaseException {
|
||||||
|
|
||||||
public const ERROR_CODE_PERMISSION_DENIED = 'permission_denied';
|
public const ERROR_CODE_PERMISSION_DENIED = 'permission_denied';
|
||||||
|
public const ERROR_CODE_CREATE_EMAIL_ADDRESS_FAILED = 'create_email_address_failed';
|
||||||
public const ERROR_CODE_UPDATE_EMAIL_ADDRESS_FAILED = 'update_email_address_failed';
|
public const ERROR_CODE_UPDATE_EMAIL_ADDRESS_FAILED = 'update_email_address_failed';
|
||||||
public const ERROR_CODE_DELETE_EMAIL_ADDRESS_FAILED = 'delete_email_address_failed';
|
public const ERROR_CODE_DELETE_EMAIL_ADDRESS_FAILED = 'delete_email_address_failed';
|
||||||
public const ERROR_CODE_GET_RECIPIENTS_FAILED = 'get_recipients_failed';
|
public const ERROR_CODE_GET_RECIPIENTS_FAILED = 'get_recipients_failed';
|
||||||
public const ERROR_CODE_GET_LOCATION_TYPES_FAILED = 'get_location_types_failed';
|
public const ERROR_CODE_GET_LOCATION_TYPES_FAILED = 'get_location_types_failed';
|
||||||
public const ERROR_CODE_INVALID_CLASS = 'invalid_class';
|
public const ERROR_CODE_INVALID_CLASS = 'invalid_class';
|
||||||
public const ERROR_CODE_INVALID_EMAIL_ADDRESS = 'invalid_email_address';
|
public const ERROR_CODE_INVALID_EMAIL_ADDRESS = 'invalid_email_address';
|
||||||
|
public const ERROR_CODE_EMAIL_DOMAIN_MISMATCH = 'email_domain_mismatch';
|
||||||
public const ERROR_CODE_GET_GROUP_MAILING_LISTS_FAILED = 'get_group_mailing_lists_failed';
|
public const ERROR_CODE_GET_GROUP_MAILING_LISTS_FAILED = 'get_group_mailing_lists_failed';
|
||||||
public const ERROR_CODE_GET_AD_GROUP_MAILING_LISTS_FAILED = 'get_group_mailing_lists_failed';
|
public const ERROR_CODE_GET_AD_GROUP_MAILING_LISTS_FAILED = 'get_group_mailing_lists_failed';
|
||||||
public const ERROR_CODE_GET_EVENT_MAILING_LISTS_FAILED = 'get_event_mailing_lists_failed';
|
public const ERROR_CODE_GET_EVENT_MAILING_LISTS_FAILED = 'get_event_mailing_lists_failed';
|
||||||
|
@ -25,8 +27,19 @@ class MailinglistException extends BaseException {
|
||||||
public const ERROR_CODE_GROUP_CREATION_FAILED = 'group_creation_failed';
|
public const ERROR_CODE_GROUP_CREATION_FAILED = 'group_creation_failed';
|
||||||
public const ERROR_CODE_UPDATE_ENTITY_FAILED = 'update_entity_failed';
|
public const ERROR_CODE_UPDATE_ENTITY_FAILED = 'update_entity_failed';
|
||||||
public const ERROR_CODE_MULTIPLE_RECIPIENTS = 'multiple_recipients';
|
public const ERROR_CODE_MULTIPLE_RECIPIENTS = 'multiple_recipients';
|
||||||
|
public const ERROR_CODE_GET_EMAIL_LOCATION_TYPES_FAILED = 'get_email_location_types_failed';
|
||||||
|
public const ERROR_CODE_ADD_CONTACT_TO_GROUP_FAILED = 'add_contact_to_group_failed';
|
||||||
|
public const ERROR_CODE_REMOVE_CONTACT_FROM_GROUP_FAILED = 'remove_contact_from_group_failed';
|
||||||
|
public const ERROR_CODE_GET_CONTACT_BY_SID_FAILED = 'get_contact_by_sid_failed';
|
||||||
|
public const ERROR_CODE_CREATE_ACTIVITY_FAILED = 'create_activity_failed';
|
||||||
|
public const ERROR_CODE_GET_USER_ID_FAILED = 'get_user_id_failed';
|
||||||
|
public const ERROR_CODE_UNSERIALIZE_SINGLETON = 'unserialize_singleton';
|
||||||
|
public const ERROR_CODE_MAILINGLIST_DOES_NOT_EXIST = 'mailinglist_does_not_exist';
|
||||||
|
public const ERROR_CODE_GET_MAILINGLIST_FAILED = 'get_mailinglist_failed';
|
||||||
|
public const ERROR_CODE_DOVECOT_CREATE_EMAIL_ADDRESS_FAILED = 'dovecot_create_email_address_failed';
|
||||||
|
public const ERROR_CODE_CREATE_MAILING_LIST_FAILED = 'create_mailing_list_failed';
|
||||||
|
public const ERROR_CODE_UPDATE_MAILING_LIST_FAILED = 'update_mailing_list_failed';
|
||||||
|
public const ERROR_CODE_UPDATE_SUBSCRIBERS_FAILED = 'update_subscribers_failed';
|
||||||
|
public const ERROR_CODE_DELETE_MAILING_LIST_FAILED = 'delete_mailing_list_failed';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,14 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Civi\Mailinglistsync\Exceptions;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A simple custom error indicating a problem with the validation of the
|
|
||||||
* synchronization of mailing lists.
|
|
||||||
*/
|
|
||||||
class MailinglistSyncException extends BaseException {
|
|
||||||
|
|
||||||
public const ERROR_CODE_UNSERIALIZE_SINGLETON = 'unserialize_singleton';
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Civi\Mailinglistsync;
|
namespace Civi\Mailinglistsync;
|
||||||
|
|
||||||
|
@ -30,7 +31,7 @@ class GroupMailingList extends BaseMailingList {
|
||||||
* Create a new GroupMailingList.
|
* Create a new GroupMailingList.
|
||||||
*
|
*
|
||||||
* @param string $title
|
* @param string $title
|
||||||
* @param string $description
|
* @param ?string $description
|
||||||
* @param string $email
|
* @param string $email
|
||||||
* @param string|null $sid
|
* @param string|null $sid
|
||||||
*
|
*
|
||||||
|
@ -39,22 +40,28 @@ class GroupMailingList extends BaseMailingList {
|
||||||
*/
|
*/
|
||||||
public static function createGroup(
|
public static function createGroup(
|
||||||
string $title,
|
string $title,
|
||||||
string $description,
|
|
||||||
string $email,
|
string $email,
|
||||||
|
string $description = NULL,
|
||||||
string $sid = NULL
|
string $sid = NULL
|
||||||
): self {
|
): self {
|
||||||
try {
|
try {
|
||||||
$request = \Civi\Api4\Group::create()
|
$request = \Civi\Api4\Group::create()
|
||||||
->addValue('title', $title)
|
->addValue('title', $title)
|
||||||
->addValue('description', $description)
|
->addValue(static::CUSTOM_GROUP_NAME . '.E_mail_address', $email)
|
||||||
->addValue(static::CUSTOM_GROUP_NAME . '.Email', $email)
|
->addValue(static::CUSTOM_GROUP_NAME . '.Enable_mailing_list', 1)
|
||||||
->addValue('is_active', 1);
|
->addValue('is_active', 1);
|
||||||
|
|
||||||
// If the group is an AD group, add the AD SID
|
// Add the description if it's not empty
|
||||||
if (!empty($values['sid'])) {
|
if (!empty($description)) {
|
||||||
$request->addValue(static::CUSTOM_GROUP_NAME . '.SID', $values['sid']);
|
$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) {
|
catch (\Exception $e) {
|
||||||
throw new MailinglistException(
|
throw new MailinglistException(
|
||||||
|
@ -78,7 +85,7 @@ class GroupMailingList extends BaseMailingList {
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isADGroup(): 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 {
|
try {
|
||||||
$recipientData = Contact::get()
|
$recipientData = Contact::get()
|
||||||
->addSelect('id', 'first_name', 'last_name', 'email.email', 'Active_Directory.SID')
|
->addSelect('id', 'first_name', 'last_name', 'email.email', 'Active_Directory.SID')
|
||||||
->addJoin('Email AS email', 'INNER', ['id', '=', 'email.contact_id'])
|
->addJoin('Email AS email', 'LEFT', ['id', '=', 'email.contact_id'])
|
||||||
->addJoin('GroupContact AS group_contact', 'INNER', ['id', '=', 'group_contact.contact_id'])
|
->addJoin('GroupContact AS group_contact', 'INNER',
|
||||||
->addJoin('LocationType AS location_type', 'INNER', ['email.location_type_id', '=', 'location_type.id'])
|
['id', '=', 'group_contact.contact_id'],
|
||||||
->addWhere('group_contact.group_id', '=', $this->group['id'])
|
['group_contact.group_id', '=', $this->group['id']],
|
||||||
->addWhere('location_type.name', '=', static::LOCATION_TYPE)
|
['group_contact.status', '=', '"Added"'],
|
||||||
|
)
|
||||||
->addGroupBy('id')
|
->addGroupBy('id')
|
||||||
->execute()
|
->execute()
|
||||||
->getArrayCopy();
|
->getArrayCopy();
|
||||||
|
@ -109,13 +117,20 @@ class GroupMailingList extends BaseMailingList {
|
||||||
|
|
||||||
$recipients = [];
|
$recipients = [];
|
||||||
foreach ($recipientData as $recipient) {
|
foreach ($recipientData as $recipient) {
|
||||||
|
try {
|
||||||
$recipients[$recipient['email.email']] = new MailingListRecipient(
|
$recipients[$recipient['email.email']] = new MailingListRecipient(
|
||||||
|
sid: $recipient['Active_Directory.SID'],
|
||||||
contact_id: $recipient['id'],
|
contact_id: $recipient['id'],
|
||||||
first_name: $recipient['first_name'],
|
first_name: $recipient['first_name'],
|
||||||
last_name: $recipient['last_name'],
|
last_name: $recipient['last_name'],
|
||||||
email: $recipient['email.email'],
|
email: $recipient['email.email'],
|
||||||
sid: $recipient['Active_Directory.SID'],
|
|
||||||
);
|
);
|
||||||
|
} 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;
|
return $recipients;
|
||||||
|
@ -139,7 +154,7 @@ class GroupMailingList extends BaseMailingList {
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getGroupDescription(): string {
|
public function getGroupDescription(): string {
|
||||||
return $this->group['description'];
|
return $this->group['description'] ?? '';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -148,10 +163,86 @@ class GroupMailingList extends BaseMailingList {
|
||||||
* @param string $description
|
* @param string $description
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
|
||||||
*/
|
*/
|
||||||
public function updateGroupDescription(string $description): void {
|
public function updateGroupDescription(string $description): void {
|
||||||
$this->update(['description' => $description]);
|
$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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
321
Civi/Mailinglistsync/MailingListApi.php
Normal file
321
Civi/Mailinglistsync/MailingListApi.php
Normal file
|
@ -0,0 +1,321 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Civi\Mailinglistsync;
|
||||||
|
|
||||||
|
use Civi\Mailinglistsync\Exceptions\MailinglistException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The MailingListApi class provides methods to interact with the mlmmj and
|
||||||
|
* Dovecot APIs.
|
||||||
|
*/
|
||||||
|
class MailingListApi {
|
||||||
|
|
||||||
|
// Use the singleton trait
|
||||||
|
use Singleton;
|
||||||
|
|
||||||
|
protected string $mlmmjUrl;
|
||||||
|
|
||||||
|
protected string $mlmmjToken;
|
||||||
|
|
||||||
|
protected int $mlmmjPort;
|
||||||
|
|
||||||
|
protected string $dovecotUrl;
|
||||||
|
|
||||||
|
protected string $dovecotToken;
|
||||||
|
|
||||||
|
protected int $dovecotPort;
|
||||||
|
|
||||||
|
protected function __construct() {
|
||||||
|
$this->mlmmjUrl = MailingListSettings::get('mlmmj_host');
|
||||||
|
$this->mlmmjToken = MailingListSettings::get('mlmmj_token');
|
||||||
|
$this->mlmmjPort = MailingListSettings::get('mlmmj_port');
|
||||||
|
$this->dovecotUrl = MailingListSettings::get('dovecot_host');
|
||||||
|
$this->dovecotToken = MailingListSettings::get('dovecot_token');
|
||||||
|
$this->dovecotPort = MailingListSettings::get('dovecot_port');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepares a curl handle for the mlmmj API.
|
||||||
|
*
|
||||||
|
* @param null $path
|
||||||
|
*
|
||||||
|
* @return \CurlHandle
|
||||||
|
*/
|
||||||
|
private function prepareMlmmjCurl($path = NULL): \CurlHandle {
|
||||||
|
// Build URL
|
||||||
|
$url = $path
|
||||||
|
? "{$this->mlmmjUrl}api/{$path}"
|
||||||
|
: "{$this->mlmmjUrl}api/";
|
||||||
|
|
||||||
|
// Set up curl handle
|
||||||
|
$curl = curl_init();
|
||||||
|
|
||||||
|
// Set options
|
||||||
|
curl_setopt_array($curl, [
|
||||||
|
CURLOPT_URL => $url,
|
||||||
|
CURLOPT_PORT => $this->mlmmjPort,
|
||||||
|
CURLOPT_RETURNTRANSFER => TRUE,
|
||||||
|
CURLOPT_HTTPHEADER => [
|
||||||
|
'X-MLMMJADMIN-API-AUTH-TOKEN: ' . $this->mlmmjToken,
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
return $curl;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepares a curl handle for the Dovecot API.
|
||||||
|
*
|
||||||
|
* @param $path
|
||||||
|
*
|
||||||
|
* @return \CurlHandle
|
||||||
|
*/
|
||||||
|
private function prepareDovecotCurl($path = NULL) {
|
||||||
|
// Build URL
|
||||||
|
$url = $path
|
||||||
|
? "{$this->dovecotUrl}/{$path}"
|
||||||
|
: "{$this->dovecotUrl}";
|
||||||
|
|
||||||
|
// Set up curl handle
|
||||||
|
$curl = curl_init();
|
||||||
|
|
||||||
|
// Set options
|
||||||
|
curl_setopt_array($curl, [
|
||||||
|
CURLOPT_URL => $url,
|
||||||
|
CURLOPT_PORT => $this->dovecotPort,
|
||||||
|
CURLOPT_RETURNTRANSFER => TRUE,
|
||||||
|
CURLOPT_HTTPHEADER => [
|
||||||
|
'Authorization: Bearer ' . $this->dovecotToken,
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
|
||||||
|
return $curl;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a mailing list from mlmmj.
|
||||||
|
*
|
||||||
|
* @param string $mailinglistEmail
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
||||||
|
*/
|
||||||
|
function getMailingList(string $mailinglistEmail): array {
|
||||||
|
$curl = $this->prepareMlmmjCurl($mailinglistEmail);
|
||||||
|
$result = json_decode(curl_exec($curl), TRUE);
|
||||||
|
|
||||||
|
// Check result
|
||||||
|
if (curl_getinfo($curl, CURLINFO_HTTP_CODE) !== 200) {
|
||||||
|
throw new MailinglistException(
|
||||||
|
"Could not get mailinglist '$mailinglistEmail'",
|
||||||
|
MailinglistException::ERROR_CODE_GET_MAILINGLIST_FAILED,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if ($result['_success'] === FALSE) {
|
||||||
|
// Return empty array if the account does not exist yet
|
||||||
|
if ($result['_msg'] === 'NO_SUCH_ACCOUNT') {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
// Throw exception if the request failed
|
||||||
|
else {
|
||||||
|
throw new MailinglistException(
|
||||||
|
"Could not get mailinglist '$mailinglistEmail'",
|
||||||
|
MailinglistException::ERROR_CODE_GET_MAILINGLIST_FAILED,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the subscribers from mlmmj.
|
||||||
|
*
|
||||||
|
* @param string $mailinglistEmail
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*
|
||||||
|
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
||||||
|
*/
|
||||||
|
function getMailingListSubscribers(string $mailinglistEmail): array {
|
||||||
|
$curl = $this->prepareMlmmjCurl($mailinglistEmail . '/subscribers');
|
||||||
|
$result = json_decode(curl_exec($curl), TRUE);
|
||||||
|
|
||||||
|
// Check result
|
||||||
|
if (!$result['_success']) {
|
||||||
|
throw new MailinglistException(
|
||||||
|
"Could not get subscribers for mailinglist '$mailinglistEmail'",
|
||||||
|
MailinglistException::ERROR_CODE_GET_RECIPIENTS_FAILED,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_map(function($subscriber) {
|
||||||
|
return $subscriber['mail'];
|
||||||
|
}, $result['_data']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create the email address via dovecot and the mailing list via mlmmj.
|
||||||
|
*
|
||||||
|
* @param string $mailinglistEmail
|
||||||
|
* @param array $options
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*
|
||||||
|
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
||||||
|
*/
|
||||||
|
function createMailingList(string $mailinglistEmail, array $options): void {
|
||||||
|
// Create email address via Dovecot API
|
||||||
|
$username = explode('@', $mailinglistEmail)[0];
|
||||||
|
$dovecotCurl = $this->prepareDovecotCurl('list/' . $username);
|
||||||
|
curl_setopt($dovecotCurl, CURLOPT_CUSTOMREQUEST, 'PUT');
|
||||||
|
$dovecutResult = json_decode(curl_exec($dovecotCurl), TRUE);
|
||||||
|
|
||||||
|
// Check dovecot result (ignore 409 for already existing email addresses)
|
||||||
|
$statusCode = curl_getinfo($dovecotCurl, CURLINFO_HTTP_CODE);
|
||||||
|
if ($statusCode !== 201 && $statusCode !== 409) {
|
||||||
|
$message = "Could not create email address for '$mailinglistEmail'";
|
||||||
|
if (!empty($dovecutResult['Message'])) {
|
||||||
|
$message .= ': ' . $dovecutResult['Message'];
|
||||||
|
}
|
||||||
|
throw new MailinglistException(
|
||||||
|
$message,
|
||||||
|
MailinglistException::ERROR_CODE_DOVECOT_CREATE_EMAIL_ADDRESS_FAILED,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create mailing list via mlmmj API
|
||||||
|
$mlmmjCurl = $this->prepareMlmmjCurl($mailinglistEmail);
|
||||||
|
curl_setopt($mlmmjCurl, CURLOPT_CUSTOMREQUEST, 'POST');
|
||||||
|
curl_setopt($mlmmjCurl, CURLOPT_POSTFIELDS, http_build_query($options));
|
||||||
|
$mlmmjResult = json_decode(curl_exec($mlmmjCurl), TRUE);
|
||||||
|
|
||||||
|
// Check mlmmj result
|
||||||
|
if (!is_array($mlmmjResult) || $mlmmjResult['_success'] !== TRUE) {
|
||||||
|
$message = "Could not create mailinglist '$mailinglistEmail'";
|
||||||
|
if (!empty($mlmmjResult['_msg'])) {
|
||||||
|
$message .= ': ' . $mlmmjResult['_msg'];
|
||||||
|
}
|
||||||
|
throw new MailinglistException(
|
||||||
|
$message,
|
||||||
|
MailinglistException::ERROR_CODE_CREATE_MAILING_LIST_FAILED,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update a mailing list.
|
||||||
|
*
|
||||||
|
* @param string $mailinglistEmail
|
||||||
|
* @param array $options
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
||||||
|
*/
|
||||||
|
function updateMailingList(string $mailinglistEmail, array $options): void {
|
||||||
|
$curl = $this->prepareMlmmjCurl($mailinglistEmail);
|
||||||
|
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
|
||||||
|
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($options));
|
||||||
|
$result = json_decode(curl_exec($curl), TRUE);
|
||||||
|
|
||||||
|
// Check result
|
||||||
|
if (!empty($result['_success']) && $result['_success'] !== TRUE) {
|
||||||
|
$message = "Could not update mailinglist '$mailinglistEmail'";
|
||||||
|
if (!empty($result['_msg'])) {
|
||||||
|
$message .= ': ' . $result['_msg'];
|
||||||
|
}
|
||||||
|
throw new MailinglistException(
|
||||||
|
$message,
|
||||||
|
MailinglistException::ERROR_CODE_UPDATE_MAILING_LIST_FAILED
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the subscribers of a mailing list.
|
||||||
|
*
|
||||||
|
* @param string $mailinglistEmail
|
||||||
|
* @param ?array $recipientsToAdd
|
||||||
|
* @param ?array $recipientsToRemove
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
||||||
|
*/
|
||||||
|
function updateSubscribers(
|
||||||
|
string $mailinglistEmail,
|
||||||
|
array $recipientsToAdd = NULL,
|
||||||
|
array $recipientsToRemove = NULL,
|
||||||
|
): void {
|
||||||
|
$query = [];
|
||||||
|
if ($recipientsToAdd) {
|
||||||
|
$query['add_subscribers'] = implode(',', $recipientsToAdd);
|
||||||
|
}
|
||||||
|
if ($recipientsToRemove) {
|
||||||
|
$query['remove_subscribers'] = implode(',', $recipientsToRemove);
|
||||||
|
}
|
||||||
|
|
||||||
|
$curl = $this->prepareMlmmjCurl($mailinglistEmail . '/subscribers');
|
||||||
|
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
|
||||||
|
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($query));
|
||||||
|
$result = json_decode(curl_exec($curl), TRUE);
|
||||||
|
|
||||||
|
// Check result
|
||||||
|
if (!empty($result['_success']) && $result['_success'] !== TRUE) {
|
||||||
|
throw new MailinglistException(
|
||||||
|
"Could not update subscribers for mailinglist '$mailinglistEmail'",
|
||||||
|
MailinglistException::ERROR_CODE_UPDATE_SUBSCRIBERS_FAILED,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a mailing list and its email address.
|
||||||
|
*
|
||||||
|
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
||||||
|
*/
|
||||||
|
function deleteMailingList(string $mailinglistEmail): void {
|
||||||
|
// Delete mlmmj mailing list
|
||||||
|
$mlmmjCurl = $this->prepareMlmmjCurl($mailinglistEmail);
|
||||||
|
curl_setopt($mlmmjCurl, CURLOPT_CUSTOMREQUEST, 'DELETE');
|
||||||
|
$mlmmjResult = json_decode(curl_exec($mlmmjCurl), TRUE);
|
||||||
|
|
||||||
|
// Check mlmmj result
|
||||||
|
if (!empty($mlmmjResult['_success']) && $mlmmjResult['_success'] !== TRUE) {
|
||||||
|
$message = "Could not delete mailinglist '$mailinglistEmail'";
|
||||||
|
if (!empty($mlmmjResult['_msg'])) {
|
||||||
|
$message .= ': ' . $mlmmjResult['_msg'];
|
||||||
|
}
|
||||||
|
throw new MailinglistException(
|
||||||
|
$message,
|
||||||
|
MailinglistException::ERROR_CODE_DELETE_MAILING_LIST_FAILED,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete dovecot email address
|
||||||
|
$username = explode('@', $mailinglistEmail)[0];
|
||||||
|
$dovecotCurl = $this->prepareDovecotCurl('list/' . $username);
|
||||||
|
curl_setopt($dovecotCurl, CURLOPT_CUSTOMREQUEST, 'DELETE');
|
||||||
|
$dovecotResult = json_decode(curl_exec($dovecotCurl), TRUE);
|
||||||
|
|
||||||
|
// Check dovecot result (ignore 404 for non-existing email addresses)
|
||||||
|
$statusCode = curl_getinfo($dovecotCurl, CURLINFO_HTTP_CODE);
|
||||||
|
if ($statusCode === 404) {
|
||||||
|
$message = "Email '$mailinglistEmail' does not exist in Dovecot";
|
||||||
|
throw new MailinglistException(
|
||||||
|
$message,
|
||||||
|
MailinglistException::ERROR_CODE_DELETE_EMAIL_ADDRESS_FAILED,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if ($statusCode !== 200) {
|
||||||
|
$message = "Could not delete email address for '$mailinglistEmail'";
|
||||||
|
if (!empty($dovecotResult['Message'])) {
|
||||||
|
$message .= ': ' . $dovecotResult['Message'];
|
||||||
|
}
|
||||||
|
throw new MailinglistException(
|
||||||
|
$message,
|
||||||
|
MailinglistException::ERROR_CODE_DOVECOT_CREATE_EMAIL_ADDRESS_FAILED,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,10 +1,13 @@
|
||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Civi\Mailinglistsync;
|
namespace Civi\Mailinglistsync;
|
||||||
|
|
||||||
|
use Civi\Mailinglistsync\Exceptions\ContactSyncException;
|
||||||
use CRM_Mailinglistsync_ExtensionUtil as E;
|
use CRM_Mailinglistsync_ExtensionUtil as E;
|
||||||
|
|
||||||
class MailingListManager {
|
class MailingListManager {
|
||||||
|
use Singleton;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Is the mailing list enabled?
|
* Is the mailing list enabled?
|
||||||
|
@ -67,7 +70,7 @@ class MailingListManager {
|
||||||
*
|
*
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
function __construct() {
|
protected function __construct() {
|
||||||
// Load settings
|
// Load settings
|
||||||
$settings = MailingListSettings::get();
|
$settings = MailingListSettings::get();
|
||||||
$this->enabled = $settings[E::SHORT_NAME . '_enable'] ?? FALSE;
|
$this->enabled = $settings[E::SHORT_NAME . '_enable'] ?? FALSE;
|
||||||
|
@ -82,17 +85,6 @@ class MailingListManager {
|
||||||
$this->dovecotToken = $settings[E::SHORT_NAME . '_dovecot_token'] ??
|
$this->dovecotToken = $settings[E::SHORT_NAME . '_dovecot_token'] ??
|
||||||
throw new \Exception('No dovecot token set');
|
throw new \Exception('No dovecot token set');
|
||||||
$this->dovecotPort = $settings[E::SHORT_NAME . '_dovecot_port'] ?? 443;
|
$this->dovecotPort = $settings[E::SHORT_NAME . '_dovecot_port'] ?? 443;
|
||||||
|
$this->mlmmjApi = MailingListApi::getInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
function createEmailAddress(string $emailAddress) {} // TODO
|
|
||||||
|
|
||||||
function deleteEmailAddress(string $emailAddress) {} // TODO
|
|
||||||
|
|
||||||
function createMailingList(BaseMailingList $mailingList) {} // TODO
|
|
||||||
|
|
||||||
function deleteMailingList(BaseMailingList $mailingList) {} // TODO
|
|
||||||
|
|
||||||
function updateMailingList(BaseMailingList $mailingList) {} // TODO
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,44 +1,41 @@
|
||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Civi\Mailinglistsync;
|
namespace Civi\Mailinglistsync;
|
||||||
|
|
||||||
|
use Civi\Api4\Contact;
|
||||||
use Civi\Mailinglistsync\Exceptions\MailinglistException;
|
use Civi\Mailinglistsync\Exceptions\MailinglistException;
|
||||||
use CRM_Mailinglistsync_ExtensionUtil as E;
|
use CRM_Mailinglistsync_ExtensionUtil as E;
|
||||||
|
|
||||||
class MailingListRecipient {
|
class MailingListRecipient {
|
||||||
|
|
||||||
protected int $contactId;
|
public const CUSTOM_GROUP_NAME = 'Active_Directory';
|
||||||
|
|
||||||
protected string $firstName;
|
protected ?string $sid;
|
||||||
|
|
||||||
protected string $lastName;
|
protected ?int $contactId;
|
||||||
|
|
||||||
protected string $email;
|
protected ?string $firstName;
|
||||||
|
|
||||||
protected string $sid;
|
protected ?string $lastName;
|
||||||
|
|
||||||
/**
|
protected ?string $email;
|
||||||
* Data to be updated. Use the `save()` method to apply the update.
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
private $updateData;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @param int $contact_id
|
|
||||||
* @param string $first_name
|
|
||||||
* @param string $last_name
|
|
||||||
* @param string $email
|
|
||||||
* @param string|null $sid
|
* @param string|null $sid
|
||||||
|
* @param int|null $contact_id
|
||||||
|
* @param string|null $first_name
|
||||||
|
* @param string|null $last_name
|
||||||
|
* @param string|null $email
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
int $contact_id,
|
|
||||||
string $first_name,
|
|
||||||
string $last_name,
|
|
||||||
string $email,
|
|
||||||
string $sid = NULL,
|
string $sid = NULL,
|
||||||
|
int $contact_id = NULL,
|
||||||
|
string $first_name = NULL,
|
||||||
|
string $last_name = NULL ,
|
||||||
|
string $email = NULL,
|
||||||
) {
|
) {
|
||||||
$this->contactId = $contact_id;
|
$this->contactId = $contact_id;
|
||||||
$this->firstName = $first_name;
|
$this->firstName = $first_name;
|
||||||
|
@ -48,83 +45,12 @@ class MailingListRecipient {
|
||||||
$this->updateData = [];
|
$this->updateData = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**
|
/**
|
||||||
// * Get or create a contact.
|
* Data to be updated. Use the `save()` method to apply the update.
|
||||||
// *
|
*
|
||||||
// * @param string $fistName
|
* @var array
|
||||||
// * @param string $lastName
|
*/
|
||||||
// * @param string $email
|
private array $updateData;
|
||||||
// * @param string $locationType The location type of the email address
|
|
||||||
// *
|
|
||||||
// * @return \Civi\Mailinglistsync\MailingListRecipient
|
|
||||||
// * @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
|
||||||
// */
|
|
||||||
// public static function getOrCreate(
|
|
||||||
// string $fistName,
|
|
||||||
// string $lastName,
|
|
||||||
// string $email,
|
|
||||||
// string $locationType,
|
|
||||||
// ): self {
|
|
||||||
//
|
|
||||||
// // Verify location type
|
|
||||||
// $locationTypes = [
|
|
||||||
// GroupMailingList::LOCATION_TYPE,
|
|
||||||
// ADGroupMailingList::LOCATION_TYPE,
|
|
||||||
// EventMailingList::LOCATION_TYPE,
|
|
||||||
// ];
|
|
||||||
// if (!in_array($locationType, $locationTypes)) {
|
|
||||||
// throw new MailinglistException(
|
|
||||||
// E::ts('Invalid location type'),
|
|
||||||
// MailinglistException::ERROR_CODE_INVALID_LOCATION_TYPE,
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // Try to get contact
|
|
||||||
// try {
|
|
||||||
// $contact = \Civi\Api4\Contact::get()
|
|
||||||
// ->addSelect('*', 'location_type.*')
|
|
||||||
// ->addJoin('Email AS email', 'LEFT', ['id', '=', 'email.contact_id'])
|
|
||||||
// ->addJoin('LocationType AS location_type', 'LEFT', ['email.location_type_id', '=', 'location_type.id'])
|
|
||||||
// ->addWhere('email.email', '=', $email)
|
|
||||||
// ->addWhere('location_type.name', '=', $locationType)
|
|
||||||
// ->execute()
|
|
||||||
// ->first();
|
|
||||||
// }
|
|
||||||
// catch (\Exception $e) {
|
|
||||||
// throw new MailinglistException(
|
|
||||||
// E::ts('Failed to get contact'),
|
|
||||||
// MailinglistException::ERROR_CODE_GET_CONTACT_FAILED,
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // Create contact if it does not exist
|
|
||||||
// if (empty($contact)) {
|
|
||||||
// try {
|
|
||||||
// $contact = \Civi\Api4\Contact::create(FALSE)
|
|
||||||
// ->addValue('contact_type', 'Individual')
|
|
||||||
// ->addValue('first_name', $fistName)
|
|
||||||
// ->addValue('last_name', $lastName)
|
|
||||||
// ->setChain([
|
|
||||||
// 'Email.create',
|
|
||||||
// \Civi\Api4\Email::create(FALSE)
|
|
||||||
// ->addValue('contact_id', '$id')
|
|
||||||
// ->addValue('email', $email)
|
|
||||||
// ->addValue('location_type_id.name', $locationType),
|
|
||||||
// ])
|
|
||||||
// ->execute()
|
|
||||||
// ->first();
|
|
||||||
// array_pop($contact['Email.create']);
|
|
||||||
// }
|
|
||||||
// catch (\Exception $e) {
|
|
||||||
// throw new MailinglistException(
|
|
||||||
// E::ts('Failed to create contact'),
|
|
||||||
// MailinglistException::ERROR_CODE_CREATE_CONTACT_FAILED,
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// return new static(contact: $contact, email: $email);
|
|
||||||
// }
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new contact and return the recipient.
|
* Create a new contact and return the recipient.
|
||||||
|
@ -133,68 +59,133 @@ class MailingListRecipient {
|
||||||
* @param string $lastName
|
* @param string $lastName
|
||||||
* @param string $email
|
* @param string $email
|
||||||
* @param string $sid
|
* @param string $sid
|
||||||
|
* @param string $locationType
|
||||||
*
|
*
|
||||||
* @return \Civi\Mailinglistsync\MailingListRecipient
|
* @return \Civi\Mailinglistsync\MailingListRecipient
|
||||||
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
||||||
*/
|
*/
|
||||||
public static function createContact(
|
public static function createContact(
|
||||||
string $firstName,
|
|
||||||
string $lastName,
|
|
||||||
string $email,
|
string $email,
|
||||||
string $sid,
|
string $sid,
|
||||||
|
string $locationType,
|
||||||
|
string $firstName = '',
|
||||||
|
string $lastName = '',
|
||||||
): self {
|
): self {
|
||||||
|
|
||||||
// Try to create a new contact
|
// Try to create a new contact
|
||||||
try {
|
try {
|
||||||
$contact = \Civi\Api4\Contact::create(FALSE)
|
$contact = \Civi\Api4\Contact::create(FALSE)
|
||||||
->addValue('contact_type', 'Individual')
|
->addValue('contact_type', 'Individual')
|
||||||
->addValue('first_name', $firstName)
|
->addValue('Active_Directory.SID', $sid)
|
||||||
->addValue('last_name', $lastName)
|
->addChain(
|
||||||
->setChain([
|
|
||||||
'Email.create',
|
'Email.create',
|
||||||
\Civi\Api4\Email::create(FALSE)
|
\Civi\Api4\Email::create(FALSE)
|
||||||
|
->addValue('contact_id', '$id')
|
||||||
->addValue('email', $email)
|
->addValue('email', $email)
|
||||||
->addValue('location_type_id.name', 'Main'),
|
->addValue('location_type_id:name', $locationType),
|
||||||
])
|
);
|
||||||
->execute();
|
|
||||||
|
// If contact has a firstname, add it to the chain
|
||||||
|
if (!empty($firstName)) {
|
||||||
|
$contact->addValue('first_name', $firstName);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If contact has a lastname, add it to the chain
|
||||||
|
if (!empty($lastName)) {
|
||||||
|
$contact->addValue('last_name', $lastName);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If contact has no firstname or lastname, add email as display name
|
||||||
|
if (empty($firstName) && empty($lastName)) {
|
||||||
|
$contact->addValue('display_name', $email);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Execute the query
|
||||||
|
$contact->execute();
|
||||||
}
|
}
|
||||||
catch (\Exception $e) {
|
catch (\Exception $e) {
|
||||||
throw new MailinglistException(
|
throw new MailinglistException(
|
||||||
E::ts("Failed to create contact: {$e->getMessage()}"),
|
"Failed to create contact: {$e->getMessage()}",
|
||||||
MailinglistException::ERROR_CODE_CREATE_CONTACT_FAILED,
|
MailinglistException::ERROR_CODE_CREATE_CONTACT_FAILED,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new static(
|
return new static(
|
||||||
|
sid: $sid,
|
||||||
contact_id: $contact['id'],
|
contact_id: $contact['id'],
|
||||||
first_name: $firstName,
|
first_name: $firstName,
|
||||||
last_name: $lastName,
|
last_name: $lastName,
|
||||||
email: $email,
|
email: $email,
|
||||||
sid: $sid,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a list of group mailing lists the contact is subscribed to.
|
* Get a recipient by its email address.
|
||||||
*
|
*
|
||||||
* @return array
|
* @param string $email
|
||||||
|
*
|
||||||
|
* @return MailingListRecipient
|
||||||
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
||||||
*/
|
*/
|
||||||
public function getMailingLists(): array {
|
public static function getContactIdEmail(string $email): MailingListRecipient {
|
||||||
|
try {
|
||||||
|
$recipientData = Contact::get()
|
||||||
|
->addSelect('id', 'first_name', 'last_name', 'email.email', 'Active_Directory.SID')
|
||||||
|
->addJoin('Email AS email', 'LEFT', ['id', '=', 'email.contact_id'])
|
||||||
|
->addWhere('email.email', '=', $email)
|
||||||
|
->addGroupBy('id')
|
||||||
|
->execute()
|
||||||
|
->first();
|
||||||
|
}
|
||||||
|
catch (\Exception $e) {
|
||||||
|
throw new MailinglistException(
|
||||||
|
"Could not get recipient by email '{$email}': {$e->getMessage()}",
|
||||||
|
MailinglistException::ERROR_CODE_GET_RECIPIENTS_FAILED
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$recipient = new MailingListRecipient(
|
||||||
|
sid: $recipientData['Active_Directory.SID'],
|
||||||
|
contact_id: $recipientData['id'],
|
||||||
|
first_name: $recipientData['first_name'],
|
||||||
|
last_name: $recipientData['last_name'],
|
||||||
|
email: $recipientData['email.email'],
|
||||||
|
);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
throw new MailinglistException(
|
||||||
|
"Could not create recipient object for contact with id '{$recipientData['id']}': {$e->getMessage()}",
|
||||||
|
MailinglistException::ERROR_CODE_GET_RECIPIENTS_FAILED
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $recipient;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a list of group mailing lists the contact is subscribed to.
|
||||||
|
*
|
||||||
|
* @return ?array
|
||||||
|
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
||||||
|
*/
|
||||||
|
public function getMailingLists(): ?array {
|
||||||
$mailingLists = [];
|
$mailingLists = [];
|
||||||
try {
|
try {
|
||||||
$groups = \Civi\Api4\GroupContact::get()
|
$groups = \Civi\Api4\GroupContact::get()
|
||||||
->addSelect('group_id')
|
->addSelect('group_id')
|
||||||
|
->addJoin(
|
||||||
|
'Group AS group', 'INNER',
|
||||||
|
['group_id', '=', 'group.id'],
|
||||||
|
['group.' . GroupMailingList::CUSTOM_GROUP_NAME . '.Enable_mailing_list', '=', 1],
|
||||||
|
)
|
||||||
->addWhere('contact_id', '=', $this->getContactId())
|
->addWhere('contact_id', '=', $this->getContactId())
|
||||||
->addWhere('status', '=', 'Added')
|
->addWhere('status', '=', 'Added')
|
||||||
->addWhere(GroupMailingList::CUSTOM_GROUP_NAME . '.Enable_mailing_list', '=', TRUE)
|
|
||||||
->addWhere(GroupMailingList::CUSTOM_GROUP_NAME . '.Active_Directory_SID', 'IS EMPTY')
|
|
||||||
->execute()
|
->execute()
|
||||||
->getArrayCopy();
|
->getArrayCopy();
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
throw new MailinglistException(
|
throw new MailinglistException(
|
||||||
E::ts('Failed to get group mailing lists'),
|
'Failed to get group mailing lists',
|
||||||
MailinglistException::ERROR_CODE_GET_GROUP_MAILING_LISTS_FAILED,
|
MailinglistException::ERROR_CODE_GET_GROUP_MAILING_LISTS_FAILED,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -213,100 +204,164 @@ class MailingListRecipient {
|
||||||
/**
|
/**
|
||||||
* Get a list of AD group mailing lists the contact is subscribed to.
|
* Get a list of AD group mailing lists the contact is subscribed to.
|
||||||
*
|
*
|
||||||
* @return array
|
* @return ?array
|
||||||
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
||||||
*/
|
*/
|
||||||
public function getAdMailingLists(): array {
|
public function getAdMailingLists(): ?array {
|
||||||
$mailingLists = [];
|
$mailingLists = [];
|
||||||
try {
|
try {
|
||||||
$groups = \Civi\Api4\GroupContact::get()
|
$groups = \Civi\Api4\GroupContact::get()
|
||||||
->addSelect('group_id')
|
->addSelect('group_id')
|
||||||
|
->addJoin(
|
||||||
|
'Group AS group', 'INNER',
|
||||||
|
['group_id', '=', 'group.id'],
|
||||||
|
['group.' . GroupMailingList::CUSTOM_GROUP_NAME . '.Enable_mailing_list', '=', 1],
|
||||||
|
['group.' . GroupMailingList::CUSTOM_GROUP_NAME . '.Active_Directory_SID', 'IS NOT EMPTY'],
|
||||||
|
)
|
||||||
->addWhere('contact_id', '=', $this->getContactId())
|
->addWhere('contact_id', '=', $this->getContactId())
|
||||||
->addWhere('status', '=', 'Added')
|
->addWhere('status', '=', 'Added')
|
||||||
->addWhere(GroupMailingList::CUSTOM_GROUP_NAME . '.Enable_mailing_list', '=', TRUE)
|
|
||||||
->addWhere(GroupMailingList::CUSTOM_GROUP_NAME . '.Active_Directory_SID', 'IS NOT EMPTY')
|
|
||||||
->execute()
|
->execute()
|
||||||
->getArrayCopy();
|
->getArrayCopy();
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
throw new MailinglistException(
|
throw new MailinglistException(
|
||||||
E::ts('Failed to get AD group mailing lists'),
|
'Failed to get AD group mailing lists',
|
||||||
MailinglistException::ERROR_CODE_GET_AD_GROUP_MAILING_LISTS_FAILED,
|
MailinglistException::ERROR_CODE_GET_AD_GROUP_MAILING_LISTS_FAILED,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($groups as $group) {
|
foreach ($groups as $group) {
|
||||||
$mailingList = new GroupMailingList($group['group_id']);
|
$mailingLists[] = new ADGroupMailingList($group['group_id']);
|
||||||
if ($mailingList->isADGroup()) {
|
|
||||||
$mailingList = new ADGroupMailingList($group['group_id']); ;
|
|
||||||
}
|
|
||||||
$mailingLists[] = $mailingList;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $mailingLists;
|
return $mailingLists;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Get a list of event mailing lists the contact is subscribed to.
|
||||||
|
*
|
||||||
|
* @return ?array
|
||||||
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
||||||
*/
|
*/
|
||||||
public function getEventMailingLists(): array {
|
public function getEventMailingLists(): ?array {
|
||||||
$mailingLists = [];
|
$mailingLists = [];
|
||||||
try {
|
try {
|
||||||
$groups = \Civi\Api4\Participant::get()
|
$groups = \Civi\Api4\Participant::get()
|
||||||
->addSelect('event_id')
|
->addSelect('event_id')
|
||||||
|
->addJoin(
|
||||||
|
'Event AS event', 'INNER',
|
||||||
|
['event_id', '=', 'event.id'],
|
||||||
|
['event.' . EventMailingList::CUSTOM_GROUP_NAME . '.Enable_mailing_list', '=', 1],
|
||||||
|
)
|
||||||
->addWhere('contact_id', '=', $this->getContactId())
|
->addWhere('contact_id', '=', $this->getContactId())
|
||||||
->addWhere('status_id', 'IN', EventMailingList::getEnabledParticipantStatus())
|
->addWhere('status_id', 'IN', EventMailingList::getEnabledParticipantStatus())
|
||||||
->addWhere(GroupMailingList::CUSTOM_GROUP_NAME . '.Enable_mailing_list', '=', TRUE)
|
|
||||||
->execute()
|
->execute()
|
||||||
->getArrayCopy();
|
->getArrayCopy();
|
||||||
}
|
}
|
||||||
catch (\Exception $e) {
|
catch (\Exception $e) {
|
||||||
throw new MailinglistException(
|
throw new MailinglistException(
|
||||||
E::ts('Failed to get event mailing lists'),
|
'Failed to get event mailing lists',
|
||||||
MailinglistException::ERROR_CODE_GET_EVENT_MAILING_LISTS_FAILED,
|
MailinglistException::ERROR_CODE_GET_EVENT_MAILING_LISTS_FAILED,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($groups as $group) {
|
foreach ($groups as $group) {
|
||||||
$mailingList = new EventMailingList($group['group_id']);
|
$mailingLists[] = new EventMailingList($group['group_id']);
|
||||||
$mailingLists[] = $mailingList;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $mailingLists;
|
return $mailingLists;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getContactId(): int {
|
/**
|
||||||
|
* Get the contact ID.
|
||||||
|
*
|
||||||
|
* @return int|NULL
|
||||||
|
*/
|
||||||
|
public function getContactId(): ?int {
|
||||||
return $this->contactId;
|
return $this->contactId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getFirstName(): string {
|
/**
|
||||||
|
* Get the first name of the contact.
|
||||||
|
*
|
||||||
|
* @return string|NULL
|
||||||
|
*/
|
||||||
|
public function getFirstName(): ?string {
|
||||||
return $this->firstName;
|
return $this->firstName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getLastName(): string {
|
/**
|
||||||
|
* Get the last name of the contact.
|
||||||
|
*
|
||||||
|
* @return string|NULL
|
||||||
|
*/
|
||||||
|
public function getLastName(): ?string {
|
||||||
return $this->lastName;
|
return $this->lastName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getEmail(): string {
|
/**
|
||||||
|
* Get the email address of the contact.
|
||||||
|
*
|
||||||
|
* @return string|NULL
|
||||||
|
*/
|
||||||
|
public function getEmail(): ?string {
|
||||||
return $this->email;
|
return $this->email;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSid(): string {
|
/**
|
||||||
|
* Get the SID of the contact.
|
||||||
|
*
|
||||||
|
* @return string|NULL
|
||||||
|
*/
|
||||||
|
public function getSid(): ?string {
|
||||||
return $this->sid;
|
return $this->sid;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setFirstName(string $firstName): void {
|
/**
|
||||||
|
* Update the first name of the contact.
|
||||||
|
*
|
||||||
|
* @param string $firstName
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function updateFirstName(string $firstName): void {
|
||||||
$this->updateData += ['first_name' => $firstName];
|
$this->updateData += ['first_name' => $firstName];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setLastName(string $lastName): void {
|
/**
|
||||||
|
* Update the last name of the contact.
|
||||||
|
*
|
||||||
|
* @param string $lastName
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function updateLastName(string $lastName): void {
|
||||||
$this->updateData += ['last_name' => $lastName];
|
$this->updateData += ['last_name' => $lastName];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setEmail(string $email): void {
|
/**
|
||||||
|
* Update the email address of the contact.
|
||||||
|
*
|
||||||
|
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
||||||
|
*/
|
||||||
|
public function updateEmail(string $email): void {
|
||||||
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||||
|
throw new MailinglistException(
|
||||||
|
"Invalid email address '$email'",
|
||||||
|
MailinglistException::ERROR_CODE_INVALID_EMAIL_ADDRESS,
|
||||||
|
);
|
||||||
|
}
|
||||||
$this->updateData += ['email' => $email];
|
$this->updateData += ['email' => $email];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setSid(string $sid): void {
|
/**
|
||||||
$this->updateData += ['sid' => $sid];
|
* Update the SID of the contact.
|
||||||
|
*
|
||||||
|
* @param string $sid
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function updateSid(string $sid): void {
|
||||||
|
$this->updateData += [self::CUSTOM_GROUP_NAME . '.SID' => $sid];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -319,17 +374,156 @@ class MailingListRecipient {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$contactValues = ['first_name', 'last_name', self::CUSTOM_GROUP_NAME . '.SID'];
|
||||||
|
$contactUpdates = array_intersect_key($this->updateData, array_flip($contactValues));
|
||||||
|
|
||||||
|
if (!empty($contactUpdates)) {
|
||||||
try {
|
try {
|
||||||
\Civi\Api4\Contact::update()
|
\Civi\Api4\Contact::update()
|
||||||
->setValues($this->updateData)
|
->setValues($contactUpdates)
|
||||||
->addWhere('id', '=', $this->getContactId())
|
->addWhere('id', '=', $this->getContactId())
|
||||||
->execute();
|
->execute();
|
||||||
} catch (\Exception $e) {
|
\Civi::log()->debug(
|
||||||
|
"Updated contact '{$this->getContactId()}' with data: " . json_encode($this->updateData)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
catch (\Exception $e) {
|
||||||
|
\Civi::log()->error(
|
||||||
|
"Failed to update contact '{$this->getContactId()}': $e"
|
||||||
|
);
|
||||||
throw new MailinglistException(
|
throw new MailinglistException(
|
||||||
E::ts("Failed to update contact: {$e->getMessage()}"),
|
"Failed to update contact: {$e->getMessage()}",
|
||||||
MailinglistException::ERROR_CODE_UPDATE_ENTITY_FAILED,
|
MailinglistException::ERROR_CODE_UPDATE_ENTITY_FAILED,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update email address
|
||||||
|
if (!empty($this->updateData['email'])) {
|
||||||
|
try {
|
||||||
|
\Civi\Api4\Email::update()
|
||||||
|
->addValue('email', $this->updateData['email'])
|
||||||
|
->addWhere('contact_id', '=', $this->getContactId())
|
||||||
|
->execute();
|
||||||
|
\Civi::log()->debug(
|
||||||
|
"Updated email address for contact '{$this->getContactId()}'"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
catch (\Exception $e) {
|
||||||
|
\Civi::log()->error(
|
||||||
|
"Failed to update email address for contact '{$this->getContactId()}': $e"
|
||||||
|
);
|
||||||
|
throw new MailinglistException(
|
||||||
|
"Failed to update email address: {$e->getMessage()}",
|
||||||
|
MailinglistException::ERROR_CODE_UPDATE_ENTITY_FAILED,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
self::createSyncActivity(
|
||||||
|
$this->getContactId(),
|
||||||
|
'Completed',
|
||||||
|
E::ts('Contact updated'),
|
||||||
|
E::ts('The contact data was updated by the mailing list synchronization: %1', [
|
||||||
|
1 => json_encode($this->updateData)
|
||||||
|
])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
catch (\Exception $e) {
|
||||||
|
\Civi::log()->error(
|
||||||
|
"Failed to create activity for contact '{$this->getContactId()}': $e"
|
||||||
|
);
|
||||||
|
throw new MailinglistException(
|
||||||
|
"Failed to create activity for contact '{$this->getContactId()}': $e",
|
||||||
|
MailinglistException::ERROR_CODE_CREATE_ACTIVITY_FAILED,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a list of email location types for the contact.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
||||||
|
*/
|
||||||
|
public function getEmailLocationTypes(): array {
|
||||||
|
try {
|
||||||
|
$result = \Civi\Api4\Email::get()
|
||||||
|
->addSelect('location_type_id:name')
|
||||||
|
->addWhere('contact_id', '=', $this->getContactId())
|
||||||
|
->execute()
|
||||||
|
->getArrayCopy();
|
||||||
|
return array_column($result, 'location_type_id:name');
|
||||||
|
}
|
||||||
|
catch (\Exception $e) {
|
||||||
|
throw new MailinglistException(
|
||||||
|
'Failed to get email location types',
|
||||||
|
MailinglistException::ERROR_CODE_GET_EMAIL_LOCATION_TYPES_FAILED,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create an e-mail address for the contact.
|
||||||
|
*
|
||||||
|
* @param string $email
|
||||||
|
* @param string $locationType
|
||||||
|
*
|
||||||
|
* @return array|NULL
|
||||||
|
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
||||||
|
*/
|
||||||
|
public function createEmail(string $email, string $locationType): ?array {
|
||||||
|
try {
|
||||||
|
$result = \Civi\Api4\Email::create(FALSE)
|
||||||
|
->addValue('contact_id', $this->getContactId())
|
||||||
|
->addValue('email', $email)
|
||||||
|
->addValue('location_type_id:name', $locationType)
|
||||||
|
->execute()
|
||||||
|
->first();
|
||||||
|
}
|
||||||
|
catch (\Exception $e) {
|
||||||
|
throw new MailinglistException(
|
||||||
|
'Failed to create email',
|
||||||
|
MailinglistException::ERROR_CODE_CREATE_EMAIL_ADDRESS_FAILED,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
$this->email = $email;
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a synchronization activity to a contact.
|
||||||
|
*
|
||||||
|
* @param int $targetContactId
|
||||||
|
* @param string $status
|
||||||
|
* @param string $subject
|
||||||
|
* @param string $details
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
||||||
|
*/
|
||||||
|
public static function createSyncActivity(
|
||||||
|
int $targetContactId,
|
||||||
|
string $status,
|
||||||
|
string $subject,
|
||||||
|
string $details,
|
||||||
|
): void {
|
||||||
|
try {
|
||||||
|
\Civi\Api4\Activity::create(FALSE)
|
||||||
|
->addValue('source_contact_id', getSessionUser())
|
||||||
|
->addValue('activity_type_id:name', 'Mailing_List_Synchronization_Activity')
|
||||||
|
->addValue('subject', $subject)
|
||||||
|
->addValue('details', $details)
|
||||||
|
->addValue('status_id:name', ucfirst($status))
|
||||||
|
->addValue('target_contact_id', $targetContactId)
|
||||||
|
->execute()->first();
|
||||||
|
}
|
||||||
|
catch (\Exception $e) {
|
||||||
|
throw new MailinglistException(
|
||||||
|
"Failed to create contact activity: $e",
|
||||||
|
MailinglistException::ERROR_CODE_CREATE_ACTIVITY_FAILED,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Civi\Mailinglistsync;
|
namespace Civi\Mailinglistsync;
|
||||||
|
|
||||||
use Civi\Authx\None;
|
|
||||||
use CRM_Mailinglistsync_ExtensionUtil as E;
|
use CRM_Mailinglistsync_ExtensionUtil as E;
|
||||||
|
|
||||||
class MailingListSettings {
|
class MailingListSettings {
|
||||||
|
@ -14,6 +14,9 @@ class MailingListSettings {
|
||||||
E::SHORT_NAME . '_logging' => [
|
E::SHORT_NAME . '_logging' => [
|
||||||
'data_type' => 'boolean',
|
'data_type' => 'boolean',
|
||||||
],
|
],
|
||||||
|
E::SHORT_NAME . '_domain' => [
|
||||||
|
'data_type' => 'string',
|
||||||
|
],
|
||||||
E::SHORT_NAME . '_mlmmj_host' => [
|
E::SHORT_NAME . '_mlmmj_host' => [
|
||||||
'data_type' => 'string',
|
'data_type' => 'string',
|
||||||
],
|
],
|
||||||
|
@ -75,7 +78,7 @@ class MailingListSettings {
|
||||||
*/
|
*/
|
||||||
public static function get($key = NULL): mixed {
|
public static function get($key = NULL): mixed {
|
||||||
if (!is_null($key)) {
|
if (!is_null($key)) {
|
||||||
return \Civi::settings()->get($key);
|
return \Civi::settings()->get(E::SHORT_NAME . '_' . $key);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$settings = [];
|
$settings = [];
|
||||||
|
@ -95,30 +98,36 @@ class MailingListSettings {
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function validate(array $values, array &$errors): void {
|
public static function validate(array $values, array &$errors): void {
|
||||||
|
|
||||||
|
// Validate domain
|
||||||
|
if (empty($values[E::SHORT_NAME . '_domain'])) {
|
||||||
|
$errors[E::SHORT_NAME . '_domain'] = E::ts('Domain is required');
|
||||||
|
}
|
||||||
|
|
||||||
// Validate url if synchronization is enabled
|
// Validate url if synchronization is enabled
|
||||||
$url = $values[E::SHORT_NAME . '_mailinglist_mlmmj_host'];
|
$url = $values[E::SHORT_NAME . '_mlmmj_host'];
|
||||||
if (!empty($values[E::SHORT_NAME . '_mailinglist_mlmmj_enable']) && !filter_var($url, FILTER_VALIDATE_URL)) {
|
if (!empty($values[E::SHORT_NAME . '__mlmmj_enable']) && !filter_var($url, FILTER_VALIDATE_URL)) {
|
||||||
$errors[E::SHORT_NAME . '_mailinglist_mlmmj_host'] = E::ts('Invalid URL');
|
$errors[E::SHORT_NAME . '_mlmmj_host'] = E::ts('Invalid URL');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate port if synchronization is enabled and port is set
|
// Validate port if synchronization is enabled and port is set
|
||||||
$port = $values[E::SHORT_NAME . '_mailinglist_mlmmj_port'] ?? NULL;
|
$port = $values[E::SHORT_NAME . '_mlmmj_port'] ?? NULL;
|
||||||
if (!empty($values[E::SHORT_NAME . '_mailinglist_mlmmj_enable']) && !empty($port)) {
|
if (!empty($values[E::SHORT_NAME . '_mlmmj_enable']) && !empty($port)) {
|
||||||
if (is_numeric($port)) {
|
if (is_numeric($port)) {
|
||||||
$errors[E::SHORT_NAME . '_mailinglist_mlmmj_port'] = E::ts('Port must be a number');
|
$errors[E::SHORT_NAME . '_mlmmj_port'] = E::ts('Port must be a number');
|
||||||
}
|
}
|
||||||
if ($port < 1 || $port > 65535) {
|
if ($port < 1 || $port > 65535) {
|
||||||
$errors[E::SHORT_NAME . '_mailinglist_mlmmj_port'] = E::ts('Port must be between 1 and 65535');
|
$errors[E::SHORT_NAME . '_mlmmj_port'] = E::ts('Port must be between 1 and 65535');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Require host and token if mlmmj is enabled
|
// Require host and token if mlmmj is enabled
|
||||||
if (!empty($values[E::SHORT_NAME . '_mailinglist_mlmmj_enable'])) {
|
if (!empty($values[E::SHORT_NAME . '_mlmmj_enable'])) {
|
||||||
if (empty($values[E::SHORT_NAME . '_mailinglist_mlmmj_host'])) {
|
if (empty($values[E::SHORT_NAME . '_mlmmj_host'])) {
|
||||||
$errors[E::SHORT_NAME . '_mailinglist_mlmmj_host'] = E::ts('Host is required');
|
$errors[E::SHORT_NAME . '_mlmmj_host'] = E::ts('Host is required');
|
||||||
}
|
}
|
||||||
if (empty($values[E::SHORT_NAME . '_mailinglist_mlmmj_token'])) {
|
if (empty($values[E::SHORT_NAME . '_mlmmj_token'])) {
|
||||||
$errors[E::SHORT_NAME . '_mailinglist_mlmmj_token'] = E::ts('Token is required');
|
$errors[E::SHORT_NAME . '_mlmmj_token'] = E::ts('Token is required');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,20 +1,13 @@
|
||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Civi\Mailinglistsync;
|
namespace Civi\Mailinglistsync;
|
||||||
|
|
||||||
use CRM_Mailinglistsync_ExtensionUtil as E;
|
|
||||||
use Civi\Mailinglistsync\Exceptions\MailinglistException;
|
use Civi\Mailinglistsync\Exceptions\MailinglistException;
|
||||||
use Civi\Mailinglistsync\Exceptions\MailinglistSyncException;
|
|
||||||
use CRM_Queue_Queue;
|
use CRM_Queue_Queue;
|
||||||
|
|
||||||
class QueueHelper {
|
class QueueHelper {
|
||||||
|
use Singleton;
|
||||||
/**
|
|
||||||
* Singleton instances.
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
private static array $instances = [];
|
|
||||||
|
|
||||||
private CRM_Queue_Queue $groupQueue;
|
private CRM_Queue_Queue $groupQueue;
|
||||||
private CRM_Queue_Queue $eventQueue;
|
private CRM_Queue_Queue $eventQueue;
|
||||||
|
@ -39,52 +32,23 @@ class QueueHelper {
|
||||||
'error' => 'drop',
|
'error' => 'drop',
|
||||||
]);
|
]);
|
||||||
$this->eventQueue = \Civi::queue(
|
$this->eventQueue = \Civi::queue(
|
||||||
'propeace-mailinglist-group-queue', [
|
'propeace-mailinglist-event-queue', [
|
||||||
'type' => 'SqlParallel',
|
'type' => 'SqlParallel',
|
||||||
'is_autorun' => FALSE,
|
'is_autorun' => FALSE,
|
||||||
'reset' => FALSE,
|
'reset' => FALSE,
|
||||||
'error' => 'drop',
|
'error' => 'drop',
|
||||||
]);
|
]);
|
||||||
$this->emailQueue = \Civi::queue(
|
$this->emailQueue = \Civi::queue(
|
||||||
'propeace-mailinglist-group-queue', [
|
'propeace-mailinglist-email-queue', [
|
||||||
'type' => 'SqlParallel',
|
'type' => 'SqlParallel',
|
||||||
'is_autorun' => FALSE,
|
'is_autorun' => FALSE,
|
||||||
'reset' => FALSE,
|
'reset' => FALSE,
|
||||||
'error' => 'drop',
|
'error' => 'drop',
|
||||||
]);
|
]);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
$this->groups = [];
|
||||||
* Protect singleton from being cloned.
|
$this->events = [];
|
||||||
*/
|
$this->emails = [];
|
||||||
protected function __clone() { }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Protect unserialize method to prevent cloning of the instance.
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
public function __wakeup()
|
|
||||||
{
|
|
||||||
throw new MailinglistSyncException(
|
|
||||||
"Cannot unserialize a singleton.",
|
|
||||||
MailinglistSyncException::ERROR_CODE_UNSERIALIZE_SINGLETON
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the singleton instance.
|
|
||||||
*
|
|
||||||
* @return \Civi\Mailinglistsync\QueueHelper
|
|
||||||
*/
|
|
||||||
public static function getInstance(): QueueHelper
|
|
||||||
{
|
|
||||||
$cls = static::class;
|
|
||||||
if (!isset(self::$instances[$cls])) {
|
|
||||||
self::$instances[$cls] = new static();
|
|
||||||
\Civi::log()->debug(E::LONG_NAME . ": Created new instance of $cls");
|
|
||||||
}
|
|
||||||
|
|
||||||
return self::$instances[$cls];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -138,17 +102,31 @@ class QueueHelper {
|
||||||
$this->emails[] = $email;
|
$this->emails[] = $email;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores an email address in the queue helper singleton.
|
||||||
|
*
|
||||||
|
* @param \CRM_Queue_TaskContext $context
|
||||||
|
* @param string $email
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function storeEmail(\CRM_Queue_TaskContext $context, string $email): bool {
|
||||||
|
self::getInstance()->addToEmails($email);
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stores an instance of the given class in the queue helper singleton.
|
* Stores an instance of the given class in the queue helper singleton.
|
||||||
* Meant to be passed as callback to the queue.
|
* Meant to be passed as callback to the queue.
|
||||||
*
|
*
|
||||||
|
* @param \CRM_Queue_TaskContext $context
|
||||||
* @param int $entityId
|
* @param int $entityId
|
||||||
* @param string $class
|
* @param string $class
|
||||||
*
|
*
|
||||||
* @return void
|
* @return bool TRUE if the instance was stored successfully.
|
||||||
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
||||||
*/
|
*/
|
||||||
public static function storeInstance(int $entityId, string $class): void {
|
public static function storeInstance(\CRM_Queue_TaskContext $context, int $entityId, string $class): bool {
|
||||||
|
|
||||||
// Throw exception if class is invalid
|
// Throw exception if class is invalid
|
||||||
if ($class != GroupMailingList::class &&
|
if ($class != GroupMailingList::class &&
|
||||||
|
@ -161,17 +139,23 @@ class QueueHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Instantiate the mailing list object
|
// Instantiate the mailing list object
|
||||||
$instance = new $class();
|
/* @var $instance GroupMailingList|ADGroupMailingList|EventMailingList */
|
||||||
$instance->load($entityId);
|
$instance = new $class($entityId);
|
||||||
|
|
||||||
|
// Ignore disabled mailing lists
|
||||||
|
if (!$instance->isEnabled()) {
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
// Store instance in the queue helper singleton
|
// Store instance in the queue helper singleton
|
||||||
match ($class) {
|
if (
|
||||||
GroupMailingList::class, ADGroupMailingList::class => self::getInstance()->addToGroups($instance),
|
$instance::class === GroupMailingList::class
|
||||||
EventMailingList::class => self::getInstance()->addToEvents($instance),
|
|| $instance::class === ADGroupMailingList::class
|
||||||
default => throw new MailinglistException(
|
) {
|
||||||
"Invalid class '$class'",
|
self::getInstance()->addToGroups($instance);
|
||||||
MailinglistException::ERROR_CODE_INVALID_CLASS
|
} else {
|
||||||
),
|
self::getInstance()->addToEvents($instance);
|
||||||
};
|
}
|
||||||
|
return TRUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
50
Civi/Mailinglistsync/Singleton.php
Normal file
50
Civi/Mailinglistsync/Singleton.php
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Civi\Mailinglistsync;
|
||||||
|
|
||||||
|
use Civi\Mailinglistsync\Exceptions\ContactSyncException;
|
||||||
|
use Civi\Mailinglistsync\Exceptions\MailinglistException;
|
||||||
|
use CRM_Mailinglistsync_ExtensionUtil as E;
|
||||||
|
|
||||||
|
trait Singleton {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Singleton instance.
|
||||||
|
*/
|
||||||
|
private static $instance;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Protect singleton from being instantiated.
|
||||||
|
*/
|
||||||
|
protected function __construct() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Protect singleton from being cloned.
|
||||||
|
*/
|
||||||
|
protected function __clone() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Protect unserialize method to prevent cloning of the instance.
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function __wakeup()
|
||||||
|
{
|
||||||
|
throw new ContactSyncException(
|
||||||
|
"Cannot unserialize a singleton.",
|
||||||
|
MailinglistException::ERROR_CODE_UNSERIALIZE_SINGLETON
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the singleton instance.
|
||||||
|
*
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public static function getInstance(): self
|
||||||
|
{
|
||||||
|
if (!self::$instance) {
|
||||||
|
self::$instance = new self();
|
||||||
|
}
|
||||||
|
return self::$instance;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Civi\Mailinglistsync;
|
namespace Civi\Mailinglistsync;
|
||||||
|
|
||||||
|
@ -37,7 +38,7 @@ function getLocationTypes(): array {
|
||||||
}
|
}
|
||||||
catch (\Exception $e) {
|
catch (\Exception $e) {
|
||||||
throw new MailinglistException(
|
throw new MailinglistException(
|
||||||
E::ts('Could not get location types'),
|
"Could not get location types: {$e->getMessage()}",
|
||||||
MailinglistException::ERROR_CODE_GET_LOCATION_TYPES_FAILED,
|
MailinglistException::ERROR_CODE_GET_LOCATION_TYPES_FAILED,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -47,3 +48,13 @@ function getLocationTypes(): array {
|
||||||
|
|
||||||
return $locationTypes;
|
return $locationTypes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a user ID of the current session.
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
function getSessionUser(): int {
|
||||||
|
$session = \CRM_Core_Session::singleton();
|
||||||
|
return (int) $session->get('userID');
|
||||||
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
use Civi\Mailinglistsync\ADGroupMailingList;
|
use Civi\Mailinglistsync\ADGroupMailingList;
|
||||||
use Civi\Mailinglistsync\Exceptions\MailinglistException;
|
use Civi\Mailinglistsync\Exceptions\MailinglistException;
|
||||||
|
use Civi\Mailinglistsync\MailingListSettings;
|
||||||
use CRM_Mailinglistsync_ExtensionUtil as E;
|
use CRM_Mailinglistsync_ExtensionUtil as E;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -14,31 +15,31 @@ use CRM_Mailinglistsync_ExtensionUtil as E;
|
||||||
*/
|
*/
|
||||||
function _civicrm_api3_mailinglistsync_Adgroupsync_spec(&$spec) {
|
function _civicrm_api3_mailinglistsync_Adgroupsync_spec(&$spec) {
|
||||||
$spec['sid'] = [
|
$spec['sid'] = [
|
||||||
'api.required' => 1,
|
'api.required' => 0,
|
||||||
'type' => CRM_Utils_Type::T_STRING,
|
'type' => CRM_Utils_Type::T_STRING,
|
||||||
'title' => E::ts('Active Directory SID'),
|
'title' => E::ts('Active Directory SID'),
|
||||||
'description' => E::ts('The Active Directory SID of the group'),
|
'description' => E::ts('The Active Directory SID of the group'),
|
||||||
];
|
];
|
||||||
$spec['email'] = [
|
$spec['email'] = [
|
||||||
'api.required' => 1,
|
'api.required' => 0,
|
||||||
'type' => CRM_Utils_Type::T_EMAIL,
|
'type' => CRM_Utils_Type::T_EMAIL,
|
||||||
'title' => 'Email Address',
|
'title' => 'Email Address',
|
||||||
'description' => 'Email address of the mailing list',
|
'description' => 'Email address of the mailing list',
|
||||||
];
|
];
|
||||||
$spec['recipients'] = [
|
$spec['recipients'] = [
|
||||||
'api.required' => 1,
|
'api.required' => 0,
|
||||||
'type' => CRM_Utils_Type::T_STRING,
|
'type' => CRM_Utils_Type::T_STRING,
|
||||||
'title' => E::ts('Recipients'),
|
'title' => E::ts('Recipients'),
|
||||||
'description' => E::ts('Array of email addresses and SIDs'),
|
'description' => E::ts('Array of email addresses and SIDs'),
|
||||||
];
|
];
|
||||||
$spec['name'] = [
|
$spec['name'] = [
|
||||||
'api.required' => 1,
|
'api.required' => 0,
|
||||||
'type' => CRM_Utils_Type::T_STRING,
|
'type' => CRM_Utils_Type::T_STRING,
|
||||||
'title' => E::ts('Mail List Name'),
|
'title' => E::ts('Mail List Name'),
|
||||||
'description' => E::ts('Name of the mailing list'),
|
'description' => E::ts('Name of the mailing list'),
|
||||||
];
|
];
|
||||||
$spec['description'] = [
|
$spec['description'] = [
|
||||||
'api.required' => 1,
|
'api.required' => 0,
|
||||||
'type' => CRM_Utils_Type::T_LONGTEXT,
|
'type' => CRM_Utils_Type::T_LONGTEXT,
|
||||||
'title' => E::ts('Mail List Description'),
|
'title' => E::ts('Mail List Description'),
|
||||||
'description' => E::ts('Description of the mailing list'),
|
'description' => E::ts('Description of the mailing list'),
|
||||||
|
@ -58,20 +59,66 @@ function _civicrm_api3_mailinglistsync_Adgroupsync_spec(&$spec) {
|
||||||
* @see civicrm_api3_create_success
|
* @see civicrm_api3_create_success
|
||||||
*/
|
*/
|
||||||
function civicrm_api3_mailinglistsync_Adgroupsync($params) {
|
function civicrm_api3_mailinglistsync_Adgroupsync($params) {
|
||||||
|
|
||||||
// Filter illegal params
|
// Filter illegal params
|
||||||
$allowed_params = [];
|
$allowed_params = [];
|
||||||
_civicrm_api3_mailinglistsync_Adgroupsync_spec($allowed_params);
|
_civicrm_api3_mailinglistsync_Adgroupsync_spec($allowed_params);
|
||||||
$params = array_intersect_key($params, $allowed_params);
|
$params = array_intersect_key($params, $allowed_params);
|
||||||
|
|
||||||
|
// Check if the mailing list sync is enabled
|
||||||
|
$enabled = (bool) MailingListSettings::get('enable');
|
||||||
|
if (!$enabled) {
|
||||||
|
return civicrm_api3_create_success(
|
||||||
|
['message' => 'Mailing list sync is disabled'], [], 'Mailinglist', 'Adgroupsync'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate params
|
||||||
|
|
||||||
|
// Decode recipients JSON if it's a string
|
||||||
|
if (!empty($params['recipients'])) {
|
||||||
|
if (is_string($params['recipients'])) {
|
||||||
|
try {
|
||||||
|
$params['recipients'] = json_decode($params['recipients'], TRUE);
|
||||||
|
}
|
||||||
|
catch (Exception $e) {
|
||||||
|
return civicrm_api3_create_error(
|
||||||
|
'Failed to decode recipients JSON',
|
||||||
|
[
|
||||||
|
'params' => $params,
|
||||||
|
'entity' => 'Mailinglistsync',
|
||||||
|
'action' => 'Adgroupsync',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Throw error if recipients is not an array
|
||||||
|
if (!is_array($params['recipients'])) {
|
||||||
|
return civicrm_api3_create_error(
|
||||||
|
'Recipients must be an array',
|
||||||
|
[
|
||||||
|
'params' => $params,
|
||||||
|
'entity' => 'Mailinglistsync',
|
||||||
|
'action' => 'Adgroupsync',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If group is updated
|
||||||
|
if (
|
||||||
|
!empty($params['sid']) &&
|
||||||
|
!empty($params['email']) &&
|
||||||
|
!empty($params['name'])
|
||||||
|
) {
|
||||||
// Prepare result array
|
// Prepare result array
|
||||||
$result = [
|
$result = [
|
||||||
|
'group' =>
|
||||||
|
[
|
||||||
'sid' => $params['sid'],
|
'sid' => $params['sid'],
|
||||||
'email' => $params['email'],
|
'email' => $params['email'],
|
||||||
'name' => $params['name'],
|
'name' => $params['name'],
|
||||||
'description' => $params['description'],
|
'description' => $params['description'] ?? '',
|
||||||
'group_created' => FALSE,
|
'created' => FALSE,
|
||||||
'group_updated' => FALSE,
|
'updated' => FALSE,
|
||||||
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -83,16 +130,16 @@ function civicrm_api3_mailinglistsync_Adgroupsync($params) {
|
||||||
try {
|
try {
|
||||||
$adGroupMailingList = ADGroupMailingList::createGroup(
|
$adGroupMailingList = ADGroupMailingList::createGroup(
|
||||||
title: $params['name'],
|
title: $params['name'],
|
||||||
description: $params['description'],
|
|
||||||
email: $params['email'],
|
email: $params['email'],
|
||||||
|
description: $params['description'],
|
||||||
sid: $params['sid'],
|
sid: $params['sid'],
|
||||||
);
|
);
|
||||||
$result['group_created']['group_id'] = $adGroupMailingList->getId();
|
$result['group']['created']['group_id'] = $adGroupMailingList->getId();
|
||||||
$result['group_created']['is_error'] = FALSE;
|
$result['group']['created']['is_error'] = 0;
|
||||||
}
|
}
|
||||||
catch (MailinglistException $e) {
|
catch (MailinglistException $e) {
|
||||||
\Civi::log(E::LONG_NAME)->error($e->getLogMessage());
|
\Civi::log(E::LONG_NAME)->error($e->getLogMessage());
|
||||||
$result['group_created']['is_error'] = TRUE;
|
$result['group']['created']['is_error'] = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,36 +151,36 @@ function civicrm_api3_mailinglistsync_Adgroupsync($params) {
|
||||||
\Civi::log(E::LONG_NAME)->info(
|
\Civi::log(E::LONG_NAME)->info(
|
||||||
"Updated group '{$adGroupMailingList->getId()}' title from '{$adGroupMailingList->getTitle()}' to '{$params['name']}'"
|
"Updated group '{$adGroupMailingList->getId()}' title from '{$adGroupMailingList->getTitle()}' to '{$params['name']}'"
|
||||||
);
|
);
|
||||||
$result['group_updated']['title'] = [
|
$result['group']['updated']['title'] = [
|
||||||
'is_error' => FALSE,
|
'is_error' => 0,
|
||||||
'old' => $adGroupMailingList->getTitle(),
|
'old' => $adGroupMailingList->getTitle(),
|
||||||
'new' => $params['name'],
|
'new' => $params['name'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
catch (MailinglistException $e) {
|
catch (MailinglistException $e) {
|
||||||
\Civi::log(E::LONG_NAME)->error($e->getLogMessage());
|
\Civi::log(E::LONG_NAME)->error($e->getLogMessage());
|
||||||
$result['group_updated']['title'] = [
|
$result['group']['updated']['title'] = [
|
||||||
'is_error' => TRUE,
|
'is_error' => 1,
|
||||||
'error' => $e->getLogMessage(),
|
'error' => $e->getLogMessage(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($adGroupMailingList->getGroupDescription() !== $params['description']) {
|
if ($adGroupMailingList->getGroupDescription() !== ($params['description'] ?? '')) {
|
||||||
try {
|
try {
|
||||||
$adGroupMailingList->updateGroupDescription($params['description']);
|
$adGroupMailingList->updateGroupDescription($params['description']);
|
||||||
\Civi::log(E::LONG_NAME)->info(
|
\Civi::log(E::LONG_NAME)->info(
|
||||||
"Updated group '{$adGroupMailingList->getId()}' description.'"
|
"Updated group '{$adGroupMailingList->getId()}' description.'"
|
||||||
);
|
);
|
||||||
$result['group_updated']['description'] = [
|
$result['group']['updated']['description'] = [
|
||||||
'is_error' => FALSE,
|
'is_error' => 0,
|
||||||
'old' => $adGroupMailingList->getGroupDescription(),
|
'old' => $adGroupMailingList->getGroupDescription(),
|
||||||
'new' => $params['description'],
|
'new' => $params['description'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
catch (MailinglistException $e) {
|
catch (MailinglistException $e) {
|
||||||
\Civi::log(E::LONG_NAME)->error($e->getLogMessage());
|
\Civi::log(E::LONG_NAME)->error($e->getLogMessage());
|
||||||
$result['group_updated']['description'] = [
|
$result['group']['updated']['description'] = [
|
||||||
'is_error' => TRUE,
|
'is_error' => 1,
|
||||||
'error' => $e->getLogMessage(),
|
'error' => $e->getLogMessage(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -144,37 +191,48 @@ function civicrm_api3_mailinglistsync_Adgroupsync($params) {
|
||||||
\Civi::log(E::LONG_NAME)->info(
|
\Civi::log(E::LONG_NAME)->info(
|
||||||
"Updated group '{$adGroupMailingList->getId()}' email address from '{$adGroupMailingList->getEmailAddress()}' to '{$params['email']}'"
|
"Updated group '{$adGroupMailingList->getId()}' email address from '{$adGroupMailingList->getEmailAddress()}' to '{$params['email']}'"
|
||||||
);
|
);
|
||||||
$result['group_updated']['email'] = [
|
$result['group']['updated']['email'] = [
|
||||||
'is_error' => FALSE,
|
'is_error' => 0,
|
||||||
'old' => $adGroupMailingList->getEmailAddress(),
|
'old' => $adGroupMailingList->getEmailAddress(),
|
||||||
'new' => $params['email'],
|
'new' => $params['email'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
catch (MailinglistException $e) {
|
catch (MailinglistException $e) {
|
||||||
\Civi::log(E::LONG_NAME)->error($e->getLogMessage());
|
\Civi::log(E::LONG_NAME)->error($e->getLogMessage());
|
||||||
$result['group_updated']['email'] = [
|
$result['group']['updated']['email'] = [
|
||||||
'is_error' => TRUE,
|
'is_error' => 1,
|
||||||
'error' => $e->getLogMessage(),
|
'error' => $e->getLogMessage(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$adGroupMailingList->save();
|
$adGroupMailingList->save();
|
||||||
|
|
||||||
if ($result['group_updated'] ?? FALSE) {
|
if ($result['group']['updated'] ?? FALSE) {
|
||||||
$result['group_updated']['error_count'] = count(array_filter($result['group_updated'], fn($v) => $v['is_error']));
|
$result['group']['updated']['error_count'] = count(array_filter($result['group']['updated'], fn($v) => $v['is_error']));
|
||||||
$result['group_updated']['is_error'] = $result['group_updated']['error_count'] > 0;
|
$result['group']['updated']['is_error'] = (int) ($result['group']['updated']['error_count'] > 0);
|
||||||
$result['group_updated']['group_id'] = $adGroupMailingList->getId();
|
|
||||||
}
|
}
|
||||||
|
$result['group']['group_id'] = $adGroupMailingList->getId();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (MailinglistException $me) {
|
||||||
|
\Civi::log(E::LONG_NAME)->error($me->getLogMessage());
|
||||||
|
return civicrm_api3_create_error($me->getLogMessage(),
|
||||||
|
[
|
||||||
|
'values' => $result,
|
||||||
|
'params' => $params,
|
||||||
|
'entity' => 'Mailinglistsync',
|
||||||
|
'action' => 'Adgroupsync',
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sync group mailing list members
|
// Sync group mailing list members
|
||||||
$result['recipients_updated'] = $adGroupMailingList->syncRecipients($params['recipients']);
|
$result['recipients'] = $adGroupMailingList->syncRecipients($params['recipients']);
|
||||||
|
|
||||||
// Return error response if any errors occurred
|
// Return error response if any errors occurred
|
||||||
$totalErrors = (int) ($result['group_created']['is_error'] ?? 0)
|
$totalErrors = (int) ($result['group']['created']['is_error'] ?? 0)
|
||||||
+ ($result['group_updated']['error_count'] ?? 0)
|
+ ($result['group']['updated']['error_count'] ?? 0)
|
||||||
+ ($result['recipients_updated']['error_count'] ?? 0);
|
+ ($result['recipients']['error_count'] ?? 0);
|
||||||
$result['is_error'] = $totalErrors > 0;
|
$result['is_error'] = (int) ($totalErrors > 0);
|
||||||
$result['error_count'] = $totalErrors;
|
$result['error_count'] = $totalErrors;
|
||||||
if ($totalErrors > 0) {
|
if ($totalErrors > 0) {
|
||||||
return civicrm_api3_create_error(
|
return civicrm_api3_create_error(
|
||||||
|
@ -185,15 +243,45 @@ function civicrm_api3_mailinglistsync_Adgroupsync($params) {
|
||||||
'entity' => 'Mailinglistsync',
|
'entity' => 'Mailinglistsync',
|
||||||
'action' => 'Adgroupsync',
|
'action' => 'Adgroupsync',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Else return success response
|
// Else return success response
|
||||||
return civicrm_api3_create_success($result, $params, 'Mailinglistsync', 'Adgroupsync');
|
return civicrm_api3_create_success($result, $params, 'Mailinglistsync', 'Adgroupsync');
|
||||||
}
|
}
|
||||||
catch (MailinglistException $me) {
|
|
||||||
\Civi::log(E::LONG_NAME)->error($me->getLogMessage());
|
// If only recipients are updated
|
||||||
return civicrm_api3_create_error($me->getLogMessage(),
|
elseif (
|
||||||
|
!empty($params['recipients']) &&
|
||||||
|
empty($params['sid']) &&
|
||||||
|
empty($params['email']) &&
|
||||||
|
empty($params['name']) &&
|
||||||
|
empty($params['description'])
|
||||||
|
) {
|
||||||
|
$result = [];
|
||||||
|
|
||||||
|
// Update recipients
|
||||||
|
$result['recipients'] = ADGroupMailingList::syncContacts($params['recipients']);
|
||||||
|
|
||||||
|
// Return error response if any errors occurred
|
||||||
|
$totalErrors = $result['recipients']['error_count'] ?? 0;
|
||||||
|
$result['count'] = $result['recipients']['count'] ?? 0;
|
||||||
|
$result['is_error'] = (int) ($totalErrors > 0);
|
||||||
|
$result['error_count'] = $totalErrors;
|
||||||
|
if ($totalErrors > 0) {
|
||||||
|
return civicrm_api3_create_error(
|
||||||
|
"Failed to sync recipients. $totalErrors errors occurred.",
|
||||||
|
[
|
||||||
|
'values' => $result,
|
||||||
|
'params' => $params,
|
||||||
|
'entity' => 'Mailinglistsync',
|
||||||
|
'action' => 'Adgroupsync',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
// Else return success response
|
||||||
|
return civicrm_api3_create_success($result ? $result['count'] : [], $params, 'Mailinglistsync', 'Adgroupsync');
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return civicrm_api3_create_error(
|
||||||
|
'Missing required parameters',
|
||||||
[
|
[
|
||||||
'params' => $params,
|
'params' => $params,
|
||||||
'entity' => 'Mailinglistsync',
|
'entity' => 'Mailinglistsync',
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use Civi\Mailinglistsync\MailingListSettings;
|
||||||
use Civi\Mailinglistsync\QueueHelper;
|
use Civi\Mailinglistsync\QueueHelper;
|
||||||
|
use Civi\Mailinglistsync\MailingListRecipient;
|
||||||
use CRM_Mailinglist_ExtensionUtil as E;
|
use CRM_Mailinglist_ExtensionUtil as E;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -11,7 +13,9 @@ use CRM_Mailinglist_ExtensionUtil as E;
|
||||||
*
|
*
|
||||||
* @see https://docs.civicrm.org/dev/en/latest/framework/api-architecture/
|
* @see https://docs.civicrm.org/dev/en/latest/framework/api-architecture/
|
||||||
*/
|
*/
|
||||||
function _civicrm_api3_mailinglistsync_Mlmmjsync_spec(&$spec) {}
|
function _civicrm_api3_mailinglistsync_Mlmmjsync_spec(&$spec) {
|
||||||
|
return $spec;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mailinglistsync.Mlmmjsync API
|
* Mailinglistsync.Mlmmjsync API
|
||||||
|
@ -21,11 +25,22 @@ function _civicrm_api3_mailinglistsync_Mlmmjsync_spec(&$spec) {}
|
||||||
* @return array
|
* @return array
|
||||||
* API result descriptor
|
* API result descriptor
|
||||||
*
|
*
|
||||||
|
* @throws CRM_Core_Exception
|
||||||
* @see civicrm_api3_create_success
|
* @see civicrm_api3_create_success
|
||||||
*
|
*
|
||||||
* @throws CRM_Core_Exception
|
|
||||||
*/
|
*/
|
||||||
function civicrm_api3_mailinglistsync_Mlmmjsync($params) {
|
function civicrm_api3_mailinglistsync_Mlmmjsync($params) {
|
||||||
|
try {
|
||||||
|
|
||||||
|
// Check if the mailing list sync is enabled
|
||||||
|
$enabled = (bool) MailingListSettings::get('enable');
|
||||||
|
if (!$enabled) {
|
||||||
|
return civicrm_api3_create_success(
|
||||||
|
['message' => 'Mailing list sync is disabled'], [], 'Mailinglist', 'Mlmmjsync'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$is_error = FALSE;
|
||||||
|
|
||||||
// Get queues
|
// Get queues
|
||||||
$qh = QueueHelper::getInstance();
|
$qh = QueueHelper::getInstance();
|
||||||
|
@ -52,21 +67,41 @@ function civicrm_api3_mailinglistsync_Mlmmjsync($params) {
|
||||||
|
|
||||||
// Run runners
|
// Run runners
|
||||||
$results = [];
|
$results = [];
|
||||||
|
foreach ([$groupRunner, $eventRunner, $emailRunner] as $runner) {
|
||||||
$continue = TRUE;
|
$continue = TRUE;
|
||||||
while($continue) {
|
$count = 0;
|
||||||
$result = $groupRunner->runNext(false);
|
$errors = [];
|
||||||
if (!$result['is_continue']) {
|
while ($continue) {
|
||||||
$continue = false;
|
$count++;
|
||||||
|
|
||||||
|
$result = $runner->runNext(FALSE);
|
||||||
|
$continue = $result['is_continue'];
|
||||||
|
|
||||||
|
if ($result['is_error']) {
|
||||||
|
$error = $result['exception']->getMessage();
|
||||||
|
// If the error is 'Failed to claim next task' we should stop the runner
|
||||||
|
if ($error === 'Failed to claim next task') {
|
||||||
|
$continue = FALSE;
|
||||||
}
|
}
|
||||||
$results['runners'][] = $result;
|
else {
|
||||||
|
$errors[] = $error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$results['runners'][$runner->title][] = [
|
||||||
|
'task_count' => $count,
|
||||||
|
'errors' => $errors,
|
||||||
|
];
|
||||||
|
|
||||||
|
$is_error = !empty($errors) || $is_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
$groups = $qh->getGroups();
|
$groups = $qh->getGroups();
|
||||||
$events = $qh->getEvents();
|
$events = $qh->getEvents();
|
||||||
$emails = $qh->getEmails();
|
$emails = $qh->getEmails();
|
||||||
|
|
||||||
// TODO: Sync groups and events just once and invoke syncing
|
// Sync groups and events just once and invoke syncing
|
||||||
|
|
||||||
$mailingListsToSync = [];
|
$mailingListsToSync = [];
|
||||||
foreach ($groups as $group) {
|
foreach ($groups as $group) {
|
||||||
$mailingListsToSync[$group->getId()] = $group;
|
$mailingListsToSync[$group->getId()] = $group;
|
||||||
|
@ -75,8 +110,9 @@ function civicrm_api3_mailinglistsync_Mlmmjsync($params) {
|
||||||
$mailingListsToSync[$event->getId()] = $event;
|
$mailingListsToSync[$event->getId()] = $event;
|
||||||
}
|
}
|
||||||
foreach ($emails as $email) {
|
foreach ($emails as $email) {
|
||||||
$emailGroups = $email->getGroups();
|
$recipient = MailingListRecipient::getContactIdEmail($email);
|
||||||
$emailEvents = $email->getEvents();
|
$emailGroups = $recipient->getMailingLists();
|
||||||
|
$emailEvents = $recipient->getEventMailingLists();
|
||||||
foreach ($emailGroups as $group) {
|
foreach ($emailGroups as $group) {
|
||||||
$mailingListsToSync[$group->getId()] = $group;
|
$mailingListsToSync[$group->getId()] = $group;
|
||||||
}
|
}
|
||||||
|
@ -86,12 +122,24 @@ function civicrm_api3_mailinglistsync_Mlmmjsync($params) {
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($mailingListsToSync as $mailingList) {
|
foreach ($mailingListsToSync as $mailingList) {
|
||||||
$results['mailing_lists'][] = $mailingList->sync();
|
$results['mailing_lists'][$mailingList->getEmailAddress()] = $mailingList->sync(); // TODO: re-add failed task to queue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($is_error) {
|
||||||
|
return civicrm_api3_create_error('One or more errors occurred during the sync process', [
|
||||||
|
'params' => $params,
|
||||||
|
'results' => $results,
|
||||||
|
'entity' => 'Mailinglistsync',
|
||||||
|
'action' => 'Mlmmjsync',
|
||||||
|
]);
|
||||||
|
}
|
||||||
return civicrm_api3_create_success($results, [], 'Mailinglist', 'Mlmmjsync');
|
return civicrm_api3_create_success($results, [], 'Mailinglist', 'Mlmmjsync');
|
||||||
|
}
|
||||||
/*
|
catch (Exception $e) {
|
||||||
throw new CRM_Core_Exception('Everyone knows that the magicword is "sesame"', 'magicword_incorrect');
|
return civicrm_api3_create_error($e->getMessage(), [
|
||||||
*/
|
'params' => $params,
|
||||||
|
'entity' => 'Mailinglistsync',
|
||||||
|
'action' => 'Mlmmjsync',
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
2
info.xml
2
info.xml
|
@ -19,7 +19,7 @@
|
||||||
</urls>
|
</urls>
|
||||||
<releaseDate>2025-03-04</releaseDate>
|
<releaseDate>2025-03-04</releaseDate>
|
||||||
<version>1.0.0</version>
|
<version>1.0.0</version>
|
||||||
<develStage>alpha</develStage>
|
<develStage>beta</develStage>
|
||||||
<compatibility>
|
<compatibility>
|
||||||
<ver>5.80</ver>
|
<ver>5.80</ver>
|
||||||
</compatibility>
|
</compatibility>
|
||||||
|
|
|
@ -155,7 +155,7 @@ function _mailinglistsync_civix_insert_navigation_menu(&$menu, $path, $item) {
|
||||||
$path = explode('/', $path);
|
$path = explode('/', $path);
|
||||||
$first = array_shift($path);
|
$first = array_shift($path);
|
||||||
foreach ($menu as $key => &$entry) {
|
foreach ($menu as $key => &$entry) {
|
||||||
if ($entry['attributes']['name'] == $first) {
|
if ($entry['attributes']['name'] === $first) {
|
||||||
if (!isset($entry['child'])) {
|
if (!isset($entry['child'])) {
|
||||||
$entry['child'] = [];
|
$entry['child'] = [];
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,6 @@ use Civi\Mailinglistsync\Exceptions\MailinglistException;
|
||||||
use CRM_Mailinglistsync_ExtensionUtil as E;
|
use CRM_Mailinglistsync_ExtensionUtil as E;
|
||||||
use Civi\Mailinglistsync\EventMailingList;
|
use Civi\Mailinglistsync\EventMailingList;
|
||||||
use Civi\Mailinglistsync\GroupMailingList;
|
use Civi\Mailinglistsync\GroupMailingList;
|
||||||
use Civi\Mailinglistsync\ADGroupMailingList;
|
|
||||||
use function Civi\Mailinglistsync\getLocationTypes;
|
use function Civi\Mailinglistsync\getLocationTypes;
|
||||||
|
|
||||||
require_once 'Civi/Mailinglistsync/Utils.php';
|
require_once 'Civi/Mailinglistsync/Utils.php';
|
||||||
|
@ -49,27 +48,152 @@ function mailinglistsync_civicrm_enable(): void {
|
||||||
*/
|
*/
|
||||||
function mailinglistsync_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors): void {
|
function mailinglistsync_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors): void {
|
||||||
// Validate custom fields in group and event forms.
|
// Validate custom fields in group and event forms.
|
||||||
if ($formName == 'CRM_Group_Form_Edit') {
|
if ($formName === 'CRM_Group_Form_Edit') {
|
||||||
|
if ($_REQUEST['action'] === 'delete' || $_REQUEST['action'] === 'update') {
|
||||||
|
$mailingList = new GroupMailingList(intval($_REQUEST['id']));
|
||||||
|
}
|
||||||
|
else {
|
||||||
$mailingList = new GroupMailingList();
|
$mailingList = new GroupMailingList();
|
||||||
}
|
}
|
||||||
elseif ($formName == 'CRM_Event_Form_ManageEvent_EventInfo') {
|
}
|
||||||
|
elseif ($formName === 'CRM_Event_Form_ManageEvent_EventInfo') {
|
||||||
|
if ($_REQUEST['action'] === 'delete' || $_REQUEST['action'] === 'update') {
|
||||||
|
$mailingList = new EventMailingList(intval($_REQUEST['id']));
|
||||||
|
}
|
||||||
|
else {
|
||||||
$mailingList = new EventMailingList();
|
$mailingList = new EventMailingList();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (!empty($mailingList)) {
|
if (!empty($mailingList)) {
|
||||||
|
// We cannot delete the corresponding mlmmj mailing list in the post hook
|
||||||
|
// so we have to deal with it here
|
||||||
|
if ($_REQUEST['action'] === 'delete' && $mailingList->isEnabled()) {
|
||||||
|
try {
|
||||||
|
$mailingList->delete();
|
||||||
|
}
|
||||||
|
catch (MailinglistException $e) {
|
||||||
|
$errorCode = $e->getCode();
|
||||||
|
|
||||||
|
if ($errorCode !== MailinglistException::ERROR_CODE_DELETE_EMAIL_ADDRESS_FAILED) {
|
||||||
|
\Civi::log(E::LONG_NAME)->error($e->getMessage());
|
||||||
|
}
|
||||||
|
CRM_Core_Session::setStatus(
|
||||||
|
E::ts('Failed to delete mlmmj mailing list: %1', [1 => $e->getMessage()]),
|
||||||
|
E::ts('Deletion failed'),
|
||||||
|
'alert',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle group and event mailing list updates which affect the mlmmj mailing list
|
||||||
|
if ($_REQUEST['action'] === 'update' && $mailingList->isEnabled()) {
|
||||||
|
$customFields = $mailingList::translateCustomFields($fields);
|
||||||
|
|
||||||
|
// If enabled changes from TRUE to FALSE, delete the mlmmj mailing list
|
||||||
|
if ($customFields['Enable_mailing_list']['value'] === "0") {
|
||||||
|
try {
|
||||||
|
$mailingList->delete();
|
||||||
|
}
|
||||||
|
catch (MailinglistException $e) {
|
||||||
|
$errorCode = $e->getCode();
|
||||||
|
if ($errorCode !== MailinglistException::ERROR_CODE_DELETE_EMAIL_ADDRESS_FAILED) {
|
||||||
|
\Civi::log(E::LONG_NAME)->error($e->getMessage());
|
||||||
|
}
|
||||||
|
CRM_Core_Session::setStatus(
|
||||||
|
E::ts('Failed to delete mlmmj mailing list: %1', [1 => $e->getMessage()]),
|
||||||
|
E::ts('Deletion failed'),
|
||||||
|
'alert',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If email address has changed, delete and mark group for creation
|
||||||
|
elseif ($customFields['E_mail_address']['value'] !== $mailingList->getEmailAddress()) {
|
||||||
|
try {
|
||||||
|
$mailingList->delete();
|
||||||
|
}
|
||||||
|
catch (MailinglistException $e) {
|
||||||
|
$errorCode = $e->getCode();
|
||||||
|
if ($errorCode !== MailinglistException::ERROR_CODE_DELETE_EMAIL_ADDRESS_FAILED) {
|
||||||
|
\Civi::log(E::LONG_NAME)->error($e->getMessage());
|
||||||
|
}
|
||||||
|
CRM_Core_Session::setStatus(
|
||||||
|
E::ts('Failed to delete mlmmj mailing list: %1', [1 => $e->getMessage()]),
|
||||||
|
E::ts('Deletion failed'),
|
||||||
|
'alert',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
// Queue mailinglist for synchronization with mlmmj
|
||||||
|
if ($mailingList::class === GroupMailingList::class) {
|
||||||
|
$queue = \Civi::queue('propeace-mailinglist-group-queue', [
|
||||||
|
'type' => 'SqlParallel',
|
||||||
|
'is_autorun' => FALSE,
|
||||||
|
'reset' => FALSE,
|
||||||
|
'error' => 'drop',
|
||||||
|
]);
|
||||||
|
$queue->createItem(new CRM_Queue_Task(
|
||||||
|
// callback
|
||||||
|
['Civi\Mailinglistsync\QueueHelper', 'storeInstance'],
|
||||||
|
// arguments
|
||||||
|
[$mailingList->getId(), $mailingList::class],
|
||||||
|
// title
|
||||||
|
"Sync Group ID '{$mailingList->getId()}'"
|
||||||
|
));
|
||||||
|
}
|
||||||
|
elseif ($mailingList::class === EventMailingList::class) {
|
||||||
|
$queue = \Civi::queue('propeace-mailinglist-event-queue', [
|
||||||
|
'type' => 'SqlParallel',
|
||||||
|
'is_autorun' => FALSE,
|
||||||
|
'reset' => FALSE,
|
||||||
|
'error' => 'drop',
|
||||||
|
]);
|
||||||
|
$queue->createItem(new CRM_Queue_Task(
|
||||||
|
// callback
|
||||||
|
['Civi\Mailinglistsync\QueueHelper', 'storeInstance'],
|
||||||
|
// arguments
|
||||||
|
[$mailingList->getId(), $mailingList::class],
|
||||||
|
// title
|
||||||
|
"Sync Event ID '{$mailingList->getId()}'"
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate custom fields
|
||||||
$mailingList::validateCustomFields($fields, $form, $errors);
|
$mailingList::validateCustomFields($fields, $form, $errors);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check permission to alter group membership via form
|
// Check permission to alter group membership via form
|
||||||
if ($formName == 'CRM_Contact_Form_GroupContact' || $formName == 'CRM_Contact_Form_Task_AddToGroup') {
|
if ($formName === 'CRM_Contact_Form_GroupContact' || $formName === 'CRM_Contact_Form_Task_AddToGroup') {
|
||||||
$mailingList = new GroupMailingList($fields['group_id']);
|
$mailingList = new GroupMailingList($fields['group_id']);
|
||||||
_check_group_membership_permissions($mailingList, $errors);
|
_mailinglistsync_check_group_membership_permissions($mailingList, $errors);
|
||||||
}
|
}
|
||||||
elseif ($formName == 'CRM_Event_Form_Participant') {
|
elseif ($formName === 'CRM_Event_Form_Participant') {
|
||||||
$mailingList = new EventMailingList($fields['event_id']);
|
$mailingList = new EventMailingList($fields['event_id']);
|
||||||
_mailinglistsync_check_event_membership_permissions($mailingList, $errors);
|
_mailinglistsync_check_event_membership_permissions($mailingList, $errors);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
function mailinglistsync_civicrm_pre($op, $objectName, $objectId, &$params) {
|
||||||
|
if ($op === 'delete' || $op === 'edit') {
|
||||||
|
|
||||||
|
if ($objectName === 'Group' || $objectName === 'Event') {
|
||||||
|
$mailingList = $objectName === 'Group'
|
||||||
|
? new GroupMailingList($objectId)
|
||||||
|
: new EventMailingList($objectId);
|
||||||
|
if ($mailingList->isEnabled()) {
|
||||||
|
|
||||||
|
// If email has changed, delete the mailing list and create a new one
|
||||||
|
if ($mailingList->getEmailAddress() !== $params['email']) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements hook_civicrm_post() to check on permissions to alter mailing list
|
* Implements hook_civicrm_post() to check on permissions to alter mailing list
|
||||||
* groups and sync group with mlmmj.
|
* groups and sync group with mlmmj.
|
||||||
|
@ -79,14 +203,14 @@ function mailinglistsync_civicrm_validateForm($formName, &$fields, &$files, &$fo
|
||||||
* @throws \CRM_Core_Exception
|
* @throws \CRM_Core_Exception
|
||||||
*/
|
*/
|
||||||
function mailinglistsync_civicrm_post(string $op, string $objectName, int $objectId, &$objectRef) {
|
function mailinglistsync_civicrm_post(string $op, string $objectName, int $objectId, &$objectRef) {
|
||||||
if ($op == 'delete' || $op == 'edit' || $op == 'create') {
|
if ($op === 'delete' || $op === 'edit' || $op === 'create') {
|
||||||
// Check on groups mailing list
|
// Check on groups mailing list
|
||||||
// Note: I wonder why $objectId refers to the group instead of the group contact here.
|
// Note: I wonder why $objectId refers to the group instead of the group contact here.
|
||||||
if ($objectName == 'GroupContact') {
|
if ($objectName === 'GroupContact') {
|
||||||
$mailingList = new GroupMailingList($objectId);
|
$mailingList = new GroupMailingList($objectRef->group_id ?? $objectId);
|
||||||
|
|
||||||
// Check permission to alter group membership
|
// Check permission to alter group membership
|
||||||
if ($mailingList->isMailingList() && !CRM_Core_Permission::check('manage_group_mailinglists')) {
|
if ($mailingList->isEnabled() && !CRM_Core_Permission::check('manage_group_mailinglists')) {
|
||||||
CRM_Core_Session::setStatus(
|
CRM_Core_Session::setStatus(
|
||||||
E::ts('You do not have permission to manage memberships of mailing list groups.'),
|
E::ts('You do not have permission to manage memberships of mailing list groups.'),
|
||||||
E::ts('Permission Denied'),
|
E::ts('Permission Denied'),
|
||||||
|
@ -113,7 +237,7 @@ function mailinglistsync_civicrm_post(string $op, string $objectName, int $objec
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check on event mailing lists
|
// Check on event mailing lists
|
||||||
elseif ($objectName == 'Participant') {
|
elseif ($objectName === 'Participant') {
|
||||||
$eventId = $objectRef->event_id ?? Event::get()
|
$eventId = $objectRef->event_id ?? Event::get()
|
||||||
->addSelect('event_id')
|
->addSelect('event_id')
|
||||||
->addWhere('id', '=', $objectId)
|
->addWhere('id', '=', $objectId)
|
||||||
|
@ -122,7 +246,7 @@ function mailinglistsync_civicrm_post(string $op, string $objectName, int $objec
|
||||||
$mailingList = new GroupMailingList($eventId);
|
$mailingList = new GroupMailingList($eventId);
|
||||||
|
|
||||||
// Check permission to alter event mailing list
|
// Check permission to alter event mailing list
|
||||||
if ($mailingList->isMailingList() && !CRM_Core_Permission::check('manage_event_mailinglists')) {
|
if ($mailingList->isEnabled() && !CRM_Core_Permission::check('manage_event_mailinglists')) {
|
||||||
CRM_Core_Session::setStatus(
|
CRM_Core_Session::setStatus(
|
||||||
E::ts('You do not have permission to manage event mailing lists.'),
|
E::ts('You do not have permission to manage event mailing lists.'),
|
||||||
E::ts('Permission Denied'),
|
E::ts('Permission Denied'),
|
||||||
|
@ -135,18 +259,8 @@ function mailinglistsync_civicrm_post(string $op, string $objectName, int $objec
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete mlmmj mailing list
|
|
||||||
if (
|
|
||||||
$op == 'delete' &&
|
|
||||||
!empty($mailingList) &&
|
|
||||||
($mailingList->isMailingList() || $mailingList->isADGroup())
|
|
||||||
) {
|
|
||||||
$mailingList->deleteMailingList(); // TODO
|
|
||||||
}
|
|
||||||
|
|
||||||
// If this is an e-mail address of a location type used for mailing lists
|
// If this is an e-mail address of a location type used for mailing lists
|
||||||
if ($objectName == 'Email' && in_array((int) $objectRef->location_type_id, array_keys(getLocationTypes()))) {
|
if ($objectName === 'Email' && in_array((int) $objectRef->location_type_id, array_keys(getLocationTypes()))) {
|
||||||
|
|
||||||
// Ensure that only one e-mail address of this location type is set for this contact
|
// Ensure that only one e-mail address of this location type is set for this contact
|
||||||
$result = Email::get()
|
$result = Email::get()
|
||||||
->addSelect('contact_id', 'contact.display_name')
|
->addSelect('contact_id', 'contact.display_name')
|
||||||
|
@ -160,7 +274,7 @@ function mailinglistsync_civicrm_post(string $op, string $objectName, int $objec
|
||||||
|
|
||||||
if (!empty($result) && $op != 'delete') {
|
if (!empty($result) && $op != 'delete') {
|
||||||
throw new MailinglistException(
|
throw new MailinglistException(
|
||||||
E::ts("An e-mail address of a mailing list type is already used for this contact"),
|
"An e-mail address of a mailing list type is already used for this contact",
|
||||||
MailinglistException::ERROR_CODE_UPDATE_EMAIL_ADDRESS_FAILED
|
MailinglistException::ERROR_CODE_UPDATE_EMAIL_ADDRESS_FAILED
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -168,7 +282,11 @@ function mailinglistsync_civicrm_post(string $op, string $objectName, int $objec
|
||||||
// Ensure that this email address is only used for one contact
|
// Ensure that this email address is only used for one contact
|
||||||
$results = Email::get()
|
$results = Email::get()
|
||||||
->addSelect('contact.id', 'contact.display_name')
|
->addSelect('contact.id', 'contact.display_name')
|
||||||
->addJoin('Contact AS contact', 'LEFT', ['contact_id', '=', 'contact.id'])
|
->addJoin('Contact AS contact', 'LEFT', [
|
||||||
|
'contact_id',
|
||||||
|
'=',
|
||||||
|
'contact.id',
|
||||||
|
])
|
||||||
->addWhere('contact_id', '!=', $objectRef->contact_id)
|
->addWhere('contact_id', '!=', $objectRef->contact_id)
|
||||||
->addWhere('email', '=', $objectRef->email)
|
->addWhere('email', '=', $objectRef->email)
|
||||||
->addWhere('location_type_id', '=', $objectRef->location_type_id)
|
->addWhere('location_type_id', '=', $objectRef->location_type_id)
|
||||||
|
@ -176,7 +294,6 @@ function mailinglistsync_civicrm_post(string $op, string $objectName, int $objec
|
||||||
->getArrayCopy();
|
->getArrayCopy();
|
||||||
|
|
||||||
if (!empty($results) && $op != 'delete') {
|
if (!empty($results) && $op != 'delete') {
|
||||||
|
|
||||||
// Delete e-mail address if it is not allowed
|
// Delete e-mail address if it is not allowed
|
||||||
// TODO: This is a workaround. We should prevent the creation of the e-mail address in the first place.
|
// TODO: This is a workaround. We should prevent the creation of the e-mail address in the first place.
|
||||||
_mailinglistsync_delete_email_address((int) $objectRef->id);
|
_mailinglistsync_delete_email_address((int) $objectRef->id);
|
||||||
|
@ -219,9 +336,9 @@ function mailinglistsync_civicrm_post(string $op, string $objectName, int $objec
|
||||||
function mailinglistsync_civicrm_postCommit(string $op, string $objectName, int $objectId, &$objectRef) {
|
function mailinglistsync_civicrm_postCommit(string $op, string $objectName, int $objectId, &$objectRef) {
|
||||||
// Sync groups mailing list
|
// Sync groups mailing list
|
||||||
// Note: I wonder why $objectId refers to the group instead of the group contact here.
|
// Note: I wonder why $objectId refers to the group instead of the group contact here.
|
||||||
if ($objectName == 'GroupContact') {
|
if ($objectName === 'GroupContact') {
|
||||||
$mailingList = new GroupMailingList($objectId);
|
$mailingList = new GroupMailingList($objectRef->group_id ?? $objectId);
|
||||||
if ($mailingList->isMailingList() || $mailingList->isADGroup()) {
|
if ($mailingList->isEnabled()) {
|
||||||
// Queue group for synchronization with mlmmj
|
// Queue group for synchronization with mlmmj
|
||||||
$queue = \Civi::queue('propeace-mailinglist-group-queue', [
|
$queue = \Civi::queue('propeace-mailinglist-group-queue', [
|
||||||
'type' => 'SqlParallel',
|
'type' => 'SqlParallel',
|
||||||
|
@ -235,20 +352,41 @@ function mailinglistsync_civicrm_postCommit(string $op, string $objectName, int
|
||||||
// arguments
|
// arguments
|
||||||
[$mailingList->getId(), $mailingList::class],
|
[$mailingList->getId(), $mailingList::class],
|
||||||
// title
|
// title
|
||||||
"Sync Group ID '$objectId'"
|
"Sync Group ID '{$mailingList->getId()}'"
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
elseif ($objectName === 'Group') {
|
||||||
|
$mailingList = new GroupMailingList($objectId);
|
||||||
|
if ($mailingList->isEnabled()) {
|
||||||
|
// Queue group for synchronization with mlmmj
|
||||||
|
$queue = \Civi::queue('propeace-mailinglist-group-queue', [
|
||||||
|
'type' => 'SqlParallel',
|
||||||
|
'is_autorun' => FALSE,
|
||||||
|
'reset' => FALSE,
|
||||||
|
'error' => 'drop',
|
||||||
|
]);
|
||||||
|
$queue->createItem(new CRM_Queue_Task(
|
||||||
|
// callback
|
||||||
|
['Civi\Mailinglistsync\QueueHelper', 'storeInstance'],
|
||||||
|
// arguments
|
||||||
|
[$mailingList->getId(), $mailingList::class],
|
||||||
|
// title
|
||||||
|
"Sync Group ID '{$mailingList->getId()}'"
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sync event mailing lists
|
// Sync event mailing lists
|
||||||
elseif ($objectName == 'Participant') {
|
elseif ($objectName === 'Participant') {
|
||||||
$eventId = $objectRef->event_id ?? Participant::get()
|
$eventId = $objectRef->event_id ?? Participant::get()
|
||||||
->addSelect('event_id')
|
->addSelect('event_id')
|
||||||
->addWhere('id', '=', $objectId)
|
->addWhere('id', '=', $objectId)
|
||||||
->execute()
|
->execute()
|
||||||
->first()['event_id'];
|
->first()['event_id'];
|
||||||
$mailingList = new EventMailingList($eventId);
|
$mailingList = new EventMailingList($eventId);
|
||||||
if ($mailingList->isMailingList()) {
|
if ($mailingList->isEnabled()) {
|
||||||
// Queue event for synchronization with mlmmj
|
// Queue event for synchronization with mlmmj
|
||||||
$queue = \Civi::queue('propeace-mailinglist-event-queue', [
|
$queue = \Civi::queue('propeace-mailinglist-event-queue', [
|
||||||
'type' => 'SqlParallel',
|
'type' => 'SqlParallel',
|
||||||
|
@ -262,28 +400,70 @@ function mailinglistsync_civicrm_postCommit(string $op, string $objectName, int
|
||||||
// arguments
|
// arguments
|
||||||
[$mailingList->getId(), $mailingList::class],
|
[$mailingList->getId(), $mailingList::class],
|
||||||
// title
|
// title
|
||||||
"Sync Event ID '$objectId'"
|
"Sync Event ID '{$mailingList->getId()}'"
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
elseif ($objectName === 'Event') {
|
||||||
|
$mailingList = new EventMailingList($objectId);
|
||||||
|
if ($mailingList->isEnabled()) {
|
||||||
|
// Queue event for synchronization with mlmmj
|
||||||
|
$queue = \Civi::queue('propeace-mailinglist-event-queue', [
|
||||||
|
'type' => 'SqlParallel',
|
||||||
|
'is_autorun' => FALSE,
|
||||||
|
'reset' => FALSE,
|
||||||
|
'error' => 'drop',
|
||||||
|
]);
|
||||||
|
$queue->createItem(new CRM_Queue_Task(
|
||||||
|
// callback
|
||||||
|
['Civi\Mailinglistsync\QueueHelper', 'storeInstance'],
|
||||||
|
// arguments
|
||||||
|
[$mailingList->getId(), $mailingList::class],
|
||||||
|
// title
|
||||||
|
"Sync Event ID '{$mailingList->getId()}'"
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($objectName === 'GroupContact') {
|
||||||
|
$mailingList = new GroupMailingList($objectRef->group_id ?? $objectId);
|
||||||
|
if ($mailingList->isEnabled()) {
|
||||||
|
// Queue group for synchronization with mlmmj
|
||||||
|
$queue = \Civi::queue('propeace-mailinglist-group-queue', [
|
||||||
|
'type' => 'SqlParallel',
|
||||||
|
'is_autorun' => FALSE,
|
||||||
|
'reset' => FALSE,
|
||||||
|
'error' => 'drop',
|
||||||
|
]);
|
||||||
|
$queue->createItem(new CRM_Queue_Task(
|
||||||
|
// callback
|
||||||
|
['Civi\Mailinglistsync\QueueHelper', 'storeInstance'],
|
||||||
|
// arguments
|
||||||
|
[$mailingList->getId(), $mailingList::class],
|
||||||
|
// title
|
||||||
|
"Sync Group ID '{$mailingList->getId()}'"
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sync e-mail addresses
|
// Sync e-mail addresses
|
||||||
elseif ($objectName == 'Email' && in_array((int) $objectRef->location_type_id, array_keys(getLocationTypes()))) {
|
elseif ($objectName === 'Email') {
|
||||||
|
// If an email address update comes from an api call, the
|
||||||
// Only sync e-mail addresses of mailing list types
|
// objectRef->location_type_id is not set. So we have to get it from the
|
||||||
$locationType = \Civi\Api4\LocationType::get()
|
// database.
|
||||||
->addSelect('name')
|
if (empty($objectRef->location_type_id)) {
|
||||||
->addWhere('id', '=', $objectRef->location_type_id)
|
$locationTypeId = \Civi\Api4\Email::get()
|
||||||
|
->addSelect('location_type_id')
|
||||||
|
->addWhere('id', '=', $objectRef->id)
|
||||||
|
->addWhere('location_type_id', 'IN', array_keys(getLocationTypes()))
|
||||||
->execute()
|
->execute()
|
||||||
->first()['name'];
|
->first()['location_type_id'];
|
||||||
if (!in_array($locationType, [
|
|
||||||
GroupMailingList::LOCATION_TYPE,
|
|
||||||
ADGroupMailingList::LOCATION_TYPE,
|
|
||||||
EventMailingList::LOCATION_TYPE,
|
|
||||||
])) {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
if (
|
||||||
|
!empty($locationTypeId) ||
|
||||||
|
in_array((int) $objectRef->location_type_id, array_keys(getLocationTypes()))
|
||||||
|
) {
|
||||||
// Queue email address for synchronization with mlmmj
|
// Queue email address for synchronization with mlmmj
|
||||||
$queue = \Civi::queue('propeace-mailinglist-email-queue', [
|
$queue = \Civi::queue('propeace-mailinglist-email-queue', [
|
||||||
'type' => 'SqlParallel',
|
'type' => 'SqlParallel',
|
||||||
|
@ -293,13 +473,14 @@ function mailinglistsync_civicrm_postCommit(string $op, string $objectName, int
|
||||||
]);
|
]);
|
||||||
$queue->createItem(new CRM_Queue_Task(
|
$queue->createItem(new CRM_Queue_Task(
|
||||||
// callback
|
// callback
|
||||||
['Civi\Mailinglistsync\QueueHelper', 'addToEmails'],
|
['Civi\Mailinglistsync\QueueHelper', 'storeEmail'],
|
||||||
// arguments
|
// arguments
|
||||||
[$objectRef->email],
|
[$objectRef->email],
|
||||||
// title
|
// title
|
||||||
"Sync E-Mail '$objectRef->email'"
|
"Sync E-Mail '$objectRef->email'"
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -330,6 +511,23 @@ function mailinglistsync_civicrm_permission(&$permissions) {
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check permissions to alter group membership.
|
||||||
|
*
|
||||||
|
* @param \Civi\Mailinglistsync\GroupMailingList $mailingList
|
||||||
|
* @param $errors
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function _mailinglistsync_check_group_membership_permissions(GroupMailingList $mailingList, &$errors): void {
|
||||||
|
if ($mailingList->isEnabled() && !CRM_Core_Permission::check('manage_group_mailinglists')) {
|
||||||
|
$errors['group_id'] = E::ts('You do not have permission to manage group membership.');
|
||||||
|
}
|
||||||
|
if ($mailingList->isADGroup() && !CRM_Core_Permission::check('manage_ad_mailinglists')) {
|
||||||
|
$errors['group_id'] = E::ts('You do not have permission to manage AD group membership.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check permissions to alter event membership.
|
* Check permissions to alter event membership.
|
||||||
*
|
*
|
||||||
|
@ -339,7 +537,7 @@ function mailinglistsync_civicrm_permission(&$permissions) {
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
function _mailinglistsync_check_event_membership_permissions(EventMailingList $mailingList, &$errors): void {
|
function _mailinglistsync_check_event_membership_permissions(EventMailingList $mailingList, &$errors): void {
|
||||||
if ($mailingList->isMailingList() && !CRM_Core_Permission::check('manage_event_mailinglists')) {
|
if ($mailingList->isEnabled() && !CRM_Core_Permission::check('manage_event_mailinglists')) {
|
||||||
$errors['event_id'] = E::ts('You do not have permission to manage event membership.');
|
$errors['event_id'] = E::ts('You do not have permission to manage event membership.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -361,7 +559,7 @@ function _mailinglistsync_delete_email_address(int $emailId): void {
|
||||||
}
|
}
|
||||||
catch (\Exception $e) {
|
catch (\Exception $e) {
|
||||||
throw new MailinglistException(
|
throw new MailinglistException(
|
||||||
E::ts('Failed to delete e-mail address'),
|
"Failed to delete e-mail address: {$e}",
|
||||||
MailinglistException::ERROR_CODE_DELETE_EMAIL_ADDRESS_FAILED
|
MailinglistException::ERROR_CODE_DELETE_EMAIL_ADDRESS_FAILED
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,10 +39,11 @@ return [
|
||||||
'is_searchable' => TRUE,
|
'is_searchable' => TRUE,
|
||||||
'help_post' => E::ts('The SID of this contact in the Active Directory.'),
|
'help_post' => E::ts('The SID of this contact in the Active Directory.'),
|
||||||
'is_view' => TRUE,
|
'is_view' => TRUE,
|
||||||
'text_length' => 36,
|
'text_length' => 50,
|
||||||
'note_columns' => 60,
|
'note_columns' => 60,
|
||||||
'note_rows' => 4,
|
'note_rows' => 4,
|
||||||
'column_name' => 'sid_42',
|
'column_name' => 'sid_42',
|
||||||
|
'is_active' => TRUE,
|
||||||
],
|
],
|
||||||
'match' => [
|
'match' => [
|
||||||
'name',
|
'name',
|
||||||
|
|
|
@ -44,6 +44,7 @@ return [
|
||||||
'note_columns' => 60,
|
'note_columns' => 60,
|
||||||
'note_rows' => 4,
|
'note_rows' => 4,
|
||||||
'column_name' => 'enable_mailing_list_30',
|
'column_name' => 'enable_mailing_list_30',
|
||||||
|
'is_active' => TRUE,
|
||||||
],
|
],
|
||||||
'match' => [
|
'match' => [
|
||||||
'name',
|
'name',
|
||||||
|
@ -68,6 +69,7 @@ return [
|
||||||
'note_columns' => 60,
|
'note_columns' => 60,
|
||||||
'note_rows' => 4,
|
'note_rows' => 4,
|
||||||
'column_name' => 'e_mail_address_10',
|
'column_name' => 'e_mail_address_10',
|
||||||
|
'is_active' => TRUE,
|
||||||
],
|
],
|
||||||
'match' => [
|
'match' => [
|
||||||
'name',
|
'name',
|
||||||
|
@ -92,6 +94,7 @@ return [
|
||||||
'note_columns' => 60,
|
'note_columns' => 60,
|
||||||
'note_rows' => 4,
|
'note_rows' => 4,
|
||||||
'column_name' => 'subject_prefix_11',
|
'column_name' => 'subject_prefix_11',
|
||||||
|
'is_active' => TRUE,
|
||||||
],
|
],
|
||||||
'match' => [
|
'match' => [
|
||||||
'name',
|
'name',
|
||||||
|
@ -118,6 +121,7 @@ return [
|
||||||
'note_columns' => 60,
|
'note_columns' => 60,
|
||||||
'note_rows' => 4,
|
'note_rows' => 4,
|
||||||
'column_name' => 'can_be_reached_externally_12',
|
'column_name' => 'can_be_reached_externally_12',
|
||||||
|
'is_active' => TRUE,
|
||||||
],
|
],
|
||||||
'match' => [
|
'match' => [
|
||||||
'name',
|
'name',
|
||||||
|
|
|
@ -44,6 +44,7 @@ return [
|
||||||
'note_columns' => 60,
|
'note_columns' => 60,
|
||||||
'note_rows' => 4,
|
'note_rows' => 4,
|
||||||
'column_name' => 'enable_mailing_list_31',
|
'column_name' => 'enable_mailing_list_31',
|
||||||
|
'is_active' => TRUE,
|
||||||
],
|
],
|
||||||
'match' => [
|
'match' => [
|
||||||
'name',
|
'name',
|
||||||
|
@ -68,6 +69,7 @@ return [
|
||||||
'note_columns' => 60,
|
'note_columns' => 60,
|
||||||
'note_rows' => 4,
|
'note_rows' => 4,
|
||||||
'column_name' => 'e_mail_address_10',
|
'column_name' => 'e_mail_address_10',
|
||||||
|
'is_active' => TRUE,
|
||||||
],
|
],
|
||||||
'match' => [
|
'match' => [
|
||||||
'name',
|
'name',
|
||||||
|
@ -89,10 +91,11 @@ return [
|
||||||
'html_type' => 'Text',
|
'html_type' => 'Text',
|
||||||
'help_post' => E::ts('The SID of the group in the Active Directory.'),
|
'help_post' => E::ts('The SID of the group in the Active Directory.'),
|
||||||
'is_view' => TRUE,
|
'is_view' => TRUE,
|
||||||
'text_length' => 36,
|
'text_length' => 50,
|
||||||
'note_columns' => 60,
|
'note_columns' => 60,
|
||||||
'note_rows' => 4,
|
'note_rows' => 4,
|
||||||
'column_name' => 'active_directory_SID_11',
|
'column_name' => 'active_directory_SID_11',
|
||||||
|
'is_active' => TRUE,
|
||||||
],
|
],
|
||||||
'match' => [
|
'match' => [
|
||||||
'name',
|
'name',
|
||||||
|
@ -119,6 +122,7 @@ return [
|
||||||
'note_columns' => 60,
|
'note_columns' => 60,
|
||||||
'note_rows' => 4,
|
'note_rows' => 4,
|
||||||
'column_name' => 'can_be_reached_externally_12',
|
'column_name' => 'can_be_reached_externally_12',
|
||||||
|
'is_active' => TRUE,
|
||||||
],
|
],
|
||||||
'match' => [
|
'match' => [
|
||||||
'name',
|
'name',
|
||||||
|
@ -143,6 +147,7 @@ return [
|
||||||
'note_columns' => 60,
|
'note_columns' => 60,
|
||||||
'note_rows' => 4,
|
'note_rows' => 4,
|
||||||
'column_name' => 'subject_prefix_13',
|
'column_name' => 'subject_prefix_13',
|
||||||
|
'is_active' => TRUE,
|
||||||
],
|
],
|
||||||
'match' => [
|
'match' => [
|
||||||
'name',
|
'name',
|
||||||
|
|
11
resources/activity_types.php
Normal file
11
resources/activity_types.php
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use CRM_Mailinglistsync_ExtensionUtil as E;
|
||||||
|
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
'name' => 'Mailing_List_Synchronization_Activity',
|
||||||
|
'label' => E::ts('Mailing List Synchronization Activity'),
|
||||||
|
'description' => E::ts('Indicates a contact has been synchronized with a mailing list'),
|
||||||
|
],
|
||||||
|
];
|
|
@ -1,66 +1,99 @@
|
||||||
<div class="crm-section">
|
{* HEADER *}
|
||||||
<div class="label">{$form[$EXTENSION_SHORT_NAME|cat:'_enable'].label}</div>
|
|
||||||
<div class="content">{$form[$EXTENSION_SHORT_NAME|cat:'_enable'].html}</div>
|
<div>
|
||||||
|
<div class="crm-section">
|
||||||
|
<div class="label">{$form.mailinglistsync_enable.label}</div>
|
||||||
|
<div class="content">{$form.mailinglistsync_enable.html}</div>
|
||||||
<div class="clear"></div>
|
<div class="clear"></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="crm-section">
|
<div class="crm-section">
|
||||||
<div class="label">{$form[$EXTENSION_SHORT_NAME|cat:'_logging'].label}</div>
|
<div class="label">{$form.mailinglistsync_logging.label}</div>
|
||||||
<div class="content">{$form[$EXTENSION_SHORT_NAME|cat:'_logging'].html}</div>
|
<div
|
||||||
|
class="content">{$form.mailinglistsync_logging.html}</div>
|
||||||
<div class="clear"></div>
|
<div class="clear"></div>
|
||||||
|
</div>
|
||||||
|
<div class="crm-section">
|
||||||
|
<div class="label">{$form.mailinglistsync_domain.label}</div>
|
||||||
|
<div class="content">{$form.mailinglistsync_domain.html}</div>
|
||||||
|
<div class="clear"></div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<h3>{ts}AD Mailing List Settings{/ts}</h3>
|
<div>
|
||||||
|
<h3>{ts}AD Mailing List Settings{/ts}</h3>
|
||||||
|
|
||||||
<div class="crm-section">
|
<div class="crm-section">
|
||||||
<div class="label">{$form[$EXTENSION_SHORT_NAME|cat:'_ad_contact_tags'].label}</div>
|
<div
|
||||||
<div class="content">{$form[$EXTENSION_SHORT_NAME|cat:'_ad_contact_tags'].html}</div>
|
class="label">{$form.mailinglistsync_ad_contact_tags.label}</div>
|
||||||
|
<div
|
||||||
|
class="content">{$form.mailinglistsync_ad_contact_tags.html}</div>
|
||||||
<div class="clear"></div>
|
<div class="clear"></div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<h3>{ts}Event Mailing List Settings{/ts}</h3>
|
<div>
|
||||||
|
<h3>{ts}Event Mailing List Settings{/ts}</h3>
|
||||||
|
|
||||||
<div class="crm-section">
|
<div class="crm-section">
|
||||||
<div class="label">{$form[$EXTENSION_SHORT_NAME|cat:'_participant_status'].label}</div>
|
<div
|
||||||
<div class="content">{$form[$EXTENSION_SHORT_NAME|cat:'_participant_status'].html}</div>
|
class="label">{$form.mailinglistsync_participant_status.label}</div>
|
||||||
|
<div
|
||||||
|
class="content">{$form.mailinglistsync_participant_status.html}</div>
|
||||||
<div class="clear"></div>
|
<div class="clear"></div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<h3>{ts}mlmmj Settings{/ts}</h3>
|
<div>
|
||||||
|
<h3>{ts}mlmmj Settings{/ts}</h3>
|
||||||
|
|
||||||
<div class="crm-section">
|
<div class="crm-section">
|
||||||
<div class="label">{$form[$EXTENSION_SHORT_NAME|cat:'_mlmmj_host'].label}</div>
|
<div
|
||||||
<div class="content">{$form[$EXTENSION_SHORT_NAME|cat:'_mlmmj_host'].html}</div>
|
class="label">{$form.mailinglistsync_mlmmj_host.label}</div>
|
||||||
|
<div
|
||||||
|
class="content">{$form.mailinglistsync_mlmmj_host.html}</div>
|
||||||
<div class="clear"></div>
|
<div class="clear"></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="crm-section">
|
<div class="crm-section">
|
||||||
<div class="label">{$form[$EXTENSION_SHORT_NAME|cat:'_mlmmj_token'].label}</div>
|
<div
|
||||||
<div class="content">{$form[$EXTENSION_SHORT_NAME|cat:'_mlmmj_token'].html}</div>
|
class="label">{$form.mailinglistsync_mlmmj_token.label}</div>
|
||||||
|
<div
|
||||||
|
class="content">{$form.mailinglistsync_mlmmj_token.html}</div>
|
||||||
<div class="clear"></div>
|
<div class="clear"></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="crm-section">
|
<div class="crm-section">
|
||||||
<div class="label">{$form[$EXTENSION_SHORT_NAME|cat:'_mlmmj_port'].label}</div>
|
<div
|
||||||
<div class="content">{$form[$EXTENSION_SHORT_NAME|cat:'_mlmmj_port'].html}</div>
|
class="label">{$form.mailinglistsync_mlmmj_port.label}</div>
|
||||||
|
<div
|
||||||
|
class="content">{$form.mailinglistsync_mlmmj_port.html}</div>
|
||||||
<div class="clear"></div>
|
<div class="clear"></div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<h3>{ts}Dovecot Settings{/ts}</h3>
|
<div>
|
||||||
|
<h3>{ts}Dovecot Settings{/ts}</h3>
|
||||||
|
|
||||||
<div class="crm-section">
|
<div class="crm-section">
|
||||||
<div class="label">{$form[$EXTENSION_SHORT_NAME|cat:'_dovecot_host'].label}</div>
|
<div
|
||||||
<div class="content">{$form[$EXTENSION_SHORT_NAME|cat:'_dovecot_host'].html}</div>
|
class="label">{$form.mailinglistsync_dovecot_host.label}</div>
|
||||||
|
<div
|
||||||
|
class="content">{$form.mailinglistsync_dovecot_host.html}</div>
|
||||||
<div class="clear"></div>
|
<div class="clear"></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="crm-section">
|
<div class="crm-section">
|
||||||
<div class="label">{$form[$EXTENSION_SHORT_NAME|cat:'_dovecot_token'].label}</div>
|
<div
|
||||||
<div class="content">{$form[$EXTENSION_SHORT_NAME|cat:'_dovecot_token'].html}</div>
|
class="label">{$form.mailinglistsync_dovecot_token.label}</div>
|
||||||
|
<div
|
||||||
|
class="content">{$form.mailinglistsync_dovecot_token.html}</div>
|
||||||
<div class="clear"></div>
|
<div class="clear"></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="crm-section">
|
<div class="crm-section">
|
||||||
<div class="label">{$form[$EXTENSION_SHORT_NAME|cat:'_dovecot_port'].label}</div>
|
<div
|
||||||
<div class="content">{$form[$EXTENSION_SHORT_NAME|cat:'_dovecot_port'].html}</div>
|
class="label">{$form.mailinglistsync_dovecot_port.label}</div>
|
||||||
|
<div
|
||||||
|
class="content">{$form.mailinglistsync_dovecot_port.html}</div>
|
||||||
<div class="clear"></div>
|
<div class="clear"></div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="crm-submit-buttons">
|
<div class="crm-submit-buttons">
|
||||||
{include file="CRM/common/formButtons.tpl" location="bottom"}
|
{include file="CRM/common/formButtons.tpl" location="bottom"}
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue