️ meet standard for getsingle api response

return array with object values instead of api success response
This commit is contained in:
Marc Michalsky forumZFD 2021-03-09 20:51:03 +01:00
parent 88a77456b8
commit d904ae931f
Signed by untrusted user who does not match committer: marc.koch
GPG key ID: 12406554CFB028B9
11 changed files with 59 additions and 95 deletions

View file

@ -79,7 +79,7 @@ class CRM_TwingleCampaign_BAO_TwingleCampaign {
*/ */
public function fetch() { public function fetch() {
$this->values = civicrm_api3('TwingleCampaign', 'getsingle', $this->values = civicrm_api3('TwingleCampaign', 'getsingle',
['id' => $this->id])['values']; ['id' => $this->id]);
} }
@ -105,7 +105,7 @@ class CRM_TwingleCampaign_BAO_TwingleCampaign {
'TwingleCampaign', 'TwingleCampaign',
'getsingle', 'getsingle',
['id' => $this->id] ['id' => $this->id]
)['values']['parent_id']; )['parent_id'];
$parent_campaign_type_id = NULL; $parent_campaign_type_id = NULL;

View file

@ -48,7 +48,7 @@ class CRM_TwingleCampaign_Utils_APIWrapper {
'TwingleEvent', 'TwingleEvent',
'getsingle', 'getsingle',
['id' => $contribution['campaign_id']] ['id' => $contribution['campaign_id']]
)['values']; );
$response['values']['soft_credit'] = $response['values']['soft_credit'] =
self::createSoftCredit($contribution, $twingle_event)['values']; self::createSoftCredit($contribution, $twingle_event)['values'];
$event->setResponse($response); $event->setResponse($response);
@ -83,7 +83,7 @@ class CRM_TwingleCampaign_Utils_APIWrapper {
'TwingleEvent', 'TwingleEvent',
'getsingle', 'getsingle',
['id' => $contribution['contribution_campaign_id']] ['id' => $contribution['contribution_campaign_id']]
)['values']; );
$response['values']['soft_credit'] = $response['values']['soft_credit'] =
self::createSoftCredit($contribution, $twingle_event)['values']; self::createSoftCredit($contribution, $twingle_event)['values'];
$event->setResponse($response); $event->setResponse($response);
@ -115,7 +115,7 @@ class CRM_TwingleCampaign_Utils_APIWrapper {
$apiRequest['params']) $apiRequest['params'])
) { ) {
if (is_numeric($apiRequest['params']['campaign_id'])) { if (is_numeric($apiRequest['params']['campaign_id'])) {
$targetCampaign['values']['id'] = $apiRequest['params']['campaign_id']; $targetCampaign['id'] = $apiRequest['params']['campaign_id'];
} }
else { else {
try { try {
@ -157,7 +157,7 @@ class CRM_TwingleCampaign_Utils_APIWrapper {
} }
if (isset($targetCampaign)) { if (isset($targetCampaign)) {
$apiRequest['params']['campaign_id'] = $targetCampaign['values']['id']; $apiRequest['params']['campaign_id'] = $targetCampaign['id'];
} }
return $callsame($apiRequest); return $callsame($apiRequest);

View file

@ -99,9 +99,5 @@ function civicrm_api3_twingle_campaign_Getsingle(array $params): array {
"Expected one TwingleCampaign but found $count" "Expected one TwingleCampaign but found $count"
); );
} }
return civicrm_api3_create_success( return $returnValues['values'][$returnValues['id']];
$returnValues['values'][$returnValues['id']],
$params, 'TwingleCampaign',
'Getsingle'
);
} }

View file

