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

Commit

Permalink
Merge pull request #106 from WoogieNoogie/preview
Browse files Browse the repository at this point in the history
#102 - add CampaignPreview
  • Loading branch information
egaiter committed Jul 10, 2015
2 parents d05bf74 + 7080b65 commit 7342054
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
@@ -1,14 +1,14 @@
# Constant Contact PHP SDK
[![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)

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

## Installing via Composer
[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.
```javascript
{
"require": {
"constantcontact/constantcontact": "2.0.*"
"constantcontact/constantcontact": "2.1.*"
}
}
```
Expand Down
32 changes: 19 additions & 13 deletions examples/addOrUpdateContact.php
Expand Up @@ -70,19 +70,25 @@
} else {
$action = "Updating Contact";

$contact = Contact::create($response->results[0]);
$contact->addList($_POST['list']);
$contact->first_name = $_POST['first_name'];
$contact->last_name = $_POST['last_name'];

/*
* The third parameter of updateContact defaults to false, but if this were set to true it would tell
* Constant Contact that this action is being performed by the contact themselves, and gives the ability to
* opt contacts back in and trigger Welcome/Change-of-interest emails.
*
* See: http://developer.constantcontact.com/docs/contacts-api/contacts-index.html#opt_in
*/
$returnContact = $cc->contactService->updateContact(ACCESS_TOKEN, $contact);
$contact = $response->results[0];
if ($contact instanceof Contact) {
$contact->addList($_POST['list']);
$contact->first_name = $_POST['first_name'];
$contact->last_name = $_POST['last_name'];

/*
* The third parameter of updateContact defaults to false, but if this were set to true it would tell
* Constant Contact that this action is being performed by the contact themselves, and gives the ability to
* opt contacts back in and trigger Welcome/Change-of-interest emails.
*
* See: http://developer.constantcontact.com/docs/contacts-api/contacts-index.html#opt_in
*/
$returnContact = $cc->contactService->updateContact(ACCESS_TOKEN, $contact);
} else {
$e = new CtctException();
$e->setErrors(array("type", "Contact type not returned"));
throw $e;
}
}

// catch any exceptions thrown during the process and print the errors to screen
Expand Down
51 changes: 51 additions & 0 deletions src/Ctct/Components/EmailMarketing/CampaignPreview.php
@@ -0,0 +1,51 @@
<?php
namespace Ctct\Components\EmailMarketing;

use Ctct\Components\Component;

class CampaignPreview extends Component {
/**
* Email address set as the from email
* @var String
*/
public $fromEmail;

/**
* Email address set as the reply-to email
* @var String
*/
public $replyToEmail;

/**
* Full HTML content of the campaign
* @var String
*/
public $htmlContent;

/**
* Text version content of the campaign
* @var
*/
public $textContent;

/**
* Subject of the email
* @var String
*/
public $subject;

/**
* Factory method to create a CampaignPreview object from an array
* @param array $props - associative array of initial properties to set
* @return CampaignPreview
*/
public static function create(array $props) {
$preview = new CampaignPreview();
$preview->fromEmail = parent::getValue($props, "from_email");
$preview->replyToEmail = parent::getValue($props, "reply_to_email");
$preview->htmlContent = parent::getValue($props, "preview_email_content");
$preview->textContent = parent::getValue($props, "preview_text_content");
$preview->subject = parent::getValue($props, "subject");
return $preview;
}
}
2 changes: 2 additions & 0 deletions src/Ctct/Services/ContactService.php
Expand Up @@ -194,6 +194,8 @@ public function updateContact($accessToken, Contact $contact, Array $params = ar
$query->add($name, $value);
}
}
$stream = Stream::factory(json_encode($contact));
$request->setBody($stream);

