rename "Models" to "BAO" in paths and namespaces

This commit is contained in:
Marc Michalsky forumZFD 2020-10-05 17:01:47 +02:00
parent 4b2047fdb0
commit aef6b64004
Signed by untrusted user who does not match committer: marc.koch
GPG key ID: 12406554CFB028B9
11 changed files with 82 additions and 50 deletions

View file

@ -0,0 +1,242 @@
<?php
namespace CRM\TwingleCampaign\BAO;
use CRM_Core_BAO_Setting;
use CRM_TwingleCampaign_ExtensionUtil as E;
use CRM\TwingleCampaign\BAO\TwingleProject as TwingleProject;
include_once E::path() . '/api/v3/TwingleSync/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.
*
* If $id parameter is given, this function returns a single project.
*
* @param null $projectId
*
* @return mixed
*/
public function getProject($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 state or
* NULL if $values is not an array
*
* @throws \CiviCRM_API3_Exception
* @throws \Exception
*/
public function syncProject(array $values, bool $is_test = FALSE) {
// If $values is an array
if (is_array($values)) {
$project = new TwingleProject($values, TwingleProject::TWINGLE);
$result = $project->create($is_test);
// If Twingle's version of the project is newer than the CiviCRM
// TwingleProject campaign update the campaign
if (
$result['state'] == 'TwingleProject already exists' &&
$values['last_update'] > $project->lastUpdate()
) {
$result = $project->update($is_test);
}
// If the CiviCRM TwingleProject campaign was changed, update the project
// on Twingle's side
elseif (
$result['state'] == 'TwingleProject already exists' &&
$values['last_update'] < $project->lastUpdate()
) {
// If this is a test do not make database changes
if ($is_test) {
$result = TwingleProject::fetch($values['id'])->getResponse(
'TwingleProject ready to push'
);
}
else {
$result = $this->updateProject($project->export());
}
}
// Return a response of the synchronization
return $result;
}
else {
return NULL;
}
}
/**
* @param array $values
* @param bool $is_test
*
* @return array
* @throws \CiviCRM_API3_Exception
* @throws \Exception
*/
public function updateProject(array $values) {
// 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
$updated_project = new TwingleProject($result, TwingleProject::TWINGLE);
$updated_project->create();
return $updated_project->getResponse("TwingleProject pushed to Twingle");
}
public function updateEvent() {
}
/**
* @return array
*/
public function getOrganisationId() {
return $this->organisationId;
}
/**
*
* Does a cURL and gives back the result array.
*
* @param $url
*
* @param null $params
*
* @return mixed
*/
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;
}
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',
]);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
$response = json_decode(curl_exec($curl), TRUE);
if (empty($response)) {
$response = curl_error($curl);
}
curl_close($curl);
return $response;
}
}

View file

@ -0,0 +1,9 @@
<?php
namespace CRM\TwingleCampaign\BAO;
class TwingleCampaign {
}

View file

@ -0,0 +1,9 @@
<?php
namespace CRM\TwingleCampaign\BAO;
class TwingleEvent {
}

View file

