Merge branch 'issue/36'

[#36] Use CiviCRM's double opt-in feature for newsletter subscription
This commit is contained in:
Jens Schuppe 2020-08-19 12:37:08 +02:00
commit 72a3515e8a
7 changed files with 120 additions and 24 deletions

View file

@ -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;
}