From ab8c16fac2ff52892a6b3ec1e18b9504fc473332 Mon Sep 17 00:00:00 2001 From: Marc Michalsky forumZFD Date: Mon, 12 Apr 2021 10:49:56 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=EF=B8=8F=20implement=20upgrade=20func?= =?UTF-8?q?tion=20to=20create=20new=20custom=20fields?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CRM/TwingleCampaign/BAO/CampaignType.php | 7 ++-- CRM/TwingleCampaign/BAO/CustomField.php | 15 ++++---- CRM/TwingleCampaign/BAO/CustomGroup.php | 10 +++++- CRM/TwingleCampaign/Upgrader.php | 44 ++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 11 deletions(-) diff --git a/CRM/TwingleCampaign/BAO/CampaignType.php b/CRM/TwingleCampaign/BAO/CampaignType.php index 486c970..8e9f686 100644 --- a/CRM/TwingleCampaign/BAO/CampaignType.php +++ b/CRM/TwingleCampaign/BAO/CampaignType.php @@ -29,7 +29,7 @@ class CRM_TwingleCampaign_BAO_CampaignType { /** * @throws \CiviCRM_API3_Exception */ - public function create() { + public function create(bool $upgrade = false) { $field = civicrm_api3( 'OptionValue', @@ -59,7 +59,7 @@ class CRM_TwingleCampaign_BAO_CampaignType { for \"$this->label\": $error_message"); } } - else { + elseif (!$upgrade) { $campaignType = self::fetch($this->name); foreach ($this as $var => $value) { if (array_key_exists($var, $campaignType->getSetAttributes())) { @@ -67,6 +67,9 @@ class CRM_TwingleCampaign_BAO_CampaignType { } } } + else { + $this->value = $field['values'][0]['value']; + } } /** diff --git a/CRM/TwingleCampaign/BAO/CustomField.php b/CRM/TwingleCampaign/BAO/CustomField.php index b393e24..2145b67 100644 --- a/CRM/TwingleCampaign/BAO/CustomField.php +++ b/CRM/TwingleCampaign/BAO/CustomField.php @@ -65,9 +65,12 @@ class CRM_TwingleCampaign_BAO_CustomField { /** * Creates a CustomField by calling CiviCRM API v.3 * + * @param bool $upgrade + * If true: Does not show UF message if custom field already exists + * * @throws \CiviCRM_API3_Exception */ - public function create() { + public function create(bool $upgrade = false) { // Check if the field already exists $field = civicrm_api3( @@ -118,14 +121,10 @@ class CRM_TwingleCampaign_BAO_CustomField { } } } - else { - CRM_Utils_System::setUFMessage("Creation of custom field '$this->name' - failed, because a custom field with that name already exists. - Find more information in the logs."); + elseif (!$upgrade) { + CRM_Utils_System::setUFMessage(E::ts('Creation of custom field \'%1\' failed, because a custom field with that name already exists. Find more information in the logs.', [1 => $this->name])); Civi::log() - ->error("$this->extensionName could not create new custom field - \"$this->name\" for group \"$this->custom_group_id\" because a - field with that name already exists."); + ->error("$this->extensionName could not create new custom field \"$this->name\" for group \"$this->custom_group_id\" because a field with that name already exists."); } } diff --git a/CRM/TwingleCampaign/BAO/CustomGroup.php b/CRM/TwingleCampaign/BAO/CustomGroup.php index 3b92099..20fa1e5 100644 --- a/CRM/TwingleCampaign/BAO/CustomGroup.php +++ b/CRM/TwingleCampaign/BAO/CustomGroup.php @@ -41,9 +41,12 @@ class CRM_TwingleCampaign_BAO_CustomGroup { } /** + * @param bool $upgrade + * If true: Does not show UF message if custom group already exists + * * @throws \CiviCRM_API3_Exception */ - public function create() { + public function create(bool $upgrade = false) { $field = civicrm_api3( 'CustomGroup', @@ -86,6 +89,11 @@ class CRM_TwingleCampaign_BAO_CustomGroup { } } + elseif (!$upgrade) { + CRM_Utils_System::setUFMessage(E::ts('Creation of custom group \'%1\' failed, because a custom group with that name already exists. Find more information in the logs.', [1 => $this->name])); + Civi::log() + ->error("$this->extensionName could not create new custom group \"$this->name\" because a group with that name already exists."); + } } /** diff --git a/CRM/TwingleCampaign/Upgrader.php b/CRM/TwingleCampaign/Upgrader.php index dea29ab..b87e152 100644 --- a/CRM/TwingleCampaign/Upgrader.php +++ b/CRM/TwingleCampaign/Upgrader.php @@ -16,6 +16,50 @@ class CRM_TwingleCampaign_Upgrader extends CRM_TwingleCampaign_Upgrader_Base { // By convention, functions that look like "function upgrade_NNNN()" are // upgrade tasks. They are executed in order (like Drupal's hook_update_N). + /** + * @throws \CiviCRM_API3_Exception + */ + public function upgrade_01() { + + $campaign_info = require E::path() . + '/CRM/TwingleCampaign/resources/campaigns.php'; + $option_values = require E::path() . + '/CRM/TwingleCampaign/resources/option_values.php'; + + // Create campaign types + foreach ($campaign_info['campaign_types'] as $campaign_type) { + new CampaignType($campaign_type); + } + foreach (CampaignType::getCampaignTypes() as $campaign_type) { + $campaign_type->create(true); + } + + // Create custom groups + foreach ($campaign_info['custom_groups'] as $custom_group) { + foreach (CampaignType::getCampaignTypes() as $campaign_type) { + if ($campaign_type->getName() == $custom_group['campaign_type']) { + $custom_group['extends_entity_column_value'] = $campaign_type->getValue(); + } + } + $cg = new CustomGroup($custom_group); + $cg->create(true); + } + + // Create custom fields + foreach ($campaign_info['custom_fields'] as $custom_field) { + $cf = new CustomField($custom_field); + $cf->create(true); + } + + // Create option values + foreach ($option_values as $option_value) { + $ov = new OptionValue($option_value); + $ov->create(); + } + + return TRUE; + } + /** * @throws \Exception */