@ -83,11 +83,11 @@ function civicrm_api3_twingle_event_Delete(array $params): array {
// provided parameters // provided parameters
if ($params['id']) { if ($params['id']) {
$events[] = civicrm_api3('TwingleEvent', 'getsingle', $events[] = civicrm_api3('TwingleEvent', 'getsingle',
[$params['id']])['values']; [$params['id']]);
} }
elseif ($params['event_id']) { elseif ($params['event_id']) {
$events[] = civicrm_api3('TwingleEvent', 'getsingle', $events[] = civicrm_api3('TwingleEvent', 'getsingle',
[$params['event_id']])['values']; [$params['event_id']]);
} }
elseif ($params['project_id']) { elseif ($params['project_id']) {
$events = civicrm_api3('TwingleEvent', 'get', $events = civicrm_api3('TwingleEvent', 'get',

View file

@ -110,10 +110,5 @@ function civicrm_api3_twingle_event_Getsingle(array $params): array {
if ($count != 1) { if ($count != 1) {
return civicrm_api3_create_error("Expected one TwingleEvent but found $count"); return civicrm_api3_create_error("Expected one TwingleEvent but found $count");
} }
return civicrm_api3_create_success( return $returnValues['values'][$returnValues['id']];
$returnValues['values'][$returnValues['id']],
$params,
'TwingleEvent',
'Getsingle'
);
} }

View file

@ -120,15 +120,15 @@ function civicrm_api3_twingle_event_Sync(array $params): array {
if ($result['is_error'] == 0) { if ($result['is_error'] == 0) {
// Get the event from Twingle // Get the event from Twingle
if ($result['values']['event_id']) { if ($result['event_id']) {
$event_from_twingle = $twingleApi->getEvent( $event_from_twingle = $twingleApi->getEvent(
$result['values']['project_id'], $result['project_id'],
$result['values']['event_id'] $result['event_id']
); );
// instantiate event from CiviCRM // instantiate event from CiviCRM
try { try {
$event = _instantiateEvent($result['values']); $event = _instantiateEvent($result);
} catch (CiviCRM_API3_Exception $e) { } catch (CiviCRM_API3_Exception $e) {
Civi::log()->error( Civi::log()->error(
$e->getMessage(), $e->getMessage(),

View file

@ -65,10 +65,5 @@ function civicrm_api3_twingle_form_Getsingle(array $params): array {
if ($count != 1){ if ($count != 1){
return civicrm_api3_create_error("Expected one TwingleForm but found $count"); return civicrm_api3_create_error("Expected one TwingleForm but found $count");
} }
return civicrm_api3_create_success( return $returnValues['values'][$returnValues['id']];
$returnValues['values'][$returnValues['id']],
$params,
'TwingleForm',
'Getsingle'
);
} }

View file

@ -82,9 +82,9 @@ function civicrm_api3_twingle_project_Create(array $params): array {
$result = civicrm_api3('TwingleProject', 'getsingle', $result = civicrm_api3('TwingleProject', 'getsingle',
['id' => $params['id']] ['id' => $params['id']]
); );
$result['values']['id'] = $result['values']['project_id']; $result['id'] = $result['values']['project_id'];
unset($result['values']['project_id']); unset($result['project_id']);
$project = new TwingleProject($result['values'], $params['id']); $project = new TwingleProject($result, $params['id']);
unset($params['id']); unset($params['id']);
$project->update($params); $project->update($params);
$project->setEmbedData($params); $project->setEmbedData($params);

View file

@ -89,7 +89,7 @@ function civicrm_api3_twingle_project_Delete(array $params): array {
if (!$params['project_id']) { if (!$params['project_id']) {
try { try {
$project = civicrm_api3('TwingleProject', 'getsingle', $params); $project = civicrm_api3('TwingleProject', 'getsingle', $params);
$params['project_id'] = $project['values']['project_id']; $params['project_id'] = $project['project_id'];
} catch (CiviCRM_API3_Exception $e) { } catch (CiviCRM_API3_Exception $e) {
$result_values['twingle'] = 'Could not delete TwingleProject: ' . $e->getMessage(); $result_values['twingle'] = 'Could not delete TwingleProject: ' . $e->getMessage();
} }
@ -119,17 +119,13 @@ function civicrm_api3_twingle_project_Delete(array $params): array {
} }
} }
// Delete the TwingleProject campaign on CiviCRM's side // Delete the TwingleProject campaign on CiviCRM's side
try { try {
$project = civicrm_api3('TwingleProject', 'getsingle', $params); $project = civicrm_api3('TwingleProject', 'getsingle', $params);
// The TwingleProject campaign may be already deleted // The TwingleProject campaign may be already deleted
if ($project['is_error'] == 0) { $project = new TwingleProject($project, $project['id']);
$project = new TwingleProject($project['values'], $project['values']['id']);
$project->delete(); $project->delete();
$result_values['civicrm'] = 'TwingleProject deleted'; $result_values['civicrm'] = 'TwingleProject deleted';
}
// If deletion fails // If deletion fails
} catch (Exception $e) { } catch (Exception $e) {
$error_occurred = TRUE; $error_occurred = TRUE;

View file

@ -126,9 +126,5 @@ function civicrm_api3_twingle_project_Getsingle(array $params): array {
"Expected one TwingleProject but found $count" "Expected one TwingleProject but found $count"
); );
} }
return civicrm_api3_create_success( return $returnValues['values'][$returnValues['id']];
$returnValues['values'][$returnValues['id']],
$params, 'TwingleProject',
'Getsingle'
);
} }

View file

@ -96,17 +96,16 @@ function civicrm_api3_twingle_project_Sync(array $params): array {
// Get project from db via API // Get project from db via API
$params['sequential'] = 1; $params['sequential'] = 1;
$result = civicrm_api3('TwingleProject', 'getsingle', $params); $result = civicrm_api3('TwingleProject', 'getsingle', $params);
if ($result['is_error'] == 0) {
// If the TwingleProject campaign already has a project_id try to get the // If the TwingleProject campaign already has a project_id try to get the
// project from Twingle // project from Twingle
if ($result['values']['project_id']) { if ($result['project_id']) {
$project_from_twingle = $twingleApi->getProject($result['values']['project_id']); $project_from_twingle = $twingleApi->getProject($result['project_id']);
// instantiate project from CiviCRM // instantiate project from CiviCRM
$id = $result['values']['id']; $id = $result['id'];
unset($result['values']['id']); unset($result['id']);
$project = new TwingleProject($result['values'], $id); $project = new TwingleProject($result, $id);
// Synchronize projects // Synchronize projects
if (!empty($project_from_twingle)) { if (!empty($project_from_twingle)) {
@ -121,35 +120,22 @@ function civicrm_api3_twingle_project_Sync(array $params): array {
); );
} }
} }
// If the TwingleProject campaign does not have a project_id, push it to // If the TwingleProject campaign does not have a project_id, push it to
// Twingle and update it with the returning values // Twingle and update it with the returning values
else { else {
// store campaign id in $id // store campaign id in $id
$id = $result['values']['id']; $id = $result['id'];
unset($result['values']['id']); unset($result['id']);
// instantiate project // instantiate project
$project = new TwingleProject($result['values'], $id); $project = new TwingleProject($result, $id);
// Push project to Twingle // Push project to Twingle
return _pushProjectToTwingle($project, $twingleApi, $params); return _pushProjectToTwingle($project, $twingleApi, $params);
} }
} }
// If the project could not get retrieved from TwingleProject.getsingle,
// forward API error message
else {
Civi::log()->error(
E::LONG_NAME .
' could retrieve project from TwingleProject.getsingle',
$result
);
return $result;
}
}
// If no id or project_id is given, synchronize all projects // If no id or project_id is given, synchronize all projects
else { else {