diff --git a/CRM/Twingle/Form/Profile.php b/CRM/Twingle/Form/Profile.php index a38dc7e..5f18715 100644 --- a/CRM/Twingle/Form/Profile.php +++ b/CRM/Twingle/Form/Profile.php @@ -327,11 +327,19 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form { ); } + $this->add( + 'checkbox', // field type + '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 E::ts('Sign up for newsletter groups'), // field label - static::getNewsletterGroups(), // list of options + static::getNewsletterGroups($this->profile->getAttribute('double_opt_in')), // list of options FALSE, // is not required array('class' => 'crm-select2 huge', 'multiple' => 'multiple') ); @@ -548,6 +556,7 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form { $values['name'] = 'default'; } $this->profile->setName($values['name']); + $this->profile->setAttribute('double_opt_in', isset($values['double_opt_in'])); foreach ($this->profile->getData() as $element_name => $value) { if (isset($values[$element_name])) { $this->profile->setAttribute($element_name, $values[$element_name]); @@ -791,15 +800,16 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form { return self::$_contributionStatusOptions; } - /** - * Retrieves active groups used as mailing lists within the system as options - * for select form elements. - * - * @return array - * - * @throws \CiviCRM_API3_Exception - */ - public static function getNewsletterGroups() { + /** + * Retrieves active groups used as mailing lists within the system as options + * for select form elements. + * + * @param $double_opt_in + * + * @return array + * + */ + public static function getNewsletterGroups($double_opt_in) { if (!isset(static::$_newsletterGroups)) { static::$_newsletterGroups = array(); $group_types = civicrm_api3('OptionValue', 'get', array( @@ -807,7 +817,18 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form { 'option_group_id' => 'group_type', 'name' => CRM_Twingle_Submission::GROUP_TYPE_NEWSLETTER, )); - if ($group_types['count'] > 0) { + if ($group_types['count'] > 0 && $double_opt_in) { + $query = civicrm_api3('Group', 'get', array( + 'is_active' => 1, + 'group_type' => "Mailing List", + 'option.limit' => 0, + 'visibility' => 'Public Pages', + 'return' => 'id,name' + )); + foreach ($query['values'] as $group) { + static::$_newsletterGroups[$group['id']] = $group['name']; + } + } elseif ($group_types['count'] > 0) { $group_type = reset($group_types['values']); $query = civicrm_api3('Group', 'get', array( 'is_active' => 1, diff --git a/CRM/Twingle/Profile.php b/CRM/Twingle/Profile.php index be93bc6..e0afe35 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', + '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, + '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..b1b7409 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ for all newly created Twingle projects. | 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 will be pending until receivement of double opt-in confirmataion. | | 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. | @@ -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 afa3ca3..d0f9061 100644 --- a/api/v3/TwingleDonation/Submit.php +++ b/api/v3/TwingleDonation/Submit.php @@ -484,8 +484,29 @@ function civicrm_api3_twingle_donation_Submit($params) { $result_values['organization'] = $organisation_id; } + // If usage of double opt-in is selected, use MailingEventSubscribe.create to add contact to newsletter groups + // defined in the profile + $result_values['newsletter']['double_opt_in'] = ($profile->getAttribute('double_opt_in')) ? 'true' : 'false'; + if ($profile->getAttribute('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) { + if (!in_array($group_id, $group_memberships)) { + $result_values['newsletter'][][$group_id] = civicrm_api3('MailingEventSubscribe', 'create', array( + 'email' => $params['user_email'], + 'group_id' => (int) $group_id, + 'contact_id' => $contact_id, + )); + } else { + $result_values['newsletter'][] = $group_id; + } + } // If requested, add contact to newsletter groups defined in the profile. - if (!empty($params['newsletter']) && !empty($groups = $profile->getAttribute('newsletter_groups'))) { + } elseif (!empty($params['newsletter']) && !empty($groups = $profile->getAttribute('newsletter_groups'))) { foreach ($groups as $group_id) { civicrm_api3('GroupContact', 'create', array( 'group_id' => $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.tpl b/templates/CRM/Twingle/Form/Profile.tpl index 708cb5a..3e6a07e 100644 --- a/templates/CRM/Twingle/Form/Profile.tpl +++ b/templates/CRM/Twingle/Form/Profile.tpl @@ -208,6 +208,11 @@
{$form.double_opt_in.label} | +{$form.double_opt_in.html} | +
{$form.newsletter_groups.label} | {$form.newsletter_groups.html} |