fix TwingleProject.sync

This commit is contained in:
Marc Michalsky forumZFD 2021-01-29 17:47:05 +01:00
commit 5e0a21e365
Signed by untrusted user who does not match committer: marc.koch
GPG key ID: 12406554CFB028B9
2 changed files with 29 additions and 34 deletions

View file

@ -2,8 +2,6 @@
use CRM_TwingleCampaign_Utils_ExtensionCache as Cache;
use CRM_TwingleCampaign_BAO_Campaign as Campaign;
use CRM_TwingleCampaign_ExtensionUtil as E;
use CRM_TwingleCampaign_BAO_TwingleApiCall as TwingleApiCall;
class CRM_TwingleCampaign_BAO_TwingleProject extends Campaign {
@ -14,14 +12,12 @@ class CRM_TwingleCampaign_BAO_TwingleProject extends Campaign {
* @param array $project
* Result array of Twingle API call to
* https://project.twingle.de/api/by-organisation/$organisation_id
*
* @param int|null $id
*
*/
public function __construct(array $project, int $id = NULL) {
parent::__construct($project);
parent::__construct($project, $id);
$this->id = $id;
$this->prefix = 'twingle_project_';
$this->values['campaign_type_id'] = 'twingle_project';
$this->id_custom_field = Cache::getInstance()
@ -67,7 +63,6 @@ class CRM_TwingleCampaign_BAO_TwingleProject extends Campaign {
* @return bool
* Returns a boolean
*
* @throws \CiviCRM_API3_Exception
* @throws \Exception
*/
public function create(bool $no_hook = FALSE): bool {
@ -106,8 +101,7 @@ class CRM_TwingleCampaign_BAO_TwingleProject extends Campaign {
return TRUE;
}
else {
$errorMessage = $result['error_message'];
throw new Exception("$errorMessage");
throw new Exception($result['error_message']);
}
}
@ -230,8 +224,8 @@ class CRM_TwingleCampaign_BAO_TwingleProject extends Campaign {
/**
* Get a response that describes the status of a TwingleProject
*
* @param string $status
* status of the TwingleProject you want the response for
* @param string|null $status
* status of the TwingleProject you want to give back along with the response
*
* @return array
* Returns a response array that contains title, id, project_id and status

View file

@ -46,7 +46,16 @@ function _civicrm_api3_twingle_project_Sync_spec(array &$spec) {
/**
* TwingleProject.Sync API
* # TwingleProject.Sync API
*
* Synchronize one ore more campaigns of the type TwingleProject between CiviCRM
* and Twingle.
*
* * If you pass an **id** or **project_id** parameter, *only one project* will
* be synchronized.
*
* * If you pass no **id** or **project_id** parameter, *all projects* will be
* synchronized.
*
* @param array $params
*
@ -215,21 +224,23 @@ function civicrm_api3_twingle_project_Sync(array $params): array {
foreach ($projects_from_civicrm['values'] as $project_from_civicrm) {
foreach ($projects_from_twingle as $project_from_twingle) {
if ($project_from_twingle['id'] == $project_from_civicrm['project_id']) {
// store campaign id in $id
$id = $project_from_civicrm['id'];
unset($project_from_civicrm['id']);
// instantiate project with values from TwingleProject.Get
$project = new TwingleProject($project_from_civicrm, $id);
// sync project
$result = sync($project, $project_from_twingle, $twingleApi, $params);
if ($result['is_error'] != 0) {
$errors_occurred++;
$result_values[$project->getId()] =
$project->getResponse($result['error_message']);
}
else {
$result_values[$project->getId()] = $result['values'];
$result_values[$project->getId()] = $result['values'][0];
}
break;
}
@ -291,29 +302,18 @@ function updateLocally(array $project_from_twingle,
);
}
// ... else, update local TwingleProject campaign
try {
$project->create(TRUE);
return civicrm_api3_create_success(
$project->getResponse('TwingleProject updated successfully'),
$params,
'TwingleProject',
'Sync'
);
} catch (Exception $e) {
$errorMessage = $e->getMessage();
Civi::log()->error(
"$extensionName could not update TwingleProject: $errorMessage",
$project->getResponse()
);
return civicrm_api3_create_error(
"TwingleProject could not get updated: $errorMessage",
$project->getResponse()
);
}
$project->create(TRUE);
return civicrm_api3_create_success(
$project->getResponse('TwingleProject updated successfully'),
$params,
'TwingleProject',
'Sync'
);
} catch (Exception $e) {
$errorMessage = $e->getMessage();
Civi::log()->error(
"$extensionName could not update TwingleProject campaign: $errorMessage"
"$extensionName could not update TwingleProject campaign: $errorMessage",
$project->getResponse()
);
return civicrm_api3_create_error(
"Could not update TwingleProject campaign: $errorMessage",
@ -437,8 +437,9 @@ function sync(TwingleProject $project,
// If both versions are still synchronized
else {
$response[] = $project->getResponse('TwingleProject up to date');
return civicrm_api3_create_success(
$project->getResponse('TwingleProject up to date'),
$response,
$params,
'TwingleProject',
'Sync'