From a81eb0c438f9dda86b617452a7f4ebcf4f1106f4 Mon Sep 17 00:00:00 2001 From: Marc Michalsky forumZFD Date: Mon, 2 Nov 2020 17:45:49 +0100 Subject: [PATCH] move formatValues() and create() methods to child classes --- CRM/TwingleCampaign/BAO/Campaign.php | 52 -------------- CRM/TwingleCampaign/BAO/TwingleEvent.php | 82 ++++++++++++++++++++-- CRM/TwingleCampaign/BAO/TwingleProject.php | 61 +++++++++++++++- 3 files changed, 138 insertions(+), 57 deletions(-) diff --git a/CRM/TwingleCampaign/BAO/Campaign.php b/CRM/TwingleCampaign/BAO/Campaign.php index 3828be8..0e43c47 100644 --- a/CRM/TwingleCampaign/BAO/Campaign.php +++ b/CRM/TwingleCampaign/BAO/Campaign.php @@ -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
- * 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 * diff --git a/CRM/TwingleCampaign/BAO/TwingleEvent.php b/CRM/TwingleCampaign/BAO/TwingleEvent.php index 32a00d8..24c8c4f 100644 --- a/CRM/TwingleCampaign/BAO/TwingleEvent.php +++ b/CRM/TwingleCampaign/BAO/TwingleEvent.php @@ -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 * @@ -162,15 +221,30 @@ class TwingleEvent extends Campaign { * * @throws Exception */ - private function formatValues(array &$values, string $direction) { + protected 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']); + if ($values['updated_at']) { + $values['updated_at'] = + 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) { diff --git a/CRM/TwingleCampaign/BAO/TwingleProject.php b/CRM/TwingleCampaign/BAO/TwingleProject.php index bf77f23..9b2d315 100644 --- a/CRM/TwingleCampaign/BAO/TwingleProject.php +++ b/CRM/TwingleCampaign/BAO/TwingleProject.php @@ -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 * @@ -208,7 +267,7 @@ class TwingleProject extends Campaign { * * @throws Exception */ - private function formatValues(array &$values, string $direction) { + protected function formatValues(array &$values, string $direction) { if ($direction == self::IN) {