✅️ meet standard for getsingle api response
return array with object values instead of api success response
This commit is contained in:
parent
88a77456b8
commit
d904ae931f
11 changed files with 59 additions and 95 deletions
|
@ -79,7 +79,7 @@ class CRM_TwingleCampaign_BAO_TwingleCampaign {
|
|||
*/
|
||||
public function fetch() {
|
||||
$this->values = civicrm_api3('TwingleCampaign', 'getsingle',
|
||||
['id' => $this->id])['values'];
|
||||
['id' => $this->id]);
|
||||
}
|
||||
|
||||
|
||||
|
@ -105,7 +105,7 @@ class CRM_TwingleCampaign_BAO_TwingleCampaign {
|
|||
'TwingleCampaign',
|
||||
'getsingle',
|
||||
['id' => $this->id]
|
||||
)['values']['parent_id'];
|
||||
)['parent_id'];
|
||||
|
||||
$parent_campaign_type_id = NULL;
|
||||
|
||||
|
|
|
@ -48,7 +48,7 @@ class CRM_TwingleCampaign_Utils_APIWrapper {
|
|||
'TwingleEvent',
|
||||
'getsingle',
|
||||
['id' => $contribution['campaign_id']]
|
||||
)['values'];
|
||||
);
|
||||
$response['values']['soft_credit'] =
|
||||
self::createSoftCredit($contribution, $twingle_event)['values'];
|
||||
$event->setResponse($response);
|
||||
|
@ -83,7 +83,7 @@ class CRM_TwingleCampaign_Utils_APIWrapper {
|
|||
'TwingleEvent',
|
||||
'getsingle',
|
||||
['id' => $contribution['contribution_campaign_id']]
|
||||
)['values'];
|
||||
);
|
||||
$response['values']['soft_credit'] =
|
||||
self::createSoftCredit($contribution, $twingle_event)['values'];
|
||||
$event->setResponse($response);
|
||||
|
@ -115,7 +115,7 @@ class CRM_TwingleCampaign_Utils_APIWrapper {
|
|||
$apiRequest['params'])
|
||||
) {
|
||||
if (is_numeric($apiRequest['params']['campaign_id'])) {
|
||||
$targetCampaign['values']['id'] = $apiRequest['params']['campaign_id'];
|
||||
$targetCampaign['id'] = $apiRequest['params']['campaign_id'];
|
||||
}
|
||||
else {
|
||||
try {
|
||||
|
@ -157,7 +157,7 @@ class CRM_TwingleCampaign_Utils_APIWrapper {
|
|||
}
|
||||
|
||||
if (isset($targetCampaign)) {
|
||||
$apiRequest['params']['campaign_id'] = $targetCampaign['values']['id'];
|
||||
$apiRequest['params']['campaign_id'] = $targetCampaign['id'];
|
||||
}
|
||||
|
||||
return $callsame($apiRequest);
|
||||
|
|
|
@ -99,9 +99,5 @@ function civicrm_api3_twingle_campaign_Getsingle(array $params): array {
|
|||
"Expected one TwingleCampaign but found $count"
|
||||
);
|
||||
}
|
||||
return civicrm_api3_create_success(
|
||||
$returnValues['values'][$returnValues['id']],
|
||||
$params, 'TwingleCampaign',
|
||||
'Getsingle'
|
||||
);
|
||||
return $returnValues['values'][$returnValues['id']];
|
||||
}
|
||||
|
|
|
@ -83,11 +83,11 @@ function civicrm_api3_twingle_event_Delete(array $params): array {
|
|||
// provided parameters
|
||||
if ($params['id']) {
|
||||
$events[] = civicrm_api3('TwingleEvent', 'getsingle',
|
||||
[$params['id']])['values'];
|
||||
[$params['id']]);
|
||||
}
|
||||
elseif ($params['event_id']) {
|
||||
$events[] = civicrm_api3('TwingleEvent', 'getsingle',
|
||||
[$params['event_id']])['values'];
|
||||
[$params['event_id']]);
|
||||
}
|
||||
elseif ($params['project_id']) {
|
||||
$events = civicrm_api3('TwingleEvent', 'get',
|
||||
|
|
|
@ -110,10 +110,5 @@ function civicrm_api3_twingle_event_Getsingle(array $params): array {
|
|||
if ($count != 1) {
|
||||
return civicrm_api3_create_error("Expected one TwingleEvent but found $count");
|
||||
}
|
||||
return civicrm_api3_create_success(
|
||||
$returnValues['values'][$returnValues['id']],
|
||||
$params,
|
||||
'TwingleEvent',
|
||||
'Getsingle'
|
||||
);
|
||||
return $returnValues['values'][$returnValues['id']];
|
||||
}
|
||||
|
|
|
@ -120,15 +120,15 @@ function civicrm_api3_twingle_event_Sync(array $params): array {
|
|||
if ($result['is_error'] == 0) {
|
||||
|
||||
// Get the event from Twingle
|
||||
if ($result['values']['event_id']) {
|
||||
if ($result['event_id']) {
|
||||
$event_from_twingle = $twingleApi->getEvent(
|
||||
$result['values']['project_id'],
|
||||
$result['values']['event_id']
|
||||
$result['project_id'],
|
||||
$result['event_id']
|
||||
);
|
||||
|
||||
// instantiate event from CiviCRM
|
||||
try {
|
||||
$event = _instantiateEvent($result['values']);
|
||||
$event = _instantiateEvent($result);
|
||||
} catch (CiviCRM_API3_Exception $e) {
|
||||
Civi::log()->error(
|
||||
$e->getMessage(),
|
||||
|
|
|
@ -65,10 +65,5 @@ function civicrm_api3_twingle_form_Getsingle(array $params): array {
|
|||
if ($count != 1){
|
||||
return civicrm_api3_create_error("Expected one TwingleForm but found $count");
|
||||
}
|
||||
return civicrm_api3_create_success(
|
||||
$returnValues['values'][$returnValues['id']],
|
||||
$params,
|
||||
'TwingleForm',
|
||||
'Getsingle'
|
||||
);
|
||||
return $returnValues['values'][$returnValues['id']];
|
||||
}
|
||||
|
|
|
@ -82,9 +82,9 @@ function civicrm_api3_twingle_project_Create(array $params): array {
|
|||
$result = civicrm_api3('TwingleProject', 'getsingle',
|
||||
['id' => $params['id']]
|
||||
);
|
||||
$result['values']['id'] = $result['values']['project_id'];
|
||||
unset($result['values']['project_id']);
|
||||
$project = new TwingleProject($result['values'], $params['id']);
|
||||
$result['id'] = $result['values']['project_id'];
|
||||
unset($result['project_id']);
|
||||
$project = new TwingleProject($result, $params['id']);
|
||||
unset($params['id']);
|
||||
$project->update($params);
|
||||
$project->setEmbedData($params);
|
||||
|
|
|
@ -89,7 +89,7 @@ function civicrm_api3_twingle_project_Delete(array $params): array {
|
|||
if (!$params['project_id']) {
|
||||
try {
|
||||
$project = civicrm_api3('TwingleProject', 'getsingle', $params);
|
||||
$params['project_id'] = $project['values']['project_id'];
|
||||
$params['project_id'] = $project['project_id'];
|
||||
} catch (CiviCRM_API3_Exception $e) {
|
||||
$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
|
||||
try {
|
||||
$project = civicrm_api3('TwingleProject', 'getsingle', $params);
|
||||
// The TwingleProject campaign may be already deleted
|
||||
if ($project['is_error'] == 0) {
|
||||
$project = new TwingleProject($project['values'], $project['values']['id']);
|
||||
$project->delete();
|
||||
$result_values['civicrm'] = 'TwingleProject deleted';
|
||||
}
|
||||
$project = new TwingleProject($project, $project['id']);
|
||||
$project->delete();
|
||||
$result_values['civicrm'] = 'TwingleProject deleted';
|
||||
// If deletion fails
|
||||
} catch (Exception $e) {
|
||||
$error_occurred = TRUE;
|
||||
|
|
|
@ -126,9 +126,5 @@ function civicrm_api3_twingle_project_Getsingle(array $params): array {
|
|||
"Expected one TwingleProject but found $count"
|
||||
);
|
||||
}
|
||||
return civicrm_api3_create_success(
|
||||
$returnValues['values'][$returnValues['id']],
|
||||
$params, 'TwingleProject',
|
||||
'Getsingle'
|
||||
);
|
||||
return $returnValues['values'][$returnValues['id']];
|
||||
}
|
||||
|
|
|
@ -96,57 +96,43 @@ function civicrm_api3_twingle_project_Sync(array $params): array {
|
|||
// Get project from db via API
|
||||
$params['sequential'] = 1;
|
||||
$result = civicrm_api3('TwingleProject', 'getsingle', $params);
|
||||
if ($result['is_error'] == 0) {
|
||||
|
||||
// If the TwingleProject campaign already has a project_id try to get the
|
||||
// project from Twingle
|
||||
if ($result['values']['project_id']) {
|
||||
$project_from_twingle = $twingleApi->getProject($result['values']['project_id']);
|
||||
// If the TwingleProject campaign already has a project_id try to get the
|
||||
// project from Twingle
|
||||
if ($result['project_id']) {
|
||||
$project_from_twingle = $twingleApi->getProject($result['project_id']);
|
||||
|
||||
// instantiate project from CiviCRM
|
||||
$id = $result['values']['id'];
|
||||
unset($result['values']['id']);
|
||||
$project = new TwingleProject($result['values'], $id);
|
||||
// instantiate project from CiviCRM
|
||||
$id = $result['id'];
|
||||
unset($result['id']);
|
||||
$project = new TwingleProject($result, $id);
|
||||
|
||||
// Synchronize projects
|
||||
if (!empty($project_from_twingle)) {
|
||||
return _projectSync($project, $project_from_twingle, $twingleApi, $params);
|
||||
}
|
||||
|
||||
// If Twingle does not know a project with the given project_id, give error
|
||||
else {
|
||||
return civicrm_api3_create_error(
|
||||
"The project_id appears to be unknown to Twingle",
|
||||
$project->getResponse()
|
||||
);
|
||||
}
|
||||
// Synchronize projects
|
||||
if (!empty($project_from_twingle)) {
|
||||
return _projectSync($project, $project_from_twingle, $twingleApi, $params);
|
||||
}
|
||||
|
||||
// If the TwingleProject campaign does not have a project_id, push it to
|
||||
// Twingle and update it with the returning values
|
||||
// If Twingle does not know a project with the given project_id, give error
|
||||
else {
|
||||
|
||||
// store campaign id in $id
|
||||
$id = $result['values']['id'];
|
||||
unset($result['values']['id']);
|
||||
|
||||
// instantiate project
|
||||
$project = new TwingleProject($result['values'], $id);
|
||||
|
||||
// Push project to Twingle
|
||||
return _pushProjectToTwingle($project, $twingleApi, $params);
|
||||
return civicrm_api3_create_error(
|
||||
"The project_id appears to be unknown to Twingle",
|
||||
$project->getResponse()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// If the project could not get retrieved from TwingleProject.getsingle,
|
||||
// forward API error message
|
||||
// If the TwingleProject campaign does not have a project_id, push it to
|
||||
// Twingle and update it with the returning values
|
||||
else {
|
||||
Civi::log()->error(
|
||||
E::LONG_NAME .
|
||||
' could retrieve project from TwingleProject.getsingle',
|
||||
$result
|
||||
);
|
||||
return $result;
|
||||
|
||||
// store campaign id in $id
|
||||
$id = $result['id'];
|
||||
unset($result['id']);
|
||||
|
||||
// instantiate project
|
||||
$project = new TwingleProject($result, $id);
|
||||
|
||||
// Push project to Twingle
|
||||
return _pushProjectToTwingle($project, $twingleApi, $params);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -292,9 +278,9 @@ function civicrm_api3_twingle_project_Sync(array $params): array {
|
|||
* @return array
|
||||
*/
|
||||
function _updateProjectLocally(array $project_from_twingle,
|
||||
TwingleProject $project,
|
||||
array $params,
|
||||
TwingleApiCall $twingleApi): array {
|
||||
TwingleProject $project,
|
||||
array $params,
|
||||
TwingleApiCall $twingleApi): array {
|
||||
|
||||
try {
|
||||
$project->update($project_from_twingle);
|
||||
|
@ -345,8 +331,8 @@ function _updateProjectLocally(array $project_from_twingle,
|
|||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
function _pushProjectToTwingle(TwingleProject $project,
|
||||
TwingleApiCall $twingleApi,
|
||||
array $params): array {
|
||||
TwingleApiCall $twingleApi,
|
||||
array $params): array {
|
||||
|
||||
// If this is a test, do not make db changes
|
||||
if ($params['is_test']) {
|
||||
|
@ -433,9 +419,9 @@ function _pushProjectToTwingle(TwingleProject $project,
|
|||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
function _projectSync(TwingleProject $project,
|
||||
array $project_from_twingle,
|
||||
TwingleApiCall $twingleApi,
|
||||
array $params): array {
|
||||
array $project_from_twingle,
|
||||
TwingleApiCall $twingleApi,
|
||||
array $params): array {
|
||||
|
||||
// If Twingle's version of the project is newer than the CiviCRM
|
||||
// TwingleProject campaign, update the campaign
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue