implement TwingleEvent
This commit is contained in:
parent
6b434e455f
commit
3554f958d2
10 changed files with 735 additions and 570 deletions
|
@ -1,483 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace CRM\TwingleCampaign\BAO;
|
||||
|
||||
use API_Exception;
|
||||
use Civi;
|
||||
use CRM_Core_BAO_Setting;
|
||||
use CRM_TwingleCampaign_ExtensionUtil as E;
|
||||
use CRM\TwingleCampaign\BAO\TwingleProject as TwingleProject;
|
||||
use Exception;
|
||||
|
||||
include_once E::path() . '/CRM/TwingleCampaign/BAO/TwingleProject.php';
|
||||
|
||||
class TwingleApiCall {
|
||||
|
||||
private $apiKey;
|
||||
|
||||
private $baseUrl = '.twingle.de/api/';
|
||||
|
||||
private $protocol = 'https://';
|
||||
|
||||
private $organisationId;
|
||||
|
||||
/**
|
||||
* TwingleApiCall constructor.
|
||||
*
|
||||
* @param $apiKey
|
||||
*
|
||||
* @throws API_Exception
|
||||
*/
|
||||
public function __construct($apiKey) {
|
||||
$this->apiKey = $apiKey;
|
||||
|
||||
// Get organisation id
|
||||
$curl = curl_init($this->protocol . 'organisation' . $this->baseUrl);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
|
||||
curl_setopt($curl, CURLOPT_HTTPHEADER, [
|
||||
"x-access-code: $apiKey",
|
||||
'Content-Type: application/json',
|
||||
]);
|
||||
|
||||
$response = json_decode(curl_exec($curl), TRUE);
|
||||
curl_close($curl);
|
||||
|
||||
if (empty($response)) {
|
||||
throw new API_Exception(
|
||||
"Twingle API call failed. Please check your api key.");
|
||||
}
|
||||
|
||||
$this->organisationId = array_column($response, 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
* If $id parameter is empty, this function returns all projects for all
|
||||
* organisations this API key is assigned to.
|
||||
*
|
||||
* TODO: Keys can only get assigned to one organisation. Save multiple keys
|
||||
* in settings instead.
|
||||
*
|
||||
* If $id parameter is given, this function returns a single project.
|
||||
*
|
||||
* @param int|null $projectId
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getProject(int $projectId = NULL) {
|
||||
$response = [];
|
||||
foreach ($this->organisationId as $organisationId) {
|
||||
$url = empty($projectId)
|
||||
? $this->protocol . 'project' . $this->baseUrl . 'by-organisation/' . $organisationId
|
||||
: $this->protocol . 'project' . $this->baseUrl . $projectId;
|
||||
|
||||
$response = array_merge($this->curlGet($url));
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Returns all Events for the given $projectId
|
||||
*
|
||||
* @param $projectId
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getEvent($projectId) {
|
||||
$result = [];
|
||||
$url = $this->protocol . 'project' . $this->baseUrl . $projectId . '/event';
|
||||
$limit = CRM_Core_BAO_Setting::getItem('', 'twingle_request_size');
|
||||
$offset = 0;
|
||||
$finished = FALSE;
|
||||
|
||||
while (!$finished) {
|
||||
$params = [
|
||||
'orderby' => 'id',
|
||||
'direction' => 'desc',
|
||||
'limit' => $limit,
|
||||
'offset' => $offset,
|
||||
'image' => 'as-boolean',
|
||||
'public' => 0,
|
||||
];
|
||||
$response = $this->curlGet($url, $params);
|
||||
$finished = count($response['data']) < $limit;
|
||||
$offset = $offset + $limit;
|
||||
$result = array_merge($result, $response['data']);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronizes projects between Twingle and CiviCRM (both directions)
|
||||
* based on the timestamp.
|
||||
*
|
||||
* @param array $values
|
||||
*
|
||||
* @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 function syncProject(array $values, bool $is_test = FALSE) {
|
||||
|
||||
// If $values is an array
|
||||
if (is_array($values)) {
|
||||
|
||||
// Instantiate TwingleProject
|
||||
try {
|
||||
$project = new TwingleProject(
|
||||
$values,
|
||||
TwingleProject::TWINGLE
|
||||
);
|
||||
} catch (Exception $e) {
|
||||
|
||||
// Log Exception
|
||||
Civi::log()->error(
|
||||
"Failed to instantiate TwingleProject: $e->getMessage()"
|
||||
);
|
||||
|
||||
// Return result array with error description
|
||||
return [
|
||||
"title" => $values['name'],
|
||||
"project_id" => (int) $values['id'],
|
||||
"status" =>
|
||||
"Failed to instantiate TwingleProject: $e->getMessage()",
|
||||
];
|
||||
}
|
||||
|
||||
// Check if the TwingleProject campaign already exists
|
||||
if (!$project->exists()) {
|
||||
|
||||
// ... if not, get embed data and create project
|
||||
try {
|
||||
$this->getEmbedData($project);
|
||||
$result = $project->create($is_test);
|
||||
} catch (Exception $e) {
|
||||
|
||||
// Log Exception
|
||||
Civi::log()->error(
|
||||
"Could not create campaign from TwingleProject: $e->getMessage()"
|
||||
);
|
||||
|
||||
// Return result array with error description
|
||||
return [
|
||||
"title" => $values['name'],
|
||||
"project_id" => (int) $values['id'],
|
||||
"status" =>
|
||||
"Could not create campaign from TwingleProject: $e->getMessage()",
|
||||
];
|
||||
}
|
||||
}
|
||||
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);
|
||||
$this->getEmbedData($project);
|
||||
$result = $project->create();
|
||||
$result['status'] = $result['status'] == 'TwingleProject created'
|
||||
? 'TwingleProject updated'
|
||||
: 'TwingleProject Update failed';
|
||||
} catch (Exception $e) {
|
||||
// Log Exception
|
||||
Civi::log()->error(
|
||||
"Could not update TwingleProject campaign: $e->getMessage()"
|
||||
);
|
||||
// Return result array with error description
|
||||
$result = $project->getResponse(
|
||||
"Could not update TwingleProject campaign: $e->getMessage()"
|
||||
);
|
||||
}
|
||||
}
|
||||
// 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 = $this->updateProject($project);
|
||||
}
|
||||
}
|
||||
elseif ($result['status'] == 'TwingleProject exists') {
|
||||
$result = $project->getResponse('TwingleProject up to date');
|
||||
}
|
||||
}
|
||||
|
||||
// Return a response of the synchronization
|
||||
return $result;
|
||||
}
|
||||
else {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \CRM\TwingleCampaign\BAO\TwingleProject $project
|
||||
*/
|
||||
private function getEmbedData(TwingleProject &$project) {
|
||||
|
||||
// Prepare url for curl
|
||||
$url = $this->protocol . 'project' . $this->baseUrl . $project->getProjectId();
|
||||
|
||||
// Send curl
|
||||
$result = $this->curlGet($url);
|
||||
|
||||
// Set embed data
|
||||
$project->setEmbedData($result['embed']);
|
||||
|
||||
// Set counter-url
|
||||
$project->setCounterUrl($result['counter-url']['url']);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends an curl post call to Twingle to update an existing project and then
|
||||
* updates the TwingleProject campaign.
|
||||
*
|
||||
* @param \CRM\TwingleCampaign\BAO\TwingleProject $project
|
||||
* The TwingleProject object that should get pushed to Twingle
|
||||
*
|
||||
* @return array
|
||||
* Returns a response array that contains title, id, project_id and status
|
||||
*
|
||||
*/
|
||||
private function updateProject(TwingleProject &$project) {
|
||||
|
||||
try {
|
||||
$values = $project->export();
|
||||
} catch (Exception $e) {
|
||||
// Log Exception
|
||||
Civi::log()->error(
|
||||
"Could not export TwingleProject values: $e->getMessage()"
|
||||
);
|
||||
// Return result array with error description
|
||||
return $project->getResponse(
|
||||
"Could not export TwingleProject values: $e->getMessage()"
|
||||
);
|
||||
}
|
||||
|
||||
// Prepare url for curl
|
||||
$url = $this->protocol . 'project' . $this->baseUrl . $values['id'];
|
||||
|
||||
// Send curl
|
||||
$result = $this->curlPost($url, $values);
|
||||
|
||||
// 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);
|
||||
$this->getEmbedData($project);
|
||||
$project->create();
|
||||
return $project->getResponse('TwingleProject pushed to Twingle');
|
||||
} catch (Exception $e) {
|
||||
// Log Exception
|
||||
Civi::log()->error(
|
||||
"Could not update TwingleProject campaign: $e->getMessage()"
|
||||
);
|
||||
// Return result array with error description
|
||||
return $project->getResponse(
|
||||
"TwingleProject was likely pushed to Twingle but the
|
||||
local update of the campaign failed: $e->getMessage()"
|
||||
);
|
||||
}
|
||||
}
|
||||
else {
|
||||
$message = $result['message'];
|
||||
return $project->getResponse(
|
||||
$message
|
||||
? "TwingleProject could not get pushed to Twingle: $message"
|
||||
: 'TwingleProject could not get pushed to Twingle'
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $values
|
||||
* @param bool $isTest
|
||||
*
|
||||
* @return array | NULL
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function syncEvent(array $values, bool $isTest) {
|
||||
// If $event is an array
|
||||
if (is_array($values)) {
|
||||
|
||||
// Instantiate TwingleEvent
|
||||
try {
|
||||
$event = new TwingleEvent(
|
||||
$values,
|
||||
TwingleEvent::TWINGLE
|
||||
);
|
||||
} catch (Exception $e) {
|
||||
|
||||
// Log Exception
|
||||
Civi::log()->error(
|
||||
"Failed to instantiate TwingleEvent: $e->getMessage()"
|
||||
);
|
||||
|
||||
// Return result array with error description
|
||||
return [
|
||||
"title" => $values['description'],
|
||||
"event_id" => (int) $values['id'],
|
||||
"project_id" => (int) $values['id'],
|
||||
"status" =>
|
||||
"Failed to instantiate TwingleEvent: $e->getMessage()",
|
||||
];
|
||||
}
|
||||
|
||||
// Check if the TwingleProject campaign already exists
|
||||
if (!$event->exists()) {
|
||||
|
||||
// ... if not, get embed data and create project
|
||||
try {
|
||||
$this->getEmbedData($event); // TODO: !
|
||||
$result = $event->create($is_test);
|
||||
} catch (Exception $e) {
|
||||
|
||||
// Log Exception
|
||||
Civi::log()->error(
|
||||
"Could not create campaign from TwingleEvent: $e->getMessage()"
|
||||
);
|
||||
|
||||
// Return result array with error description
|
||||
return [
|
||||
"title" => $values['description'],
|
||||
"event_id" => (int) $values['id'],
|
||||
"project_id" => (int) $values['id'],
|
||||
"Could not create campaign from TwingleEvent: $e->getMessage()",
|
||||
];
|
||||
}
|
||||
}
|
||||
else {
|
||||
$result = $event->getResponse('TwingleEvent exists');
|
||||
|
||||
// If Twingle's version of the event is newer than the CiviCRM
|
||||
// TwingleEvent campaign update the campaign
|
||||
if ($values['last_update'] > $event->lastUpdate()) {
|
||||
try {
|
||||
$event->update($values);
|
||||
$this->getEmbedData($event); // TODO: !
|
||||
$result = $event->create();
|
||||
$result['status'] = $result['status'] == 'TwingleEvent created'
|
||||
? 'TwingleEvent updated'
|
||||
: 'TwingleEvent Update failed';
|
||||
} catch (Exception $e) {
|
||||
// Log Exception
|
||||
Civi::log()->error(
|
||||
"Could not update TwingleEvent campaign: $e->getMessage()"
|
||||
);
|
||||
// Return result array with error description
|
||||
$result = $event->getResponse(
|
||||
"Could not update TwingleProject campaign: $e->getMessage()"
|
||||
);
|
||||
}
|
||||
}
|
||||
elseif ($result['status'] == 'TwingleEvent exists') {
|
||||
$result = $event->getResponse('TwingleEvent up to date');
|
||||
}
|
||||
}
|
||||
|
||||
// Return a response of the synchronization
|
||||
return $result;
|
||||
}
|
||||
else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Does a cURL and gives back the result array.
|
||||
*
|
||||
* @param $url
|
||||
* The url the curl should get sent to
|
||||
*
|
||||
* @param null $params
|
||||
* The parameters you want to send (optional)
|
||||
*
|
||||
* @return array|bool
|
||||
* Returns the result array of the curl or FALSE, if the curl failed
|
||||
*/
|
||||
private function curlGet($url, $params = NULL) {
|
||||
if (!empty($params)) {
|
||||
$url = $url . '?' . http_build_query($params);
|
||||
}
|
||||
$curl = curl_init($url);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
|
||||
curl_setopt($curl, CURLOPT_HTTPHEADER, [
|
||||
"x-access-code: $this->apiKey",
|
||||
'Content-Type: application/json',
|
||||
]);
|
||||
$response = json_decode(curl_exec($curl), TRUE);
|
||||
if (empty($response)) {
|
||||
$response = curl_error($curl);
|
||||
}
|
||||
curl_close($curl);
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a curl post and gives back the result array.
|
||||
*
|
||||
* @param $url
|
||||
* The url the curl should get sent to
|
||||
*
|
||||
* @param $data
|
||||
* The data that should get posted
|
||||
*
|
||||
* @return false|mixed
|
||||
* Returns the result array of the curl or FALSE, if the curl failed
|
||||
*/
|
||||
private function curlPost($url, $data) {
|
||||
$curl = curl_init($url);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
|
||||
curl_setopt($curl, CURLOPT_POST, TRUE);
|
||||
curl_setopt($curl, CURLOPT_HTTPHEADER, [
|
||||
"x-access-code: $this->apiKey",
|
||||
'Content-Type: application/json',
|
||||
]);
|
||||
$json = json_encode($data);
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
|
||||
$response = json_decode(curl_exec($curl), TRUE);
|
||||
if (empty($response)) {
|
||||
$response = FALSE;
|
||||
}
|
||||
curl_close($curl);
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a result array
|
||||
*
|
||||
* @param $values
|
||||
* Project values to generate result array from
|
||||
*
|
||||
* @param $status
|
||||
* Status of the array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getResultArray($values, $status) {
|
||||
return [
|
||||
"title" => $values['name'],
|
||||
"project_id" => (int) $values['id'],
|
||||
"project_type" => $values['project_type'],
|
||||
"status" => $status,
|
||||
];
|
||||
}
|
||||
|
||||
}
|
|
@ -1,9 +1,15 @@
|
|||
<?php
|
||||
|
||||
use CRM_TwingleCampaign_ExtensionUtil as E;
|
||||
use CRM\TwingleCampaign\BAO\TwingleApiCall as TwingleApiCall;
|
||||
use CRM\TwingleCampaign\BAO\TwingleProject;
|
||||
use CRM\TwingleCampaign\BAO\TwingleEvent;
|
||||
use CRM\TwingleCampaign\BAO\TwingleCampaign;
|
||||
use CRM_TwingleCampaign_ExtensionUtil as E;
|
||||
|
||||
include_once E::path() . '/api/v3/TwingleSync/BAO/TwingleApiCall.php';
|
||||
include_once E::path() . '/CRM/TwingleCampaign/BAO/TwingleApiCall.php';
|
||||
include_once E::path() . '/CRM/TwingleCampaign/BAO/TwingleProject.php';
|
||||
include_once E::path() . '/CRM/TwingleCampaign/BAO/TwingleEvent.php';
|
||||
include_once E::path() . '/CRM/TwingleCampaign/BAO/TwingeCampaign.php';
|
||||
|
||||
/**
|
||||
* TwingleSync.Post API specification (optional)
|
||||
|
@ -13,7 +19,7 @@ include_once E::path() . '/api/v3/TwingleSync/BAO/TwingleApiCall.php';
|
|||
*
|
||||
* @see https://docs.civicrm.org/dev/en/latest/framework/api-architecture/
|
||||
*/
|
||||
function _civicrm_api3_twingle_sync_Post_spec(&$spec) {
|
||||
function _civicrm_api3_twingle_sync_Post_spec(array &$spec) {
|
||||
$spec['twingle_api_key'] = [
|
||||
'name' => 'twingle_api_key',
|
||||
'title' => E::ts('Twingle API key'),
|
||||
|
@ -38,11 +44,11 @@ function _civicrm_api3_twingle_sync_Post_spec(&$spec) {
|
|||
* @return array
|
||||
* API result descriptor
|
||||
*
|
||||
* @throws \CiviCRM_API3_Exception|\API_Exception
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
* @throws \API_Exception
|
||||
* @see civicrm_api3_create_success
|
||||
*
|
||||
*/
|
||||
function civicrm_api3_twingle_sync_Post($params) {
|
||||
function civicrm_api3_twingle_sync_Post(array $params) {
|
||||
$result_values = [];
|
||||
|
||||
// Is this call a test?
|
||||
|
@ -63,8 +69,8 @@ function civicrm_api3_twingle_sync_Post($params) {
|
|||
$i = 0;
|
||||
foreach ($projects as $project) {
|
||||
if (is_array($project)) {
|
||||
$result_values['sync']['projects'][$i++] = $twingleApi
|
||||
->syncProject($project, $is_test);
|
||||
$result_values['sync']['projects'][$i++] =
|
||||
TwingleProject::sync($project, $twingleApi, $is_test);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -80,10 +86,12 @@ function civicrm_api3_twingle_sync_Post($params) {
|
|||
$j = 0;
|
||||
if ($events) {
|
||||
foreach ($events as $event) {
|
||||
$result_values['sync']['events'][$j++] = $twingleApi
|
||||
->syncEvent($event, $is_test);
|
||||
$result_values['sync']['events'][$j++] =
|
||||
TwingleEvent::sync($event, $twingleApi, $is_test);
|
||||
}
|
||||
}
|
||||
|
||||
return civicrm_api3_create_success($result_values);
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue