event; } /** * Set the related event. * * @param array $value * * @return void */ protected function setEntity(array $value): void { $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() ->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_id', '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']}': $e", MailinglistException::ERROR_CODE_GET_RECIPIENTS_FAILED ); } } return $recipients; } /** * Get a list of participants status that are enabled for the mailing list. * * @return ?array */ public static function getEnabledParticipantStatus(): ?array { return MailingListSettings::get('participant_status'); } }