create TwingleProjecr.sync api
This commit is contained in:
commit
7c8d70327d
5 changed files with 649 additions and 277 deletions
|
@ -1,6 +1,8 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use CRM_TwingleCampaign_BAO_TwingleProject as TwingleProject;
|
use CRM_TwingleCampaign_BAO_TwingleProject as TwingleProject;
|
||||||
|
use CRM_TwingleCampaign_ExtensionUtil as E;
|
||||||
|
|
||||||
|
|
||||||
class CRM_TwingleCampaign_BAO_TwingleApiCall {
|
class CRM_TwingleCampaign_BAO_TwingleApiCall {
|
||||||
|
|
||||||
|
@ -14,6 +16,8 @@ class CRM_TwingleCampaign_BAO_TwingleApiCall {
|
||||||
|
|
||||||
private $limit;
|
private $limit;
|
||||||
|
|
||||||
|
private $extensionName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TwingleApiCall constructor.
|
* TwingleApiCall constructor.
|
||||||
*
|
*
|
||||||
|
@ -25,30 +29,39 @@ class CRM_TwingleCampaign_BAO_TwingleApiCall {
|
||||||
*
|
*
|
||||||
* @throws API_Exception
|
* @throws API_Exception
|
||||||
*/
|
*/
|
||||||
public function __construct(string $apiKey, int $limit) {
|
public function __construct(string $apiKey, int $limit = 20) {
|
||||||
$this->apiKey = $apiKey;
|
$this->apiKey = $apiKey;
|
||||||
$this->limit = $limit;
|
$this->limit = $limit;
|
||||||
|
$this->extensionName = E::LONG_NAME;
|
||||||
|
|
||||||
// Get organisation id
|
// Try to retrieve organisation id from cache
|
||||||
$curl = curl_init($this->protocol . 'organisation' . $this->baseUrl);
|
$this->organisationId = Civi::cache()
|
||||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
|
->get('twinglecampaign_organisation_id');
|
||||||
curl_setopt($curl, CURLOPT_HTTPHEADER, [
|
|
||||||
"x-access-code: $apiKey",
|
|
||||||
'Content-Type: application/json',
|
|
||||||
]);
|
|
||||||
|
|
||||||
$response = json_decode(curl_exec($curl), TRUE);
|
// else: retrieve organisation id via Twingle api
|
||||||
curl_close($curl);
|
if (NULL === $this->organisationId) {
|
||||||
|
|
||||||
if (empty($response)) {
|
$curl = curl_init($this->protocol . 'organisation' . $this->baseUrl);
|
||||||
// Delete api key from cache
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
|
||||||
Civi::cache()->delete('twinglecampaign_twingle_api');
|
curl_setopt($curl, CURLOPT_HTTPHEADER, [
|
||||||
// Throw exception
|
"x-access-code: $apiKey",
|
||||||
throw new API_Exception(
|
'Content-Type: application/json',
|
||||||
"Twingle API call failed. Please check your api key.");
|
]);
|
||||||
|
|
||||||
|
$response = json_decode(curl_exec($curl), TRUE);
|
||||||
|
curl_close($curl);
|
||||||
|
|
||||||
|
if (empty($response)) {
|
||||||
|
// Delete api key from cache
|
||||||
|
Civi::cache()->delete('twinglecampaign_twingle_api');
|
||||||
|
// Throw exception
|
||||||
|
throw new API_Exception(
|
||||||
|
"Twingle API call failed. Please check your api key.");
|
||||||
|
}
|
||||||
|
$this->organisationId = array_column($response, 'id')[0];
|
||||||
|
Civi::cache('long')->set('twinglecampaign_organisation_id',
|
||||||
|
$this->organisationId);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->organisationId = array_column($response, 'id')[0];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -60,10 +73,8 @@ class CRM_TwingleCampaign_BAO_TwingleApiCall {
|
||||||
* @param int|null $projectId
|
* @param int|null $projectId
|
||||||
*
|
*
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws \Exception
|
|
||||||
*/
|
*/
|
||||||
public function getProject(int $projectId = NULL) {
|
public function getProject(int $projectId = NULL) {
|
||||||
$response = [];
|
|
||||||
|
|
||||||
$url = empty($projectId)
|
$url = empty($projectId)
|
||||||
? $this->protocol . 'project' . $this->baseUrl . 'by-organisation/' . $this->organisationId
|
? $this->protocol . 'project' . $this->baseUrl . 'by-organisation/' . $this->organisationId
|
||||||
|
@ -81,22 +92,11 @@ class CRM_TwingleCampaign_BAO_TwingleApiCall {
|
||||||
*
|
*
|
||||||
* @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
|
||||||
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public function pushProject(TwingleProject &$project) {
|
public function pushProject(TwingleProject &$project) {
|
||||||
|
|
||||||
try {
|
$values = $project->export();
|
||||||
$values = $project->export();
|
|
||||||
} catch (Exception $e) {
|
|
||||||
// Log Exception
|
|
||||||
$errorMessage = $e->getMessage();
|
|
||||||
Civi::log()->error(
|
|
||||||
"Could not export TwingleProject values: $errorMessage"
|
|
||||||
);
|
|
||||||
// Return result array with error description
|
|
||||||
return $project->getResponse(
|
|
||||||
"Could not export TwingleProject values: $errorMessage"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prepare url for curl
|
// Prepare url for curl
|
||||||
if ($values['id']) {
|
if ($values['id']) {
|
||||||
|
@ -121,8 +121,7 @@ class CRM_TwingleCampaign_BAO_TwingleApiCall {
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public
|
public function getEvent($projectId, $eventId = NULL) {
|
||||||
function getEvent($projectId, $eventId = NULL) {
|
|
||||||
$result = [];
|
$result = [];
|
||||||
|
|
||||||
$url = empty($eventId)
|
$url = empty($eventId)
|
||||||
|
@ -144,13 +143,22 @@ class CRM_TwingleCampaign_BAO_TwingleApiCall {
|
||||||
'public' => 0,
|
'public' => 0,
|
||||||
];
|
];
|
||||||
$response = $this->curlGet($url, $params);
|
$response = $this->curlGet($url, $params);
|
||||||
$finished = ($eventId) || count($response['data']) < $this->limit;
|
|
||||||
$offset = $offset + $this->limit;
|
// If no $eventId was given, expect one or more events.
|
||||||
|
// Store the events, increase the offset and ask again until there
|
||||||
|
// are no more events incoming.
|
||||||
if ($response['data'] && !$eventId) {
|
if ($response['data'] && !$eventId) {
|
||||||
$result = array_merge($result, $response['data']);
|
$result = array_merge($result, $response['data']);
|
||||||
|
$offset = $offset + $this->limit;
|
||||||
|
$finished = count($response['data']) < $this->limit;
|
||||||
}
|
}
|
||||||
|
// If $eventId was given, expect only one event
|
||||||
else {
|
else {
|
||||||
$result = $response['data'];
|
// If result the array contains 'message', the $eventId does not exist
|
||||||
|
if (!$result['message']) {
|
||||||
|
$result = $response;
|
||||||
|
}
|
||||||
|
$finished = TRUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $result;
|
return $result;
|
||||||
|
@ -160,9 +168,10 @@ class CRM_TwingleCampaign_BAO_TwingleApiCall {
|
||||||
* @param $projectId
|
* @param $projectId
|
||||||
*
|
*
|
||||||
* @return array|NULL
|
* @return array|NULL
|
||||||
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public
|
public
|
||||||
function getProjectEmbedData($projectId) {
|
function getProjectEmbedData($projectId): array {
|
||||||
|
|
||||||
$result = $this->getProject($projectId);
|
$result = $this->getProject($projectId);
|
||||||
|
|
||||||
|
@ -173,8 +182,9 @@ class CRM_TwingleCampaign_BAO_TwingleApiCall {
|
||||||
return $result['embed'];
|
return $result['embed'];
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Civi::log()->error("Could not get embed data for project $projectId.");
|
throw new Exception(
|
||||||
return NULL;
|
"Could not get embed data for project $projectId."
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,10 @@
|
||||||
|
|
||||||
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;
|
use CRM_TwingleCampaign_BAO_TwingleApiCall as TwingleApiCall;
|
||||||
|
|
||||||
|
|
||||||
class CRM_TwingleCampaign_BAO_TwingleProject extends Campaign {
|
class CRM_TwingleCampaign_BAO_TwingleProject extends Campaign {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -15,9 +17,8 @@ class CRM_TwingleCampaign_BAO_TwingleProject extends Campaign {
|
||||||
*
|
*
|
||||||
* @param int|null $id
|
* @param int|null $id
|
||||||
*
|
*
|
||||||
* @throws \Exception
|
|
||||||
*/
|
*/
|
||||||
function __construct(array $project, int $id = NULL) {
|
public function __construct(array $project, int $id = NULL) {
|
||||||
parent::__construct($project);
|
parent::__construct($project);
|
||||||
|
|
||||||
$this->id = $id;
|
$this->id = $id;
|
||||||
|
@ -27,161 +28,7 @@ class CRM_TwingleCampaign_BAO_TwingleProject extends Campaign {
|
||||||
->getCustomFieldMapping()['twingle_project_id'];
|
->getCustomFieldMapping()['twingle_project_id'];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Synchronizes projects between Twingle and CiviCRM (both directions)
|
|
||||||
* based on the timestamp.
|
|
||||||
*
|
|
||||||
* @param array $values
|
|
||||||
* @param TwingleApiCall $twingleApi
|
|
||||||
* @param bool $is_test
|
|
||||||
* If TRUE, don't do any changes
|
|
||||||
*
|
|
||||||
* @return array|null
|
|
||||||
* Returns a response array that contains title, id, project_id and status or
|
|
||||||
* NULL if $values is not an array
|
|
||||||
*
|
|
||||||
* @throws CiviCRM_API3_Exception
|
|
||||||
*/
|
|
||||||
public static function sync(
|
|
||||||
array $values,
|
|
||||||
TwingleApiCall $twingleApi,
|
|
||||||
bool $is_test = FALSE
|
|
||||||
): ?array {
|
|
||||||
|
|
||||||
// If $values is an array
|
|
||||||
if (is_array($values)) {
|
|
||||||
|
|
||||||
// Instantiate TwingleProject
|
|
||||||
try {
|
|
||||||
$project = new self($values);
|
|
||||||
} catch (Exception $e) {
|
|
||||||
$errorMessage = $e->getMessage();
|
|
||||||
|
|
||||||
// Log Exception
|
|
||||||
Civi::log()->error(
|
|
||||||
"Failed to instantiate TwingleProject: $errorMessage"
|
|
||||||
);
|
|
||||||
|
|
||||||
// Return result array with error description
|
|
||||||
return [
|
|
||||||
"title" => $values['name'],
|
|
||||||
"project_id" => (int) $values['id'],
|
|
||||||
"status" =>
|
|
||||||
"Failed to instantiate TwingleProject: $errorMessage",
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the TwingleProject campaign already exists
|
|
||||||
if (!$project->exists()) {
|
|
||||||
|
|
||||||
// ... if not, get embed data and create project
|
|
||||||
try {
|
|
||||||
$project->setEmbedData(
|
|
||||||
$twingleApi->getProjectEmbedData($project->getProjectId())
|
|
||||||
);
|
|
||||||
$result = $project->create($is_test);
|
|
||||||
} catch (Exception $e) {
|
|
||||||
$errorMessage = $e->getMessage();
|
|
||||||
|
|
||||||
// Log Exception
|
|
||||||
Civi::log()->error(
|
|
||||||
"Could not create campaign from TwingleProject: $errorMessage"
|
|
||||||
);
|
|
||||||
|
|
||||||
// Return result array with error description
|
|
||||||
return [
|
|
||||||
"title" => $values['name'],
|
|
||||||
"project_id" => (int) $values['id'],
|
|
||||||
"status" =>
|
|
||||||
"Could not create campaign from TwingleProject: $errorMessage",
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$result = $project->getResponse('TwingleProject exists');
|
|
||||||
|
|
||||||
// If Twingle's version of the project is newer than the CiviCRM
|
|
||||||
// TwingleProject campaign update the campaign
|
|
||||||
if ($values['last_update'] > $project->lastUpdate()) {
|
|
||||||
try {
|
|
||||||
$project->update($values);
|
|
||||||
$project->setEmbedData(
|
|
||||||
$twingleApi->getProjectEmbedData($project->getProjectId())
|
|
||||||
);
|
|
||||||
$result = $project->create();
|
|
||||||
$result['status'] = $result['status'] == 'TwingleProject created'
|
|
||||||
? 'TwingleProject updated'
|
|
||||||
: 'TwingleProject Update failed';
|
|
||||||
} catch (Exception $e) {
|
|
||||||
$errorMessage = $e->getMessage();
|
|
||||||
|
|
||||||
// Log Exception
|
|
||||||
Civi::log()->error(
|
|
||||||
"Could not update TwingleProject campaign: $errorMessage"
|
|
||||||
);
|
|
||||||
// Return result array with error description
|
|
||||||
$result = $project->getResponse(
|
|
||||||
"Could not update TwingleProject campaign: $errorMessage"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// If the CiviCRM TwingleProject campaign was changed, update the project
|
|
||||||
// on Twingle's side
|
|
||||||
elseif ($values['last_update'] < $project->lastUpdate()) {
|
|
||||||
// If this is a test do not make database changes
|
|
||||||
if ($is_test) {
|
|
||||||
$result = $project->getResponse(
|
|
||||||
'TwingleProject ready to push'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$result = $twingleApi->pushProject($project);
|
|
||||||
// Update TwingleProject in Civi with results from api call
|
|
||||||
if (is_array($result) && !array_key_exists('message', $result)) {
|
|
||||||
// Try to update the local TwingleProject campaign
|
|
||||||
try {
|
|
||||||
$project->update($result);
|
|
||||||
$project->create();
|
|
||||||
return $project->getResponse('TwingleProject pushed to Twingle');
|
|
||||||
} catch (Exception $e) {
|
|
||||||
// Log Exception
|
|
||||||
$errorMessage = $e->getMessage();
|
|
||||||
Civi::log()->error(
|
|
||||||
"Could not push TwingleProject campaign: $errorMessage"
|
|
||||||
);
|
|
||||||
// Return result array with error description
|
|
||||||
return $project->getResponse(
|
|
||||||
"TwingleProject was likely pushed to Twingle but the local " .
|
|
||||||
"update of the campaign failed: $errorMessage"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$message = $result['message'];
|
|
||||||
return $project->getResponse(
|
|
||||||
$message
|
|
||||||
? "TwingleProject could not get pushed to Twingle: $message"
|
|
||||||
: 'TwingleProject could not get pushed to Twingle'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
elseif ($result['status'] == 'TwingleProject exists') {
|
|
||||||
$result = $project->getResponse('TwingleProject up to date');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return the result of the synchronization
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Export values. Ensures that only those values will be exported which the
|
* Export values. Ensures that only those values will be exported which the
|
||||||
|
@ -214,59 +61,54 @@ class CRM_TwingleCampaign_BAO_TwingleProject extends Campaign {
|
||||||
/**
|
/**
|
||||||
* Create the Campaign as a campaign in CiviCRM if it does not exist
|
* Create the Campaign as a campaign in CiviCRM if it does not exist
|
||||||
*
|
*
|
||||||
* @param bool $is_test
|
* @param bool $no_hook
|
||||||
* If true: don't do any changes
|
* Do not trigger postSave hook to prevent recursion
|
||||||
*
|
*
|
||||||
* @return array
|
* @return bool
|
||||||
* Returns a response array that contains title, id, project_id and status
|
* Returns a boolean
|
||||||
*
|
*
|
||||||
* @throws CiviCRM_API3_Exception
|
* @throws \CiviCRM_API3_Exception
|
||||||
* @throws Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public function create(bool $is_test = FALSE): array {
|
public function create(bool $no_hook = FALSE): bool {
|
||||||
|
|
||||||
// Create campaign only if this is not a test
|
// Prepare project values for import into database
|
||||||
if (!$is_test) {
|
$values_prepared_for_import = $this->values;
|
||||||
|
self::formatValues(
|
||||||
|
$values_prepared_for_import,
|
||||||
|
self::IN
|
||||||
|
);
|
||||||
|
self::translateKeys(
|
||||||
|
$values_prepared_for_import,
|
||||||
|
self::IN
|
||||||
|
);
|
||||||
|
$this->translateCustomFields(
|
||||||
|
$values_prepared_for_import,
|
||||||
|
self::IN
|
||||||
|
);
|
||||||
|
|
||||||
// Prepare project values for import into database
|
// Set id
|
||||||
$values_prepared_for_import = $this->values;
|
$values_prepared_for_import['id'] = $this->id;
|
||||||
self::formatValues(
|
|
||||||
$values_prepared_for_import,
|
|
||||||
self::IN
|
|
||||||
);
|
|
||||||
self::translateKeys(
|
|
||||||
$values_prepared_for_import,
|
|
||||||
self::IN
|
|
||||||
);
|
|
||||||
$this->translateCustomFields(
|
|
||||||
$values_prepared_for_import,
|
|
||||||
self::IN
|
|
||||||
);
|
|
||||||
|
|
||||||
// Set id
|
|
||||||
$values_prepared_for_import['id'] = $this->id;
|
|
||||||
|
|
||||||
// Create campaign
|
|
||||||
$result = civicrm_api3('Campaign', 'create', $values_prepared_for_import);
|
|
||||||
|
|
||||||
// Update id
|
|
||||||
$this->id = $result['id'];
|
|
||||||
|
|
||||||
// Check if campaign was created successfully
|
|
||||||
if ($result['is_error'] == 0) {
|
|
||||||
$response = $this->getResponse("$this->className created");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$response = $this->getResponse("$this->className creation failed");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Set a flag to not trigger the hook
|
||||||
|
if ($no_hook) {
|
||||||
|
$_SESSION['CiviCRM']['de.forumzfd.twinglecampaign']['no_hook'] = TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create campaign
|
||||||
|
$result = civicrm_api3('Campaign', 'create', $values_prepared_for_import);
|
||||||
|
|
||||||
|
// Update id
|
||||||
|
$this->id = $result['id'];
|
||||||
|
|
||||||
|
// Check if campaign was created successfully
|
||||||
|
if ($result['is_error'] == 0) {
|
||||||
|
return TRUE;
|
||||||
}
|
}
|
||||||
// If this is a test, do not create campaign
|
|
||||||
else {
|
else {
|
||||||
$response = $this->getResponse("$this->className not yet created");
|
$errorMessage = $result['error_message'];
|
||||||
|
throw new Exception("$errorMessage");
|
||||||
}
|
}
|
||||||
|
|
||||||
return $response;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -313,8 +155,8 @@ class CRM_TwingleCampaign_BAO_TwingleProject extends Campaign {
|
||||||
|
|
||||||
// Set default for 'allow_more'
|
// Set default for 'allow_more'
|
||||||
$values['allow_more'] = empty($values['allow_more'])
|
$values['allow_more'] = empty($values['allow_more'])
|
||||||
? False
|
? FALSE
|
||||||
: True;
|
: TRUE;
|
||||||
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -394,15 +236,19 @@ class CRM_TwingleCampaign_BAO_TwingleProject extends Campaign {
|
||||||
* @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
|
||||||
*/
|
*/
|
||||||
public function getResponse(string $status): array {
|
public function getResponse(string $status = NULL): array {
|
||||||
$project_type = empty($this->values['type']) ? 'default' : $this->values['type'];
|
$project_type = empty($this->values['type']) ? 'default' : $this->values['type'];
|
||||||
return [
|
$response =
|
||||||
'title' => $this->values['name'],
|
[
|
||||||
'id' => (int) $this->id,
|
'title' => $this->values['name'],
|
||||||
'project_id' => (int) $this->values['id'],
|
'id' => (int) $this->id,
|
||||||
'project_type' => $project_type,
|
'project_id' => (int) $this->values['id'],
|
||||||
'status' => $status,
|
'project_type' => $project_type,
|
||||||
];
|
];
|
||||||
|
if ($status) {
|
||||||
|
$response['status'] = $status;
|
||||||
|
}
|
||||||
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
447
api/v3/TwingleProject/Sync.php
Normal file
447
api/v3/TwingleProject/Sync.php
Normal file
|
@ -0,0 +1,447 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use CRM_TwingleCampaign_BAO_TwingleProject as TwingleProject;
|
||||||
|
use CRM_TwingleCampaign_BAO_TwingleApiCall as TwingleApiCall;
|
||||||
|
use CRM_TwingleCampaign_ExtensionUtil as E;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TwingleProject.Sync API specification (optional)
|
||||||
|
* This is used for documentation and validation.
|
||||||
|
*
|
||||||
|
* @param array $spec description of fields supported by this API call
|
||||||
|
*
|
||||||
|
* @see https://docs.civicrm.org/dev/en/latest/framework/api-architecture/
|
||||||
|
*/
|
||||||
|
function _civicrm_api3_twingle_project_Sync_spec(array &$spec) {
|
||||||
|
$spec['id'] = [
|
||||||
|
'name' => 'id',
|
||||||
|
'title' => E::ts('Campaign ID'),
|
||||||
|
'type' => CRM_Utils_Type::T_INT,
|
||||||
|
'api.required' => 0,
|
||||||
|
'description' => E::ts('Unique Campaign ID'),
|
||||||
|
];
|
||||||
|
$spec['project_id'] = [
|
||||||
|
'name' => 'project_id',
|
||||||
|
'title' => E::ts('Twingle Project ID'),
|
||||||
|
'type' => CRM_Utils_Type::T_INT,
|
||||||
|
'api.required' => 0,
|
||||||
|
'description' => E::ts('Twingle ID for this project'),
|
||||||
|
];
|
||||||
|
$spec['is_test'] = [
|
||||||
|
'name' => 'is_test',
|
||||||
|
'title' => E::ts('Test'),
|
||||||
|
'type' => CRM_Utils_Type::T_BOOLEAN,
|
||||||
|
'api.required' => 0,
|
||||||
|
'description' => E::ts('If this is set true, no database change will be made'),
|
||||||
|
];
|
||||||
|
$spec['twingle_api_key'] = [
|
||||||
|
'name' => 'twingle_api_key',
|
||||||
|
'title' => E::ts('Twingle API key'),
|
||||||
|
'type' => CRM_Utils_Type::T_STRING,
|
||||||
|
'api.required' => 0,
|
||||||
|
'description' => E::ts('The key to access the Twingle API'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TwingleProject.Sync API
|
||||||
|
*
|
||||||
|
* @param array $params
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
* API result descriptor
|
||||||
|
* @throws \CiviCRM_API3_Exception
|
||||||
|
* @see civicrm_api3_create_success
|
||||||
|
*/
|
||||||
|
function civicrm_api3_twingle_project_Sync(array $params): array {
|
||||||
|
|
||||||
|
// For logging purpose
|
||||||
|
$extensionName = E::LONG_NAME;
|
||||||
|
|
||||||
|
// If call provides an API key, use it instead of the API key set
|
||||||
|
// on the extension settings page
|
||||||
|
$apiKey = empty($params['twingle_api_key'])
|
||||||
|
? trim(Civi::settings()->get('twingle_api_key'))
|
||||||
|
: trim($params['twingle_api_key']);
|
||||||
|
|
||||||
|
// Try to retrieve twingleApi from cache or create a new
|
||||||
|
$twingleApi = Civi::cache()->get('twinglecampaign_twingle_api');
|
||||||
|
if (NULL === $twingleApi || $params['twingle_api_key']) {
|
||||||
|
try {
|
||||||
|
$twingleApi = new TwingleApiCall($apiKey);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
return civicrm_api3_create_error($e->getMessage());
|
||||||
|
}
|
||||||
|
Civi::cache('long')->set('twinglecampaign_twingle_api', $twingleApi);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If an id or a project_id is given, synchronize only this one campaign
|
||||||
|
if ($params['id'] || $params['project_id']) {
|
||||||
|
|
||||||
|
// 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'][0]['project_id']) {
|
||||||
|
$project_from_twingle = $twingleApi->getProject($result['values'][0]['project_id']);
|
||||||
|
|
||||||
|
// instantiate project from CiviCRM
|
||||||
|
$id = $result['values'][0]['id'];
|
||||||
|
unset($result['values'][0]['id']);
|
||||||
|
$project = new TwingleProject($result['values'][0], $id);
|
||||||
|
|
||||||
|
// Synchronize projects
|
||||||
|
if (!empty($project_from_twingle)) {
|
||||||
|
return sync($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
|
||||||
|
// Twingle and update it with the returning values
|
||||||
|
else {
|
||||||
|
|
||||||
|
// store campaign id in $id
|
||||||
|
$id = $result['values'][0]['id'];
|
||||||
|
unset($result['values'][0]['id']);
|
||||||
|
|
||||||
|
// instantiate project
|
||||||
|
$project = new TwingleProject($result['values'][0], $id);
|
||||||
|
|
||||||
|
// Push project to Twingle
|
||||||
|
return pushToTwingle($project, $twingleApi, $params);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the project could not get retrieved from TwingleProject.getsingle,
|
||||||
|
// forward API error message
|
||||||
|
else {
|
||||||
|
Civi::log()->error(
|
||||||
|
"$extensionName could retrieve project from TwingleProject.getsingle",
|
||||||
|
$result
|
||||||
|
);
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no id or project_id is given, synchronize all projects
|
||||||
|
else {
|
||||||
|
|
||||||
|
// Counter for sync errors
|
||||||
|
$errors_occurred = 0;
|
||||||
|
|
||||||
|
// Get all projects from Twingle
|
||||||
|
$projects_from_twingle = $twingleApi->getProject();
|
||||||
|
|
||||||
|
// Get all TwingleProjects from CiviCRM
|
||||||
|
$projects_from_civicrm = civicrm_api3('TwingleProject', 'get',
|
||||||
|
['is_active' => 1,]);
|
||||||
|
|
||||||
|
// If call to TwingleProject.get failed, forward error message
|
||||||
|
if ($projects_from_civicrm['is_error'] != 0) {
|
||||||
|
Civi::log()->error(
|
||||||
|
"$extensionName could retrieve projects from TwingleProject.get",
|
||||||
|
$projects_from_civicrm
|
||||||
|
);
|
||||||
|
return $projects_from_civicrm;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Push missing projects to Twingle
|
||||||
|
$result_values = [];
|
||||||
|
foreach ($projects_from_civicrm['values'] as $project_from_civicrm) {
|
||||||
|
if (!in_array($project_from_civicrm['project_id'],
|
||||||
|
array_column($projects_from_twingle, '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);
|
||||||
|
// push project to Twingle
|
||||||
|
$result = pushToTwingle($project, $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'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create missing projects as campaigns in CiviCRM
|
||||||
|
foreach ($projects_from_twingle as $project_from_twingle) {
|
||||||
|
if (!in_array($project_from_twingle['id'],
|
||||||
|
array_column($projects_from_civicrm['values'], 'project_id'))) {
|
||||||
|
$project = new TwingleProject($project_from_twingle);
|
||||||
|
|
||||||
|
// If this is a test, do not make db changes
|
||||||
|
if ($params['is_test']) {
|
||||||
|
$result_values[$project->getId()] =
|
||||||
|
$project->getResponse('Ready to create TwingleProject');
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$project->create(TRUE);
|
||||||
|
$result_values[$project->getId()] =
|
||||||
|
$project->getResponse('TwingleProject created');
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$errors_occurred++;
|
||||||
|
$errorMessage = $e->getMessage();
|
||||||
|
Civi::log()->error(
|
||||||
|
"$extensionName could not create TwingleProject: $errorMessage",
|
||||||
|
$project->getResponse()
|
||||||
|
);
|
||||||
|
$result_values[$project->getId()] = $project->getResponse(
|
||||||
|
"TwingleProject could not get created: $errorMessage"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Synchronize existing projects
|
||||||
|
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'];
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Give back results
|
||||||
|
if ($errors_occurred > 0) {
|
||||||
|
$errorMessage = ($errors_occurred > 1)
|
||||||
|
? "$errors_occurred synchronisation processes resulted with an error"
|
||||||
|
: "1 synchronisation process resulted with an error";
|
||||||
|
return civicrm_api3_create_error(
|
||||||
|
$errorMessage,
|
||||||
|
$result_values
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return civicrm_api3_create_success(
|
||||||
|
$result_values,
|
||||||
|
$params,
|
||||||
|
'TwingleProject',
|
||||||
|
'Sync'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update a TwingleProject campaign locally
|
||||||
|
*
|
||||||
|
* @param array $project_from_twingle
|
||||||
|
* @param \CRM_TwingleCampaign_BAO_TwingleProject $project
|
||||||
|
* @param array $params
|
||||||
|
* @param \CRM_TwingleCampaign_BAO_TwingleApiCall $twingleApi
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
function updateLocally(array $project_from_twingle,
|
||||||
|
TwingleProject $project,
|
||||||
|
array $params,
|
||||||
|
TwingleApiCall $twingleApi): array {
|
||||||
|
|
||||||
|
// For logging purpose
|
||||||
|
$extensionName = E::LONG_NAME;
|
||||||
|
|
||||||
|
try {
|
||||||
|
$project->update($project_from_twingle);
|
||||||
|
$project->setEmbedData(
|
||||||
|
$twingleApi->getProjectEmbedData($project->getProjectId())
|
||||||
|
);
|
||||||
|
// If this is a test, do not make db changes
|
||||||
|
if ($params['is_test']) {
|
||||||
|
return civicrm_api3_create_success(
|
||||||
|
$project->getResponse('TwingleProject ready to update'),
|
||||||
|
$params,
|
||||||
|
'TwingleProject',
|
||||||
|
'Sync'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
// ... 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()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$errorMessage = $e->getMessage();
|
||||||
|
Civi::log()->error(
|
||||||
|
"$extensionName could not update TwingleProject campaign: $errorMessage"
|
||||||
|
);
|
||||||
|
return civicrm_api3_create_error(
|
||||||
|
"Could not update TwingleProject campaign: $errorMessage",
|
||||||
|
$project->getResponse()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Push a TwingleProject via API to Twingle
|
||||||
|
*
|
||||||
|
* @param \CRM_TwingleCampaign_BAO_TwingleProject $project
|
||||||
|
* @param \CRM_TwingleCampaign_BAO_TwingleApiCall $twingleApi
|
||||||
|
* @param array $params
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
* @throws \CiviCRM_API3_Exception
|
||||||
|
*/
|
||||||
|
function pushToTwingle(TwingleProject $project,
|
||||||
|
TwingleApiCall $twingleApi,
|
||||||
|
array $params): array {
|
||||||
|
|
||||||
|
// For logging purpose
|
||||||
|
$extensionName = E::LONG_NAME;
|
||||||
|
|
||||||
|
// If this is a test, do not make db changes
|
||||||
|
if ($params['is_test']) {
|
||||||
|
return civicrm_api3_create_success(
|
||||||
|
$project->getResponse('TwingleProject ready to push to Twingle'),
|
||||||
|
$params,
|
||||||
|
'TwingleProject',
|
||||||
|
'Sync'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Push project to Twingle
|
||||||
|
try {
|
||||||
|
$result = $twingleApi->pushProject($project);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$errorMessage = $e->getMessage();
|
||||||
|
Civi::log()->error(
|
||||||
|
"$extensionName could not push TwingleProject to Twingle: $errorMessage",
|
||||||
|
$project->getResponse()
|
||||||
|
);
|
||||||
|
return civicrm_api3_create_error(
|
||||||
|
"Could not push TwingleProject to Twingle: $errorMessage",
|
||||||
|
$project->getResponse()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update local campaign with data returning from Twingle
|
||||||
|
if ($result) {
|
||||||
|
$project->update($result);
|
||||||
|
// Get embed data
|
||||||
|
try {
|
||||||
|
$project->setEmbedData(
|
||||||
|
$twingleApi->getProjectEmbedData($project->getProjectId())
|
||||||
|
);
|
||||||
|
// Create updated campaign
|
||||||
|
$project->create(TRUE);
|
||||||
|
return civicrm_api3_create_success(
|
||||||
|
$project->getResponse('TwingleProject pushed to Twingle'),
|
||||||
|
$params,
|
||||||
|
'TwingleProject',
|
||||||
|
'Sync'
|
||||||
|
);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$errorMessage = $e->getMessage();
|
||||||
|
Civi::log()->error(
|
||||||
|
"$extensionName pushed TwingleProject to Twingle but local update failed: $errorMessage",
|
||||||
|
$project->getResponse()
|
||||||
|
);
|
||||||
|
return civicrm_api3_create_error(
|
||||||
|
"TwingleProject was pushed to Twingle but local update failed: $errorMessage",
|
||||||
|
$project->getResponse()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If the curl fails, the $result may be empty
|
||||||
|
else {
|
||||||
|
Civi::log()->error(
|
||||||
|
"$extensionName could not push TwingleProject campaign",
|
||||||
|
$project->getResponse()
|
||||||
|
);
|
||||||
|
return civicrm_api3_create_error(
|
||||||
|
"Could not push TwingleProject campaign",
|
||||||
|
$project->getResponse()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Synchronize a TwingleProject campaign with a project from Twingle
|
||||||
|
*
|
||||||
|
* @param \CRM_TwingleCampaign_BAO_TwingleProject $project
|
||||||
|
* @param array $project_from_twingle
|
||||||
|
* @param \CRM_TwingleCampaign_BAO_TwingleApiCall $twingleApi
|
||||||
|
* @param array $params
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
* @throws \CiviCRM_API3_Exception
|
||||||
|
*/
|
||||||
|
function sync(TwingleProject $project,
|
||||||
|
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
|
||||||
|
if ($project_from_twingle['last_update'] > $project->lastUpdate()) {
|
||||||
|
return updateLocally($project_from_twingle, $project, $params, $twingleApi);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the CiviCRM TwingleProject campaign was changed, update the project
|
||||||
|
// on Twingle's side
|
||||||
|
elseif ($project_from_twingle['last_update'] < $project->lastUpdate()) {
|
||||||
|
return pushToTwingle($project, $twingleApi, $params);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If both versions are still synchronized
|
||||||
|
else {
|
||||||
|
return civicrm_api3_create_success(
|
||||||
|
$project->getResponse('TwingleProject up to date'),
|
||||||
|
$params,
|
||||||
|
'TwingleProject',
|
||||||
|
'Sync'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
53
tests/phpunit/api/v3/TwingleProject/SyncTest.php
Normal file
53
tests/phpunit/api/v3/TwingleProject/SyncTest.php
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Civi\Test\HeadlessInterface;
|
||||||
|
use Civi\Test\HookInterface;
|
||||||
|
use Civi\Test\TransactionalInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TwingleProject.Sync API Test Case
|
||||||
|
* This is a generic test class implemented with PHPUnit.
|
||||||
|
* @group headless
|
||||||
|
*/
|
||||||
|
class api_v3_TwingleProject_SyncTest extends \PHPUnit\Framework\TestCase implements HeadlessInterface, HookInterface, TransactionalInterface {
|
||||||
|
use \Civi\Test\Api3TestTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set up for headless tests.
|
||||||
|
*
|
||||||
|
* Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile().
|
||||||
|
*
|
||||||
|
* See: https://docs.civicrm.org/dev/en/latest/testing/phpunit/#civitest
|
||||||
|
*/
|
||||||
|
public function setUpHeadless() {
|
||||||
|
return \Civi\Test::headless()
|
||||||
|
->installMe(__DIR__)
|
||||||
|
->apply();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The setup() method is executed before the test is executed (optional).
|
||||||
|
*/
|
||||||
|
public function setUp() {
|
||||||
|
parent::setUp();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The tearDown() method is executed after the test was executed (optional)
|
||||||
|
* This can be used for cleanup.
|
||||||
|
*/
|
||||||
|
public function tearDown() {
|
||||||
|
parent::tearDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple example test case.
|
||||||
|
*
|
||||||
|
* Note how the function name begins with the word "test".
|
||||||
|
*/
|
||||||
|
public function testApiExample() {
|
||||||
|
$result = civicrm_api3('TwingleProject', 'Sync', array('magicword' => 'sesame'));
|
||||||
|
$this->assertEquals('Twelve', $result['values'][12]['name']);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -2,49 +2,65 @@
|
||||||
|
|
||||||
use CRM_TwingleCampaign_BAO_CampaignType as CampaignType;
|
use CRM_TwingleCampaign_BAO_CampaignType as CampaignType;
|
||||||
use CRM_TwingleCampaign_Utils_ExtensionCache as ExtensionCache;
|
use CRM_TwingleCampaign_Utils_ExtensionCache as ExtensionCache;
|
||||||
|
use CRM_TwingleCampaign_ExtensionUtil as E;
|
||||||
|
|
||||||
require_once 'twinglecampaign.civix.php';
|
require_once 'twinglecampaign.civix.php';
|
||||||
|
|
||||||
use CRM_TwingleCampaign_ExtensionUtil as E;
|
|
||||||
|
|
||||||
//
|
|
||||||
//Civi::dispatcher()->addListener('hook_civicrm_postSave_civicrm_campaign',
|
|
||||||
// "twinglecampaign_postSave_civicrm_campaign", -1000);
|
|
||||||
/**
|
/**
|
||||||
* Implements hook_civicrm_postSave_Campaigns().
|
* Implements hook_civicrm_postSave_Campaigns().
|
||||||
|
* This function synchronizes TwingleProject campaigns between CiviCRM and
|
||||||
|
* Twingle when they get created, edited or cloned. To prevent recursion a no
|
||||||
|
* hook flag is getting used.
|
||||||
*
|
*
|
||||||
* @param $dao
|
* @param $dao
|
||||||
*
|
*
|
||||||
* @throws CiviCRM_API3_Exception
|
* @throws CiviCRM_API3_Exception
|
||||||
*/
|
*/
|
||||||
function twinglecampaign_civicrm_postSave_civicrm_campaign($dao) {
|
function twinglecampaign_civicrm_postSave_civicrm_campaign($dao) {
|
||||||
$twingle_campaign_types = ExtensionCache::getInstance()
|
|
||||||
->getCampaigns()['campaign_types'];
|
if (empty($_SESSION['CiviCRM']['de.forumzfd.twinglecampaign']['no_hook']) ||
|
||||||
$twingle_campaign_type_ids = [];
|
$_SESSION['CiviCRM']['de.forumzfd.twinglecampaign']['no_hook'] != TRUE) {
|
||||||
$hook_campaign_type_id = $dao->campaign_type_id;
|
|
||||||
$hook_campaign_id = $dao->id;
|
// extract variables from $dao object
|
||||||
foreach ($twingle_campaign_types as $twingle_campaign_type) {
|
$hook_campaign_type_id = $dao->campaign_type_id;
|
||||||
$id = civicrm_api3('OptionValue', 'get',
|
$hook_campaign_id = $dao->id;
|
||||||
['sequential' => 1, 'name' => $twingle_campaign_type['name']]
|
|
||||||
|
// Get campaign type id for TwingleProject
|
||||||
|
$twingle_project_campaign_type_id = civicrm_api3(
|
||||||
|
'OptionValue',
|
||||||
|
'get',
|
||||||
|
['sequential' => 1, 'name' => 'twingle_project']
|
||||||
)['values'][0]['value'];
|
)['values'][0]['value'];
|
||||||
array_push($twingle_campaign_type_ids, $id);
|
|
||||||
}
|
// If $dao is a TwingleProject campaign, synchronize it
|
||||||
if (in_array($hook_campaign_type_id, $twingle_campaign_type_ids)) {
|
if ($hook_campaign_type_id == $twingle_project_campaign_type_id) {
|
||||||
if (CRM_Core_Transaction::isActive()) {
|
// If the db transaction is still running, add a function to it that will
|
||||||
CRM_Core_Transaction::addCallback(
|
// be called afterwards
|
||||||
CRM_Core_Transaction::PHASE_POST_COMMIT,
|
if (CRM_Core_Transaction::isActive()) {
|
||||||
'twinglecampaign_postSave_callback',
|
CRM_Core_Transaction::addCallback(
|
||||||
[$hook_campaign_id]
|
CRM_Core_Transaction::PHASE_POST_COMMIT,
|
||||||
);
|
'twinglecampaign_postSave_callback',
|
||||||
}
|
[$hook_campaign_id]
|
||||||
else {
|
);
|
||||||
twinglecampaign_postSave_callback($hook_campaign_id);
|
}
|
||||||
|
// If the transaction is already finished, call the function directly
|
||||||
|
else {
|
||||||
|
twinglecampaign_postSave_callback($hook_campaign_id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Remove no hook flag
|
||||||
|
unset($_SESSION['CiviCRM']['de.forumzfd.twinglecampaign']['no_hook']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This callback function synchronizes a recently updated TwingleProject campaign
|
||||||
|
* @param $campaign_id
|
||||||
|
* @throws \CiviCRM_API3_Exception
|
||||||
|
*/
|
||||||
function twinglecampaign_postSave_callback($campaign_id) {
|
function twinglecampaign_postSave_callback($campaign_id) {
|
||||||
civicrm_api3('TwingleSync', 'sync', ['id' => $campaign_id]);
|
civicrm_api3('TwingleProject', 'sync', ['id' => $campaign_id]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue