Skip to content

Commit

Permalink
Merge pull request #12 from onedesign/feature/update-subscribers
Browse files Browse the repository at this point in the history
Feature/Update Subscribers
  • Loading branch information
brianjhanson authored Apr 19, 2017
2 parents 79eb985 + 7355d8a commit c2f2340
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,27 @@ $resubscribe = false;
craft()->oneCampaignMonitor_subscribers->add($list_id, $email, $name, $customFields, $resubscribe);
```

Update a subscriber in a list:

```
$list_id = '123';
$email = 'email@email.com';
$name = 'Steve';
$customFields = ['city': 'New York'];
$resubscribe = true;
craft()->oneCampaignMonitor_subscribers->update($list_id, $email, $name, $customFields, $resubscribe);
```

Determine if a subscriber exists in a list:

```
$list_id = '123';
$email = 'email@email.com';
if (craft()->oneCampaignMonitor_subscribers->exists($list_id, $email)) {
// subscriber exists
}
```

### Checking if the user has subscribed to a list

You can check in a template if the current user (by session) has already subscribed to a list:
Expand Down
67 changes: 67 additions & 0 deletions consolecommands/OneCampaignMonitorSubscribersCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
namespace Craft;

class OneCampaignMonitorSubscribersCommand extends BaseCommand {

/**
* Adds a subscriber to a list
* Usage:
* php ./craft/app/etc/console/yiic onecampaignmonitorsubscribers add \
* --listId="asdf..." \
* --email="test@example.com" \
* --name="Mr. Test" \
* --resubscribe=1 \
* --customFields='[{"Key":"City","Value":"Chicago"}]' \
*/
public function actionAdd($listId, $email, $name=null, $customFields='[]', $resubscribe=true) {
$customFields = $this->_decodeCustomFields($customFields);
craft()->oneCampaignMonitor_subscribers->add($listId, $email, $name, $customFields, $resubscribe ? true : false);
}

/**
* Updates a subscriber in a list
* Usage:
* php ./craft/app/etc/console/yiic onecampaignmonitorsubscribers update \
* --listId="asdf..." \
* --email="test@example.com" \
* --name="Joan Doe" \
* --resubscribe=0 \
* --customFields='[{"Key":"City","Value":"New York"}]' \
* --merge=1
*/
public function actionUpdate($listId, $email, $name=null, $customFields='[]', $resubscribe=false, $merge=true) {
$customFields = $this->_decodeCustomFields($customFields);
craft()->oneCampaignMonitor_subscribers->update($listId, $email, $name, $customFields, $resubscribe ? true : false, $merge ? true : false);
}

/**
* Determines is an email exists in a list
* Usage:
* php ./craft/app/etc/console/yiic onecampaignmonitorsubscribers update \
* --listId="asdf..." \
* --email="test@example.com"
*/
public function actionExists($listId, $email) {
if (craft()->oneCampaignMonitor_subscribers->exists($listId, $email)) {
OneCampaignMonitorPlugin::log('Subscriber exists in this list.', LogLevel::Info);
} else {
OneCampaignMonitorPlugin::log('Subscriber does not exist in the list.', LogLevel::Info);
}
}

/**
* Decodes the customFields input option
* @param String $customFields JSON array of key/value objects
* @return Array
*/
private function _decodeCustomFields($customFields) {
$decoded = json_decode($customFields, true);

if (!is_array($decoded)) {
throw new Exception('customFields must be specified as a JSON array of key/value objects');
}

return $decoded;
}

}
88 changes: 88 additions & 0 deletions services/OneCampaignMonitor_SubscribersService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@

class OneCampaignMonitor_SubscribersService extends OneCampaignMonitor_BaseService {

/**
* Adds a subscriber to a list
* @param $listId
* @param $email
* @param $name
* @param $customFields
* @param $resubscribe
* @param $merge
* @throws Exception
*/
public function add($listId, $email, $name=null, $customFields=array(), $resubscribe=true) {
if (!$listId) {
throw new Exception('List ID is required');
Expand All @@ -29,4 +39,82 @@ public function add($listId, $email, $name=null, $customFields=array(), $resubsc
return true;
}

/**
* Determines is a subscriber exists in a list
* @param $listId
* @param $email
* @return Boolean
* @throws Exception
*/
public function exists($listId, $email) {
if (!$listId) {
throw new Exception('List ID is required');
}
if (!$email) {
throw new Exception('Please provide a valid email address');
}

$connection = new \CS_REST_Subscribers($listId, $this->auth());

$result = $connection->get($email);

$error = null;
return $this->response($result, $error);
}

/**
* Updates a subscriber in a list
* @param $listId
* @param $email
* @param $name
* @param $customFields
* @param Boolean $resubscribe Re-activate an existing user if they have been deactivated
* @param Boolean $mergeMultiFields Multi-Valued Select Many fields will be merged together instead of overwritten
* @throws Exception
*/
public function update($listId, $email, $name=null, $customFields=array(), $resubscribe=false, $mergeMultiFields=false) {
if (!$listId) {
throw new Exception('List ID is required');
}
if (!$email) {
throw new Exception('Please provide a valid email address');
}

$connection = new \CS_REST_Subscribers($listId, $this->auth());

$subscriber = [
'Resubscribe' => $resubscribe
];

if (!empty($name)) {
$subscriber['Name'] = $name;
}

$subscriber['CustomFields'] = $this->parseCustomFields($customFields);

if ($mergeMultiFields) {
$result = $connection->get($email);

$error = null;
if (!$this->response($result, $error)) {
throw new Exception($error);
}

$existingSubscriber = $result->response;

foreach ($existingSubscriber->CustomFields as $existingField) {
$subscriber['CustomFields'][] = [
'Key' => $existingField->Key,
'Value' => $existingField->Value
];
}
}

$result = $connection->update($email, $subscriber);

$error = null;
if (!$this->response($result, $error)) {
throw new Exception($error);
}
}
}

0 comments on commit c2f2340

Please sign in to comment.