implement CiviCRM's double opt-in feature for newsletter

This commit is contained in:
Marc Michalsky 2020-07-24 10:57:14 +02:00
parent 675b682e0e
commit 4ff060884a
No known key found for this signature in database
GPG key ID: AC2D4E00990A6767
6 changed files with 67 additions and 13 deletions

View file

@ -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,

View file

@ -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) {

View file

@ -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. |

View file

@ -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,

View file

@ -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"

View file

@ -208,6 +208,11 @@
<table class="form-layout-compressed">
<tr class="crm-section">
<td class="label">{$form.double_opt_in.label}</td>
<td class="content">{$form.double_opt_in.html}</td>
</tr>
<tr class="crm-section">
<td class="label">{$form.newsletter_groups.label}</td>
<td class="content">{$form.newsletter_groups.html}</td>