From ddfb60d47239f6e845cc5dfd902de53b2f625b2e Mon Sep 17 00:00:00 2001 From: mkornatz Date: Tue, 18 Apr 2017 13:57:10 -0400 Subject: [PATCH 1/8] Adds update method for subscribers --- .../OneCampaignMonitor_SubscribersService.php | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/services/OneCampaignMonitor_SubscribersService.php b/services/OneCampaignMonitor_SubscribersService.php index 20e7c53..63c3019 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,71 @@ public function add($listId, $email, $name=null, $customFields=array(), $resubsc return true; } + /** + * 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); + $existingSubscriber = $result->response; + + // Count the number of occurances of custom field to know + // which is a Multi-Valued Select Many field + $fieldOccurances = []; + foreach ($existingSubscriber->CustomFields as $existingField) { + if (array_key_exists($existingField->Key, $fieldOccurances)) { + $fieldOccurances[$existingField->Key]++; + } else { + $fieldOccurances[$existingField->Key] = 1; + } + } + + // For any Multi-Valued Select Many field, make sure to append the + // existing field values + foreach($fieldOccurances as $key => $value) { + if ($value > 1) { + foreach($existingSubscriber->CustomFields as $existingField) { + if ($existingField->Key == $key) { + $subscriber['CustomFields'][] = $existingField; + } + } + } + } + } else { + $subscriber['CustomFields'] = $parsedCustomFields; + } + + $result = $connection->update($email, $subscriber); + + $error = null; + if (!$this->response($result, $error)) { + throw new Exception($error); + } + } } From 531a39016e4d0cecbe2f95709cfd91feea217aab Mon Sep 17 00:00:00 2001 From: mkornatz Date: Tue, 18 Apr 2017 14:11:46 -0400 Subject: [PATCH 2/8] Adds a simple note on how to update a user in a list. --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index 4f93e23..21ebbd1 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,17 @@ $resubscribe = false; craft()->oneCampaignMonitor_subscribers->add($list_id, $email, $name, $customFields, $resubscribe); ``` +Update a user 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); +``` + ### 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: From 365c12bc58586bc3aa5150afe5182a17772c6668 Mon Sep 17 00:00:00 2001 From: mkornatz Date: Tue, 18 Apr 2017 14:45:16 -0400 Subject: [PATCH 3/8] Throws an exception if subscriber doesn't exist when trying to update --- services/OneCampaignMonitor_SubscribersService.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/services/OneCampaignMonitor_SubscribersService.php b/services/OneCampaignMonitor_SubscribersService.php index 63c3019..7e82216 100644 --- a/services/OneCampaignMonitor_SubscribersService.php +++ b/services/OneCampaignMonitor_SubscribersService.php @@ -71,6 +71,12 @@ public function update($listId, $email, $name=null, $customFields=array(), $resu if ($mergeMultiFields) { $result = $connection->get($email); + + $error = null; + if (!$this->response($result, $error)) { + throw new Exception($error); + } + $existingSubscriber = $result->response; // Count the number of occurances of custom field to know From 28052ee5c094702feb4ea3617a6b73344461d758 Mon Sep 17 00:00:00 2001 From: mkornatz Date: Tue, 18 Apr 2017 15:25:37 -0400 Subject: [PATCH 4/8] Adds check to see if custom fields are already in proper format. --- services/OneCampaignMonitor_BaseService.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/services/OneCampaignMonitor_BaseService.php b/services/OneCampaignMonitor_BaseService.php index 8602c8e..0d614f2 100644 --- a/services/OneCampaignMonitor_BaseService.php +++ b/services/OneCampaignMonitor_BaseService.php @@ -18,7 +18,11 @@ protected function auth() { protected function parseCustomFields($fields) { $data = array(); foreach ($fields as $key => $value) { - $data[] = ['Key' => $key, 'Value' => $value]; + if (is_array($value) && array_key_exists('Key', $value) && array_key_exists('Value', $value) ) { + $data[] = $value; + } else { + $data[] = ['Key' => $key, 'Value' => $value]; + } } return $data; } From 6d8604bfe6cf5866b718540d5e656c51ba2d5d0d Mon Sep 17 00:00:00 2001 From: mkornatz Date: Tue, 18 Apr 2017 15:26:01 -0400 Subject: [PATCH 5/8] Adds console commands to add and update subscribers --- .../OneCampaignMonitorSubscribersCommand.php | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 consolecommands/OneCampaignMonitorSubscribersCommand.php diff --git a/consolecommands/OneCampaignMonitorSubscribersCommand.php b/consolecommands/OneCampaignMonitorSubscribersCommand.php new file mode 100644 index 0000000..760f089 --- /dev/null +++ b/consolecommands/OneCampaignMonitorSubscribersCommand.php @@ -0,0 +1,52 @@ +_decodeCustomFields($customFields); + craft()->oneCampaignMonitor_subscribers->add($listId, $email, $name, $customFields, $resubscribe ? true : false); + } + + /** + * Looks through all orders and adds users to User Groups based on their subscription items + * Usage: + * php ./craft/app/etc/console/yiic onecampaignmonitorsubscribers update \ + * --listId="asdf..." \ + * --email="test@example.com" \ + * --name="Joan Doe" \ + * --resubscribe=0 \ + * --customFields='[{"Key":"Subscriptions","Value":"FN-SUB36-Renewal"}]' \ + * --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); + } + + /** + * 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; + } + +} From fab0050166e198adee83fc50ee065efb69101fc5 Mon Sep 17 00:00:00 2001 From: mkornatz Date: Tue, 18 Apr 2017 15:34:01 -0400 Subject: [PATCH 6/8] Adds a feature to check if a user exists in a list and updates some comments --- .../OneCampaignMonitorSubscribersCommand.php | 23 +++++++++++++++---- .../OneCampaignMonitor_SubscribersService.php | 23 +++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/consolecommands/OneCampaignMonitorSubscribersCommand.php b/consolecommands/OneCampaignMonitorSubscribersCommand.php index 760f089..b5c424e 100644 --- a/consolecommands/OneCampaignMonitorSubscribersCommand.php +++ b/consolecommands/OneCampaignMonitorSubscribersCommand.php @@ -4,14 +4,14 @@ class OneCampaignMonitorSubscribersCommand extends BaseCommand { /** - * Adds a user to a list + * 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":"Subscriptions","Value":"FN-SUB36-Renewal"}]' \ + * --customFields='[{"Key":"City","Value":"Chicago"}]' \ */ public function actionAdd($listId, $email, $name=null, $customFields='[]', $resubscribe=true) { $customFields = $this->_decodeCustomFields($customFields); @@ -19,14 +19,14 @@ public function actionAdd($listId, $email, $name=null, $customFields='[]', $resu } /** - * Looks through all orders and adds users to User Groups based on their subscription items + * 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":"Subscriptions","Value":"FN-SUB36-Renewal"}]' \ + * --customFields='[{"Key":"City","Value":"New York"}]' \ * --merge=1 */ public function actionUpdate($listId, $email, $name=null, $customFields='[]', $resubscribe=false, $merge=true) { @@ -34,6 +34,21 @@ public function actionUpdate($listId, $email, $name=null, $customFields='[]', $r 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 diff --git a/services/OneCampaignMonitor_SubscribersService.php b/services/OneCampaignMonitor_SubscribersService.php index 7e82216..cdd71c8 100644 --- a/services/OneCampaignMonitor_SubscribersService.php +++ b/services/OneCampaignMonitor_SubscribersService.php @@ -39,6 +39,29 @@ 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 From 282f897a0201d440babe47a99d437bf80bbe2a0c Mon Sep 17 00:00:00 2001 From: mkornatz Date: Tue, 18 Apr 2017 15:44:18 -0400 Subject: [PATCH 7/8] Cuts out complex buggy logic for a simpler approach --- .../OneCampaignMonitor_SubscribersService.php | 28 ++++--------------- 1 file changed, 5 insertions(+), 23 deletions(-) diff --git a/services/OneCampaignMonitor_SubscribersService.php b/services/OneCampaignMonitor_SubscribersService.php index cdd71c8..0478fe6 100644 --- a/services/OneCampaignMonitor_SubscribersService.php +++ b/services/OneCampaignMonitor_SubscribersService.php @@ -102,32 +102,14 @@ public function update($listId, $email, $name=null, $customFields=array(), $resu $existingSubscriber = $result->response; - // Count the number of occurances of custom field to know - // which is a Multi-Valued Select Many field - $fieldOccurances = []; foreach ($existingSubscriber->CustomFields as $existingField) { - if (array_key_exists($existingField->Key, $fieldOccurances)) { - $fieldOccurances[$existingField->Key]++; - } else { - $fieldOccurances[$existingField->Key] = 1; - } + $subscriber['CustomFields'][] = [ + 'Key' => $existingField->Key, + 'Value' => $existingField->Value + ]; } - - // For any Multi-Valued Select Many field, make sure to append the - // existing field values - foreach($fieldOccurances as $key => $value) { - if ($value > 1) { - foreach($existingSubscriber->CustomFields as $existingField) { - if ($existingField->Key == $key) { - $subscriber['CustomFields'][] = $existingField; - } - } - } - } - } else { - $subscriber['CustomFields'] = $parsedCustomFields; } - + $result = $connection->update($email, $subscriber); $error = null; From 7355d8aaa42509e8abd473c1bdcf850e5acca3fb Mon Sep 17 00:00:00 2001 From: mkornatz Date: Tue, 18 Apr 2017 15:59:18 -0400 Subject: [PATCH 8/8] Adds an example to check if a user exists. --- README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 21ebbd1..b78186e 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,7 @@ $resubscribe = false; craft()->oneCampaignMonitor_subscribers->add($list_id, $email, $name, $customFields, $resubscribe); ``` -Update a user in a list: +Update a subscriber in a list: ``` $list_id = '123'; @@ -135,6 +135,16 @@ $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: