-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Campaign.php
203 lines (161 loc) · 7.25 KB
/
Campaign.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
namespace verbb\formie\integrations\emailmarketing;
use verbb\formie\base\Integration;
use verbb\formie\base\EmailMarketing;
use verbb\formie\elements\Submission;
use verbb\formie\models\IntegrationCollection;
use verbb\formie\models\IntegrationField;
use verbb\formie\models\IntegrationFormSettings;
use Craft;
use craft\fields;
use craft\helpers\ArrayHelper;
use craft\helpers\Json;
use putyourlightson\campaign\Campaign as CampaignPlugin;
use putyourlightson\campaign\elements\ContactElement;
use putyourlightson\campaign\elements\MailingListElement;
use putyourlightson\campaign\models\PendingContactModel;
use putyourlightson\campaign\records\MailingListRecord;
use putyourlightson\campaign\records\MailingListTypeRecord;
use Throwable;
class Campaign extends EmailMarketing
{
// Static Methods
// =========================================================================
public static function supportsConnection(): bool
{
return false;
}
/**
* @inheritDoc
*/
public static function displayName(): string
{
return Craft::t('formie', 'Campaign');
}
// Public Methods
// =========================================================================
public function getDescription(): string
{
return Craft::t('formie', 'Integrate with the [Craft Campaign](https://plugins.craftcms.com/campaign) plugin.');
}
public function fetchFormSettings(): IntegrationFormSettings
{
$settings = [];
$lists = MailingListElement::find()
->site('*')
->orderBy(['elements_sites.slug' => 'ASC', 'content.title' => 'ASC'])
->all();
$isMultiSite = Craft::$app->getIsMultiSite();
foreach ($lists as $list) {
$name = $isMultiSite ? "({$list->site}) {$list->title}" : $list->title;
$listFields = array_merge([
new IntegrationField([
'handle' => 'email',
'name' => Craft::t('formie', 'Email'),
'required' => true,
]),
], $this->_getCustomFields($list));
$settings['lists'][] = new IntegrationCollection([
'id' => (string)$list->id,
'name' => $name,
'fields' => $listFields,
]);
}
return new IntegrationFormSettings($settings);
}
public function sendPayload(Submission $submission): bool
{
try {
// Get the Campaign mailing list
$list = CampaignPlugin::$plugin->mailingLists->getMailingListById($this->listId);
if (!$list) {
Integration::error($this, 'Unable to find list “' . $this->listId . '”.', true);
return false;
}
// Fetch our mapped values
$fieldValues = $this->getFieldMappingValues($submission, $this->fieldMapping);
// Pull out email, as it needs to be top level
$email = ArrayHelper::remove($fieldValues, 'email');
// The `createAndSubscribeContact` method was added in Campaign v2.1.0.
if (method_exists(Campaign::$plugin->forms, 'createAndSubscribeContact')) {
$contact = Campaign::$plugin->forms->createAndSubscribeContact($email, $fieldValues, $list, 'formie', $this->referrer);
if ($contact->hasErrors()) {
Integration::error($this, Craft::t('formie', 'Unable to save contact: “{errors}”.', [
'errors' => Json::encode($contact->getErrors()),
]), true);
return false;
}
}
// TODO: remove this in Formie v3, assuming it requires Craft 5, in which case Campaign v3 will be required.
else {
// Get the Campaign contact
$contact = CampaignPlugin::$plugin->contacts->getContactByEmail($email);
if ($contact === null) {
$contact = new ContactElement();
$contact->email = $email;
}
// Set field values
$contact->setFieldValues($fieldValues);
// If subscribe verification required
if ($list->getMailingListType()->subscribeVerificationRequired) {
// Create a pending contact
$pendingContact = new PendingContactModel();
$pendingContact->email = $email;
$pendingContact->mailingListId = $list->id;
$pendingContact->source = $this->referrer;
$pendingContact->fieldData = $contact->getSerializedFieldValues();
if (!CampaignPlugin::$plugin->pendingContacts->savePendingContact($pendingContact)) {
Integration::error($this, Craft::t('formie', 'Unable to save pending contact: “{errors}”.', [
'errors' => Json::encode($pendingContact->getErrors()),
]), true);
return false;
}
CampaignPlugin::$plugin->forms->sendVerifySubscribeEmail($pendingContact, $list);
} else {
// Save contact
if (!Craft::$app->getElements()->saveElement($contact)) {
Integration::error($this, Craft::t('formie', 'Unable to save contact: “{errors}”.', [
'errors' => Json::encode($contact->getErrors()),
]), true);
return false;
}
// Subscribe them to the mailing list
CampaignPlugin::$plugin->forms->subscribeContact($contact, $list, 'formie', $this->referrer);
}
}
} catch (Throwable $e) {
Integration::apiError($this, $e);
return false;
}
return true;
}
// Private Methods
// =========================================================================
private function _convertFieldType($fieldType)
{
$fieldTypes = [
fields\Checkboxes::class => IntegrationField::TYPE_ARRAY,
fields\Lightswitch::class => IntegrationField::TYPE_BOOLEAN,
fields\Entries::class => IntegrationField::TYPE_ARRAY,
fields\MultiSelect::class => IntegrationField::TYPE_ARRAY,
fields\Number::class => IntegrationField::TYPE_FLOAT,
fields\Tags::class => IntegrationField::TYPE_ARRAY,
fields\Users::class => IntegrationField::TYPE_ARRAY,
];
return $fieldTypes[$fieldType] ?? IntegrationField::TYPE_STRING;
}
private function _getCustomFields($list): array
{
$customFields = [];
$fieldLayout = Craft::$app->getFields()->getLayoutByType(ContactElement::class);
foreach ($fieldLayout->getCustomFields() as $field) {
$customFields[] = new IntegrationField([
'handle' => $field->handle,
'name' => $field->name,
'type' => $this->_convertFieldType(get_class($field)),
'required' => (bool)$field->required,
]);
}
return $customFields;
}
}