group; } /** * Create a new GroupMailingList. * * @param string $title * @param string $description * @param string $email * @param string|null $sid * * @return \Civi\Mailinglistsync\GroupMailingList * @throws \Civi\Mailinglistsync\Exceptions\MailinglistException */ public static function createGroup( string $title, string $description, string $email, string $sid = NULL ): self { try { $request = \Civi\Api4\Group::create() ->addValue('title', $title) ->addValue('description', $description) ->addValue(static::CUSTOM_GROUP_NAME . '.Email', $email) ->addValue('is_active', 1); // If the group is an AD group, add the AD SID if (!empty($values['sid'])) { $request->addValue(static::CUSTOM_GROUP_NAME . '.SID', $values['sid']); } return new self($request->execute()->getArrayCopy()['id']); } catch (\Exception $e) { throw new MailinglistException( "Could not create group\n$e", MailinglistException::ERROR_CODE_GROUP_CREATION_FAILED ); } } /** * Set the related group. * * @param array $value */ protected function setEntity(array $value): void { $this->group = $value; } /** * Check if the group is an AD mailing list. * @return bool */ public function isADGroup(): bool { return !empty($this->group[static::CUSTOM_GROUP_NAME . '.Active_Directory_UUID']); } /** * 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', 'INNER', ['id', '=', 'email.contact_id']) ->addJoin('GroupContact AS group_contact', 'INNER', ['id', '=', 'group_contact.contact_id']) ->addJoin('LocationType AS location_type', 'INNER', ['email.location_type_id', '=', 'location_type.id']) ->addWhere('group_contact.group_id', '=', $this->group['id']) ->addWhere('location_type.name', '=', static::LOCATION_TYPE) ->addGroupBy('id') ->execute() ->getArrayCopy(); } catch (\Exception $e) { throw new MailinglistException( "Could not get recipients for group with id '{$this->group['id']}'\n$e", MailinglistException::ERROR_CODE_GET_RECIPIENTS_FAILED ); } $recipients = []; foreach ($recipientData as $recipient) { $recipients[$recipient['email.email']] = new MailingListRecipient( contact_id: $recipient['id'], first_name: $recipient['first_name'], last_name: $recipient['last_name'], email: $recipient['email.email'], sid: $recipient['Active_Directory.SID'], ); } return $recipients; } /** * Update the title of the group. * * @param string $title * * @return void * @throws \Civi\Mailinglistsync\Exceptions\MailinglistException */ public function updateGroupTitle(string $title): void { $this->update(['title' => $title]); } /** * Update the description of the group. * * @return string */ public function getGroupDescription(): string { return $this->group['description']; } /** * Update the description of the group. * * @param string $description * * @return void * @throws \Civi\Mailinglistsync\Exceptions\MailinglistException */ public function updateGroupDescription(string $description): void { $this->update(['description' => $description]); } }