@ -0,0 +1,582 @@
<?php
namespace CRM\TwingleCampaign\BAO;
use CRM_TwingleCampaign_ExtensionUtil as E;
use DateTime;
use CRM\TwingleCampaign\BAO\CustomField as CustomField;
include_once E::path() . '/CRM/TwingleCampaign/Upgrader/BAO/CustomField.php';
class TwingleProject {
public const IN = 'IN';
public const OUT = 'OUT';
public const CIVICRM = 'CIVICRM';
public const TWINGLE = 'TWINGLE';
private static $bInitialized = FALSE;
private static $customFieldMapping;
private $id;
private $values;
private $settings;
/**
* TwingleProject constructor.
*
* @param array $values
* Array of values from which to create a TwingleProject
*
* @param string $origin
* Origin of the array. It can be one of two constants:
* TwingleProject::TWINGLE|CIVICRM
*
* @throws \Exception
*/
public function __construct(array $values, string $origin) {
// If values come from CiviCRM Campaign API
if ($origin == self::CIVICRM) {
// Set id (campaign id) attribute
$this->id = $values['id'];
// Translate custom field names into Twingle field names
self::translateCustomFields($values, self::$OUT);
}
// If values come from Twingle API
elseif ($origin == self::TWINGLE) {
// Translate keys for import
self::translateKeys($values, self::IN);
// Format values for import
self::formatValues($values, self::IN);
}
// Add value for campaign type
$values['campaign_type_id'] = 'twingle_project';
// Import values
$this->values = $values;
// Fetch custom field mapping once
self::init();
}
/**
* Get custom field mapping.
* This function will be fully executed only once, when the TwingleProject
* class gets instantiated for the first time.
*
* @throws \Exception
*/
private static function init() {
if (self::$bInitialized) {
return;
}
self::$customFieldMapping = CustomField::getMapping();
self::$bInitialized = TRUE;
}
/**
* Create the TwingleProject as a campaign in CiviCRM if it does not exist
*
* @param bool $is_test
* If true: don't do any changes
*
* @return array
* Returns a response array that contains title, id, project_id and state
*
* @throws \CiviCRM_API3_Exception
*/
public function create(bool $is_test = FALSE) {
// Create campaign only if it does not already exist
if (!$this->exists()) {
if (!$is_test) {
// Translate Twingle field names into custom field names
$translatedFields = $this->values;
self::translateCustomFields($translatedFields, self::OUT);
// Create campaign
$result = civicrm_api3('Campaign', 'create', $translatedFields);
// Set id attribute
$this->id = $result['id'];
// Check if campaign was created successfully
if ($result['is_error'] == 0) {
$response = $this->getResponse('TwingleProject created');
}
else {
$response = $this->getResponse('TwingleProject creation failed');
}
}
// If this is a test, do not create campaign
else {
$response = $this->getResponse('TwingleProject not yet created');
}
}
else {
// Give information back if campaign already exists
$response = $this->getResponse('TwingleProject already exists');
}
return $response;
}
/**
* Update an existing project
*
* If true: don't do any changes
*
* @param bool $is_test
*
* @return array
* @throws \CiviCRM_API3_Exception
*/
public function update(bool $is_test = FALSE) {
$response = '';
// Translate Twingle field names to custom field names
$translatedFields = $this->values;
self::translateCustomFields($translatedFields, self::OUT);
if (!$is_test) {
$result = civicrm_api3('Campaign', 'create', $translatedFields);
// If the TwingleProject campaign was updated successfully
if ($result['is_error'] == 0) {
$response = $this->getResponse('TwingleProject updated from Twingle');
}
// If the update failed for any reason
else {
$response = $this->getResponse('Updated from Twingle failed');
}
}
// If the TwingleProjects campaign has to get updated but this is in test mode
else {
$response = $this->getResponse('TwingleProject outdated');
}
return $response;
}
/**
* Export values
*
* @return array
* @throws \Exception
*/
public function export() {
$values = $this->values;
self::formatValues($values, self::OUT);
self::translateKeys($values, self::OUT);
unset($values['campaign_type_id']);
return $values;
}
/**
* Check if a project already exists
*
* @return bool
* @throws \CiviCRM_API3_Exception
*/
public function exists() {
// Get custom field name for project_id
$cf_project_id = TwingleProject::$customFieldMapping['twingle_project_id'];
$single = FALSE;
$result = [];
// If there is more than one campaign for a project, handle the duplicates
while (!$single) {
$result = civicrm_api3('Campaign', 'get', [
'sequential' => 1,
'return' => ['id', 'last_modified_date'],
'is_active' => '1',
$cf_project_id => $this->values['id'],
]);
if ($result['count'] > 1) {
TwingleProject::handleDuplicates($result);
}
else {
$single = TRUE;
}
}
// If the campaign for the TwingleProject already exists, some of the
// project's attributes must be updated from the campaign
if ($result['count'] == 1) {
// set campaign id attribute
$this->id = $result['values'][0]['id'];
// set last_modified_date
$this->values['last_modified_date'] =
$result['values'][0]['last_modified_date'];
return TRUE;
}
else {
return FALSE;
}
}
/**
* Instantiate an existing project by campaign id
*
* @param $id
*
* @return \CRM\TwingleCampaign\BAO\TwingleProject
* @throws \CiviCRM_API3_Exception
* @throws \Exception
*/
public static function fetch($id) {
$result = civicrm_api3('Campaign', 'getsingle', [
'sequential' => 1,
'id' => $id,
]);
return new TwingleProject($result, TRUE);
}
/**
* Deactivate all duplicates of a project but the newest one
*
* @param array $result
* The $result array of a civicrm_api3-get-project call
*
* @throws \CiviCRM_API3_Exception
*/
private function handleDuplicates(array $result) {
// Sort projects ascending by the value of the last_modified_date
uasort($result['values'], function ($a, $b) {
return $a['last_modified_date'] <=> $b['last_modified_date'];
});
// Delete the newest project from array to keep it active
array_shift($result['values']);
// Instantiate the left projects to deactivate them
foreach ($result['values'] as $p) {
$project = TwingleProject::fetch($p['id']);
$project->deactivate();
}
}
/**
* Translate array keys between CiviCRM Campaigns and Twingle
*
* @param array $values
* array of which keys shall be translated
*
* @param string $direction
* TwingleProject::IN -> translate array keys from Twingle format into
* CiviCRM format <br>
* TwingleProject::OUT -> translate array keys from CiviCRM format into
* Twingle format
*
* @throws \Exception
*/
private static function translateKeys(array &$values, string $direction) {
// Get json file with translations
$file_path = E::path() .
'/api/v3/TwingleSync/resources/dictionary.json';
$json_file = file_get_contents($file_path);
$json_file_name = pathinfo($file_path)['filename'];
$translations = json_decode($json_file, TRUE);
// Throw an error if json file can't be read
if (!$translations) {
$message = ($json_file_name)
? "Could not read json file $json_file_name"
: "Could not locate json file in path: $file_path";
throw new \Exception($message);
//TODO: use specific exception or create own
}
// Select only fields
$translations = $translations['fields'];
// Set the direction of the translation
if ($direction == self::OUT) {
$translations = array_flip($translations);
}
// Throw error if $direction constant does not match IN or OUT
elseif ($direction != self::IN) {
throw new \Exception(
"Invalid Parameter $direction for translateKeys()"
);
// TODO: use specific exception or create own
}
// Translate keys
foreach ($translations as $origin => $translation) {
$values[$translation] = $values[$origin];
unset($values[$origin]);
}
}
/**
* Translate values between CiviCRM Campaigns and Twingle
*
* @param array $values
* array of which values shall be translated
*
* @param string $direction
* TwingleProject::IN -> translate array values from Twingle to CiviCRM <br>
* TwingleProject::OUT -> translate array values from CiviCRM to Twingle
*
* @throws \Exception
*/
private function formatValues(array &$values, string $direction) {
if ($direction == self::IN) {
// Change timestamp into DateTime string
$values['last_modified_date'] =
self::getDateTime($values['last_modified_date']);
// empty project_type to 'default
$values['type'] = $values['type'] == ''
? 'default'
: $values['type'];
}
elseif ($direction == self::OUT) {
// Change DateTime string into timestamp
$values['last_modified_date'] =
self::getTimestamp($values['last_modified_date']);
// default project_type to ''
$values['type'] = $values['type'] == 'default'
? ''
: $values['type'];
}
else {
throw new \Exception(
"Invalid Parameter $direction for formatValues()"
);
// TODO: use specific exception or create own
}
}
/**
* Translate between Twingle field names and custom field names
*
* @param array $values
* array of which keys shall be translated
*
* @param string $direction
* TwingleProject::IN -> translate field names into custom field names <br>
* TwingleProject::OUT -> translate custom field names into Twingle field
* names
*
*/
private static function translateCustomFields(array &$values, string $direction) {
// Translate from Twingle field name to custom field name
if ($direction == self::IN) {
foreach (TwingleProject::$customFieldMapping as $field => $custom) {
if (array_key_exists(
str_replace(
'twingle_project_',
'',
$field
),
$values)
) {
$values[$custom] = $values[str_replace(
'twingle_project_',
'',
$field
)];
unset($values[$field]);
}
}
}
// Translate from custom field name to Twingle field name
elseif ($direction == self::OUT) {
foreach (TwingleProject::$customFieldMapping as $field => $custom) {
if (array_key_exists(
$custom,
$values
)
) {
$values[str_replace(
'twingle_project_',
'',
$field
)] = $values[$custom];
unset($values[$custom]);
}
}
}
}
/**
* Deactivate a TwingleProject campaign
*
* @return bool
* TRUE if deactivation was successful
*
* @throws \CiviCRM_API3_Exception
*/
public function deactivate() {
$result = civicrm_api3('Campaign', 'create', [
'title' => $this->values['title'],
'id' => $this->id,
'is_active' => '0',
]);
// Return TRUE if TwingleProject campaign was deactivated successfully
if ($result['is_error'] == 0) {
return TRUE;
}
// Return FALSE if deactivation failed
else {
return FALSE;
}
}
public function syncSettings() {
// TODO: sync project settings
}
/**
* Get a response that describes the state of a TwingleProject
*
* @param string $state
* State of the TwingleProject you want the response for
*
* @return array
* Returns a response array that contains title, id, project_id and state
*/
public function getResponse(string $state) {
return [
'title' => $this->values['title'],
'id' => $this->id,
'project_id' => $this->values['id'],
'state' => $state,
];
}
/**
* Validates $input to be either a DateTime string or an Unix timestamp
*
* @param $input
* Pass a DateTime string or a Unix timestamp
*
* @return int
* Returns a Unix timestamp or NULL if $input is invalid
*/
public static function getTimestamp($input) {
// Check whether $input is a Unix timestamp
if (
$dateTime = DateTime::createFromFormat('U', $input)
) {
return $input;
}
// ... or a DateTime string
elseif (
$dateTime = DateTime::createFromFormat('Y-m-d H:i:s', $input)
) {
return $dateTime->getTimestamp();
}
// ... or invalid
else {
return NULL;
}
}
/**
* Validates $input to be either a DateTime string or an Unix timestamp
*
* @param $input
* Pass a DateTime string or a Unix timestamp
*
* @return string
* Returns a DateTime string or NULL if $input is invalid
*/
public static function getDateTime($input) {
// Check whether $input is a Unix timestamp
if (
$dateTime = DateTime::createFromFormat('U', $input)
) {
return $dateTime->format('Y-m-d H:i:s');
}
// ... or a DateTime string
elseif (
$dateTime = DateTime::createFromFormat('Y-m-d H:i:s', $input)
) {
return $input;
}
// ... or invalid
else {
return NULL;
}
}
/**
* Return a timestamp of the last update of the TwingleProject
*
* @return int|null
*/
public function lastUpdate() {
return self::getTimestamp($this->values['last_modified_date']);
}
/**
* @return mixed
*/
public function getId() {
return $this->id;
}
}