diff --git a/Civi/Mailinglistsync/ADGroupMailingList.php b/Civi/Mailinglistsync/ADGroupMailingList.php index ae72c92..e1eaf30 100644 --- a/Civi/Mailinglistsync/ADGroupMailingList.php +++ b/Civi/Mailinglistsync/ADGroupMailingList.php @@ -340,7 +340,8 @@ class ADGroupMailingList extends GroupMailingList { * @throws \Civi\Mailinglistsync\Exceptions\MailinglistException */ public - static function syncContacts($recipients): array { + static function syncContacts($recipients + ): array { $results = []; foreach ($recipients as $contact) { @@ -470,13 +471,12 @@ class ADGroupMailingList extends GroupMailingList { * @return array|array[] */ private - function addRecipient(int $contactId): array { + function addRecipient(int $contactId + ): array { $result = []; // Add the contact to the group try { - $contactEmail = MailingListRecipient::getContactById($contactId) - ->getEmailId(self::LOCATION_TYPE); - $this->addContactToGroup($contactId, $contactEmail); + $this->addContactToGroup($contactId); $result['added'] = TRUE; } catch (MailinglistException $e) { @@ -496,7 +496,8 @@ class ADGroupMailingList extends GroupMailingList { * @return array */ public - function removeRecipient(MailingListRecipient $recipient): array { + function removeRecipient(MailingListRecipient $recipient + ): array { $result = [ 'sid' => $recipient->getSid(), 'contact_id' => $recipient->getContactId(), diff --git a/Civi/Mailinglistsync/BaseMailingList.php b/Civi/Mailinglistsync/BaseMailingList.php index a5b3a15..c928635 100644 --- a/Civi/Mailinglistsync/BaseMailingList.php +++ b/Civi/Mailinglistsync/BaseMailingList.php @@ -95,7 +95,7 @@ abstract class BaseMailingList */ static protected function getCustomFields(): array { - return CustomField::get(FALSE) + return CustomField::get() ->addSelect('*') ->addWhere('custom_group_id:name', '=', static::CUSTOM_GROUP_NAME) ->execute() diff --git a/Civi/Mailinglistsync/EventMailingList.php b/Civi/Mailinglistsync/EventMailingList.php index d9c6ba4..79898b2 100644 --- a/Civi/Mailinglistsync/EventMailingList.php +++ b/Civi/Mailinglistsync/EventMailingList.php @@ -25,7 +25,7 @@ class EventMailingList extends BaseMailingList { * @return array */ protected function getEntity(): array { - return $this->event; + return $this->event ?? NULL; } /** @@ -39,12 +39,6 @@ class EventMailingList extends BaseMailingList { $this->event = $value; } - /** - * Get a list of recipients indexed by email address. - * - * @return array List of recipients (MailListRecipient) - * @throws \Civi\Mailinglistsync\Exceptions\MailinglistException - */ public function getRecipients(): array { try { $recipientData = Contact::get() @@ -53,7 +47,7 @@ class EventMailingList extends BaseMailingList { ->addJoin('Participant AS participant', 'INNER', ['id', '=', 'participant.contact_id'], ['participant.event_id', '=', $this->event['id']], - ['participant.status_id', 'IN', self::getEnabledParticipantStatus()], + ['participant.status', 'IN', self::getEnabledParticipantStatus()], ) ->addGroupBy('id') ->execute() @@ -78,7 +72,7 @@ class EventMailingList extends BaseMailingList { ); } catch (\Exception $e) { throw new MailinglistException( - "Could not create recipient object for contact with id '{$recipient['id']}': $e", + "Could not create recipient object for contact with id '{$recipient['id']}'\n$e", MailinglistException::ERROR_CODE_GET_RECIPIENTS_FAILED ); } @@ -93,6 +87,6 @@ class EventMailingList extends BaseMailingList { * @return ?array */ public static function getEnabledParticipantStatus(): ?array { - return MailingListSettings::get('participant_status'); + return MailingListSettings::get(E::SHORT_NAME . '_participant_status'); } } diff --git a/Civi/Mailinglistsync/Exceptions/MailinglistException.php b/Civi/Mailinglistsync/Exceptions/MailinglistException.php index 5e2fcf5..750ddec 100644 --- a/Civi/Mailinglistsync/Exceptions/MailinglistException.php +++ b/Civi/Mailinglistsync/Exceptions/MailinglistException.php @@ -41,6 +41,5 @@ class MailinglistException extends BaseException { 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'; - public const ERROR_CODE_GET_EMAIL_ID_FAILED = 'get_email_id_failed'; } diff --git a/Civi/Mailinglistsync/GroupMailingList.php b/Civi/Mailinglistsync/GroupMailingList.php index e998783..a1af0d7 100644 --- a/Civi/Mailinglistsync/GroupMailingList.php +++ b/Civi/Mailinglistsync/GroupMailingList.php @@ -110,7 +110,7 @@ class GroupMailingList extends BaseMailingList { } catch (\Exception $e) { throw new MailinglistException( - "Could not get recipients for group with id '{$this->group['id']}': {$e->getMessage()}", + "Could not get recipients for group with id '{$this->group['id']}'\n$e", MailinglistException::ERROR_CODE_GET_RECIPIENTS_FAILED ); } @@ -172,20 +172,16 @@ class GroupMailingList extends BaseMailingList { * Add a contact to this mailing lists related group. * * @param int $contactId - * @param ?int $contactEmailId * * @return void * @throws \Civi\Mailinglistsync\Exceptions\MailinglistException */ - protected function addContactToGroup(int $contactId, int $contactEmailId = NULL): void { + protected function addContactToGroup(int $contactId): void { try { - $query = \Civi\Api4\GroupContact::create() + \Civi\Api4\GroupContact::create() ->addValue('contact_id', $contactId) - ->addValue('group_id', $this->group['id']); - if ($contactEmailId) { - $query->addValue('email_id', $contactEmailId); - } - $query->execute(); + ->addValue('group_id', $this->group['id']) + ->execute(); } catch (\Exception $e) { if ($e->getMessage() === 'DB Error: already exists') { diff --git a/Civi/Mailinglistsync/MailingListRecipient.php b/Civi/Mailinglistsync/MailingListRecipient.php index 0241c26..fcfe6d2 100644 --- a/Civi/Mailinglistsync/MailingListRecipient.php +++ b/Civi/Mailinglistsync/MailingListRecipient.php @@ -128,7 +128,7 @@ class MailingListRecipient { * @return MailingListRecipient * @throws \Civi\Mailinglistsync\Exceptions\MailinglistException */ - public static function getContactByEmail(string $email): MailingListRecipient { + public static function getContactIdEmail(string $email): MailingListRecipient { try { $recipientData = Contact::get() ->addSelect('id', 'first_name', 'last_name', 'email.email', 'Active_Directory.SID') @@ -163,49 +163,6 @@ class MailingListRecipient { return $recipient; } - /** - * Get a recipient by its id. - * - * @param int $id - * - * @return MailingListRecipient - * @throws \Civi\Mailinglistsync\Exceptions\MailinglistException - */ - public static function getContactById(int $id): 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('id', '=', $id) - ->addGroupBy('id') - ->execute() - ->first(); - } - catch (\Exception $e) { - throw new MailinglistException( - "Could not get recipient by id '{$id}': {$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. * @@ -350,32 +307,6 @@ class MailingListRecipient { return $this->email; } - /** - * Get the email id. - * - * @param string $locationType - * - * @return int|NULL - * @throws \Civi\Mailinglistsync\Exceptions\MailinglistException - */ - public function getEmailId(string $locationType): ?int { - try { - return \Civi\Api4\Email::get() - ->addSelect('id') - ->addWhere('contact_id', '=', $this->getContactId()) - ->addWhere('email', '=', $this->email) - ->addWhere('location_type_id:name', '=', $locationType) - ->execute() - ->first()['id'] ?? NULL; - } - catch (\Exception $e) { - throw new MailinglistException( - "Failed to get email id: {$e->getMessage()}", - MailinglistException::ERROR_CODE_GET_EMAIL_ID_FAILED, - ); - } - } - /** * Get the SID of the contact. * diff --git a/Civi/Mailinglistsync/QueueHelper.php b/Civi/Mailinglistsync/QueueHelper.php index 036ee2a..3988dbc 100644 --- a/Civi/Mailinglistsync/QueueHelper.php +++ b/Civi/Mailinglistsync/QueueHelper.php @@ -29,21 +29,21 @@ class QueueHelper { 'type' => 'SqlParallel', 'is_autorun' => FALSE, 'reset' => FALSE, - 'error' => 'abort', + 'error' => 'drop', ]); $this->eventQueue = \Civi::queue( 'propeace-mailinglist-event-queue', [ 'type' => 'SqlParallel', 'is_autorun' => FALSE, 'reset' => FALSE, - 'error' => 'abort', + 'error' => 'drop', ]); $this->emailQueue = \Civi::queue( 'propeace-mailinglist-email-queue', [ 'type' => 'SqlParallel', 'is_autorun' => FALSE, 'reset' => FALSE, - 'error' => 'abort', + 'error' => 'drop', ]); $this->groups = []; @@ -106,14 +106,12 @@ class QueueHelper { * Stores an email address in the queue helper singleton. * * @param \CRM_Queue_TaskContext $context - * @param string|null $email + * @param string $email * * @return bool */ - public static function storeEmail(\CRM_Queue_TaskContext $context, ?string $email): bool { - if (!empty($email)) { - self::getInstance()->addToEmails($email); - } + public static function storeEmail(\CRM_Queue_TaskContext $context, string $email): bool { + self::getInstance()->addToEmails($email); return TRUE; } diff --git a/LICENSE.txt b/LICENSE.txt index d72e56f..41c2284 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,5 +1,5 @@ Package: de.propeace.mailinglistsync -Copyright (C) 2025, Pro Peace +Copyright (C) 2025, FIXME Licensed under the GNU Affero Public License 3.0 (below). ------------------------------------------------------------------------------- diff --git a/api/v3/Mailinglistsync/Mlmmjsync.php b/api/v3/Mailinglistsync/Mlmmjsync.php index ceed3e1..8f08ec2 100644 --- a/api/v3/Mailinglistsync/Mlmmjsync.php +++ b/api/v3/Mailinglistsync/Mlmmjsync.php @@ -110,7 +110,7 @@ function civicrm_api3_mailinglistsync_Mlmmjsync($params) { $mailingListsToSync[$event->getId()] = $event; } foreach ($emails as $email) { - $recipient = MailingListRecipient::getContactByEmail($email); + $recipient = MailingListRecipient::getContactIdEmail($email); $emailGroups = $recipient->getMailingLists(); $emailEvents = $recipient->getEventMailingLists(); foreach ($emailGroups as $group) { @@ -122,7 +122,6 @@ function civicrm_api3_mailinglistsync_Mlmmjsync($params) { } foreach ($mailingListsToSync as $mailingList) { - /* @var \Civi\Mailinglistsync\BaseMailingList $mailingList */ $results['mailing_lists'][$mailingList->getEmailAddress()] = $mailingList->sync(); // TODO: re-add failed task to queue } diff --git a/info.xml b/info.xml index e90cf7f..41e1e8b 100644 --- a/info.xml +++ b/info.xml @@ -7,12 +7,12 @@ Marc Koch - marc.koch@propeace.de + koch@forumZFD.de Maintainer - https://git.propeace.de/ProPeace/de.propeace.mailinglistsync--> + https://www.gnu.org/licenses/agpl-3.0.html diff --git a/mailinglistsync.php b/mailinglistsync.php index 4307197..97ee9df 100644 --- a/mailinglistsync.php +++ b/mailinglistsync.php @@ -129,7 +129,7 @@ function mailinglistsync_civicrm_validateForm($formName, &$fields, &$files, &$fo 'type' => 'SqlParallel', 'is_autorun' => FALSE, 'reset' => FALSE, - 'error' => 'abort', + 'error' => 'drop', ]); $queue->createItem(new CRM_Queue_Task( // callback @@ -145,7 +145,7 @@ function mailinglistsync_civicrm_validateForm($formName, &$fields, &$files, &$fo 'type' => 'SqlParallel', 'is_autorun' => FALSE, 'reset' => FALSE, - 'error' => 'abort', + 'error' => 'drop', ]); $queue->createItem(new CRM_Queue_Task( // callback @@ -174,6 +174,26 @@ function mailinglistsync_civicrm_validateForm($formName, &$fields, &$files, &$fo } } + + +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 * groups and sync group with mlmmj. @@ -223,12 +243,10 @@ function mailinglistsync_civicrm_post(string $op, string $objectName, int $objec ->addWhere('id', '=', $objectId) ->execute() ->first()['event_id']; - $mailingList = new EventMailingList($eventId); + $mailingList = new GroupMailingList($eventId); - // Check permission to alter event mailing list if it is not a deletion - if ($op !== 'delete' - && $mailingList->isEnabled() - && !CRM_Core_Permission::check('manage_event_mailinglists')) { + // Check permission to alter event mailing list + if ($mailingList->isEnabled() && !CRM_Core_Permission::check('manage_event_mailinglists')) { CRM_Core_Session::setStatus( E::ts('You do not have permission to manage event mailing lists.'), E::ts('Permission Denied'), @@ -326,7 +344,7 @@ function mailinglistsync_civicrm_postCommit(string $op, string $objectName, int 'type' => 'SqlParallel', 'is_autorun' => FALSE, 'reset' => FALSE, - 'error' => 'abort', + 'error' => 'drop', ]); $queue->createItem(new CRM_Queue_Task( // callback @@ -347,7 +365,7 @@ function mailinglistsync_civicrm_postCommit(string $op, string $objectName, int 'type' => 'SqlParallel', 'is_autorun' => FALSE, 'reset' => FALSE, - 'error' => 'abort', + 'error' => 'drop', ]); $queue->createItem(new CRM_Queue_Task( // callback @@ -374,7 +392,7 @@ function mailinglistsync_civicrm_postCommit(string $op, string $objectName, int 'type' => 'SqlParallel', 'is_autorun' => FALSE, 'reset' => FALSE, - 'error' => 'abort', + 'error' => 'drop', ]); $queue->createItem(new CRM_Queue_Task( // callback @@ -395,7 +413,7 @@ function mailinglistsync_civicrm_postCommit(string $op, string $objectName, int 'type' => 'SqlParallel', 'is_autorun' => FALSE, 'reset' => FALSE, - 'error' => 'abort', + 'error' => 'drop', ]); $queue->createItem(new CRM_Queue_Task( // callback @@ -416,7 +434,7 @@ function mailinglistsync_civicrm_postCommit(string $op, string $objectName, int 'type' => 'SqlParallel', 'is_autorun' => FALSE, 'reset' => FALSE, - 'error' => 'abort', + 'error' => 'drop', ]); $queue->createItem(new CRM_Queue_Task( // callback @@ -451,7 +469,7 @@ function mailinglistsync_civicrm_postCommit(string $op, string $objectName, int 'type' => 'SqlParallel', 'is_autorun' => FALSE, 'reset' => FALSE, - 'error' => 'abort', + 'error' => 'drop', ]); $queue->createItem(new CRM_Queue_Task( // callback