Skip to content
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 member tags manipulation #42

Merged
merged 5 commits into from Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -24,7 +24,7 @@
"php": "^7.4 || ^8.0",
"codefog/contao-haste": "^4.0",
"contao/core-bundle": "^4.9",
"oneup/mailchimp-api-v3": "^2.6.1",
"oneup/mailchimp-api-v3": "^2.7",
"patchwork/utf8": "^1.3",
"symfony/event-dispatcher": "^4.4 || ^5.4"
},
Expand Down
38 changes: 29 additions & 9 deletions src/EventListener/FormSubscriptionListener.php
Expand Up @@ -127,26 +127,46 @@ public function onProcessFormData(array $submittedData, array $formData, ?array
return;
}

// Extract member tags
$memberTags = [];
foreach (StringUtil::deserialize($form->mailchimpMemberTags, true) as $memberTag) {
$memberTags[] = [
'name' => $memberTag['key'],
'status' => $memberTag['value'],
];
}

$api = new Client($model->listApiKey);
$email = $mergeVars['EMAIL'];
$result = $api->subscribeToList($model->listId, $email, $mergeVars, (bool) $form->mailchimpOptIn, $interests);

if ($result) {
$this->logger->log(LogLevel::INFO, sprintf('Successfully subscribed "%s" to Mailchimp list "%s".', $email, $model->listName), ['contao' => new ContaoContext(__METHOD__, TL_GENERAL)]);
} else {
$msg = sprintf('Could not subscribe "%s" to Mailchimp list "%s".', $email, $model->listName);
$this->handleError(sprintf('Could not subscribe "%s" to Mailchimp list "%s".', $email, $model->listName), $api->getLastError());
}

if ($lastError = $api->getLastError()) {
if ($lastError->detail ?? null) {
$msg .= ' ' . $lastError->detail;
}
if (\count($memberTags)) {
$result = $api->addOrRemoveMemberTags($model->listId, $email, $memberTags);

foreach ($lastError->errors ?? [] as $error) {
$msg .= ' ' . implode(': ', (array) $error) . '.';
}
if ($result) {
$this->logger->log(LogLevel::INFO, sprintf('Successfully added/removed tags from "%s".', $email), ['contao' => new ContaoContext(__METHOD__, TL_GENERAL)]);
} else {
$this->handleError(sprintf('Could not modify tags for "%s".', $email), $api->getLastError());
}
}
}

$this->logger->log(LogLevel::INFO, $msg, ['contao' => new ContaoContext(__METHOD__, TL_ERROR)]);
private function handleError(string $message, object $lastError = null): void
{
if ($lastError->detail ?? null) {
$message .= ' ' . $lastError->detail;
}

foreach ($lastError->errors ?? [] as $error) {
$message .= ' ' . implode(': ', (array) $error) . '.';
}

$this->logger->log(LogLevel::INFO, $message, ['contao' => new ContaoContext(__METHOD__, TL_ERROR)]);
}
}
8 changes: 7 additions & 1 deletion src/Resources/contao/dca/tl_form.php
Expand Up @@ -53,11 +53,17 @@
'sql' => ['type' => 'boolean', 'default' => false],
];

$GLOBALS['TL_DCA']['tl_form']['fields']['mailchimpMemberTags'] = [
'inputType' => 'keyValueWizard',
'eval' => ['tl_class' => 'clr'],
'sql' => ['type' => 'blob', 'notnull' => false],
];

PaletteManipulator::create()
->addLegend('mailchimp_legend', null)
->addField('enableMailchimp', 'mailchimp_legend', PaletteManipulator::POSITION_APPEND)
->applyToPalette('default', 'tl_form')
;

$GLOBALS['TL_DCA']['tl_form']['palettes']['__selector__'][] = 'enableMailchimp';
$GLOBALS['TL_DCA']['tl_form']['subpalettes']['enableMailchimp'] = 'mailchimpList,mailchimpGroups,mailchimpConfirmField,mailchimpOptIn,mailchimpMergeTags';
$GLOBALS['TL_DCA']['tl_form']['subpalettes']['enableMailchimp'] = 'mailchimpList,mailchimpGroups,mailchimpConfirmField,mailchimpOptIn,mailchimpMergeTags,mailchimpMemberTags';
1 change: 1 addition & 0 deletions src/Resources/contao/languages/de/tl_form.php
Expand Up @@ -9,3 +9,4 @@
$GLOBALS['TL_LANG']['tl_form']['mailchimpMergeTags'] = ['Merge-Tags', 'Die Merge-Tag Definitonen für dieses Formular. Der "Schlüssel" ist der Merge-Tag, der "Wert" ist der Name des Formularfelds, welches dann den Wert enthält.'];
$GLOBALS['TL_LANG']['tl_form']['mailchimpConfirmField'] = ['Bestätigungsfeld', 'Definiert ein optionales Bestätigungsfeld, welches ausgewählt sein muss, damit ein Abonnement erzeugt wird.'];
$GLOBALS['TL_LANG']['tl_form']['mailchimpOptIn'] = ['Double Opt-in verwenden', 'Double Opt-in für die Abonnements verwenden.'];
$GLOBALS['TL_LANG']['tl_form']['mailchimpMemberTags'] = ['Member-Tags', 'Die Member-Tag Definitonen für dieses Formular. Der "Schlüssel" ist der Tag, der "Wert" ist entweder "active" (Tag hinzufügen) oder "inactive" (Tag entfernen).'];
1 change: 1 addition & 0 deletions src/Resources/contao/languages/en/tl_form.php
Expand Up @@ -9,3 +9,4 @@
$GLOBALS['TL_LANG']['tl_form']['mailchimpMergeTags'] = ['Merge tags', 'Define the merge tags for this form. The "Key" is the merge tag while the "Value" is the form field holding the value.'];
$GLOBALS['TL_LANG']['tl_form']['mailchimpConfirmField'] = ['Confirm field', 'Set an optional form field that is required to be set in order to initiate the subscription process.'];
$GLOBALS['TL_LANG']['tl_form']['mailchimpOptIn'] = ['Use double opt-in', 'Use double opt-in for the subscription.'];
$GLOBALS['TL_LANG']['tl_form']['mailchimpMemberTags'] = ['Member tags', 'Define the member tags for this form. The "Key" is the tag while the "Value" is "active" (add tag) or "inactive" (remove tag).'];