rebuild TwingleSync.sync api
the main part of the sync logic is now done in the TwingleProject and TwingleEvent sync apis
This commit is contained in:
parent
6e80fa7526
commit
abcca83e0d
1 changed files with 25 additions and 116 deletions
|
@ -28,20 +28,6 @@ function _civicrm_api3_twingle_sync_Sync_spec(array &$spec) {
|
||||||
'api.required' => 0,
|
'api.required' => 0,
|
||||||
'description' => E::ts('Limit for the number of events that should get requested per call to the Twingle API'),
|
'description' => E::ts('Limit for the number of events that should get requested per call to the Twingle API'),
|
||||||
];
|
];
|
||||||
$spec['project_id'] = [
|
|
||||||
'name' => 'project_id',
|
|
||||||
'title' => E::ts('Twingle Project ID'),
|
|
||||||
'type' => CRM_Utils_Type::T_INT,
|
|
||||||
'api.required' => 0,
|
|
||||||
'description' => E::ts('Twingle ID for a project'),
|
|
||||||
];
|
|
||||||
$spec['id'] = [
|
|
||||||
'name' => 'id',
|
|
||||||
'title' => E::ts('Campaign ID'),
|
|
||||||
'type' => CRM_Utils_Type::T_INT,
|
|
||||||
'api.required' => 0,
|
|
||||||
'description' => E::ts('Unique Campaign ID'),
|
|
||||||
];
|
|
||||||
$spec['is_test'] = [
|
$spec['is_test'] = [
|
||||||
'name' => 'is_test',
|
'name' => 'is_test',
|
||||||
'title' => E::ts('Test'),
|
'title' => E::ts('Test'),
|
||||||
|
@ -52,121 +38,44 @@ function _civicrm_api3_twingle_sync_Sync_spec(array &$spec) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TwingleSync.Sync API
|
* # TwingleSync.Sync API
|
||||||
|
* This API synchronizes all *TwingleProject* and *TwingleEvent* campaigns.
|
||||||
*
|
*
|
||||||
* @param array $params
|
* @param array $params
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
* API result descriptor
|
* API result descriptor
|
||||||
*
|
*
|
||||||
* @throws API_Exception|\CiviCRM_API3_Exception
|
* @throws CiviCRM_API3_Exception
|
||||||
* @throws \Exception
|
|
||||||
* @see civicrm_api3_create_success
|
* @see civicrm_api3_create_success
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
function civicrm_api3_twingle_sync_Sync($params) {
|
function civicrm_api3_twingle_sync_Sync(array $params): array {
|
||||||
|
|
||||||
$result_values = [];
|
$result_values = [];
|
||||||
$is_test = FALSE;
|
unset($params['sequential']);
|
||||||
|
|
||||||
// Is this call a test?
|
// Synchronize all TwingleProject campaigns
|
||||||
if ($params['is_test']) {
|
$projects = civicrm_api3(
|
||||||
$is_test = $params['is_test'];
|
'TwingleProject',
|
||||||
}
|
'sync',
|
||||||
|
$params
|
||||||
|
)['values'];
|
||||||
|
|
||||||
// If call provides an API key, use it instead of the API key set
|
// Synchronize all TwingleEvent campaigns
|
||||||
// on the extension settings page
|
foreach ($projects as $project) {
|
||||||
$apiKey = empty($params['twingle_api_key'])
|
|
||||||
? trim(Civi::settings()->get('twingle_api_key'))
|
|
||||||
: trim($params['twingle_api_key']);
|
|
||||||
// If function call does not provide a limit, set a default value
|
|
||||||
$limit = ($params['limit']) ?? 20;
|
|
||||||
|
|
||||||
// Try to retrieve twingleApi from cache
|
|
||||||
$twingleApi = Civi::cache()->get('twinglecampaign_twingle_api');
|
|
||||||
if (NULL === $twingleApi || $params['twingle_api_key']) {
|
|
||||||
$twingleApi = new TwingleApiCall($apiKey, $limit);
|
|
||||||
Civi::cache('long')->set('twinglecampaign_twingle_api', $twingleApi);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($params['id'] && !$params['project_id']) {
|
|
||||||
// Get single TwingleProject
|
|
||||||
$projects_from_civicrm =
|
|
||||||
civicrm_api3('TwingleProject', 'getsingle',
|
|
||||||
['id' => $params['id'], 'is_active' => 1])['values'];
|
|
||||||
|
|
||||||
// Get single project from Twingle
|
|
||||||
$projects_from_twingle[0] =
|
|
||||||
$projects_from_civicrm[$params['id']]['project_id']
|
|
||||||
? $twingleApi->getProject($projects_from_civicrm[$params['id']]['project_id'])
|
|
||||||
: NULL;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if ($params['project_id']) {
|
|
||||||
// Get single project from Twingle
|
|
||||||
$projects_from_twingle[0] = $twingleApi->getProject($params['project_id']);
|
|
||||||
|
|
||||||
// Get single TwingleProject
|
|
||||||
$projects_from_civicrm = civicrm_api3('TwingleProject', 'get',
|
|
||||||
['is_active' => 1, 'project_id' => $params['project_id']]);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// Get all projects from Twingle
|
|
||||||
$projects_from_twingle = $twingleApi->getProject();
|
|
||||||
|
|
||||||
// Get all TwingleProjects from CiviCRM
|
|
||||||
$projects_from_civicrm = civicrm_api3('TwingleProject', 'get',
|
|
||||||
['is_active' => 1,])['values'];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$i = 0;
|
|
||||||
|
|
||||||
// Push missing projects to Twingle
|
|
||||||
foreach ($projects_from_civicrm as $project_from_civicrm) {
|
|
||||||
if (!in_array($project_from_civicrm['project_id'],
|
|
||||||
array_column($projects_from_twingle, 'id'))) {
|
|
||||||
// store campaign id in $id
|
|
||||||
$id = $project_from_civicrm['id'];
|
|
||||||
unset($project_from_civicrm['id']);
|
|
||||||
// instantiate project with values from TwingleProject.Get
|
|
||||||
$project = new TwingleProject($project_from_civicrm, $id);
|
|
||||||
// push project to Twingle
|
|
||||||
$result = $twingleApi->pushProject($project);
|
|
||||||
// update local campaign with data coming back from Twingle
|
|
||||||
$project->update($result);
|
|
||||||
$project_create = $project->create();
|
|
||||||
// set status
|
|
||||||
$project_create['status'] =
|
|
||||||
$project_create['status'] == 'TwingleProject created'
|
|
||||||
? 'TwingleProject pushed to Twingle'
|
|
||||||
: 'TwingleProject got likely pushed to Twingle but local update failed';
|
|
||||||
$result_values['sync']['projects'][$i++] = $project_create;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sync existing projects
|
|
||||||
foreach ($projects_from_twingle as $project) {
|
|
||||||
if (is_array($project)) {
|
if (is_array($project)) {
|
||||||
$result_values['sync']['projects'][$i++] =
|
$_params = $params;
|
||||||
TwingleProject::sync($project, $twingleApi, $is_test);
|
$_params['project_id'] = $project['project_id'];
|
||||||
}
|
$result_values[] = [
|
||||||
}
|
'project' => $project,
|
||||||
|
'events' => array_values(
|
||||||
// Get all events from projects of type "event" and create them as campaigns
|
civicrm_api3(
|
||||||
// if they do not exist yet
|
'TwingleEvent',
|
||||||
$j = 0;
|
'sync',
|
||||||
foreach ($result_values['sync']['projects'] as $project) {
|
$_params
|
||||||
if ($project['project_type'] == 'event') {
|
)['values']
|
||||||
$events = $twingleApi->getEvent($project['project_id']);
|
),
|
||||||
if (is_array($events)) {
|
];
|
||||||
foreach ($events as $event) {
|
|
||||||
if ($event) {
|
|
||||||
$result_values['sync']['events'][$j++] =
|
|
||||||
TwingleEvent::sync($event, $twingleApi, $is_test);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue