implement TwingleEvent

This commit is contained in:
Marc Michalsky forumZFD 2020-10-30 16:27:56 +01:00
parent 6b434e455f
commit 3554f958d2
Signed by untrusted user who does not match committer: marc.koch
GPG key ID: 12406554CFB028B9
10 changed files with 735 additions and 570 deletions

View file

@ -4,16 +4,14 @@
namespace CRM\TwingleCampaign\BAO;
use Civi;
use CRM\TwingleCampaign\Utils\ExtensionCache as Cache;
use CRM_TwingleCampaign_ExtensionUtil as E;
use CRM_Utils_Array;
use DateTime;
use CRM\TwingleCampaign\BAO\CustomField as CustomField;
use CRM\TwingleCampaign\Utils\ExtensionCache as Cache;
use CRM\TwingleCampaign\BAO\Campaign;
use Exception;
use CiviCRM_API3_Exception;
include_once E::path() . '/CRM/TwingleCampaign/BAO/CustomField.php';
include_once E::path() . '/CRM/TwingleCampaign/BAO/Campaign.php';
include_once E::path() . '/CRM/TwingleCampaign/Utils/ExtensionCache.php';
class TwingleEvent extends Campaign {
@ -33,7 +31,8 @@ class TwingleEvent extends Campaign {
public function __construct(array $event, string $origin) {
parent::__construct($event, $origin);
$this->className = get_class($this);
$this->className = (new \ReflectionClass($this))->getShortName();
$this->prefix = 'twingle_event_';
// Add value for campaign type
$event['campaign_type_id'] = 'twingle_event';
@ -44,6 +43,112 @@ class TwingleEvent extends Campaign {
}
/**
* Synchronizes events 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, event_id, project_
* 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
) {
// If $values is an array
if (is_array($values)) {
// Instantiate TwingleEvent
try {
$event = new TwingleEvent(
$values,
self::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['project_id'],
"status" =>
"Failed to instantiate TwingleEvent: $e->getMessage()",
];
}
// Check if the TwingleEvent campaign already exists
if (!$event->exists()) {
// ... if not, get embed data and create event
try {
$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['project_id'],
"status" =>
"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['updated_at'] > $event->lastUpdate()) {
try {
$event->update($values);
$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 TwingleEvent 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;
}
}
/**
* Translate values between CiviCRM Campaigns and Twingle
@ -52,8 +157,8 @@ class TwingleEvent extends Campaign {
* array of which values shall be translated
*
* @param string $direction
* TwingleEvent::IN -> translate array values from Twingle to CiviCRM <br>
* TwingleEvent::OUT -> translate array values from CiviCRM to Twingle
* self::IN -> translate array values from Twingle to CiviCRM <br>
* self::OUT -> translate array values from CiviCRM to Twingle
*
* @throws Exception
*/
@ -67,21 +172,16 @@ class TwingleEvent extends Campaign {
self::getDateTime($values['last_update']);
}
// empty project_type to 'default'
if (!$values['type']) {
$values['type'] = 'default';
}
}
elseif ($direction == self::OUT) {
// Change DateTime string into timestamp
$values['last_update'] =
self::getTimestamp($values['last_update']);
// Default project_type to ''
$values['type'] = $values['type'] == 'default'
? ''
: $values['type'];
$values['updated_at'] =
self::getTimestamp($values['updated_at']);
$values['confirmed_at'] =
self::getTimestamp($values['confirmed_at']);
$values['created_at'] =
self::getTimestamp($values['created_at']);
// Cast project target to integer
$values['project_target'] = (int) $values['project_target'];
@ -97,25 +197,6 @@ class TwingleEvent extends Campaign {
}
/**
* Set embed data fields
*
* @param array $embedData
* Array with embed data from Twingle API
*/
public function setEmbedData(array $embedData) {
// Get all embed_data keys from template
$embed_data_keys = Cache::getInstance()
->getTemplates()['event_embed_data'];
// Transfer all embed_data values
foreach ($embed_data_keys as $key) {
$this->values[$key] = htmlspecialchars($embedData[$key]);
}
}
/**
* Get a response that describes the status of a TwingleEvent
*
@ -127,7 +208,7 @@ class TwingleEvent extends Campaign {
*/
public function getResponse(string $status) {
return [
'title' => $this->values['name'],
'title' => $this->values['description'],
'id' => (int) $this->id,
'event_id' => (int) $this->values['id'],
'project_id' => (int) $this->values['project_id'],
@ -135,6 +216,16 @@ class TwingleEvent extends Campaign {
];
}
/**
* Return a timestamp of the last update of the Campaign
*
* @return int|null
*/
public function lastUpdate() {
return self::getTimestamp($this->values['updated_at']);
}
/**
* Returns the project_id of a TwingleEvent