diff --git a/CRM/Twingle/Form/Profile.php b/CRM/Twingle/Form/Profile.php index a38dc7e..be9dd27 100644 --- a/CRM/Twingle/Form/Profile.php +++ b/CRM/Twingle/Form/Profile.php @@ -327,6 +327,14 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form { ); } + $this->add( + 'checkbox', // field type + 'newsletter_double_opt_in', // field name + E::ts('Use Double-Opt-In for newsletter'), // field label + FALSE, // is not required + array() + ); + $this->add( 'select', // field type 'newsletter_groups', // field name @@ -549,6 +557,9 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form { } $this->profile->setName($values['name']); foreach ($this->profile->getData() as $element_name => $value) { + if ($element_name == 'newsletter_double_opt_in') { + $values[$element_name] = (int) isset($values[$element_name]); + } if (isset($values[$element_name])) { $this->profile->setAttribute($element_name, $values[$element_name]); } @@ -798,6 +809,7 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form { * @return array * * @throws \CiviCRM_API3_Exception + * */ public static function getNewsletterGroups() { if (!isset(static::$_newsletterGroups)) { diff --git a/CRM/Twingle/Profile.php b/CRM/Twingle/Profile.php index be93bc6..9c86f1a 100644 --- a/CRM/Twingle/Profile.php +++ b/CRM/Twingle/Profile.php @@ -221,6 +221,7 @@ class CRM_Twingle_Profile { 'membership_type_id', 'membership_type_id_recur', 'membership_postprocess_call', + 'newsletter_double_opt_in' ), // Add payment methods. array_keys(static::paymentInstruments()), @@ -293,6 +294,7 @@ class CRM_Twingle_Profile { 'custom_field_mapping' => NULL, 'membership_type_id' => NULL, 'membership_type_id_recur' => NULL, + 'newsletter_double_opt_in' => NULL, ) // Add contribution status for all payment methods. + array_fill_keys(array_map(function($attribute) { diff --git a/README.md b/README.md index e8c2a1d..bb52ecd 100644 --- a/README.md +++ b/README.md @@ -43,23 +43,24 @@ The *default* profile is used whenever the plugin cannot match the Twingle project ID from any other profile. Therefore the default profile will be used for all newly created Twingle projects. -| Label | Description | -|---------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| Profile name | Internal name, used inside the extension. | -| Project IDs | Twingle project IDs. Separate multiple IDs with commas. | -| Location type | Specify how the address data sent by the form should be categorised in CiviCRM. The list is based on your CiviCRM configuration. | -| Location type for organisations | Specify how the address data sent by the form should be categorised in CiviCRM for organisational donations. The list is based on your CiviCRM configuration. | -| Financial type | Specify which financial type incoming one-time donations should be recorded with in CiviCRM. The list is based on your CiviCRM configuration. | -| Financial type (recurring) | Specify which financial type incoming recurring donations should be recorded with in CiviCRM. The list is based on your CiviCRM configuration. | -| CiviSEPA creditor | When enabled to integrate with CiviSEPA, specify the CiviSEPA creditor to use. | -| Gender options | Specify which CiviCRM gender option the incoming Twingle gender value should be mapped to. The list is based on your CiviCRM configuration. | -| Record *Payment method* as | Specifiy the payment methods mapping for incoming donations for each Twingle payment method. | -| Sign up for groups | Whenever the donor checked the newsletter/postal mailing/donation receipt checkbox on the Twingle form, the contact will be added to the groups listed here. | -| Assign donation to campaign | The donation will be assigned to the selected campaign. If a campaign ID is being submitted using the `campaign_id` parameter, this setting will be overridden with the submitted value. | -| Create membership of type | A membership of the selected type will be created for the Individual contact for incoming one-time donations. If no membership type is selected, no membership will be created. | -| Create membership of type (recurring) | A membership of the selected type will be created for the Individual contact for incoming recurring donations. If no membership type is selected, no membership will be created. | -| Contribution source | The configured value will be set as the "Source" field for the contribution. | -| Custom field mapping | Additional field values may be set to CiviCRM custom fields using a mapping. See the option's help text for the exact format. | +| Label | Description | +|---------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| Profile name | Internal name, used inside the extension. | +| Project IDs | Twingle project IDs. Separate multiple IDs with commas. | +| Location type | Specify how the address data sent by the form should be categorised in CiviCRM. The list is based on your CiviCRM configuration. | +| Location type for organisations | Specify how the address data sent by the form should be categorised in CiviCRM for organisational donations. The list is based on your CiviCRM configuration. | +| Financial type | Specify which financial type incoming one-time donations should be recorded with in CiviCRM. The list is based on your CiviCRM configuration. | +| Financial type (recurring) | Specify which financial type incoming recurring donations should be recorded with in CiviCRM. The list is based on your CiviCRM configuration. | +| CiviSEPA creditor | When enabled to integrate with CiviSEPA, specify the CiviSEPA creditor to use. | +| Gender options | Specify which CiviCRM gender option the incoming Twingle gender value should be mapped to. The list is based on your CiviCRM configuration. | +| Record *Payment method* as | Specifiy the payment methods mapping for incoming donations for each Twingle payment method. | +| Double Opt-In | Let CiviCRM handle the double opt-in. Group memberships for newsletter mailing lists stay pending until the subscription gets confirmed. Note that this only works for public mailing lists. Any non-public mailing list will be ignored. Do not forget to disable Twingle's double opt-in option in the Twingle Manager. | +| Sign up for groups | Whenever the donor checked the newsletter/postal mailing/donation receipt checkbox on the Twingle form, the contact will be added to the groups listed here. | +| Assign donation to campaign | The donation will be assigned to the selected campaign. If a campaign ID is being submitted using the `campaign_id` parameter, this setting will be overridden with the submitted value. | +| Create membership of type | A membership of the selected type will be created for the Individual contact for incoming one-time donations. If no membership type is selected, no membership will be created. | +| Create membership of type (recurring) | A membership of the selected type will be created for the Individual contact for incoming recurring donations. If no membership type is selected, no membership will be created. | +| Contribution source | The configured value will be set as the "Source" field for the contribution. | +| Custom field mapping | Additional field values may be set to CiviCRM custom fields using a mapping. See the option's help text for the exact format. | ## API documentation @@ -147,4 +148,4 @@ The action accepts the following parameters: You may also refer to [the code](https://github.com/systopia/de.systopia.twingle/blob/master/api/v3/TwingleDonation/Cancel.php) -for more insight into this API action. +for more insight into this API action. \ No newline at end of file diff --git a/api/v3/TwingleDonation/Submit.php b/api/v3/TwingleDonation/Submit.php index 75220bb..8d95be3 100644 --- a/api/v3/TwingleDonation/Submit.php +++ b/api/v3/TwingleDonation/Submit.php @@ -484,13 +484,63 @@ function civicrm_api3_twingle_donation_Submit($params) { $result_values['organization'] = $organisation_id; } - // If requested, add contact to newsletter groups defined in the profile. - if (!empty($params['newsletter']) && !empty($groups = $profile->getAttribute('newsletter_groups'))) { + // If usage of double opt-in is selected, use MailingEventSubscribe.create + // to add contact to newsletter groups defined in the profile + $result_values['newsletter']['newsletter_double_opt_in'] = ($profile->getAttribute('newsletter_double_opt_in')) ? 'true' : 'false'; + if ( + $profile->getAttribute('newsletter_double_opt_in') && + !empty($params['newsletter']) && + !empty($groups = $profile->getAttribute('newsletter_groups')) + ) { + $group_memberships = array_column( + civicrm_api3( + 'GroupContact', + 'get', + array( + 'sequential' => 1, + 'contact_id' => $contact_id, + ) + )['values'], + 'group_id' + ); foreach ($groups as $group_id) { - civicrm_api3('GroupContact', 'create', array( - 'group_id' => $group_id, - 'contact_id' => $contact_id, - )); + $is_public_group = civicrm_api3( + 'Group', + 'getsingle', + array( + 'id' => (int) $group_id, + ) + )['visibility'] == 'Public Pages'; + if (!in_array($group_id, $group_memberships) && $is_public_group) { + $result_values['newsletter'][][$group_id] = civicrm_api3( + 'MailingEventSubscribe', + 'create', + array( + 'email' => $params['user_email'], + 'group_id' => (int) $group_id, + 'contact_id' => $contact_id, + ) + ); + } + elseif ($is_public_group) { + $result_values['newsletter'][] = $group_id; + } + } + // If requested, add contact to newsletter groups defined in the profile. + } + elseif ( + !empty($params['newsletter']) + && !empty($groups = $profile->getAttribute('newsletter_groups')) + ) { + foreach ($groups as $group_id) { + civicrm_api3( + 'GroupContact', + 'create', + array( + 'group_id' => $group_id, + 'contact_id' => $contact_id, + ) + ); $result_values['newsletter'][] = $group_id; } diff --git a/l10n/de_DE/LC_MESSAGES/twingle.po b/l10n/de_DE/LC_MESSAGES/twingle.po index f914552..921eedb 100644 --- a/l10n/de_DE/LC_MESSAGES/twingle.po +++ b/l10n/de_DE/LC_MESSAGES/twingle.po @@ -306,3 +306,7 @@ msgstr "Profil %1 zurücksetzen" #: templates/CRM/Twingle/Page/Profiles.tpl msgid "Delete profile %1" msgstr "Profil % 1 löschen" + +#: templates/CRM/Twingle/Page/Profiles.tpl +msgid "Use Double-Opt-In for newsletter" +msgstr "Nutze Double-Opt-In für Newsletter" diff --git a/templates/CRM/Twingle/Form/Profile.hlp b/templates/CRM/Twingle/Form/Profile.hlp index 8a89223..bcbe78a 100644 --- a/templates/CRM/Twingle/Form/Profile.hlp +++ b/templates/CRM/Twingle/Form/Profile.hlp @@ -38,6 +38,11 @@ {ts domain="de.systopia.twingle"}Select which financial type to use for recurring contributions.{/ts} {/htxt} +{htxt id='id-newsletter-double-opt-in'} +

{ts domain="de.systopia.twingle"}Select whether to use CiviCRM's Double-Opt-In feature for subscribing to mailing lists. Note that this only works for public mailing lists. Any non-public mailing list selected above will be ignored when this setting is enabled.{/ts}

+

{ts domain="de.systopia.twingle"}Also, do not forget to disable Twingle's own Double Opt-In option in the Twingle Manager to avoid subscribers receiving multiple confirmation e-mails. Only one or the other option should be enabled.{/ts}

+{/htxt} + {htxt id='id-membership-postprocessing-call'} {ts domain="de.systopia.twingle"}Some organisations have specific conventions on how a membership should be created. Since the Twingle-API can only create a "bare bone" membership object, you can enter a API Call (as 'Entity.Action') to adjust any newly created membership to your organisation's needs.{/ts} {ts domain="de.systopia.twingle"}The API call would receive the following parameters: