/
FormBuilderProcessorMailchimp.module
171 lines (153 loc) · 5.09 KB
/
FormBuilderProcessorMailchimp.module
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
<?php namespace ProcessWire;
use MailchimpMarketing\ApiClient as Client;
class FormBuilderProcessorMailchimp extends FormBuilderProcessorAction implements Module
{
protected $client;
public function init()
{
$this->client = new Client();
$config_key = wire('config')->mailchimp_api_key;
if($config_key){
$api_key = $config_key;
}else{
$api_key = $this->api_key;
}
$this->client->setConfig([
'apiKey' => $api_key,
'server' => $this->server,
]);
}
/**
* @param $text string
* @param $form InputfieldForm
* @return array
*/
public function mergeFieldValues($text, $form)
{
$value = explode("\n", $text);
$newValue = [];
foreach ($value as $row) {
$pos = strpos($row, '=');
if ($pos) {
$key = trim(substr($row, 0, $pos));
$field = trim(substr($row, $pos + 1));
$value = $form->get($field)->value;
$newValue[$key] = $value;
}
}
return $newValue;
}
public function textToArray($text)
{
$value = explode("\n", $text);
$newValue = [];
foreach ($value as $row) {
$pos = strpos($row, '=');
if ($pos) {
$key = trim(substr($row, 0, $pos));
$value = trim(substr($row, $pos + 1));
$newValue[$key] = $value;
}
}
return $newValue;
}
public function processReady()
{
$this->addHookAfter('FormBuilderProcessor::processInputDone', function ($event) {
/** @var InputfieldForm $form */
$form = $event->arguments(0);
$email_field = $this->get('mailchimp_email_field');
if($email_field){
$email = $form->get($email_field)->value;
}else{
$email = $form->get('email|email_address')->value;
}
if(!$email){
wire('log')->save('fb-mailchimp-error', "No email field found for Mailchimp action");
return;
}
$merge_fields = $this->get('mailchimp_merge_fields');
$status = $this->get('mailchimp_status');
$condition_passed = [];
$conditions = $this->get('mailchimp_action_condition');
if ($conditions) {
$values = $this->textToArray($conditions);
foreach ($values as $field => $val) {
$field_with_condition = $form->getChildByName($field);
try {
if ($field_with_condition->value() == $val) {
$condition_passed[] = true;
} else {
$condition_passed[] = false;
}
} catch (Exception $e) {
$form->error($e);
}
}
if (in_array(false, $condition_passed, true) === true) {
return;
}
}
if ($merge_fields) {
$fields_to_add = $this->mergeFieldValues($merge_fields, $form);
}
$data = [
"email_address" => $email,
"status" => $status,
];
if ($fields_to_add) {
$data['merge_fields'] = $fields_to_add;
}
try {
$response = $this->client->lists->setListMember($this->get('mailchimp_list_id'), $email ,$data);
if ($this->get('mailchimp_log')) {
wire('log')->save('fb-mailchimp', json_encode($response, true));
}
} catch (\Exception $e) {
//$form->error($e->getMessage());
wire('log')->save('fb-mailchimp-error', $e->getMessage());
}
});
}
public function getConfigInputfields(InputfieldWrapper $inputfields)
{
// make field(s) needed to configure this action
$f = $this->modules->get('InputfieldText');
$f->attr('name', 'mailchimp_list_id');
$f->label = 'List ID';
$f->val($this->mailchimp_list_id);
$inputfields->add($f);
$f = $this->modules->get('InputfieldText');
$f->attr('name', 'mailchimp_email_field');
$f->label = 'Email field name';
$f->val($this->mailchimp_email_field);
$inputfields->add($f);
/** @var InputfieldRadios $f */
$f = $this->modules->get('InputfieldRadios');
$f->attr('name', 'mailchimp_status');
$f->label = 'Default subscription status';
$f->defaultValue = 'subscribed';
$f->addOptions([
'subscribed' => 'Subscribed',
'pending' => 'Pending',
'cleaned' => 'Cleaned',
'transactional' => 'Transactional'
]);
$f->val($this->get('mailchimp_status') ?: 'subscribed');
$inputfields->add($f);
$f = $this->modules->get('InputfieldTextarea');
$f->attr('name', 'mailchimp_merge_fields');
$f->label = 'Additional merge fields';
$f->notes = 'Add merge fields per line using [merge field labels](https://mailchimp.com/help/getting-started-with-merge-tags/): FNAME=firs_name ';
$f->val($this->get('mailchimp_merge_fields'));
$inputfields->add($f);
$f = new InputfieldTextarea();
$f->label = "Conditions";
$f->description = "Set matching conditions for the forwarding to mailchimp to happen.";
$f->notes = "If multiple conditions are set, all have to evaluate to true for the action to occur. Only the equal sign operator is supported. e.g. \"opt_in=1\"";
$f->name = "mailchimp_action_condition";
$f->setAttribute('rows', 5);
$f->value = $this->get('mailchimp_action_condition') ?: "";
$inputfields->add($f);
}
}