[#36] Do not alter the mailing lists available for the profile field since that might be confusing and still produce invalid states

This commit is contained in:
Jens Schuppe 2020-08-14 10:33:00 +02:00
parent 94cc262c21
commit 3553ed83b9
2 changed files with 53 additions and 41 deletions

View file

@ -339,7 +339,7 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
'select', // field type
'newsletter_groups', // field name
E::ts('Sign up for newsletter groups'), // field label
static::getNewsletterGroups($this->profile->getAttribute('newsletter_double_opt_in')), // list of options
static::getNewsletterGroups(), // list of options
FALSE, // is not required
array('class' => 'crm-select2 huge', 'multiple' => 'multiple')
);
@ -811,7 +811,7 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
* @return array
*
*/
public static function getNewsletterGroups($double_opt_in) {
public static function getNewsletterGroups() {
if (!isset(static::$_newsletterGroups)) {
static::$_newsletterGroups = array();
$group_types = civicrm_api3('OptionValue', 'get', array(
@ -819,18 +819,7 @@ 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 && $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) {
if ($group_types['count'] > 0) {
$group_type = reset($group_types['values']);
$query = civicrm_api3('Group', 'get', array(
'is_active' => 1,

View file

@ -484,34 +484,57 @@ 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']['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) {
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.
} elseif (!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'
);
// TODO: Filter for public mailing list groups?
foreach ($groups as $group_id) {
civicrm_api3('GroupContact', 'create', array(
'group_id' => $group_id,
'contact_id' => $contact_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.
}
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;
}