🎉 initial commit
This commit is contained in:
commit
c93a06972b
27 changed files with 4189 additions and 0 deletions
203
api/v3/Mailinglistsync/Adgroupsync.php
Normal file
203
api/v3/Mailinglistsync/Adgroupsync.php
Normal file
|
@ -0,0 +1,203 @@
|
|||
<?php
|
||||
|
||||
use Civi\Mailinglistsync\ADGroupMailingList;
|
||||
use Civi\Mailinglistsync\Exceptions\MailinglistException;
|
||||
use CRM_Mailinglistsync_ExtensionUtil as E;
|
||||
|
||||
/**
|
||||
* Mailinglistsync.Adgroupsync API specification (optional)
|
||||
* This is used for documentation and validation.
|
||||
*
|
||||
* @param array $spec description of fields supported by this API call
|
||||
*
|
||||
* @see https://docs.civicrm.org/dev/en/latest/framework/api-architecture/
|
||||
*/
|
||||
function _civicrm_api3_mailinglistsync_Adgroupsync_spec(&$spec) {
|
||||
$spec['sid'] = [
|
||||
'api.required' => 1,
|
||||
'type' => CRM_Utils_Type::T_STRING,
|
||||
'title' => E::ts('Active Directory SID'),
|
||||
'description' => E::ts('The Active Directory SID of the group'),
|
||||
];
|
||||
$spec['email'] = [
|
||||
'api.required' => 1,
|
||||
'type' => CRM_Utils_Type::T_EMAIL,
|
||||
'title' => 'Email Address',
|
||||
'description' => 'Email address of the mailing list',
|
||||
];
|
||||
$spec['recipients'] = [
|
||||
'api.required' => 1,
|
||||
'type' => CRM_Utils_Type::T_STRING,
|
||||
'title' => E::ts('Recipients'),
|
||||
'description' => E::ts('Array of email addresses and SIDs'),
|
||||
];
|
||||
$spec['name'] = [
|
||||
'api.required' => 1,
|
||||
'type' => CRM_Utils_Type::T_STRING,
|
||||
'title' => E::ts('Mail List Name'),
|
||||
'description' => E::ts('Name of the mailing list'),
|
||||
];
|
||||
$spec['description'] = [
|
||||
'api.required' => 1,
|
||||
'type' => CRM_Utils_Type::T_LONGTEXT,
|
||||
'title' => E::ts('Mail List Description'),
|
||||
'description' => E::ts('Description of the mailing list'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Mailinglistsync.Adgroupsync API
|
||||
*
|
||||
* @param array $params
|
||||
*
|
||||
* @return array
|
||||
* API result descriptor
|
||||
*
|
||||
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
||||
* @throws CRM_Core_Exception
|
||||
* @see civicrm_api3_create_success
|
||||
*/
|
||||
function civicrm_api3_mailinglistsync_Adgroupsync($params) {
|
||||
|
||||
// Filter illegal params
|
||||
$allowed_params = [];
|
||||
_civicrm_api3_mailinglistsync_Adgroupsync_spec($allowed_params);
|
||||
$params = array_intersect_key($params, $allowed_params);
|
||||
|
||||
// Prepare result array
|
||||
$result = [
|
||||
'sid' => $params['sid'],
|
||||
'email' => $params['email'],
|
||||
'name' => $params['name'],
|
||||
'description' => $params['description'],
|
||||
'group_created' => FALSE,
|
||||
'group_updated' => FALSE,
|
||||
];
|
||||
|
||||
try {
|
||||
// Try to get mailing list by SID
|
||||
$adGroupMailingList = ADGroupMailingList::getBySID($params['sid']);
|
||||
|
||||
// If no AD group mailing list found, create new
|
||||
if (!$adGroupMailingList) {
|
||||
try {
|
||||
$adGroupMailingList = ADGroupMailingList::createGroup(
|
||||
title: $params['name'],
|
||||
description: $params['description'],
|
||||
email: $params['email'],
|
||||
sid: $params['sid'],
|
||||
);
|
||||
$result['group_created']['group_id'] = $adGroupMailingList->getId();
|
||||
$result['group_created']['is_error'] = FALSE;
|
||||
}
|
||||
catch (MailinglistException $e) {
|
||||
\Civi::log(E::LONG_NAME)->error($e->getLogMessage());
|
||||
$result['group_created']['is_error'] = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
// Sync AD group mailing list values
|
||||
else {
|
||||
if ($adGroupMailingList->getTitle() !== $params['name']) {
|
||||
try {
|
||||
$adGroupMailingList->updateGroupTitle($params['name']);
|
||||
\Civi::log(E::LONG_NAME)->info(
|
||||
"Updated group '{$adGroupMailingList->getId()}' title from '{$adGroupMailingList->getTitle()}' to '{$params['name']}'"
|
||||
);
|
||||
$result['group_updated']['title'] = [
|
||||
'is_error' => FALSE,
|
||||
'old' => $adGroupMailingList->getTitle(),
|
||||
'new' => $params['name'],
|
||||
];
|
||||
}
|
||||
catch (MailinglistException $e) {
|
||||
\Civi::log(E::LONG_NAME)->error($e->getLogMessage());
|
||||
$result['group_updated']['title'] = [
|
||||
'is_error' => TRUE,
|
||||
'error' => $e->getLogMessage(),
|
||||
];
|
||||
}
|
||||
}
|
||||
if ($adGroupMailingList->getGroupDescription() !== $params['description']) {
|
||||
try {
|
||||
$adGroupMailingList->updateGroupDescription($params['description']);
|
||||
\Civi::log(E::LONG_NAME)->info(
|
||||
"Updated group '{$adGroupMailingList->getId()}' description.'"
|
||||
);
|
||||
$result['group_updated']['description'] = [
|
||||
'is_error' => FALSE,
|
||||
'old' => $adGroupMailingList->getGroupDescription(),
|
||||
'new' => $params['description'],
|
||||
];
|
||||
}
|
||||
catch (MailinglistException $e) {
|
||||
\Civi::log(E::LONG_NAME)->error($e->getLogMessage());
|
||||
$result['group_updated']['description'] = [
|
||||
'is_error' => TRUE,
|
||||
'error' => $e->getLogMessage(),
|
||||
];
|
||||
}
|
||||
}
|
||||
if ($adGroupMailingList->getEmailAddress() !== $params['email']) {
|
||||
try {
|
||||
$adGroupMailingList->updateEmailAddress($params['email']);
|
||||
\Civi::log(E::LONG_NAME)->info(
|
||||
"Updated group '{$adGroupMailingList->getId()}' email address from '{$adGroupMailingList->getEmailAddress()}' to '{$params['email']}'"
|
||||
);
|
||||
$result['group_updated']['email'] = [
|
||||
'is_error' => FALSE,
|
||||
'old' => $adGroupMailingList->getEmailAddress(),
|
||||
'new' => $params['email'],
|
||||
];
|
||||
}
|
||||
catch (MailinglistException $e) {
|
||||
\Civi::log(E::LONG_NAME)->error($e->getLogMessage());
|
||||
$result['group_updated']['email'] = [
|
||||
'is_error' => TRUE,
|
||||
'error' => $e->getLogMessage(),
|
||||
];
|
||||
}
|
||||
}
|
||||
$adGroupMailingList->save();
|
||||
|
||||
if ($result['group_updated'] ?? FALSE) {
|
||||
$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']['group_id'] = $adGroupMailingList->getId();
|
||||
}
|
||||
}
|
||||
|
||||
// Sync group mailing list members
|
||||
$result['recipients_updated'] = $adGroupMailingList->syncRecipients($params['recipients']);
|
||||
|
||||
// Return error response if any errors occurred
|
||||
$totalErrors = (int) ($result['group_created']['is_error'] ?? 0)
|
||||
+ ($result['group_updated']['error_count'] ?? 0)
|
||||
+ ($result['recipients_updated']['error_count'] ?? 0);
|
||||
$result['is_error'] = $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, $params, 'Mailinglistsync', 'Adgroupsync');
|
||||
}
|
||||
catch (MailinglistException $me) {
|
||||
\Civi::log(E::LONG_NAME)->error($me->getLogMessage());
|
||||
return civicrm_api3_create_error($me->getLogMessage(),
|
||||
[
|
||||
'params' => $params,
|
||||
'entity' => 'Mailinglistsync',
|
||||
'action' => 'Adgroupsync',
|
||||
]);
|
||||
}
|
||||
}
|
97
api/v3/Mailinglistsync/Mlmmjsync.php
Normal file
97
api/v3/Mailinglistsync/Mlmmjsync.php
Normal file
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
|
||||
use Civi\Mailinglistsync\QueueHelper;
|
||||
use CRM_Mailinglist_ExtensionUtil as E;
|
||||
|
||||
/**
|
||||
* Mailinglistsync.Mlmmjsync API specification (optional)
|
||||
* This is used for documentation and validation.
|
||||
*
|
||||
* @param array $spec description of fields supported by this API call
|
||||
*
|
||||
* @see https://docs.civicrm.org/dev/en/latest/framework/api-architecture/
|
||||
*/
|
||||
function _civicrm_api3_mailinglistsync_Mlmmjsync_spec(&$spec) {}
|
||||
|
||||
/**
|
||||
* Mailinglistsync.Mlmmjsync API
|
||||
*
|
||||
* @param array $params
|
||||
*
|
||||
* @return array
|
||||
* API result descriptor
|
||||
*
|
||||
* @see civicrm_api3_create_success
|
||||
*
|
||||
* @throws CRM_Core_Exception
|
||||
*/
|
||||
function civicrm_api3_mailinglistsync_Mlmmjsync($params) {
|
||||
|
||||
// Get queues
|
||||
$qh = QueueHelper::getInstance();
|
||||
$groupQueue = $qh->getGroupQueue();
|
||||
$eventQueue = $qh->getEventQueue();
|
||||
$emailQueue = $qh->getEmailQueue();
|
||||
|
||||
// Create runners
|
||||
$groupRunner = new CRM_Queue_Runner([
|
||||
'title' => ts('ProPeace GroupMailinglist Runner'),
|
||||
'queue' => $groupQueue,
|
||||
'errorMode' => CRM_Queue_Runner::ERROR_CONTINUE,
|
||||
]);
|
||||
$eventRunner = new CRM_Queue_Runner([
|
||||
'title' => ts('ProPeace EventMailinglist Runner'),
|
||||
'queue' => $eventQueue,
|
||||
'errorMode' => CRM_Queue_Runner::ERROR_CONTINUE,
|
||||
]);
|
||||
$emailRunner = new CRM_Queue_Runner([
|
||||
'title' => ts('ProPeace EmailMailinglist Runner'),
|
||||
'queue' => $emailQueue,
|
||||
'errorMode' => CRM_Queue_Runner::ERROR_CONTINUE,
|
||||
]);
|
||||
|
||||
// Run runners
|
||||
$results = [];
|
||||
$continue = TRUE;
|
||||
while($continue) {
|
||||
$result = $groupRunner->runNext(false);
|
||||
if (!$result['is_continue']) {
|
||||
$continue = false;
|
||||
}
|
||||
$results['runners'][] = $result;
|
||||
}
|
||||
|
||||
$groups = $qh->getGroups();
|
||||
$events = $qh->getEvents();
|
||||
$emails = $qh->getEmails();
|
||||
|
||||
// TODO: Sync groups and events just once and invoke syncing
|
||||
|
||||
$mailingListsToSync = [];
|
||||
foreach ($groups as $group) {
|
||||
$mailingListsToSync[$group->getId()] = $group;
|
||||
}
|
||||
foreach ($events as $event) {
|
||||
$mailingListsToSync[$event->getId()] = $event;
|
||||
}
|
||||
foreach ($emails as $email) {
|
||||
$emailGroups = $email->getGroups();
|
||||
$emailEvents = $email->getEvents();
|
||||
foreach ($emailGroups as $group) {
|
||||
$mailingListsToSync[$group->getId()] = $group;
|
||||
}
|
||||
foreach ($emailEvents as $event) {
|
||||
$mailingListsToSync[$event->getId()] = $event;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($mailingListsToSync as $mailingList) {
|
||||
$results['mailing_lists'][] = $mailingList->sync();
|
||||
}
|
||||
|
||||
return civicrm_api3_create_success($results, [], 'Mailinglist', 'Mlmmjsync');
|
||||
|
||||
/*
|
||||
throw new CRM_Core_Exception('Everyone knows that the magicword is "sesame"', 'magicword_incorrect');
|
||||
*/
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue