-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
implement Twingle's double opt-in #35
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
/*------------------------------------------------------------+ | ||
| SYSTOPIA Twingle Integration | | ||
| Copyright (C) 2018 SYSTOPIA | | ||
| Author: J. Schuppe (schuppe@systopia.de) | | ||
+-------------------------------------------------------------+ | ||
| This program is released as free software under the | | ||
| Affero GPL license. You can redistribute it and/or | | ||
| modify it under the terms of this license which you | | ||
| can read by viewing the included agpl.txt or online | | ||
| at www.gnu.org/licenses/agpl.html. Removal of this | | ||
| copyright header is strictly prohibited without | | ||
| written permission from the original author(s). | | ||
+-------------------------------------------------------------*/ | ||
|
||
use CRM_Twingle_ExtensionUtil as E; | ||
|
||
/** | ||
* TwingleDonation.DoubleOptInConfirm API specification (optional) | ||
* This is used for documentation and validation. | ||
* | ||
* @param array $params description of fields supported by this API call | ||
* | ||
* @return void | ||
* | ||
* @see http://wiki.civicrm.org/confluence/display/CRMDOC/API+Architecture+Standards | ||
*/ | ||
function _civicrm_api3_twingle_donation_DoubleOptInConfirm_spec(&$params) | ||
{ | ||
$params['project_id'] = array( | ||
'name' => 'project_id', | ||
'title' => E::ts('Project ID'), | ||
'type' => CRM_Utils_Type::T_STRING, | ||
'api.required' => 1, | ||
'description' => E::ts('The Twingle project ID.'), | ||
); | ||
$params['user_email'] = array( | ||
'name' => 'user_email', | ||
'title' => E::ts('Email address'), | ||
'type' => CRM_Utils_Type::T_STRING, | ||
'api.required' => 1, | ||
'description' => E::ts('The e-mail address of the contact.') | ||
); | ||
} | ||
|
||
/** | ||
* TwingleDonation.Cancel API | ||
* | ||
* @param array $params | ||
* @return array API result descriptor | ||
* @see civicrm_api3_create_success | ||
* @see civicrm_api3_create_error | ||
*/ | ||
function civicrm_api3_twingle_donation_DoubleOptInConfirm($params) | ||
{ | ||
// Log call if debugging is enabled within civicrm.settings.php. | ||
if (defined('TWINGLE_API_LOGGING') && TWINGLE_API_LOGGING) { | ||
CRM_Core_Error::debug_log_message('TwingleDonation.DoubleOptInConfirm: ' . json_encode($params, JSON_PRETTY_PRINT)); | ||
} | ||
|
||
try { | ||
// Get the profile defined for the given form ID, or the default profile | ||
// if none matches. | ||
$profile = CRM_Twingle_Profile::getProfileForProject($params['project_id']); | ||
|
||
// Get the newsletter groups defined in the profile | ||
$newsletter_groups = $profile->getAttribute('newsletter_groups'); | ||
|
||
// Extract user email from API call | ||
if (!empty($params['user_email'])) { | ||
$contacts = civicrm_api3('Email', 'get', array( | ||
'seqential' => 1, | ||
'email' => $params['user_email'], | ||
)); | ||
|
||
// Get pending group memberships for user | ||
if (!empty($contacts['values'])) { | ||
foreach ($contacts['values'] as $contact) { | ||
$groups = civicrm_api3('GroupContact', 'get', array( | ||
'sequential' => 1, | ||
'contact_id' => $contact['contact_id'], | ||
'status' => "Pending", | ||
)); | ||
|
||
// Only in newsletter groups: change group membership from pending to added | ||
if (!empty($groups['values'])) { | ||
foreach ($groups['values'] as $group) { | ||
if (in_array($group['group_id'], $newsletter_groups)) { | ||
civicrm_api3('GroupContact', 'create', array( | ||
'group_id' => $group['group_id'], | ||
'contact_id' => $contact['contact_id'], | ||
'status' => "Added", | ||
$result_values['groups'][] = $group['group_id'], | ||
)); | ||
// Display message if group membership was confirmed correctly | ||
$result_values['double_opt_in'][$group['group_id']] = "Subscription confirmed"; | ||
} | ||
} | ||
// Display message if there is no pending group membership | ||
} else { | ||
$result_values['double_opt_in'][] = "Could not confirm subscription: No pending group membership"; | ||
} | ||
} | ||
// Display message if email can't be found | ||
} else { | ||
$result_values['double_opt_in'][] = "Could not confirm subscription: Email not found"; | ||
} | ||
} | ||
$result = civicrm_api3_create_success($result_values); | ||
} catch (Exception $exception) { | ||
$result = civicrm_api3_create_error($exception->getMessage()); | ||
} | ||
|
||
return $result; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -484,8 +484,28 @@ function civicrm_api3_twingle_donation_Submit($params) { | |
$result_values['organization'] = $organisation_id; | ||
} | ||
|
||
// If Twingle's Double-Opt-In procedure is used, add contact with status "pending" to the newsletter groups | ||
// defined in the profile | ||
if (!empty($profile->getAttribute('double_opt_in')) && | ||
!empty($params['newsletter']) && | ||
!empty($groups = $profile->getAttribute('newsletter_groups'))) { | ||
foreach ($groups as $group_id) { | ||
if (empty(civicrm_api3('GroupContact', 'get', array( | ||
'group_id' => $group_id, | ||
'contact_id' => $contact_id, | ||
))['values'])) { | ||
civicrm_api3('GroupContact', 'create', array( | ||
'group_id' => $group_id, | ||
'contact_id' => $contact_id, | ||
'status' => "Pending", | ||
)); | ||
$result_values['double_opt_in'][] = "Pending"; | ||
} | ||
Comment on lines
+493
to
+503
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we make the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea! |
||
$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, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The contact should be identified using XCM, just as the
submit
API action does. Otherwise, this might alter group memberships for other contacts with the same e-mail address. We should also only target that one single contact identified by XCM with the same profile.Therefore, all
user_
parameters (and maybecustom_fields
also) should be added to this API action.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Considering that a Double-Opt-In confirmation always refers to an email address, I thought that matching a contact wouldn't be necessary. Especially when the contact has already been matched in the
submit
step. How should we keep DOI confirmations from different contacts with the same email apart, anyway?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what XCM takes care of: either match a single contact or create one, according to the XCM profile configuration being used. I.e. contacts with the same e-mail address don't matter, since XCM will only return one contact ID. Since
GroupContact
is a thing that belongs to a contact (not an e-mail entity) - it's a crosstab withgroup_id
andcontact_id
- we should really make sure we select the correct one and thus identify the previously matched contact again using XCM.