Skip to content
This repository was archived by the owner on Dec 16, 2021. It is now read-only.

Commit 7342054

Browse files
committed
Merge pull request #106 from WoogieNoogie/preview
#102 - add CampaignPreview
2 parents d05bf74 + 7080b65 commit 7342054

File tree

9 files changed

+131
-23
lines changed

9 files changed

+131
-23
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Constant Contact PHP SDK
22
[![Build Status](https://secure.travis-ci.org/constantcontact/php-sdk.png?branch=master)](http://travis-ci.org/constantcontact/php-sdk) [![Latest Stable Version](https://poser.pugx.org/constantcontact/constantcontact/v/stable.svg)](https://packagist.org/packages/constantcontact/constantcontact) [![Latest Unstable Version](https://poser.pugx.org/constantcontact/constantcontact/v/unstable.svg)](https://packagist.org/packages/constantcontact/constantcontact)
33

4-
### This library utilizes [GuzzlePHP](https://guzzlephp.org)
4+
### This library utilizes [GuzzlePHP](http://guzzle.readthedocs.org/)
55

66
## Installing via Composer
77
[Composer](https://getcomposer.org/) is a dependency management tool for PHP that allows you to declare the dependencies your project needs and installs them into your project. In order to use the Constant Contact PHP SDK through composer, you must add "constantcontact/constantcontact" as a dependency in your project's composer.json file.
88
```javascript
99
{
1010
"require": {
11-
"constantcontact/constantcontact": "2.0.*"
11+
"constantcontact/constantcontact": "2.1.*"
1212
}
1313
}
1414
```

examples/addOrUpdateContact.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,25 @@
7070
} else {
7171
$action = "Updating Contact";
7272

73-
$contact = Contact::create($response->results[0]);
74-
$contact->addList($_POST['list']);
75-
$contact->first_name = $_POST['first_name'];
76-
$contact->last_name = $_POST['last_name'];
77-
78-
/*
79-
* The third parameter of updateContact defaults to false, but if this were set to true it would tell
80-
* Constant Contact that this action is being performed by the contact themselves, and gives the ability to
81-
* opt contacts back in and trigger Welcome/Change-of-interest emails.
82-
*
83-
* See: http://developer.constantcontact.com/docs/contacts-api/contacts-index.html#opt_in
84-
*/
85-
$returnContact = $cc->contactService->updateContact(ACCESS_TOKEN, $contact);
73+
$contact = $response->results[0];
74+
if ($contact instanceof Contact) {
75+
$contact->addList($_POST['list']);
76+
$contact->first_name = $_POST['first_name'];
77+
$contact->last_name = $_POST['last_name'];
78+
79+
/*
80+
* The third parameter of updateContact defaults to false, but if this were set to true it would tell
81+
* Constant Contact that this action is being performed by the contact themselves, and gives the ability to
82+
* opt contacts back in and trigger Welcome/Change-of-interest emails.
83+
*
84+
* See: http://developer.constantcontact.com/docs/contacts-api/contacts-index.html#opt_in
85+
*/
86+
$returnContact = $cc->contactService->updateContact(ACCESS_TOKEN, $contact);
87+
} else {
88+
$e = new CtctException();
89+
$e->setErrors(array("type", "Contact type not returned"));
90+
throw $e;
91+
}
8692
}
8793

8894
// catch any exceptions thrown during the process and print the errors to screen
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
namespace Ctct\Components\EmailMarketing;
3+
4+
use Ctct\Components\Component;
5+
6+
class CampaignPreview extends Component {
7+
/**
8+
* Email address set as the from email
9+
* @var String
10+
*/
11+
public $fromEmail;
12+
13+
/**
14+
* Email address set as the reply-to email
15+
* @var String
16+
*/
17+
public $replyToEmail;
18+
19+
/**
20+
* Full HTML content of the campaign
21+
* @var String
22+
*/
23+
public $htmlContent;
24+
25+
/**
26+
* Text version content of the campaign
27+
* @var
28+
*/
29+
public $textContent;
30+
31+
/**
32+
* Subject of the email
33+
* @var String
34+
*/
35+
public $subject;
36+
37+
/**
38+
* Factory method to create a CampaignPreview object from an array
39+
* @param array $props - associative array of initial properties to set
40+
* @return CampaignPreview
41+
*/
42+
public static function create(array $props) {
43+
$preview = new CampaignPreview();
44+
$preview->fromEmail = parent::getValue($props, "from_email");
45+
$preview->replyToEmail = parent::getValue($props, "reply_to_email");
46+
$preview->htmlContent = parent::getValue($props, "preview_email_content");
47+
$preview->textContent = parent::getValue($props, "preview_text_content");
48+
$preview->subject = parent::getValue($props, "subject");
49+
return $preview;
50+
}
51+
}

src/Ctct/Services/ContactService.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ public function updateContact($accessToken, Contact $contact, Array $params = ar
194194
$query->add($name, $value);
195195
}
196196
}
197+
$stream = Stream::factory(json_encode($contact));
198+
$request->setBody($stream);
197199

198200
try {
199201
$response = parent::getClient()->send($request);

src/Ctct/Services/EmailMarketingService.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Ctct\Exceptions\CtctException;
55
use Ctct\Util\Config;
66
use Ctct\Components\EmailMarketing\Campaign;
7+
use Ctct\Components\EmailMarketing\CampaignPreview;
78
use Ctct\Components\ResultSet;
89
use GuzzleHttp\Exception\ClientException;
910
use GuzzleHttp\Stream\Stream;
@@ -85,13 +86,13 @@ public function getCampaigns($accessToken, Array $params = array())
8586
/**
8687
* Get campaign details for a specific campaign
8788
* @param string $accessToken - Constant Contact OAuth2 access token
88-
* @param int $campaign_id - Valid campaign id
89+
* @param int $campaignId - Valid campaign id
8990
* @return Campaign
9091
* @throws CtctException
9192
*/
92-
public function getCampaign($accessToken, $campaign_id)
93+
public function getCampaign($accessToken, $campaignId)
9394
{
94-
$baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.campaign'), $campaign_id);
95+
$baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.campaign'), $campaignId);
9596

9697
$request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
9798

@@ -107,13 +108,13 @@ public function getCampaign($accessToken, $campaign_id)
107108
/**
108109
* Delete an email campaign
109110
* @param string $accessToken - Constant Contact OAuth2 access token
110-
* @param int $campaign_id - Valid campaign id
111+
* @param int $campaignId - Valid campaign id
111112
* @return boolean
112113
* @throws CtctException
113114
*/
114-
public function deleteCampaign($accessToken, $campaign_id)
115+
public function deleteCampaign($accessToken, $campaignId)
115116
{
116-
$baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.campaign'), $campaign_id);
117+
$baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.campaign'), $campaignId);
117118

118119
$request = parent::createBaseRequest($accessToken, 'DELETE', $baseUrl);
119120

@@ -149,4 +150,24 @@ public function updateCampaign($accessToken, Campaign $campaign)
149150

150151
return Campaign::create($response->json());
151152
}
153+
154+
/**
155+
* Get a preview of an email campaign
156+
* @param string $accessToken - Constant Contact OAuth2 access token
157+
* @param int $campaignId - Valid campaign id
158+
* @return CampaignPreview
159+
* @throws CtctException
160+
*/
161+
public function getPreview($accessToken, $campaignId) {
162+
$baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.campaign_preview'), $campaignId);
163+
164+
$request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
165+
try {
166+
$response = parent::getClient()->send($request);
167+
} catch (ClientException $e) {
168+
throw parent::convertException($e);
169+
}
170+
171+
return CampaignPreview::create($response->json());
172+
}
152173
}

src/Ctct/Util/Config.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class Config
3838
'campaign_schedules' => 'emailmarketing/campaigns/%s/schedules',
3939
'campaign_schedule' => 'emailmarketing/campaigns/%s/schedules/%s',
4040
'campaign_test_sends' => 'emailmarketing/campaigns/%s/tests',
41+
'campaign_preview' => 'emailmarketing/campaigns/%s/preview',
4142
'campaign_tracking_summary' => 'emailmarketing/campaigns/%s/tracking/reports/summary',
4243
'campaign_tracking_bounces' => 'emailmarketing/campaigns/%s/tracking/bounces',
4344
'campaign_tracking_clicks' => 'emailmarketing/campaigns/%s/tracking/clicks',
@@ -124,7 +125,7 @@ class Config
124125
* Setting the version fo the application used in Rest Calls when setting the version header
125126
*/
126127
'settings' => array(
127-
'version' => '2.0.0'
128+
'version' => '2.1.0'
128129
),
129130
);
130131

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"subject":"Subject Test",
3+
"from_email":"[email protected]",
4+
"reply_to_email":"[email protected]",
5+
"preview_email_content":"<head ><meta /></head><body><center><table bgcolor=\"#ffffff\" id=\"VWPLINK\" width=\"595\"><tr><td style=\"font-size: 8pt; font-family: Verdana, Arial, Helvetica, sans-serif; color: #000000;\" width=\"100%\">View this message as a web page\n<a >Click here\n</a></td></tr></table></center><center ><table bgcolor=\"#ffffff\" width=\"595\" ><tr ><td width=\"100%\" ><font color=\"#000000\" face=\"verdana,arial\" size=\"1\" ><div >As a reminder, you're receiving this email because you have expressed an interest in MyCompany. Don't forget to add [email protected] to your address book so we'll be sure to land in your inbox! You may unsubscribe if you no longer wish to receive our emails. <div >&nbsp;</div><div >You may <a >unsubscribe</a> if you no longer wish to receive our emails.</div></div></font></td></tr></table></center><img /><p>This is text of the email message.</p><br />\n<table bgcolor=\"#ffffff\" padding=\"0\" width=\"100%\" ><tr align=\"center\" ><td ><table bgcolor=\"#ffffff\" width=\"595\" ><tr ><td colspan=\"2\" ><font face=\"tahoma,sans-serif\" size=\"1\" ><b ><a >Click here to forward this message</a></b></font><br />\n<br />\n</td></tr>\n<tr ><td ><FooterContent ><a ><img /></a></FooterContent></td><td align=\"right\" ><font face=\"tahoma,sans-serif\" size=\"1\" ><FooterLogo ><a ><img /></a></FooterLogo></font>\n</td>\n</tr><tr ><td colspan=\"2\" ><font face=\"tahoma,sans-serif\" size=\"1\" ><FooterContent ><div >This email was sent to {Email Address} by <a >[email protected]</a> <span style=\"color: #bababa;\" > | </span> &nbsp; </div>\n<div ><a >Update Profile/Email Address</a> <span style=\"color: #bababa;\" >|</span> Instant removal with <a >SafeUnsubscribe</a>&trade; <span style=\"color: #bababa;\" >|</span> <a >Privacy Policy</a>.</div></FooterContent></font>\n</td>\n</tr>\n<tr ><td colspan=\"2\" ><font face=\"tahoma,sans-serif\" size=\"1\" ><br />My Organization | 123 Maple Street | Suite 1 | Anytown | MA | 01444</font>\n</td>\n</tr>\n</table>\n</td>\n</tr>\n</table>\n<br />\n&lt;/body&gt;",
6+
"preview_text_content":"View this message as a web page\nClick here\nhttp://campaign.r20.l1.constantcontact.com/render?ca=025eff86-6378-4f53-9301-5897ecf50b30&c={Contact Id}&ch={Contact Id}\n\nAs a reminder, you're receiving this email because you have expressed an interest in MyCompany. Don't forget to add [email protected] to your address book so we'll be sure to land in your inbox! You may unsubscribe if you no longer wish to receive our emails. You may unsubscribe\nhttp://visitor.l1.constantcontact.com/do?p=un&m=001JZtDyxcvPiye1EthMqSLGA%3D%3D&ch={Contact Id}&ca=025eff86-6378-4f53-9301-5897ecf50b30\n if you no longer wish to receive our emails.\n------------------------------------------------------------\nThis is the text of the email message.\n\nClick here to forward this message\nhttp://ui.l1.constantcontact.com/sa/fwtf.jsp?llr=cqmhk9aab&m=1100394770946&ea=rmarcucella%40constantcontact.com&a=1100400205633\n\n\n\n\n\nThis email was sent to {Email Address} by [email protected].\n\nUpdate Profile/Email Address\nhttp://visitor.l1.constantcontact.com/do?p=oo&m=001JZtDyxcvPiye1EthMqSLGA%3D%3D&ch={Contact Id}&ca=025eff86-6378-4f53-9301-5897ecf50b30\n\n\nInstant removal with SafeUnsubscribe(TM)\nhttp://visitor.l1.constantcontact.com/do?p=un&m=001JZtDyxcvPiye1EthMqSLGA%3D%3D&ch={Contact Id}&ca=025eff86-6378-4f53-9301-5897ecf50b30\n\n\nPrivacy Policy:\nhttp://ui.l1.constantcontact.com/roving/CCPrivacyPolicy.jsp\n\n\n\n\n\nOnline Marketing by\nhttp://img.l1.constantcontact.com/letters/images/cc-logo-color-sm.gif\nhttp://www.constantcontact.com\n\n\n\nMy Organization | 123 Maple Street | Suite 1 | Anytown | MA | 01444\n\n\n\n\n\n\n\n\n"
7+
}

