diff --git a/README.md b/README.md index 5df477c..3c018f9 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/consolecommands/OneCampaignMonitorSubscribersCommand.php b/consolecommands/OneCampaignMonitorSubscribersCommand.php new file mode 100644 index 0000000..b5c424e --- /dev/null +++ b/consolecommands/OneCampaignMonitorSubscribersCommand.php @@ -0,0 +1,67 @@ +_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; + } + +} diff --git a/services/OneCampaignMonitor_SubscribersService.php b/services/OneCampaignMonitor_SubscribersService.php index 20e7c53..0478fe6 100644 --- a/services/OneCampaignMonitor_SubscribersService.php +++ b/services/OneCampaignMonitor_SubscribersService.php @@ -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'); @@ -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); + } + } }