fix TwingleProject.sync
This commit is contained in:
commit
5e0a21e365
2 changed files with 29 additions and 34 deletions
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
use CRM_TwingleCampaign_Utils_ExtensionCache as Cache;
|
use CRM_TwingleCampaign_Utils_ExtensionCache as Cache;
|
||||||
use CRM_TwingleCampaign_BAO_Campaign as Campaign;
|
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 {
|
class CRM_TwingleCampaign_BAO_TwingleProject extends Campaign {
|
||||||
|
@ -14,14 +12,12 @@ class CRM_TwingleCampaign_BAO_TwingleProject extends Campaign {
|
||||||
* @param array $project
|
* @param array $project
|
||||||
* Result array of Twingle API call to
|
* Result array of Twingle API call to
|
||||||
* https://project.twingle.de/api/by-organisation/$organisation_id
|
* https://project.twingle.de/api/by-organisation/$organisation_id
|
||||||
*
|
|
||||||
* @param int|null $id
|
* @param int|null $id
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public function __construct(array $project, int $id = NULL) {
|
public function __construct(array $project, int $id = NULL) {
|
||||||
parent::__construct($project);
|
parent::__construct($project, $id);
|
||||||
|
|
||||||
$this->id = $id;
|
|
||||||
$this->prefix = 'twingle_project_';
|
$this->prefix = 'twingle_project_';
|
||||||
$this->values['campaign_type_id'] = 'twingle_project';
|
$this->values['campaign_type_id'] = 'twingle_project';
|
||||||
$this->id_custom_field = Cache::getInstance()
|
$this->id_custom_field = Cache::getInstance()
|
||||||
|
@ -67,7 +63,6 @@ class CRM_TwingleCampaign_BAO_TwingleProject extends Campaign {
|
||||||
* @return bool
|
* @return bool
|
||||||
* Returns a boolean
|
* Returns a boolean
|
||||||
*
|
*
|
||||||
* @throws \CiviCRM_API3_Exception
|
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public function create(bool $no_hook = FALSE): bool {
|
public function create(bool $no_hook = FALSE): bool {
|
||||||
|
@ -106,8 +101,7 @@ class CRM_TwingleCampaign_BAO_TwingleProject extends Campaign {
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$errorMessage = $result['error_message'];
|
throw new Exception($result['error_message']);
|
||||||
throw new Exception("$errorMessage");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -230,8 +224,8 @@ class CRM_TwingleCampaign_BAO_TwingleProject extends Campaign {
|
||||||
/**
|
/**
|
||||||
* Get a response that describes the status of a TwingleProject
|
* Get a response that describes the status of a TwingleProject
|
||||||
*
|
*
|
||||||
* @param string $status
|
* @param string|null $status
|
||||||
* status of the TwingleProject you want the response for
|
* status of the TwingleProject you want to give back along with the response
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
* Returns a response array that contains title, id, project_id and status
|
* Returns a response array that contains title, id, project_id and status
|
||||||
|
|
|
@ -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
|
* @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_civicrm['values'] as $project_from_civicrm) {
|
||||||
foreach ($projects_from_twingle as $project_from_twingle) {
|
foreach ($projects_from_twingle as $project_from_twingle) {
|
||||||
if ($project_from_twingle['id'] == $project_from_civicrm['project_id']) {
|
if ($project_from_twingle['id'] == $project_from_civicrm['project_id']) {
|
||||||
|
|
||||||
// store campaign id in $id
|
// store campaign id in $id
|
||||||
$id = $project_from_civicrm['id'];
|
$id = $project_from_civicrm['id'];
|
||||||
unset($project_from_civicrm['id']);
|
unset($project_from_civicrm['id']);
|
||||||
|
|
||||||
// instantiate project with values from TwingleProject.Get
|
// instantiate project with values from TwingleProject.Get
|
||||||
$project = new TwingleProject($project_from_civicrm, $id);
|
$project = new TwingleProject($project_from_civicrm, $id);
|
||||||
|
|
||||||
// sync project
|
// sync project
|
||||||
$result = sync($project, $project_from_twingle, $twingleApi, $params);
|
$result = sync($project, $project_from_twingle, $twingleApi, $params);
|
||||||
if ($result['is_error'] != 0) {
|
if ($result['is_error'] != 0) {
|
||||||
$errors_occurred++;
|
$errors_occurred++;
|
||||||
$result_values[$project->getId()] =
|
$result_values[$project->getId()] =
|
||||||
$project->getResponse($result['error_message']);
|
$project->getResponse($result['error_message']);
|
||||||
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$result_values[$project->getId()] = $result['values'];
|
$result_values[$project->getId()] = $result['values'][0];
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -291,7 +302,6 @@ function updateLocally(array $project_from_twingle,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
// ... else, update local TwingleProject campaign
|
// ... else, update local TwingleProject campaign
|
||||||
try {
|
|
||||||
$project->create(TRUE);
|
$project->create(TRUE);
|
||||||
return civicrm_api3_create_success(
|
return civicrm_api3_create_success(
|
||||||
$project->getResponse('TwingleProject updated successfully'),
|
$project->getResponse('TwingleProject updated successfully'),
|
||||||
|
@ -302,19 +312,9 @@ function updateLocally(array $project_from_twingle,
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
$errorMessage = $e->getMessage();
|
$errorMessage = $e->getMessage();
|
||||||
Civi::log()->error(
|
Civi::log()->error(
|
||||||
"$extensionName could not update TwingleProject: $errorMessage",
|
"$extensionName could not update TwingleProject campaign: $errorMessage",
|
||||||
$project->getResponse()
|
$project->getResponse()
|
||||||
);
|
);
|
||||||
return civicrm_api3_create_error(
|
|
||||||
"TwingleProject could not get updated: $errorMessage",
|
|
||||||
$project->getResponse()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} catch (Exception $e) {
|
|
||||||
$errorMessage = $e->getMessage();
|
|
||||||
Civi::log()->error(
|
|
||||||
"$extensionName could not update TwingleProject campaign: $errorMessage"
|
|
||||||
);
|
|
||||||
return civicrm_api3_create_error(
|
return civicrm_api3_create_error(
|
||||||
"Could not update TwingleProject campaign: $errorMessage",
|
"Could not update TwingleProject campaign: $errorMessage",
|
||||||
$project->getResponse()
|
$project->getResponse()
|
||||||
|
@ -437,8 +437,9 @@ function sync(TwingleProject $project,
|
||||||
|
|
||||||
// If both versions are still synchronized
|
// If both versions are still synchronized
|
||||||
else {
|
else {
|
||||||
|
$response[] = $project->getResponse('TwingleProject up to date');
|
||||||
return civicrm_api3_create_success(
|
return civicrm_api3_create_success(
|
||||||
$project->getResponse('TwingleProject up to date'),
|
$response,
|
||||||
$params,
|
$params,
|
||||||
'TwingleProject',
|
'TwingleProject',
|
||||||
'Sync'
|
'Sync'
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue