From 0ce2d2530bcd339550ce7c227c00563028e36d96 Mon Sep 17 00:00:00 2001 From: Marc Michalsky forumZFD Date: Tue, 9 Mar 2021 21:39:09 +0100 Subject: [PATCH 1/6] fix [#47] | Don't create new address only from user_country --- api/v3/TwingleDonation/Submit.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/api/v3/TwingleDonation/Submit.php b/api/v3/TwingleDonation/Submit.php index 21890d4..e1e7a13 100644 --- a/api/v3/TwingleDonation/Submit.php +++ b/api/v3/TwingleDonation/Submit.php @@ -335,6 +335,16 @@ function civicrm_api3_twingle_donation_Submit($params) { } } + // Do not creat a new address if user_country is the only address + // parameter. See issue #47 + if ( + !array_key_exists('street_address', $params) && + !array_key_exists('postal_code', $params) && + !array_key_exists('city', $params) + ) { + unset($params['country']); + } + // Prepare parameter mapping for organisation. if (!empty($params['user_company'])) { $params['organization_name'] = $params['user_company']; From a30b8f02cb583dc537cdd58bf148af5d65546d81 Mon Sep 17 00:00:00 2001 From: Marc Michalsky forumZFD Date: Tue, 9 Mar 2021 21:39:09 +0100 Subject: [PATCH 2/6] fix [#47] | Don't create new address only from user_country --- api/v3/TwingleDonation/Submit.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/api/v3/TwingleDonation/Submit.php b/api/v3/TwingleDonation/Submit.php index 9ea475f..1aa4197 100644 --- a/api/v3/TwingleDonation/Submit.php +++ b/api/v3/TwingleDonation/Submit.php @@ -335,6 +335,16 @@ function civicrm_api3_twingle_donation_Submit($params) { } } + // Do not creat a new address if user_country is the only address + // parameter. See issue #47 + if ( + !array_key_exists('street_address', $params) && + !array_key_exists('postal_code', $params) && + !array_key_exists('city', $params) + ) { + unset($params['country']); + } + // Prepare parameter mapping for organisation. if (!empty($params['user_company'])) { $params['organization_name'] = $params['user_company']; From 9969c99f7070da60d6661bff6d66f3d2b3aa2a9a Mon Sep 17 00:00:00 2001 From: Marc Michalsky Date: Wed, 17 Nov 2021 16:23:29 +0100 Subject: [PATCH 3/6] Make required address components configurable The required address components can be selected in the corresponding profile. --- CRM/Twingle/Form/Profile.php | 14 ++++++++++++++ CRM/Twingle/Profile.php | 9 ++++++++- api/v3/TwingleDonation/Submit.php | 22 ++++++++++++++-------- templates/CRM/Twingle/Form/Profile.hlp | 6 ++++++ templates/CRM/Twingle/Form/Profile.tpl | 21 +++++++++++++++++++++ 5 files changed, 63 insertions(+), 9 deletions(-) diff --git a/CRM/Twingle/Form/Profile.php b/CRM/Twingle/Form/Profile.php index 8322940..b95ddc4 100644 --- a/CRM/Twingle/Form/Profile.php +++ b/CRM/Twingle/Form/Profile.php @@ -416,6 +416,20 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form { array() ); + $this->add( + 'select', + 'required_address_components', + E::ts('Required address components'), + [ + 'street_address' => E::ts("Street"), + 'postal_code' => E::ts("Postal Code"), + 'city' => E::ts("City"), + 'country' => E::ts("Country"), + ], + FALSE, // is not required + ['class' => 'crm-select2 huge', 'multiple' => 'multiple'] + ); + $this->add( 'textarea', // field type 'custom_field_mapping', // field name diff --git a/CRM/Twingle/Profile.php b/CRM/Twingle/Profile.php index f59d236..e6a6642 100644 --- a/CRM/Twingle/Profile.php +++ b/CRM/Twingle/Profile.php @@ -226,7 +226,8 @@ class CRM_Twingle_Profile { 'membership_type_id', 'membership_type_id_recur', 'membership_postprocess_call', - 'newsletter_double_opt_in' + 'newsletter_double_opt_in', + 'required_address_components', ), // Add payment methods. array_keys(static::paymentInstruments()), @@ -300,6 +301,12 @@ class CRM_Twingle_Profile { 'membership_type_id' => NULL, 'membership_type_id_recur' => NULL, 'newsletter_double_opt_in' => NULL, + 'required_address_components' => [ + 'street_address', + 'postal_code', + 'city', + 'country', + ], ) // Add contribution status for all payment methods. + array_fill_keys(array_map(function($attribute) { diff --git a/api/v3/TwingleDonation/Submit.php b/api/v3/TwingleDonation/Submit.php index 1aa4197..ce75369 100644 --- a/api/v3/TwingleDonation/Submit.php +++ b/api/v3/TwingleDonation/Submit.php @@ -335,14 +335,20 @@ function civicrm_api3_twingle_donation_Submit($params) { } } - // Do not creat a new address if user_country is the only address - // parameter. See issue #47 - if ( - !array_key_exists('street_address', $params) && - !array_key_exists('postal_code', $params) && - !array_key_exists('city', $params) - ) { - unset($params['country']); + // Do not creat a new address if 'country' is the only address + // component. See issue #47 + $required_address_components = + $profile->getAttribute('required_address_components'); + $unset_address_params = False; + foreach ($required_address_components as $req) { + if (empty($params[$req])) { + $unset_address_params = True; + } + } + if ($unset_address_params) { + foreach($required_address_components as $req) { + unset($params[$req]); + } } // Prepare parameter mapping for organisation. diff --git a/templates/CRM/Twingle/Form/Profile.hlp b/templates/CRM/Twingle/Form/Profile.hlp index d83b35e..b4c6ff9 100644 --- a/templates/CRM/Twingle/Form/Profile.hlp +++ b/templates/CRM/Twingle/Form/Profile.hlp @@ -54,6 +54,12 @@ {/ts} {/htxt} +{htxt id='id-required_address_components'} +

{ts domain="de.systopia.twingle"}Select the address components that must be present to create a new address for the contact.{/ts}

+

{ts domain="de.systopia.twingle"}Depending on your XCM settings, the newly transferred address will replace the old one. This behavior may be intentional, as you always want to have the most current address.{/ts}

+

{ts domain="de.systopia.twingle"}ATTENTION: In some cases Twingle sends only the country of the user. If no other address components are selected in the required address components, the current user address will be overwritten with an address containing only the country, depending on the XCM settings.{/ts}

+{/htxt} + {htxt id='id-custom_field_mapping'} {ts domain="de.systopia.twingle"}

Map Twingle custom fields to CiviCRM custom fields using the following format (each assignment in a separate line):

twingle_field_1=custom_123
twingle_field_2=custom_789
diff --git a/templates/CRM/Twingle/Form/Profile.tpl b/templates/CRM/Twingle/Form/Profile.tpl index f5e95f5..49a0097 100644 --- a/templates/CRM/Twingle/Form/Profile.tpl +++ b/templates/CRM/Twingle/Form/Profile.tpl @@ -290,6 +290,27 @@ {$form.contribution_source.html} + + {$form.required_address_components.label} + + + {$form.required_address_components.html} + + {$form.custom_field_mapping.label} From d910ce4846b38c380c17ce0daf0da7cd1b4eab1c Mon Sep 17 00:00:00 2001 From: Marc Michalsky Date: Wed, 17 Nov 2021 16:52:14 +0100 Subject: [PATCH 4/6] Comment updated --- api/v3/TwingleDonation/Submit.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/api/v3/TwingleDonation/Submit.php b/api/v3/TwingleDonation/Submit.php index ce75369..1dddb72 100644 --- a/api/v3/TwingleDonation/Submit.php +++ b/api/v3/TwingleDonation/Submit.php @@ -335,8 +335,9 @@ function civicrm_api3_twingle_donation_Submit($params) { } } - // Do not creat a new address if 'country' is the only address - // component. See issue #47 + // Make required address components configurable to prevent existing + // contact addresses from being overwritten with incomplete address + // information. See issue #47 $required_address_components = $profile->getAttribute('required_address_components'); $unset_address_params = False; From 48164479f72d7e16d2cd1c84f5f549755894db2d Mon Sep 17 00:00:00 2001 From: Marc Michalsky Date: Wed, 17 Nov 2021 18:20:25 +0100 Subject: [PATCH 5/6] hard code array of address components to unset --- api/v3/TwingleDonation/Submit.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/api/v3/TwingleDonation/Submit.php b/api/v3/TwingleDonation/Submit.php index 1dddb72..f773054 100644 --- a/api/v3/TwingleDonation/Submit.php +++ b/api/v3/TwingleDonation/Submit.php @@ -347,7 +347,12 @@ function civicrm_api3_twingle_donation_Submit($params) { } } if ($unset_address_params) { - foreach($required_address_components as $req) { + foreach([ + 'street_address', + 'postal_code', + 'city', + 'country', + ] as $req) { unset($params[$req]); } } From 6779349505ba35a54fa5a69948c40d50b6d68dc3 Mon Sep 17 00:00:00 2001 From: Jens Schuppe Date: Mon, 2 May 2022 14:30:35 +0200 Subject: [PATCH 6/6] [#47] Simplify evaluating required address components and re-arrange profile form --- api/v3/TwingleDonation/Submit.php | 33 +++++++++----------- templates/CRM/Twingle/Form/Profile.hlp | 8 +++-- templates/CRM/Twingle/Form/Profile.tpl | 42 +++++++++++++------------- 3 files changed, 40 insertions(+), 43 deletions(-) diff --git a/api/v3/TwingleDonation/Submit.php b/api/v3/TwingleDonation/Submit.php index f773054..4025f97 100644 --- a/api/v3/TwingleDonation/Submit.php +++ b/api/v3/TwingleDonation/Submit.php @@ -335,25 +335,20 @@ function civicrm_api3_twingle_donation_Submit($params) { } } - // Make required address components configurable to prevent existing - // contact addresses from being overwritten with incomplete address - // information. See issue #47 - $required_address_components = - $profile->getAttribute('required_address_components'); - $unset_address_params = False; - foreach ($required_address_components as $req) { - if (empty($params[$req])) { - $unset_address_params = True; - } - } - if ($unset_address_params) { - foreach([ - 'street_address', - 'postal_code', - 'city', - 'country', - ] as $req) { - unset($params[$req]); + // Remove address data when any address component that is configured as + // required is missing. + // See https://github.com/systopia/de.systopia.twingle/issues/47 + foreach ($profile->getAttribute('required_address_components', []) as $required_address_component) { + if (empty($params[$required_address_component])) { + foreach ([ + 'street_address', + 'postal_code', + 'city', + 'country', + ] as $address_param) { + unset($params[$address_param]); + } + break; } } diff --git a/templates/CRM/Twingle/Form/Profile.hlp b/templates/CRM/Twingle/Form/Profile.hlp index b4c6ff9..e3841d7 100644 --- a/templates/CRM/Twingle/Form/Profile.hlp +++ b/templates/CRM/Twingle/Form/Profile.hlp @@ -12,6 +12,7 @@ | written permission from the original author(s). | +-------------------------------------------------------------*} +{crmScope extensionKey='de.systopia.twingle'} {htxt id='id-location_type_id'} {ts domain="de.systopia.twingle"}Select which location type to use for addresses for individuals, either when no organisation name is specified, or an organisation address can not be shared with the individual contact.{/ts} {/htxt} @@ -55,9 +56,9 @@ {/htxt} {htxt id='id-required_address_components'} -

