use inheritance for campaign classes

This commit is contained in:
Marc Michalsky forumZFD 2020-10-29 23:14:18 +01:00
parent 24d224cc38
commit 6b434e455f
Signed by untrusted user who does not match committer: marc.koch
GPG key ID: 12406554CFB028B9
10 changed files with 932 additions and 475 deletions

View file

@ -10,24 +10,7 @@ use Exception;
use CiviCRM_API3_Exception;
class TwingleProject {
// IN means: heading into CiviCRM database
public const IN = 'IN';
// OUT means: coming from the CiviCRM database
public const OUT = 'OUT';
public const CIVICRM = 'CIVICRM';
public const TWINGLE = 'TWINGLE';
private static $bInitialized = FALSE;
private $id;
private $values;
class TwingleProject extends Campaign {
/**
* TwingleProject constructor.
@ -43,101 +26,17 @@ class TwingleProject {
* @throws Exception
*/
public function __construct(array $project, string $origin) {
parent::__construct($project, $origin);
// If values come from CiviCRM Campaign API
if ($origin == self::CIVICRM) {
// Set id (campaign id) attribute
$this->id = $project['id'];
// Translate custom field names into Twingle field names
self::translateCustomFields($project, self::OUT);
// Translate keys and values
self::formatValues($project, self::OUT);
self::translateKeys($project, self::OUT);
}
$this->className = get_class($this);
// Add value for campaign type
$project['campaign_type_id'] = 'twingle_project';
$this->values['campaign_type_id'] = 'twingle_project';
// Set project values attribute
$this->values = $project;
}
// Get custom field name for project_id
$this->id_custom_field = Cache::getInstance()
->getCustomFieldMapping()['twingle_project_id'];
/**
* 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 status
*
* @throws CiviCRM_API3_Exception
* @throws Exception
*/
public function create(bool $is_test = FALSE) {
// Create campaign only if it does not already exist
if (!$is_test) {
// Prepare project values for import into database
$values_prepared_for_import = $this->values;
self::formatValues(
$values_prepared_for_import,
self::IN
);
self::translateKeys(
$values_prepared_for_import,
self::IN
);
self::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('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');
}
return $response;
}
/**
* Update an existing project
*
* @param array $values
* Array with values to update
*
* @throws Exception
*/
public function update(array $values) {
// Update project values
$this->values = array_merge($this->values, $values);
}
@ -156,7 +55,7 @@ class TwingleProject {
self::formatValues($values, self::OUT);
// Get template for project
$project = self::$templates['project'];
$project = Cache::getInstance()->getTemplates()['project'];
// Replace array items which the Twingle API does not expect
foreach ($values as $key => $value) {
@ -169,158 +68,6 @@ class TwingleProject {
}
/**
* Check if a TwingleProject campaign already exists and if so set attributes
* to the values of the existing campaign.
*
* @return bool
* Returns TRUE id the TwingleProject campaign already exists
*
* @throws CiviCRM_API3_Exception
*
* @throws Exception
*/
public function exists() {
$result = [];
$single = FALSE;
// Get custom field name for project_id
$cf_project_id = Cache::getInstance()
->getCustomFieldMapping()['twingle_project_id'];
// If there is more than one campaign for a project, handle the duplicates
while (!$single) {
$result = civicrm_api3('Campaign', 'get', [
'sequential' => 1,
'is_active' => 1,
$cf_project_id => $this->values['id'],
]);
if ($result['count'] > 1) {
// TODO: abort loop if function fails
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) {
// Extract project values from result array
$values = $result['values'][0];
// Set campaign id attribute
$this->id = $values['id'];
// Translate custom field names back
self::translateCustomFields($values, self::OUT);
// Translate keys from CiviCRM format to Twingle format
self::translateKeys($values, self::OUT);
// Set attributes to the values of the existing TwingleProject campaign
// to reflect the state of the actual campaign in the database
$this->update($values);
return TRUE;
}
else {
return FALSE;
}
}
/**
* Instantiate an existing project by campaign id
*
* @param $id
*
* @return 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['values'],
self::CIVICRM
);
}
/**
* 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']);
// deactivate the projects
foreach ($result['values'] as $p) {
self::deactivateById($p['id']);
}
}
/**
* 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
*/
public static function translateKeys(array &$values, string $direction) {
// Get translations for fields
$field_translations = Cache::getInstance()->getTranslations()['fields'];
// Set the direction of the translation
if ($direction == self::OUT) {
$field_translations = array_flip($field_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 ($field_translations as $origin => $translation) {
$values[$translation] = $values[$origin];
unset($values[$origin]);
}
}
/**
* Translate values between CiviCRM Campaigns and Twingle
*
@ -373,72 +120,6 @@ class TwingleProject {
}
/**
* 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
*
*/
public static function translateCustomFields(array &$values, string $direction) {
// Translate from Twingle field name to custom field name
if ($direction == self::IN) {
foreach (Cache::getInstance()
->getCustomFieldMapping() as $field => $custom) {
if (array_key_exists(
str_replace(
'twingle_project_',
'',
$field
),
$values)
) {
$values[$custom] = $values[str_replace(
'twingle_project_',
'',
$field
)];
unset($values[str_replace(
'twingle_project_',
'',
$field
)]
);
}
}
}
// Translate from custom field name to Twingle field name
elseif ($direction == self::OUT) {
foreach (Cache::getInstance()
->getCustomFieldMapping() as $field => $custom) {
if (array_key_exists(
$custom,
$values
)
) {
$values[str_replace(
'twingle_project_',
'',
$field
)] = $values[$custom];
unset($values[$custom]);
}
}
}
}
/**
* Set embed data fields
*
@ -481,41 +162,6 @@ class TwingleProject {
}
/**
* Deactivate a TwingleProject campaign by ID
*
* @param $id
* ID of the TwingleProject campaign that should get deactivated
*
* @return bool
* TRUE if deactivation was successful
*
* @throws CiviCRM_API3_Exception
*/
public static function deactivateById($id) {
$result = civicrm_api3('Campaign', 'getsingle', [
'id' => $id,
'sequential' => 1,
]);
$result = civicrm_api3('Campaign', 'create', [
'title' => $result['title'],
'id' => $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;
}
}
/**
* Get a response that describes the status of a TwingleProject
@ -537,94 +183,13 @@ class TwingleProject {
}
/**
* 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_update']);
}
/**
* Returns the project_id of a TwingleProject
*
* @return int
*/
public function getProjectId() {
return $this->values['id'];
}
/**
* @return mixed
*/
public function getId() {
return $this->id;
return (int) $this->values['id'];
}
}