try {
$response = parent::getClient()->send($request);
Expand Down
33 changes: 27 additions & 6 deletions src/Ctct/Services/EmailMarketingService.php
Expand Up @@ -4,6 +4,7 @@
use Ctct\Exceptions\CtctException;
use Ctct\Util\Config;
use Ctct\Components\EmailMarketing\Campaign;
use Ctct\Components\EmailMarketing\CampaignPreview;
use Ctct\Components\ResultSet;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Stream\Stream;
Expand Down Expand Up @@ -85,13 +86,13 @@ public function getCampaigns($accessToken, Array $params = array())
/**
* Get campaign details for a specific campaign
* @param string $accessToken - Constant Contact OAuth2 access token
* @param int $campaign_id - Valid campaign id
* @param int $campaignId - Valid campaign id
* @return Campaign
* @throws CtctException
*/
public function getCampaign($accessToken, $campaign_id)
public function getCampaign($accessToken, $campaignId)
{
$baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.campaign'), $campaign_id);
$baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.campaign'), $campaignId);

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

Expand All @@ -107,13 +108,13 @@ public function getCampaign($accessToken, $campaign_id)
/**
* Delete an email campaign
* @param string $accessToken - Constant Contact OAuth2 access token
* @param int $campaign_id - Valid campaign id
* @param int $campaignId - Valid campaign id
* @return boolean
* @throws CtctException
*/
public function deleteCampaign($accessToken, $campaign_id)
public function deleteCampaign($accessToken, $campaignId)
{
$baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.campaign'), $campaign_id);
$baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.campaign'), $campaignId);

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

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

return Campaign::create($response->json());
}

/**
* Get a preview of an email campaign
* @param string $accessToken - Constant Contact OAuth2 access token
* @param int $campaignId - Valid campaign id
* @return CampaignPreview
* @throws CtctException
*/
public function getPreview($accessToken, $campaignId) {
$baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.campaign_preview'), $campaignId);

$request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
try {
$response = parent::getClient()->send($request);
} catch (ClientException $e) {
throw parent::convertException($e);
}

return CampaignPreview::create($response->json());
}
}
3 changes: 2 additions & 1 deletion src/Ctct/Util/Config.php
Expand Up @@ -38,6 +38,7 @@ class Config
'campaign_schedules' => 'emailmarketing/campaigns/%s/schedules',
'campaign_schedule' => 'emailmarketing/campaigns/%s/schedules/%s',
'campaign_test_sends' => 'emailmarketing/campaigns/%s/tests',
'campaign_preview' => 'emailmarketing/campaigns/%s/preview',
'campaign_tracking_summary' => 'emailmarketing/campaigns/%s/tracking/reports/summary',
'campaign_tracking_bounces' => 'emailmarketing/campaigns/%s/tracking/bounces',
'campaign_tracking_clicks' => 'emailmarketing/campaigns/%s/tracking/clicks',
Expand Down Expand Up @@ -124,7 +125,7 @@ class Config
* Setting the version fo the application used in Rest Calls when setting the version header
*/
'settings' => array(
'version' => '2.0.0'
'version' => '2.1.0'
),
);

Expand Down
7 changes: 7 additions & 0 deletions test/Json/Campaigns/get_preview.json
@@ -0,0 +1,7 @@
{
"subject":"Subject Test",
"from_email":"myemail@example.com",
"reply_to_email":"myemail@example.com",
"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 from_email@example.com 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 >rmarcucella@constantcontact.com</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;",
"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 from_email@example.com 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 rmarcucella@constantcontact.com.\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"
}
4 changes: 4 additions & 0 deletions test/Json/JsonLoader.php
Expand Up @@ -106,6 +106,10 @@ public static function getTestSendJson()
return file_get_contents(__DIR__ . self::CAMPAIGNS_FOLDER . "/post_test_send.json");
}

public static function getPreviewJson() {
return file_get_contents(__DIR__ . self::CAMPAIGNS_FOLDER . "/get_preview.json");
}

public static function getClicks()
{
return file_get_contents(__DIR__ . self::CAMPAIGN_TRACKING_FOLDER . "/get_clicks.json");
Expand Down
18 changes: 17 additions & 1 deletion test/Services/EmailCampaignServiceUnitTest.php
Expand Up @@ -2,6 +2,7 @@

use Ctct\Components\ResultSet;
use Ctct\Components\EmailMarketing\Campaign;
use Ctct\Components\EmailMarketing\CampaignPreview;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
Expand All @@ -21,13 +22,15 @@ public static function setUpBeforeClass()
self::$client = new Client();
$getCampaignsStream = Stream::factory(JsonLoader::getCampaignsJson());
$getCampaignStream = Stream::factory(JsonLoader::getCampaignJson());
$getPreviewStream = Stream::factory(JsonLoader::getPreviewJson());
$mock = new Mock([
new Response(200, array(), $getCampaignsStream),
new Response(204, array()),
new Response(400, array()),
new Response(200, array(), $getCampaignStream),
new Response(201, array(), $getCampaignStream),
new Response(200, array(), $getCampaignStream)
new Response(200, array(), $getCampaignStream),
new Response(200, array(), $getPreviewStream)
]);
self::$client->getEmitter()->attach($mock);
}
Expand Down Expand Up @@ -272,4 +275,17 @@ public function testUpdateCampaign()
$this->assertEquals("1100394163874", $campaign->click_through_details[0]->url_uid);
$this->assertEquals(10, $campaign->click_through_details[0]->click_count);
}

public function testGetPreview() {
$response = self::$client->get('/');

$preview = CampaignPreview::create($response->json());
$this->assertEquals("Subject Test", $preview->subject);
$this->assertEquals("myemail@example.com", $preview->fromEmail);
$this->assertEquals("myemail@example.com", $preview->replyToEmail);
$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 from_email@example.com 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 >rmarcucella@constantcontact.com</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;";
$this->assertEquals($htmlContent, $preview->htmlContent);
$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 from_email@example.com 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 rmarcucella@constantcontact.com.\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";
$this->assertEquals($textContent, $preview->textContent);
}
}

0 comments on commit 7342054

Please sign in to comment.