{ts domain="de.systopia.twingle"}Select the address components that must be present to create a new address for the contact.{/ts}

-

{ts domain="de.systopia.twingle"}Depending on your XCM settings, the newly transferred address will replace the old one. This behavior may be intentional, as you always want to have the most current address.{/ts}

-

{ts domain="de.systopia.twingle"}ATTENTION: In some cases Twingle sends only the country of the user. If no other address components are selected in the required address components, the current user address will be overwritten with an address containing only the country, depending on the XCM settings.{/ts}

+

{ts}Select the address components that must be present to create or update an address for the contact.{/ts}

+

{ts}Depending on your XCM settings, the transferred address might replace an existing one.{/ts}

+

{ts}Since in some cases Twingle send the country of the user as the only address parameter, depending on your XCM configuration, not declaring other address components as required might lead to the current user address being overwritten with an address containing the country only.{/ts}

{/htxt} {htxt id='id-custom_field_mapping'} @@ -74,3 +75,4 @@
  • ContributionRecur – Will be set on the recurring contribution and deriving single contributions
  • {/ts} {/htxt} +{/crmScope} \ No newline at end of file diff --git a/templates/CRM/Twingle/Form/Profile.tpl b/templates/CRM/Twingle/Form/Profile.tpl index 49a0097..00794ef 100644 --- a/templates/CRM/Twingle/Form/Profile.tpl +++ b/templates/CRM/Twingle/Form/Profile.tpl @@ -113,6 +113,27 @@ {$form.location_type_id_organisation.html} + + {$form.required_address_components.label} + + + {$form.required_address_components.html} + + {$form.financial_type_id.label} @@ -290,27 +311,6 @@ {$form.contribution_source.html} - - {$form.required_address_components.label} - - - {$form.required_address_components.html} - - {$form.custom_field_mapping.label}