This repository has been archived by the owner on Dec 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
ConstantContact.php
561 lines (506 loc) · 23.5 KB
/
ConstantContact.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
<?php
require_once('Authentication.php'); // OAuth & Basic Authentication classes (CTCTDataStore, CTCTRequest, OAuthToken...)
require_once('Collections.php'); // Constant Contact collection resource classes (ContactsCollection, ListsCollection..)
require_once('Components.php'); // Constant Contact object classes (Contact, List, Campaign, Event...)
class ConstantContact{
public $apiKey;
public $username;
public $password;
public $consumerSecret;
public $CTCTRequest;
public $authType;
/**
* ConstantContact constructor
* @param string $authType - Authentication type, 'basic' or 'oauth'
* @param string $apiKey - Constant Contact API key
* @param string $username - Constant Contact Username
* @param string $param - For basic - password, for oauth - consumerSecret
* @return void
*/
public function __construct($authType, $apiKey, $username, $param){
// Set username to the instance so we can use it if neccessary
$this->username = $username;
try{
$this->authType = strtolower($authType);
if($this->authType != 'basic' && $this->authType != 'oauth' && $this->authType != 'oauth2'){
throw new CTCTException('Authentication Error: type '.$this->authType.' is not valid');
};
$this->CTCTRequest = new CTCTRequest($this->authType, $apiKey, $username, $param);
} catch (CTCTException $e){
$e->generateError();
}
}
/**
* Get a page of Lists
* @param string $page - optional 'nextLink' returned from a previous getLists() call
* @param bool $systemLists - set to true to return the 'Active', 'Removed' and 'Do Not Mail' system lists
* @return array - Up to 50 Lists and a link to the next page if one exists
*/
public function getLists($page=null, $systemLists=false){
$ListsCollection = new ListsCollection($this->CTCTRequest);
return $ListsCollection->getLists($page, $systemLists);
}
/**
* Get a page of list members
* @param ContactList $item - ContactList object
* @param string $page - optional 'nextLink' returned from a previous getListMembers() call
* @return array
*/
public function getListMembers(ContactList $List, $page=null){
$ListsCollection = new ListsCollection($this->CTCTRequest);
if($page){$url = $this->CTCTRequest->baseUri;}
return $ListsCollection->getListMembers($List, $page);
}
/**
* Get full details for a list
* @param ContactList $item - ContactList object
* @return ContactList
*/
public function getListDetails(ContactList $List){
$ListsCollection = new ListsCollection($this->CTCTRequest);
return $ListsCollection->getListDetails($this->CTCTRequest->baseUri.$List->link);
}
/**
* Create a new ContactList
* @param ContactList $List - ContactList object
* @return ContactList
*/
public function addList(ContactList $List){
$ListsCollection = new ListsCollection($this->CTCTRequest);
return $ListsCollection->addList($List);
}
/**
* Deletes a ContactList
* @param ContactList $List - ContactList object
* @return bool
*/
public function deleteList(ContactList $List){
$ListsCollection = new ListsCollection($this->CTCTRequest);
return $ListsCollection->deleteList($this->CTCTRequest->baseUri.$List->link);
}
/**
* Update a ContactList with its current properties
* @param ContactList $List - ContactList object
* @return bool
*/
public function updateList(ContactList $List){
$ListsCollection = new ListsCollection($this->CTCTRequest);
return $ListsCollection->updateList($List);
}
/**
* Add a new Contact to a Constant Contact account
* @param Contact $Contact - Contact Object
* @return Contact
*/
public function addContact(Contact $Contact){
$ContactsCollection = new ContactsCollection($this->CTCTRequest);
return $ContactsCollection->addContact($Contact);
}
/**
* Get a page of Contacts
* @param string $page - optional 'nextLink' returned from a previous getContacts() call
* @return array - Up to 50 Contacts and a link to the next page if one exists
*/
public function getContacts($page=null){
$ContactsCollection = new ContactsCollection($this->CTCTRequest);
return $ContactsCollection->getContacts($page);
}
/**
* Get full details for a Contact
* @param Contact $Contact - Contact object
* @return Contact
*/
public function getContactDetails(Contact $Contact){
$ContactsCollection = new ContactsCollection($this->CTCTRequest);
return $ContactsCollection->getContactDetails($this->CTCTRequest->baseUri.$Contact->link);
}
/**
* Get open events for a contact
* @param Contact $Contact
* @param string $page - optional 'nextLink' from previous getContactOpens call
* @return array - Up to 50 CampaignEvents and a link to the next page if one exists
*/
public function getContactOpens(Contact $Contact, $page=null){
$ContactsCollection = new ContactsCollection($this->CTCTRequest);
$url = ($page) ? $this->CTCTRequest->baseUri.$page : $this->CTCTRequest->baseUri.$Contact->link.'/events/opens';
return $ContactsCollection->getContactEvents($url, 'OpenEvent');
}
/**
* Get click events for a contact
* @param Contact $Contact
* @param string $page - optional 'nextLink' from previous getContactClicks call
* @return array - Up to 50 CampaignEvents and a link to the next page if one exists
*/
public function getContactClicks(Contact $Contact, $page=null){
$ContactsCollection = new ContactsCollection($this->CTCTRequest);
$url = ($page) ? $this->CTCTRequest->baseUri.$page : $this->CTCTRequest->baseUri.$Contact->link.'/events/clicks';
return $ContactsCollection->getContactEvents($url, 'ClickEvent');
}
/**
* Get forwards events for a contact
* @param Contact $Contact
* @param string $page - optional 'nextLink' from previous getContactForwards call
* @return array - Up to 50 CampaignEvents and a link to the next page if one exists
*/
public function getContactForwards(Contact $Contact, $page=null){
$ContactsCollection = new ContactsCollection($this->CTCTRequest);
$url = ($page) ? $this->CTCTRequest->baseUri.$page : $this->CTCTRequest->baseUri.$Contact->link.'/events/forwards';
return $ContactsCollection->getContactEvents($url, 'ForwardEvent');
}
/**
* Get bounce events for a contact
* @param Contact $Contact
* @param string $page - optional 'nextLink' from previous getContactBounces call
* @return array - Up to 50 CampaignEvents and a link to the next page if one exists
*/
public function getContactBounces(Contact $Contact, $page=null){
$ContactsCollection = new ContactsCollection($this->CTCTRequest);
$url = ($page) ? $this->CTCTRequest->baseUri.$page : $this->CTCTRequest->baseUri.$Contact->link.'/events/bounces';
return $ContactsCollection->getContactEvents($url, 'BounceEvent');
}
/**
* Get opt out events for a contact
* @param Contact $Contact
* @param string $page - optional 'nextLink' from previous getContactOptOuts call
* @return array - Up to 50 CampaignEvents and a link to the next page if one exists
*/
public function getContactOptOuts(Contact $Contact, $page=null){
$ContactsCollection = new ContactsCollection($this->CTCTRequest);
$url = ($page) ? $this->CTCTRequest->baseUri.$page : $this->CTCTRequest->baseUri.$Contact->link.'/events/optouts';
return $ContactsCollection->getContactEvents($url, 'OptoutEvent');
}
/**
* Get send events for a contact
* @param Contact $Contact
* @param string $page - optional 'nextLink' from previous getContactSends call
* @return array - Up to 50 CampaignEvents and a link to the next page if one exists
*/
public function getContactSends(Contact $Contact, $page=null){
$ContactsCollection = new ContactsCollection($this->CTCTRequest);
$url = ($page) ? $this->CTCTRequest->baseUri.$page : $this->CTCTRequest->baseUri.$Contact->link.'/events/sends';
return $ContactsCollection->getContactEvents($url, 'SentEvent');
}
/**
* Update a contact with current properties
* @param Contact $Contact - Contact object
* @return Contact
*/
public function updateContact(Contact $Contact){
$ContactsCollection = new ContactsCollection($this->CTCTRequest);
return $ContactsCollection->updateContact($Contact);
}
/**
* Delete a contact
* @param Contact $Contact - Contact
* @return bool
*/
public function deleteContact(Contact $Contact){
$ContactsCollection = new ContactsCollection($this->CTCTRequest);
return $ContactsCollection->deleteContact($this->CTCTRequest->baseUri.$Contact->link);
}
/**
* Search for contacts by email address
* @param string $emailAddress - email address of user to search for
* @param array $emailAddress - array of email addresses to search for
* @return - array of found contacts, otherwise returns false
*/
public function searchContactsByEmail($emailAddress){
$ext = '';
if(is_string($emailAddress)){$ext = '?email='.strtolower($emailAddress);}
if(is_array($emailAddress)){
for($i=0; $i<count($emailAddress); $i++){
$loweredAddress = strtolower($emailAddress[$i]);
$ext .= ($i==0) ? '?email='.$loweredAddress : '&email='.$loweredAddress;
}
}
$ContactsCollection = new ContactsCollection($this->CTCTRequest);
return $ContactsCollection->searchContactsByEmail($ext);
}
/**
* Search for contacts in a list who have been updated since a given date
* @param ContactList $List - ContactList object to search
* @param string $date - Last updated date to search from
* @return array - Up to 50 contacts and a link to the next page if one exists
*/
public function searchContactsByLastUpdate(ContactList $List, $date){
$ContactsCollection = new ContactsCollection($this->CTCTRequest);
return $ContactsCollection->searchContactsByLastUpdate($List, $date);
}
/**
* Get a page of Campaigns
* @param string $page - optional 'nextLink' returned from a previous getLists() call
* @return array - Up to 50 Campaigns and a link to the next page if one exists
*/
public function getCampaigns($page=null){
$CampaignsCollection = new CampaignsCollection($this->CTCTRequest);
return $CampaignsCollection->getCampaigns($page);
}
/**
* Get a Campaign from an ID
* @param string $ID - Must be an ID of a campaign
* @return Campaign Object of Campaign of given ID
*/
public function getCampaignByID($ID){
$CampaignsCollection = new CampaignsCollection($this->CTCTRequest);
return $CampaignsCollection->getCampaignDetails($this->CTCTRequest->baseUri."/ws/customers/". $this->CTCTRequest->username . "/campaigns/" . $ID);
}
/**
* Get full details for a Campaign
* @param Campaign $item - Campaign object
*/
public function getCampaignDetails(Campaign $Campaign){
$CampaignsCollection = new CampaignsCollection($this->CTCTRequest);
return $CampaignsCollection->getCampaignDetails($this->CTCTRequest->baseUri.$Campaign->link);
}
/**
* Get events from a Campaign
* @param Campaign $Campaign - Campaign to get events for
* @param string $eventType - Sends, Forwards, Bounces, OptOuts, Opens
* @return array
*/
public function getCampaignEvents(Campaign $Campaign, $eventType){
$CampaignsCollection = new CampaignsCollection($this->CTCTRequest);
return $CampaignsCollection->getCampaignEvents($Campaign, $eventType);
}
/**
* Get open events for a Campaign
* @param Campaign $Campaign
* @param string $page - optional 'nextLink' from previous getContactOpens call
* @return array - Up to 50 CampaignEvents and a link to the next page if one exists
*/
public function getCampaignOpens(Campaign $Campaign, $page=null){
$CampaignsCollection = new CampaignsCollection($this->CTCTRequest);
$url = ($page) ? $this->CTCTRequest->baseUri.$page : $this->CTCTRequest->baseUri.$Campaign->link.'/events/opens';
return $CampaignsCollection->getCampaignEvents($url, 'OpenEvent');
}
/**
* Get forwards events for a Campaign
* @param Contact $Campaign
* @param string $page - optional 'nextLink' from previous getContactForwards call
* @return array - Up to 50 CampaignEvents and a link to the next page if one exists
*/
public function getCampaignForwards(Campaign $Campaign, $page=null){
$CampaignsCollection = new CampaignsCollection($this->CTCTRequest);
$url = ($page) ? $this->CTCTRequest->baseUri.$page : $this->CTCTRequest->baseUri.$Campaign->link.'/events/forwards';
return $CampaignsCollection->getCampaignEvents($url, 'ForwardEvent');
}
/**
* Get bounce events for a Campaign
* @param Contact $Campaign
* @param string $page - optional 'nextLink' from previous getContactBounces call
* @return array - Up to 50 CampaignEvents and a link to the next page if one exists
*/
public function getCampaignBounces(Campaign $Campaign, $page=null){
$CampaignsCollection = new CampaignsCollection($this->CTCTRequest);
$url = ($page) ? $this->CTCTRequest->baseUri.$page : $this->CTCTRequest->baseUri.$Campaign->link.'/events/bounces';
return $CampaignsCollection->getCampaignEvents($url, 'BounceEvent');
}
/**
* Get opt out events for a Campaign
* @param Contact $Campaign
* @param string $page - optional 'nextLink' from previous getContactOptOuts call
* @return array - Up to 50 CampaignEvents and a link to the next page if one exists
*/
public function getCampaignOptOuts(Campaign $Campaign, $page=null){
$CampaignsCollection = new CampaignsCollection($this->CTCTRequest);
$url = ($page) ? $this->CTCTRequest->baseUri.$page : $this->CTCTRequest->baseUri.$Campaign->link.'/events/optouts';
return $CampaignsCollection->getCampaignEvents($url, 'OptoutEvent');
}
/**
* Get send events for a Campaign
* @param Contact $Campaign
* @param string $page - optional 'nextLink' from previous getContactSends call
* @return array - Up to 50 CampaignEvents and a link to the next page if one exists
*/
public function getCampaignSends(Campaign $Campaign, $page=null){
$CampaignsCollection = new CampaignsCollection($this->CTCTRequest);
$url = ($page) ? $this->CTCTRequest->baseUri.$page : $this->CTCTRequest->baseUri.$Campaign->link.'/events/sends';
return $CampaignsCollection->getCampaignEvents($url, 'SentEvent');
}
/**
* Get a page of campaigns in a particular status
* @param string $status - status of campaign to search for (sent, draft, running, scheduled)
* @return array - Up to 50 Campaigns and a link to the next page if one exists
*/
public function getCampaignsByStatus($status, $page=null){
$CampaignsCollection = new CampaignsCollection($this->CTCTRequest);
return $CampaignsCollection->getCampaignsByStatus($status, $page);
}
/**
* Schedule an email campaign for delivery
* @param Campaign $Campaign - Email to be delivered
* @param string $time - Date/Time for email to be sent
* @return bool - true if successful, else false
*/
public function scheduleCampaign(Campaign $Campaign, $time){
$CampaignCollection = new CampaignsCollection($this->CTCTRequest);
return $CampaignCollection->scheduleCampaign($Campaign, $time);
}
/**
* Get Schedule for a campaign in scheduled status
* @param Campaign $Campaign - Campaign to get a schedule for
* @return bool|Schedule - returns Schedule if found, else false
*/
public function getCampaignSchedule(Campaign $Campaign){
$CampaignsCollection = new CampaignsCollection($this->CTCTRequest);
return $CampaignsCollection->getSchedule($Campaign);
}
/**
* Delete a schedule from a campaign (prevent it from sending)
* @param Campaign $Campaign - Campaign to delete schedule for
* @return bool - true if successful, else false
*/
public function deleteCampaignSchedule(Campaign $Campaign){
$CampaignsCollection = new CampaignsCollection($this->CTCTRequest);
return $CampaignsCollection->deleteSchedule($Campaign);
}
/**
* Delete an email Campaign
* @param Campaign $Campaign - Campaign object
*/
public function deleteCampaign(Campaign $Campaign){
$CampaignsCollection = new CampaignsCollection($this->CTCTRequest);
return $CampaignsCollection->deleteCampaign($this->CTCTRequest->baseUri.$Campaign->link);
}
/**
* @param array $params - associate array of Campaign properties
* @param VerifiedAddress $fromEmail - from email for the campaign
* @param VerifiedAddress $replyEmail - OPTIONAL: reply email if different than fromEmail
* @return Campaign
*/
public function addCampaign(Campaign $Campaign, VerifiedAddress $fromEmail, VerifiedAddress $replyEmail = null){
$replyEmail = ($replyEmail) ? $replyEmail : $fromEmail;
$Campaign->fromAddress = $fromEmail;
$Campaign->replyAddress = $replyEmail;
$CampaignsCollection = new CampaignsCollection($this->CTCTRequest);
return $CampaignsCollection->addCampaign($Campaign);
}
/**
* Update a campaign with its current properties
* @param Campaign $Campaign - Campaign to update
* @return
*/
public function updateCampaign(Campaign $Campaign){
$CampaignsCollection = new CampaignsCollection($this->CTCTRequest);
return $CampaignsCollection->updateCampaign($Campaign);
}
/**
* @param Folder $Folder - Folder object
* @return Folder - Folder object
*/
public function addFolder(Folder $Folder){
$LibraryCollection = new LibraryCollection($this->CTCTRequest);
return $LibraryCollection->addFolder($Folder);
}
/**
* Get a page of Folders
* @param string $page - optional 'nextLink' returned from a previous getFolders() call
* @return array - Up to 50 Folders and a link to the next page if one exists
*/
public function getFolders($page=null){
$LibraryCollection = new LibraryCollection($this->CTCTRequest);
return $LibraryCollection->getFolders($page);
}
/**
* Get a page of images from a folder
* @param mixed $folder - Folder object
* @return array - All images contained within the folder
*/
public function getImagesFromFolder($folder){
$LibraryCollection = new LibraryCollection($this->CTCTRequest);
return $LibraryCollection->getImagesFromFolder($this->CTCTRequest->baseUri.$folder->link.'/images');
}
/**
* Get detailed results for an image
* @param Image $image - image object
* @return Image - Image object
*/
public function getImageDetails(Image $Image){
$LibraryCollection = new LibraryCollection($this->CTCTRequest);
return $LibraryCollection->getImageDetails($this->CTCTRequest->baseUri.$Image->link);
}
/**
* @param string $imageLocation - current location of image on disk
* @param Folder $folder - Folder to upload image to
* @return Image - created Image object
*/
public function uploadImage($imageLocation, Folder $Folder){
$LibraryCollection = new LibraryCollection($this->CTCTRequest);
return $LibraryCollection->uploadImage($imageLocation, $this->CTCTRequest->baseUri.$Folder->link);
}
/**
* Delete an image from your account
* @param Image $Image - Image object
* @return bool - true if successful, else false
*/
public function deleteImage(Image $Image){
$LibraryCollection = new LibraryCollection($this->CTCTRequest);
return $LibraryCollection->deleteImage($this->CTCTRequest->baseUri.$Image->link);
}
/**
* Delete all images from a folder
* @param Folder $Folder - Folder object
* @return bool - true if successful, else false
*/
public function deleteImagesFromFolder(Folder $Folder){
$LibraryCollection = new LibraryCollection($this->CTCTRequest);
return $LibraryCollection->deleteImagesFromFolder($this->CTCTRequest->baseUri.$Folder->link.'/images');
}
/**
* Get a list of verified email addresses in the account
* @return array - Up to 50 Verified addresses and a link to the next page if one exists
*/
public function getVerifiedAddresses($page=null){
$SettingsCollection = new SettingsCollection($this->CTCTRequest);
return $SettingsCollection->getAddresses($page);
}
/**
* Get a page of Event objects
* @param string $page - optional 'nextLink' returned from a previous getEvents() call
* @return array - Up to 50 Images and a link to the next page if one exists
*/
public function getEvents($page=null){
$EventsCollection = new EventsCollection($this->CTCTRequest);
return $EventsCollection->getEvents($page);
}
/**
* Get all details for an event
* @param Event Event - Event object to get details for
* @return Event
*/
public function getEventDetails(Event $Event){
$EventsCollection = new EventsCollection($this->CTCTRequest);
return $EventsCollection->getEventDetails($this->CTCTRequest->baseUri.$Event->link);
}
/**
* Get registrants from a particular event
* @param Event $Event - Event Object
* @return array - up to 50 registrants and a link to the next page if one exists
*/
public function getRegistrants(Event $Event, $page = null){
$EventsCollection = new EventsCollection($this->CTCTRequest);
if($page !== null)
{
return $EventsCollection->getRegistrants($this->CTCTRequest->baseUri.$page);
}
return $EventsCollection->getRegistrants($this->CTCTRequest->baseUri.$Event->link.'/registrants?pageNumber=1');
}
/**
* Get detailed information on a Registrant
* @param Registrant $Registrant - Registrant Object
* @return Registrant
*/
public function getRegistrantDetails(Registrant $Registrant){
$EventsCollection = new EventsCollection($this->CTCTRequest);
return $EventsCollection->getRegistrantDetails($this->CTCTRequest->baseUri.$Registrant->link);
}
/**
* Add multiple contacts at once
* @param string $postString - urlencoded string of data
* @return bool
*/
public function bulkAddContacts($postString){
$ActivitiesCollection = new ActivitiesCollection($this->CTCTRequest);
return $ActivitiesCollection->bulkAddContacts($postString);
}
}