✅️ 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() {
|
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;
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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'
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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',
|
||||||
|
|
|
@ -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'
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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(),
|
||||||
|
|
|
@ -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'
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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'
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -96,57 +96,43 @@ 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)) {
|
||||||
return _projectSync($project, $project_from_twingle, $twingleApi, $params);
|
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()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the TwingleProject campaign does not have a project_id, push it to
|
// If Twingle does not know a project with the given project_id, give error
|
||||||
// Twingle and update it with the returning values
|
|
||||||
else {
|
else {
|
||||||
|
return civicrm_api3_create_error(
|
||||||
// store campaign id in $id
|
"The project_id appears to be unknown to Twingle",
|
||||||
$id = $result['values']['id'];
|
$project->getResponse()
|
||||||
unset($result['values']['id']);
|
);
|
||||||
|
|
||||||
// instantiate project
|
|
||||||
$project = new TwingleProject($result['values'], $id);
|
|
||||||
|
|
||||||
// Push project to Twingle
|
|
||||||
return _pushProjectToTwingle($project, $twingleApi, $params);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// If the TwingleProject campaign does not have a project_id, push it to
|
||||||
// If the project could not get retrieved from TwingleProject.getsingle,
|
// Twingle and update it with the returning values
|
||||||
// forward API error message
|
|
||||||
else {
|
else {
|
||||||
Civi::log()->error(
|
|
||||||
E::LONG_NAME .
|
// store campaign id in $id
|
||||||
' could retrieve project from TwingleProject.getsingle',
|
$id = $result['id'];
|
||||||
$result
|
unset($result['id']);
|
||||||
);
|
|
||||||
return $result;
|
// 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
|
* @return array
|
||||||
*/
|
*/
|
||||||
function _updateProjectLocally(array $project_from_twingle,
|
function _updateProjectLocally(array $project_from_twingle,
|
||||||
TwingleProject $project,
|
TwingleProject $project,
|
||||||
array $params,
|
array $params,
|
||||||
TwingleApiCall $twingleApi): array {
|
TwingleApiCall $twingleApi): array {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$project->update($project_from_twingle);
|
$project->update($project_from_twingle);
|
||||||
|
@ -345,8 +331,8 @@ function _updateProjectLocally(array $project_from_twingle,
|
||||||
* @throws \CiviCRM_API3_Exception
|
* @throws \CiviCRM_API3_Exception
|
||||||
*/
|
*/
|
||||||
function _pushProjectToTwingle(TwingleProject $project,
|
function _pushProjectToTwingle(TwingleProject $project,
|
||||||
TwingleApiCall $twingleApi,
|
TwingleApiCall $twingleApi,
|
||||||
array $params): array {
|
array $params): array {
|
||||||
|
|
||||||
// If this is a test, do not make db changes
|
// If this is a test, do not make db changes
|
||||||
if ($params['is_test']) {
|
if ($params['is_test']) {
|
||||||
|
@ -433,9 +419,9 @@ function _pushProjectToTwingle(TwingleProject $project,
|
||||||
* @throws \CiviCRM_API3_Exception
|
* @throws \CiviCRM_API3_Exception
|
||||||
*/
|
*/
|
||||||
function _projectSync(TwingleProject $project,
|
function _projectSync(TwingleProject $project,
|
||||||
array $project_from_twingle,
|
array $project_from_twingle,
|
||||||
TwingleApiCall $twingleApi,
|
TwingleApiCall $twingleApi,
|
||||||
array $params): array {
|
array $params): array {
|
||||||
|
|
||||||
// If Twingle's version of the project is newer than the CiviCRM
|
// If Twingle's version of the project is newer than the CiviCRM
|
||||||
// TwingleProject campaign, update the campaign
|
// TwingleProject campaign, update the campaign
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue