|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @author nicolaas [at] sunnysideup.co.nz |
|
5
|
|
|
* TO DO: only apply the on afterwrite to people in the subscriber group. |
|
6
|
|
|
* |
|
7
|
|
|
**/ |
|
8
|
|
|
|
|
9
|
|
|
class CampaignMonitorMemberDOD extends DataExtension |
|
|
|
|
|
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* name of the field to use for sign-ups |
|
14
|
|
|
* @var String |
|
15
|
|
|
*/ |
|
16
|
|
|
private static $campaign_monitor_signup_fieldname = "CampaignMonitorSubscriptions"; |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* array of fields where the member value is set as the default for the |
|
20
|
|
|
* custom field ... |
|
21
|
|
|
* The should be like this |
|
22
|
|
|
* |
|
23
|
|
|
* CustomFieldCode => MemberFieldOrMethod |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
private static $custom_fields_member_field_or_method_map = array(); |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* |
|
30
|
|
|
* |
|
31
|
|
|
* @var null | CampaignMonitorAPIConnector |
|
32
|
|
|
* |
|
33
|
|
|
*/ |
|
34
|
|
|
private static $_api = null; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* |
|
38
|
|
|
* @return CampaignMonitorAPIConnector |
|
39
|
|
|
*/ |
|
40
|
|
|
private function getCMAPI() |
|
41
|
|
|
{ |
|
42
|
|
|
if (!self::$_api) { |
|
43
|
|
|
self::$_api = CampaignMonitorAPIConnector::create(); |
|
|
|
|
|
|
44
|
|
|
self::$_api->init(); |
|
45
|
|
|
} |
|
46
|
|
|
return self::$_api; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* returns a form field for signing up to all available lists |
|
51
|
|
|
* or if a list is provided, for that particular list. |
|
52
|
|
|
* |
|
53
|
|
|
* @param CampaignMonitorSignupPage | string | Null $listPage |
|
54
|
|
|
* @param string $fieldName |
|
55
|
|
|
* @param string $fieldTitle |
|
56
|
|
|
* |
|
57
|
|
|
* @return FormField |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getCampaignMonitorSignupField($listPage = null, $fieldName = "", $fieldTitle = "") |
|
60
|
|
|
{ |
|
61
|
|
|
if (!is_object($listPage)) { |
|
62
|
|
|
$listPage = CampaignMonitorSignupPage::get()->filter(array("ListID" => $listPage))->first(); |
|
63
|
|
|
} |
|
64
|
|
|
$field = null; |
|
65
|
|
|
if (!$fieldName) { |
|
66
|
|
|
$fieldName = Config::inst()->get("CampaignMonitorMemberDOD", "campaign_monitor_signup_fieldname"); |
|
67
|
|
|
} |
|
68
|
|
|
$api = $this->getCMAPI(); |
|
69
|
|
|
$currentValues = null; |
|
70
|
|
|
if ($listPage) { |
|
71
|
|
|
if ($listPage->ReadyToReceiveSubscribtions()) { |
|
72
|
|
|
$currentSelection = "Subscribe"; |
|
73
|
|
|
$optionArray = array(); |
|
74
|
|
|
$optionArray["Subscribe"] = _t("CampaignMonitorSignupPage.SUBSCRIBE_TO", "subscribe to")." ".$listPage->getListTitle(); |
|
75
|
|
|
$optionArray["Unsubscribe"] = _t("CampaignMonitorSignupPage.UNSUBSCRIBE_FROM", "unsubscribe from ")." ".$listPage->getListTitle(); |
|
76
|
|
|
if ($this->owner->exists()) { |
|
77
|
|
|
if ($api->getSubscriberCanReceiveEmailsForThisList($listPage->ListID, $this->owner)) { |
|
78
|
|
|
$currentValues = $api->getSubscriber($listPage->ListID, $this->owner); |
|
79
|
|
|
//$currentSelection = "Unsubscribe"; |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
if (!$fieldTitle) { |
|
83
|
|
|
$fieldTitle = _t("CampaignMonitorSignupPage.SIGNUP_FOR", "Sign up for ")." ".$listPage->getListTitle(); |
|
84
|
|
|
} |
|
85
|
|
|
$subscribeField = OptionsetField::create($fieldName, $fieldTitle, $optionArray, $currentSelection); |
|
86
|
|
|
$field = CompositeField::create($subscribeField); |
|
87
|
|
|
$field->addExtraClass("CMFieldsCustomFieldsHolder"); |
|
88
|
|
|
//add custom fields |
|
89
|
|
|
$linkedMemberFields = Config::inst()->get("CampaignMonitorMemberDOD", "custom_fields_member_field_or_method_map"); |
|
90
|
|
|
$customFields = $listPage->CampaignMonitorCustomFields()->filter(array("Visible" => 1)); |
|
91
|
|
|
foreach ($customFields as $customField) { |
|
92
|
|
|
$valueSet = false; |
|
93
|
|
|
$customFormField = $customField->getFormField("CMCustomField"); |
|
94
|
|
|
if ($currentValues && isset($currentValues->CustomFields)) { |
|
95
|
|
|
foreach ($currentValues->CustomFields as $customFieldObject) { |
|
|
|
|
|
|
96
|
|
|
if ($customFieldObject->Key == $customField->Title) { |
|
97
|
|
|
if ($value = $customFieldObject->Value) { |
|
98
|
|
|
$valueSet = true; |
|
99
|
|
|
} |
|
100
|
|
|
$customFormField->setValue($value); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
if (isset($linkedMemberFields[$customFormField->Code]) && !$valueSet) { |
|
105
|
|
|
$fieldOrMethod = $linkedMemberFields[$custom->Code]; |
|
|
|
|
|
|
106
|
|
|
if ($this->owner->hasMethod($fieldOrMethod)) { |
|
107
|
|
|
$value = $this->owner->$fieldOrMethod(); |
|
108
|
|
|
} else { |
|
109
|
|
|
$value = $this->owner->$fieldOrMethod; |
|
110
|
|
|
} |
|
111
|
|
|
if ($value) { |
|
112
|
|
|
$customFormField->setValue($value); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
$field->push($customFormField); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} else { |
|
119
|
|
|
if (!$fieldTitle) { |
|
120
|
|
|
$fieldTitle = _t("CampaignMonitorMemberDOD.NEWSLETTERSIGNUP", "Newsletter sign-up"); |
|
121
|
|
|
} |
|
122
|
|
|
$lists = CampaignMonitorSignupPage::get_ready_ones(); |
|
123
|
|
|
$array = array(); |
|
124
|
|
|
foreach ($lists as $list) { |
|
125
|
|
|
$array[$list->ListID] = $list->getListTitle(); |
|
126
|
|
|
} |
|
127
|
|
|
if (count($array)) { |
|
128
|
|
|
$field = new CheckboxSetField( |
|
129
|
|
|
$fieldName, |
|
130
|
|
|
$fieldTitle, |
|
131
|
|
|
$array |
|
132
|
|
|
); |
|
133
|
|
|
$field->setDefaultItems($this->owner->CampaignMonitorSignupPageIDs()); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
if (!$field) { |
|
137
|
|
|
$field = ReadonlyField::create( |
|
138
|
|
|
$fieldName, |
|
139
|
|
|
$fieldTitle, |
|
140
|
|
|
_t("CampaignMonitorMemberDOD.NO_LISTS_AVAILABLE", "No lists available right now. Please come back soon.") |
|
141
|
|
|
); |
|
142
|
|
|
} |
|
143
|
|
|
return $field; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* action subscription form |
|
149
|
|
|
* @param CampaignMonitorSignUpPage $page |
|
|
|
|
|
|
150
|
|
|
* @param Array $array |
|
|
|
|
|
|
151
|
|
|
* @param Form $form |
|
152
|
|
|
* |
|
153
|
|
|
* return string: can be subscribe / unsubscribe / error |
|
154
|
|
|
*/ |
|
155
|
|
|
public function processCampaignMonitorSignupField($listPage, $data, $form) |
|
|
|
|
|
|
156
|
|
|
{ |
|
157
|
|
|
$typeOfAction = "unsubscribe"; |
|
158
|
|
|
//many choices |
|
159
|
|
|
if (isset($data["SubscribeManyChoices"])) { |
|
160
|
|
|
$listPages = CampaignMonitorSignupPage::get_ready_ones(); |
|
161
|
|
|
foreach ($listPages as $listPage) { |
|
162
|
|
|
if (isset($data["SubscribeManyChoices"][$listPage->ListID]) && $data["SubscribeManyChoices"][$listPage->ListID]) { |
|
163
|
|
|
$this->owner->addCampaignMonitorList($listPage->ListID); |
|
164
|
|
|
$typeOfAction = "subscribe"; |
|
165
|
|
|
} else { |
|
166
|
|
|
$this->owner->removeCampaignMonitorList($listPage->ListID); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
//one choice |
|
171
|
|
|
elseif (isset($data["SubscribeChoice"])) { |
|
172
|
|
|
if ($data["SubscribeChoice"] == "Subscribe") { |
|
173
|
|
|
$customFields = $listPage->CampaignMonitorCustomFields()->filter(array("Visible" => 1)); |
|
174
|
|
|
$customFieldsArray = array(); |
|
175
|
|
|
foreach ($customFields as $customField) { |
|
176
|
|
|
if (isset($data["CMCustomField".$customField->Code])) { |
|
177
|
|
|
$customFieldsArray[$customField->Code] = $data["CMCustomField".$customField->Code]; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
$this->owner->addCampaignMonitorList($listPage->ListID, $customFieldsArray); |
|
181
|
|
|
$typeOfAction = "subscribe"; |
|
182
|
|
|
} else { |
|
183
|
|
|
$this->owner->removeCampaignMonitorList($listPage->ListID); |
|
184
|
|
|
} |
|
185
|
|
|
} else { |
|
186
|
|
|
user_error("Subscriber field missing", E_USER_WARNING); |
|
187
|
|
|
} |
|
188
|
|
|
return $typeOfAction; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* immediately unsubscribe if you are logged in. |
|
193
|
|
|
* @param HTTPRequest |
|
194
|
|
|
*/ |
|
195
|
|
View Code Duplication |
public function unsubscribe($request) |
|
|
|
|
|
|
196
|
|
|
{ |
|
197
|
|
|
$member = Member::currentUser(); |
|
198
|
|
|
if ($member) { |
|
199
|
|
|
$member->removeCampaignMonitorList($this->ListID); |
|
|
|
|
|
|
200
|
|
|
$this->Content = $member->Email." has been removed from this list: ".$this->getListTitle(); |
|
|
|
|
|
|
201
|
|
|
} else { |
|
202
|
|
|
Security::permissionFailure($this, _t("CAMPAIGNMONITORSIGNUPPAGE.LOGINFIRST", "Please login first.")); |
|
|
|
|
|
|
203
|
|
|
} |
|
204
|
|
|
return array(); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* is this user currently signed up to one or more newsletters |
|
209
|
|
|
* |
|
210
|
|
|
* @return Boolean |
|
|
|
|
|
|
211
|
|
|
*/ |
|
212
|
|
|
public function IsCampaignMonitorSubscriber() |
|
213
|
|
|
{ |
|
214
|
|
|
CampaignMonitorSignupPage::get_ready_ones() |
|
215
|
|
|
->where("MemberID = ".$this->owner->ID) |
|
|
|
|
|
|
216
|
|
|
->innerJoin("Group_Members", "CampaignMonitorSignupPage ON CampaignMonitorSignupPage.GroupID = Group_Members.GroupID") |
|
217
|
|
|
->count() ? true : false; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* add to Group |
|
222
|
|
|
* add to CM database... |
|
223
|
|
|
* @param CampaignMonitorSignupPage | Int $listPage |
|
224
|
|
|
* @param array $customFields |
|
225
|
|
|
* @return Boolean - returns true on success |
|
226
|
|
|
*/ |
|
227
|
|
|
public function addCampaignMonitorList($listPage, $customFields = array()) |
|
228
|
|
|
{ |
|
229
|
|
|
$api = $this->getCMAPI(); |
|
230
|
|
|
$outcome = 0; |
|
231
|
|
|
if (is_string($listPage)) { |
|
232
|
|
|
$listPage = CampaignMonitorSignupPage::get()->filter(array("ListID" => $listPage))->first(); |
|
233
|
|
|
} |
|
234
|
|
|
//internal database |
|
235
|
|
View Code Duplication |
if ($listPage && $listPage->GroupID) { |
|
|
|
|
|
|
236
|
|
|
if ($gp = Group::get()->byID($listPage->GroupID)) { |
|
237
|
|
|
$groups = $this->owner->Groups(); |
|
238
|
|
|
if ($groups) { |
|
239
|
|
|
$this->owner->Groups()->add($gp); |
|
240
|
|
|
$outcome++; |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
if ($listPage && $listPage->ListID) { |
|
245
|
|
|
if ($api->getSubscriber($listPage->ListID, $this->owner)) { |
|
246
|
|
|
if ($api->updateSubscriber( |
|
247
|
|
|
$listPage->ListID, |
|
248
|
|
|
$oldEmailAddress = "", |
|
249
|
|
|
$this->owner, |
|
|
|
|
|
|
250
|
|
|
$customFields, |
|
251
|
|
|
$resubscribe = true, |
|
252
|
|
|
$restartSubscriptionBasedAutoResponders = false |
|
253
|
|
|
)) { |
|
254
|
|
|
$outcome++; |
|
255
|
|
|
} |
|
256
|
|
|
} elseif (!$api->addSubscriber( |
|
257
|
|
|
$listPage->ListID, |
|
258
|
|
|
$this->owner, |
|
|
|
|
|
|
259
|
|
|
$customFields, |
|
260
|
|
|
true, |
|
261
|
|
|
false |
|
262
|
|
|
)) { |
|
263
|
|
|
$outcome++; |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
if ($outcome > 1) { |
|
|
|
|
|
|
267
|
|
|
return true; |
|
268
|
|
|
} |
|
269
|
|
|
return false; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* |
|
274
|
|
|
* remove from Group |
|
275
|
|
|
* remove from CM database... |
|
276
|
|
|
* @param CampaignMonitorSignupPage | Int $listPage |
|
277
|
|
|
* @return boolean returns true if successful. |
|
278
|
|
|
*/ |
|
279
|
|
|
public function removeCampaignMonitorList($listPage) |
|
280
|
|
|
{ |
|
281
|
|
|
$api = $this->getCMAPI(); |
|
282
|
|
|
$outcome = 0; |
|
283
|
|
|
if (is_string($listPage)) { |
|
284
|
|
|
$listPage = CampaignMonitorSignupPage::get()->filter(array("ListID" => $listPage))->first(); |
|
285
|
|
|
} |
|
286
|
|
View Code Duplication |
if ($listPage->GroupID) { |
|
|
|
|
|
|
287
|
|
|
if ($gp = Group::get()->byID($listPage->GroupID)) { |
|
288
|
|
|
$groups = $this->owner->Groups(); |
|
289
|
|
|
if ($groups) { |
|
290
|
|
|
$this->owner->Groups()->remove($gp); |
|
291
|
|
|
$outcome++; |
|
292
|
|
|
} |
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
|
|
if ($listPage->ListID) { |
|
296
|
|
|
if (!$api->unsubscribeSubscriber($listPage->ListID, $this->owner)) { |
|
297
|
|
|
$outcome++; |
|
298
|
|
|
} |
|
299
|
|
|
} |
|
300
|
|
|
if ($outcome > 1) { |
|
|
|
|
|
|
301
|
|
|
return true; |
|
302
|
|
|
} |
|
303
|
|
|
return false; |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* returns a list of list IDs |
|
308
|
|
|
* that the user is currently subscribed to. |
|
309
|
|
|
* |
|
310
|
|
|
* @return Array |
|
311
|
|
|
*/ |
|
312
|
|
|
public function CampaignMonitorSignupPageIDs() |
|
313
|
|
|
{ |
|
314
|
|
|
$api = $this->getCMAPI(); |
|
315
|
|
|
$lists = $api->getListsForEmail($this->owner); |
|
316
|
|
|
$array = array(); |
|
317
|
|
|
if ($lists && count($lists)) { |
|
318
|
|
|
foreach ($lists as $listArray) { |
|
|
|
|
|
|
319
|
|
|
if (in_array($listArray["SubscriberState"], array("Active", "Bounced"))) { |
|
320
|
|
|
$array[$listArray["ListID"]] = $listArray["ListID"]; |
|
321
|
|
|
} |
|
322
|
|
|
} |
|
323
|
|
|
} |
|
324
|
|
|
return $array; |
|
325
|
|
|
} |
|
326
|
|
|
} |
|
327
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.