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

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