From 4b9700da9fa8eecf4b7fad8ac760141b85f1a236 Mon Sep 17 00:00:00 2001 From: Marc Michalsky forumZFD Date: Thu, 4 Feb 2021 09:36:52 +0100 Subject: [PATCH] implement TiwngleEvent.delete api --- api/v3/TwingleEvent/Delete.php | 124 ++++++++++++++++++++++++++++----- 1 file changed, 106 insertions(+), 18 deletions(-) diff --git a/api/v3/TwingleEvent/Delete.php b/api/v3/TwingleEvent/Delete.php index 03c271a..01fc601 100644 --- a/api/v3/TwingleEvent/Delete.php +++ b/api/v3/TwingleEvent/Delete.php @@ -1,4 +1,6 @@ '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 { + $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 { - throw new API_Exception(/*error_message*/ 'Everyone knows that the magicword is "sesame"', /*error_code*/ 'magicword_incorrect'); + return civicrm_api3_create_success( + $result_values, + $params, + 'TwingleEvent', + 'Delete' + ); } }