diff --git a/composer.json b/composer.json index cb129f4..e177f18 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "mediatoolkit/activecampaign-v3-php", "description": "PHP Wrapper for ActiveCampaign", "require": { - "guzzlehttp/guzzle": "~6.3" + "guzzlehttp/guzzle": "^6.3" }, "require-dev": { "phpunit/phpunit": "^7" diff --git a/src/contacts/Contacts.php b/src/Contacts/Contacts.php similarity index 87% rename from src/contacts/Contacts.php rename to src/Contacts/Contacts.php index 169ffb8..bf730ac 100644 --- a/src/contacts/Contacts.php +++ b/src/Contacts/Contacts.php @@ -323,4 +323,54 @@ public function removeAutomation(int $contactAutomationId) return 200 === $req->getStatusCode(); } + /** + * @param string $email + * @return string + */ + public function findEmail(string $email) + { + $req = $this->client + ->getClient() + ->get('/api/3/contacts/?search=' . $email); + + return $req->getBody()->getContents(); + } + + /** + * @param string $email + * @param array $contactNew + * @return int + */ + public function findOrCreate(string $email, array $contactNew) + { + $contact = $this->findEmail($email); + $contact = json_decode($contact); + if ( count($contact->contacts) > 0 ) { + $contactId = $contact->contacts[0]->id; + if ($contactId !== 0) { + return $contactId; + } + } + + $contact = $this->create($contactNew); + $contact = json_decode($contact); + $contactId = $contact->contact->id; + return $contactId; + } + + + + /** + * @param int $id + * @return string + */ + public function listTags(int $id) + { + $req = $this->client + ->getClient() + ->get('/api/3/contacts/' . $id . '/contactTags' ); + + return $req->getBody()->getContents(); + } + } diff --git a/src/deals/Deals.php b/src/Deals/Deals.php similarity index 100% rename from src/deals/Deals.php rename to src/Deals/Deals.php diff --git a/src/lists/Lists.php b/src/Lists/Lists.php similarity index 100% rename from src/lists/Lists.php rename to src/Lists/Lists.php diff --git a/src/organizations/Organizations.php b/src/Organizations/Organizations.php similarity index 100% rename from src/organizations/Organizations.php rename to src/Organizations/Organizations.php diff --git a/src/Tags/Tags.php b/src/Tags/Tags.php new file mode 100644 index 0000000..dfb4e10 --- /dev/null +++ b/src/Tags/Tags.php @@ -0,0 +1,89 @@ +client + ->getClient() + ->post('/api/3/tags', [ + 'json' => [ + 'tag' => [ + 'tag' => $name, + 'description' => $description, + 'tagType' => 'contact', + ] + ] + ]); + + return $req->getBody()->getContents(); + } + + /** + * @param string $name + * @param string $description + * @return int + */ + public function findOrCreate(string $name, string $description = null) : int + { + $tagId = $this->find($name); + if ( $tagId !== 0 ) { + return $tagId; + } + $createdTag = $this->create($name, $description); + $createdTag = json_decode($createdTag); + return $createdTag->tag->id; + } + + /** + * @param string $name + * @return int + */ + public function find(string $name) : int + { + $allTags = $this->listAll(); + $allTags = json_decode($allTags); + $allTags = $allTags->tags; + + foreach($allTags as $tag) { + if (0 === strcasecmp($tag->tag, $name)) { + return $tag->id; + } + } + return 0; + } + + /** + * List all tags + * @see https://developers.activecampaign.com/reference#retrieve-all-tags + * @param array $query_params + * @return string + */ + public function listAll(array $query_params = []) + { + $query_params = array_merge($query_params, ['limit' => '20000']); + $req = $this->client + ->getClient() + ->get('/api/3/tags', [ + 'query' => $query_params + ]); + + return $req->getBody()->getContents(); + } + +} diff --git a/src/tracking/EventTracking.php b/src/Tracking/EventTracking.php similarity index 100% rename from src/tracking/EventTracking.php rename to src/Tracking/EventTracking.php diff --git a/src/tracking/SiteTracking.php b/src/Tracking/SiteTracking.php similarity index 100% rename from src/tracking/SiteTracking.php rename to src/Tracking/SiteTracking.php diff --git a/src/tags/Tags.php b/src/tags/Tags.php deleted file mode 100644 index f8c2312..0000000 --- a/src/tags/Tags.php +++ /dev/null @@ -1,32 +0,0 @@ -client - ->getClient() - ->get('/api/3/tags', [ - 'query' => $query_params - ]); - - return $req->getBody()->getContents(); - } - -} \ No newline at end of file