implement TwingleSync.singlesync

This commit is contained in:
Marc Michalsky forumZFD 2020-12-10 15:20:29 +01:00
parent 8ed58aa625
commit 12b941e01d
Signed by untrusted user who does not match committer: marc.koch
GPG key ID: 12406554CFB028B9
3 changed files with 155 additions and 18 deletions

View file

@ -1,4 +1,10 @@
<?php
use CRM_TwingleCampaign_BAO_TwingleApiCall as TwingleApiCall;
use CRM_TwingleCampaign_BAO_TwingleProject as TwingleProject;
use CRM_TwingleCampaign_BAO_TwingleEvent as TwingleEvent;
use CRM_TwingleCampaign_BAO_CampaignType as CampaignType;
use CRM_TwingleCampaign_Utils_ExtensionCache as Cache;
use CRM_TwingleCampaign_ExtensionUtil as E;
/**
@ -9,8 +15,28 @@ use CRM_TwingleCampaign_ExtensionUtil as E;
*
* @see https://docs.civicrm.org/dev/en/latest/framework/api-architecture/
*/
function _civicrm_api3_twingle_sync_Singlesync_spec(&$spec) {
$spec['magicword']['api.required'] = 1;
function _civicrm_api3_twingle_sync_Singlesync_spec(array &$spec) {
$spec['campaign_id'] = [
'name' => 'campaign_id',
'title' => E::ts('Campaign ID'),
'type' => CRM_Utils_Type::T_INT,
'api.required' => 1,
'description' => E::ts('ID of the campaign that should get synced'),
];
$spec['twingle_api_key'] = [
'name' => 'twingle_api_key',
'title' => E::ts('Twingle API key'),
'type' => CRM_Utils_Type::T_STRING,
'api.required' => 0,
'description' => E::ts('The key to access the Twingle API'),
];
$spec['is_test'] = [
'name' => 'is_test',
'title' => E::ts('Test'),
'type' => CRM_Utils_Type::T_BOOLEAN,
'api.required' => 0,
'description' => E::ts('If this is set true, no database change will be made'),
];
}
/**
@ -21,25 +47,80 @@ function _civicrm_api3_twingle_sync_Singlesync_spec(&$spec) {
* @return array
* API result descriptor
*
* @throws API_Exception
* @see civicrm_api3_create_success
*
* @throws API_Exception
*/
function civicrm_api3_twingle_sync_Singlesync($params) {
if (array_key_exists('magicword', $params) && $params['magicword'] == 'sesame') {
$returnValues = array(
// OK, return several data rows
12 => ['id' => 12, 'name' => 'Twelve'],
34 => ['id' => 34, 'name' => 'Thirty four'],
56 => ['id' => 56, 'name' => 'Fifty six'],
);
// ALTERNATIVE: $returnValues = []; // OK, success
// ALTERNATIVE: $returnValues = ["Some value"]; // OK, return a single value
// Spec: civicrm_api3_create_success($values = 1, $params = [], $entity = NULL, $action = NULL)
return civicrm_api3_create_success($returnValues, $params, 'TwingleSync', 'Singlesync');
$result_values = [];
// Is this call a test?
$is_test = (boolean) $params['is_test'];
// If function call provides an API key, use it instead of the API key set
// on the extension settings page
$apiKey = empty($params['twingle_api_key'])
? trim(Civi::settings()->get('twingle_api_key'))
: trim($params['twingle_api_key']);
$twingleApi = new TwingleApiCall($apiKey, 1);
$campaign_type_id = 0;
$project_campaign_type_id =
CampaignType::fetch('twingle_project')->getValue();
$event_campaign_type_id =
CampaignType::fetch('twingle_event')->getValue();
$custom_field_mapping = Cache::getInstance()->getCustomFieldMapping();
$result = civicrm_api3('Campaign', 'get', [
'sequential' => 1,
'id' => $params['campaign_id'],
]);
if ($result['is_error'] == 0) {
if ($result['count'] == 1) {
$campaign_type_id = $result['values'][0]['campaign_type_id'];
}
else {
$count = $result['count'];
throw new API_Exception(
"Expected one Campaign but found $count",
);
}
}
else {
throw new API_Exception(/*error_message*/ 'Everyone knows that the magicword is "sesame"', /*error_code*/ 'magicword_incorrect');
throw new API_Exception($result['error_message']);
}
switch ($campaign_type_id) {
case $project_campaign_type_id:
// Get a single project from the Twingle API
// TODO: Es darf nur ein Projekt sein! $result['values'][0][$custom_field_mapping['twingle_project_id']] pruefen! Sonst project pushen
$project = $twingleApi->getProject(
$result['values'][0][$custom_field_mapping['twingle_project_id']]
);
// Create project as campaign and store results in $result_values
$result_values['project'] =
TwingleProject::sync($project, $twingleApi, $is_test);
break;
case $event_campaign_type_id:
$event = $twingleApi->getEvent(
$result['values'][0][$custom_field_mapping['twingle_event_project_id']],
$result['values'][0][$custom_field_mapping['twingle_event_id']]
);
$result_values['event'] =
TwingleEvent::sync($event, $twingleApi, $is_test);
break;
default:
throw new API_Exception(
"Campaign does not belong to TwingleCampaign Extension"
);
}
return civicrm_api3_create_success($result_values);
}