implement TiwngleEvent.delete api

This commit is contained in:
Marc Michalsky forumZFD 2021-02-04 09:36:52 +01:00
parent aef0297daf
commit 4b9700da9f
Signed by untrusted user who does not match committer: marc.koch
GPG key ID: 12406554CFB028B9

View file

@ -1,4 +1,6 @@
<?php
use CRM_TwingleCampaign_BAO_TwingleEvent as TwingleEvent;
use CRM_TwingleCampaign_ExtensionUtil as E;
/**
@ -9,37 +11,123 @@ use CRM_TwingleCampaign_ExtensionUtil as E;
*
* @see https://docs.civicrm.org/dev/en/latest/framework/api-architecture/
*/
function _civicrm_api3_twingle_event_Delete_spec(&$spec) {
$spec['magicword']['api.required'] = 1;
function _civicrm_api3_twingle_event_Delete_spec(array &$spec) {
$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['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 this project'),
];
$spec['event_id'] = [
'name' => 'event_id',
'title' => E::ts('Twingle Event ID'),
'type' => CRM_Utils_Type::T_INT,
'api.required' => 0,
'description' => E::ts('Twingle ID for this Event'),
];
$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'),
];
$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'),
];
}
/**
* TwingleEvent.Delete API
* # TwingleEvent.Delete API
* Delete one or more TwingleEvent campaigns.
* * If you provide an **id** or **event_id** parameter, only one event will be
* deleted.
* * If you provide a **project_id** as parameter, all events of that project
* will be deleted.
* *NOTE:* This API won't delete TwingleEvents on Twingle's side
*
* @param array $params
*
* @return array
* API result descriptor
*
* @throws \CiviCRM_API3_Exception
* @see civicrm_api3_create_success
*
* @throws API_Exception
*/
function civicrm_api3_twingle_event_Delete($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
function civicrm_api3_twingle_event_Delete(array $params): array {
// Spec: civicrm_api3_create_success($values = 1, $params = [], $entity = NULL, $action = NULL)
return civicrm_api3_create_success($returnValues, $params, 'TwingleEvent', 'Delete');
$result_values = [];
$events = [];
$errors_occurred = 0;
$params['sequential'] = 1;
// Get one or more events from the TwingleEvent.get api depending on the
// provided parameters
if ($params['id']) {
$events[] = civicrm_api3('TwingleEvent', 'getsingle',
[$params['id']])['values'];
}
elseif ($params['event_id']) {
$events[] = civicrm_api3('TwingleEvent', 'getsingle',
[$params['event_id']])['values'];
}
elseif ($params['project_id']) {
$events = civicrm_api3('TwingleEvent', 'get',
[$params['project_id']])['values'];
}
// Delete TwingleEvents
foreach ($events as $event) {
try {
$delete_event = new TwingleEvent($event, $event['id']);
try {
if ($delete_event->delete()) {
$result_values[] = $delete_event->getResponse('Event deleted');
}
else {
throw new API_Exception(/*error_message*/ 'Everyone knows that the magicword is "sesame"', /*error_code*/ 'magicword_incorrect');
$errors_occurred++;
$result_values[] = $delete_event->getResponse('Could not delete Event');
}
} catch (CiviCRM_API3_Exception $e) {
$errors_occurred++;
$result_values[] = $delete_event->getResponse(
'Could not delete Project: ' . $e->getMessage()
);
}
} catch (Exception $e) {
$event['status'] = 'Could not delete Event: ' . $e->getMessage();
$result_values[] = $event;
}
}
// Return results
if ($errors_occurred > 0) {
$errorMessage = $errors_occurred . ' of ' . count($events) .
' TwingleEvents could not get deleted.';
return civicrm_api3_create_error(
$errorMessage,
$result_values
);
}
else {
return civicrm_api3_create_success(
$result_values,
$params,
'TwingleEvent',
'Delete'
);
}
}