move formatValues() and create() methods to child classes

This commit is contained in:
Marc Michalsky forumZFD 2020-11-02 17:45:49 +01:00
parent 0492687610
commit a81eb0c438
Signed by untrusted user who does not match committer: marc.koch
GPG key ID: 12406554CFB028B9
3 changed files with 138 additions and 57 deletions

View file

@ -316,58 +316,6 @@ abstract class Campaign {
} }
/**
* Translate values between CiviCRM Campaigns and Twingle
*
* @param array $values
* array of which values shall be translated
*
* @param string $direction
* Campaign::IN -> translate array values from Twingle to CiviCRM <br>
* Campaign::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
if ($values['last_update']) {
$values['last_update'] =
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'];
// Cast project target to integer
$values['project_target'] = (int) $values['project_target'];
}
else {
throw new Exception(
"Invalid Parameter $direction for formatValues()"
);
}
}
/** /**
* Translate between Twingle field names and custom field names * Translate between Twingle field names and custom field names
* *

View file

@ -150,6 +150,65 @@ class TwingleEvent extends Campaign {
} }
/**
* Create the Event 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
);
$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");
}
}
// If this is a test, do not create campaign
else {
$response = $this->getResponse("$this->className not yet created");
}
return $response;
}
/** /**
* Translate values between CiviCRM Campaigns and Twingle * Translate values between CiviCRM Campaigns and Twingle
* *
@ -162,15 +221,30 @@ class TwingleEvent extends Campaign {
* *
* @throws Exception * @throws Exception
*/ */
private function formatValues(array &$values, string $direction) { protected function formatValues(array &$values, string $direction) {
if ($direction == self::IN) { if ($direction == self::IN) {
// Change timestamp into DateTime string // Change timestamp into DateTime string
if ($values['last_update']) { if ($values['updated_at']) {
$values['last_update'] = $values['updated_at'] =
self::getDateTime($values['last_update']); self::getDateTime($values['updated_at']);
} }
if ($values['confirmed_at']) {
$values['confirmed_at'] =
self::getDateTime($values['confirmed_at']);
}
if ($values['created_at']) {
$values['created_at'] =
self::getDateTime($values['created_at']);
}
if ($values['user_name']) {
$values['user_name'] = $this->matchContact(
$values['user_name'],
$values['user_email']
);
}
} }
elseif ($direction == self::OUT) { elseif ($direction == self::OUT) {

View file

@ -196,6 +196,65 @@ class TwingleProject extends Campaign {
} }
/**
* Create the Campaign 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
);
$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");
}
}
// If this is a test, do not create campaign
else {
$response = $this->getResponse("$this->className not yet created");
}
return $response;
}
/** /**
* Translate values between CiviCRM Campaigns and Twingle * Translate values between CiviCRM Campaigns and Twingle
* *
@ -208,7 +267,7 @@ class TwingleProject extends Campaign {
* *
* @throws Exception * @throws Exception
*/ */
private function formatValues(array &$values, string $direction) { protected function formatValues(array &$values, string $direction) {
if ($direction == self::IN) { if ($direction == self::IN) {