test/Json/JsonLoader.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ public static function getTestSendJson()
106106
return file_get_contents(__DIR__ . self::CAMPAIGNS_FOLDER . "/post_test_send.json");
107107
}
108108

109+
public static function getPreviewJson() {
110+
return file_get_contents(__DIR__ . self::CAMPAIGNS_FOLDER . "/get_preview.json");
111+
}
112+
109113
public static function getClicks()
110114
{
111115
return file_get_contents(__DIR__ . self::CAMPAIGN_TRACKING_FOLDER . "/get_clicks.json");

test/Services/EmailCampaignServiceUnitTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Ctct\Components\ResultSet;
44
use Ctct\Components\EmailMarketing\Campaign;
5+
use Ctct\Components\EmailMarketing\CampaignPreview;
56

67
use GuzzleHttp\Client;
78
use GuzzleHttp\Exception\ClientException;
@@ -21,13 +22,15 @@ public static function setUpBeforeClass()
2122
self::$client = new Client();
2223
$getCampaignsStream = Stream::factory(JsonLoader::getCampaignsJson());
2324
$getCampaignStream = Stream::factory(JsonLoader::getCampaignJson());
25+
$getPreviewStream = Stream::factory(JsonLoader::getPreviewJson());
2426
$mock = new Mock([
2527
new Response(200, array(), $getCampaignsStream),
2628
new Response(204, array()),
2729
new Response(400, array()),
2830
new Response(200, array(), $getCampaignStream),
2931
new Response(201, array(), $getCampaignStream),
30-
new Response(200, array(), $getCampaignStream)
32+
new Response(200, array(), $getCampaignStream),
33+
new Response(200, array(), $getPreviewStream)
3134
]);
3235
self::$client->getEmitter()->attach($mock);
3336
}
@@ -272,4 +275,17 @@ public function testUpdateCampaign()
272275
$this->assertEquals("1100394163874", $campaign->click_through_details[0]->url_uid);
273276
$this->assertEquals(10, $campaign->click_through_details[0]->click_count);
274277
}
278+
279+
public function testGetPreview() {
280+
$response = self::$client->get('/');
281+
282+
$preview = CampaignPreview::create($response->json());
283+
$this->assertEquals("Subject Test", $preview->subject);
284+
$this->assertEquals("[email protected]", $preview->fromEmail);
285+
$this->assertEquals("[email protected]", $preview->replyToEmail);
286+
$htmlContent = "<head ><meta /></head><body><center><table bgcolor=\"#ffffff\" id=\"VWPLINK\" width=\"595\"><tr><td style=\"font-size: 8pt; font-family: Verdana, Arial, Helvetica, sans-serif; color: #000000;\" width=\"100%\">View this message as a web page\n<a >Click here\n</a></td></tr></table></center><center ><table bgcolor=\"#ffffff\" width=\"595\" ><tr ><td width=\"100%\" ><font color=\"#000000\" face=\"verdana,arial\" size=\"1\" ><div >As a reminder, you're receiving this email because you have expressed an interest in MyCompany. Don't forget to add [email protected] to your address book so we'll be sure to land in your inbox! You may unsubscribe if you no longer wish to receive our emails. <div >&nbsp;</div><div >You may <a >unsubscribe</a> if you no longer wish to receive our emails.</div></div></font></td></tr></table></center><img /><p>This is text of the email message.</p><br />\n<table bgcolor=\"#ffffff\" padding=\"0\" width=\"100%\" ><tr align=\"center\" ><td ><table bgcolor=\"#ffffff\" width=\"595\" ><tr ><td colspan=\"2\" ><font face=\"tahoma,sans-serif\" size=\"1\" ><b ><a >Click here to forward this message</a></b></font><br />\n<br />\n</td></tr>\n<tr ><td ><FooterContent ><a ><img /></a></FooterContent></td><td align=\"right\" ><font face=\"tahoma,sans-serif\" size=\"1\" ><FooterLogo ><a ><img /></a></FooterLogo></font>\n</td>\n</tr><tr ><td colspan=\"2\" ><font face=\"tahoma,sans-serif\" size=\"1\" ><FooterContent ><div >This email was sent to {Email Address} by <a >[email protected]</a> <span style=\"color: #bababa;\" > | </span> &nbsp; </div>\n<div ><a >Update Profile/Email Address</a> <span style=\"color: #bababa;\" >|</span> Instant removal with <a >SafeUnsubscribe</a>&trade; <span style=\"color: #bababa;\" >|</span> <a >Privacy Policy</a>.</div></FooterContent></font>\n</td>\n</tr>\n<tr ><td colspan=\"2\" ><font face=\"tahoma,sans-serif\" size=\"1\" ><br />My Organization | 123 Maple Street | Suite 1 | Anytown | MA | 01444</font>\n</td>\n</tr>\n</table>\n</td>\n</tr>\n</table>\n<br />\n&lt;/body&gt;";
287+
$this->assertEquals($htmlContent, $preview->htmlContent);
288+
$textContent = "View this message as a web page\nClick here\nhttp://campaign.r20.l1.constantcontact.com/render?ca=025eff86-6378-4f53-9301-5897ecf50b30&c={Contact Id}&ch={Contact Id}\n\nAs a reminder, you're receiving this email because you have expressed an interest in MyCompany. Don't forget to add [email protected] to your address book so we'll be sure to land in your inbox! You may unsubscribe if you no longer wish to receive our emails. You may unsubscribe\nhttp://visitor.l1.constantcontact.com/do?p=un&m=001JZtDyxcvPiye1EthMqSLGA%3D%3D&ch={Contact Id}&ca=025eff86-6378-4f53-9301-5897ecf50b30\n if you no longer wish to receive our emails.\n------------------------------------------------------------\nThis is the text of the email message.\n\nClick here to forward this message\nhttp://ui.l1.constantcontact.com/sa/fwtf.jsp?llr=cqmhk9aab&m=1100394770946&ea=rmarcucella%40constantcontact.com&a=1100400205633\n\n\n\n\n\nThis email was sent to {Email Address} by [email protected].\n\nUpdate Profile/Email Address\nhttp://visitor.l1.constantcontact.com/do?p=oo&m=001JZtDyxcvPiye1EthMqSLGA%3D%3D&ch={Contact Id}&ca=025eff86-6378-4f53-9301-5897ecf50b30\n\n\nInstant removal with SafeUnsubscribe(TM)\nhttp://visitor.l1.constantcontact.com/do?p=un&m=001JZtDyxcvPiye1EthMqSLGA%3D%3D&ch={Contact Id}&ca=025eff86-6378-4f53-9301-5897ecf50b30\n\n\nPrivacy Policy:\nhttp://ui.l1.constantcontact.com/roving/CCPrivacyPolicy.jsp\n\n\n\n\n\nOnline Marketing by\nhttp://img.l1.constantcontact.com/letters/images/cc-logo-color-sm.gif\nhttp://www.constantcontact.com\n\n\n\nMy Organization | 123 Maple Street | Suite 1 | Anytown | MA | 01444\n\n\n\n\n\n\n\n\n";
289+
$this->assertEquals($textContent, $preview->textContent);
290+
}
275291
}

0 commit comments

Comments
 (0)