diff --git a/.gitignore b/.gitignore index 5970802..0ebeed0 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ composer.phar composer.lock .DS_Store Thumbs.db -sftp-config.json \ No newline at end of file +sftp-config.json +.idea \ No newline at end of file diff --git a/src/Benhawker/Pipedrive/Library/Activities.php b/src/Benhawker/Pipedrive/Library/Activities.php index cf93df3..3f067ca 100644 --- a/src/Benhawker/Pipedrive/Library/Activities.php +++ b/src/Benhawker/Pipedrive/Library/Activities.php @@ -32,8 +32,9 @@ public function __construct(\Benhawker\Pipedrive\Pipedrive $master) /** * Adds a activity * - * @param array $data activity detials - * @return array returns detials of the activity + * @param array $data activity details + * @return array returns details of the activity + * @throws PipedriveMissingFieldError */ public function add(array $data) { @@ -45,4 +46,54 @@ public function add(array $data) return $this->curl->post('activities', $data); } + + /** + * @param $id + * @return array returns deleted id + */ + public function delete($id){ + return $this->curl->delete('activities/'.$id); + } + + /** + * Update activity + * @param $id + * @param $data + * @return array + */ + public function update($id, $data) + { + return $this->curl->put('activities/' . $id, $data); + } + + /** + * Return activity + * + * @param int $id pipedrive activity id + * @return array returns details of activity + */ + public function getById($id) + { + return $this->curl->get('activities/' . $id); + } + + /** + * @param array $data + * @return array returns all activities by data fields + */ + public function getByUser(array $data) + { + return $this->curl->get('activities', $data); + } + + /** + * List activities by condition + * + * @param array $data + * @return array return all activities by data fields + */ + public function getList(array $data) + { + return $this->curl->get('activities/list', $data); + } } diff --git a/src/Benhawker/Pipedrive/Library/Curl.php b/src/Benhawker/Pipedrive/Library/Curl.php index 8ed21fc..1c0716c 100644 --- a/src/Benhawker/Pipedrive/Library/Curl.php +++ b/src/Benhawker/Pipedrive/Library/Curl.php @@ -44,6 +44,7 @@ public function __construct($url, $apiKey) $this->setOpt(CURLOPT_USERAGENT, self::USER_AGENT) ->setOpt(CURLOPT_HEADER, false) ->setOpt(CURLOPT_RETURNTRANSFER, true) + ->setOpt(CURLOPT_TIMEOUT_MS, 15000) ->setOpt(CURLOPT_HTTPHEADER, array("Accept: application/json")); } @@ -122,6 +123,26 @@ public function delete($method) ->exec(); } + + /** + * Bulk delete request + * + * @param $method + * @param array $data + * @return array + * @throws PipedriveApiError + * @throws PipedriveHttpError + */ + public function bulkDelete($method, array $data) + { + + return $this->createEndPoint($method) + ->setOpt(CURLOPT_CUSTOMREQUEST, 'DELETE') + ->setOpt(CURLOPT_POSTFIELDS, http_build_query($data)) + ->exec(); + + } + /** * Execute current cURL session * @@ -232,7 +253,7 @@ protected function postfields($data) } /** - * Build multidimenianl query + * Build multidimensional query * from: https://github.com/php-curl-class/php-curl-class * * @param array $data post data diff --git a/src/Benhawker/Pipedrive/Library/DealFields.php b/src/Benhawker/Pipedrive/Library/DealFields.php index 3c8f50e..33adcab 100644 --- a/src/Benhawker/Pipedrive/Library/DealFields.php +++ b/src/Benhawker/Pipedrive/Library/DealFields.php @@ -57,8 +57,9 @@ public function getById($id) /** * Adds a dealField * - * @param array $data deal field detials + * @param array $data deal field details * @return array returns details of the deal field + * @throws PipedriveMissingFieldError */ public function add(array $data) { diff --git a/src/Benhawker/Pipedrive/Library/Deals.php b/src/Benhawker/Pipedrive/Library/Deals.php index 533c2d8..ff5ab95 100644 --- a/src/Benhawker/Pipedrive/Library/Deals.php +++ b/src/Benhawker/Pipedrive/Library/Deals.php @@ -1,5 +1,6 @@ $name); - if($personId) { + if ($personId) { $params['person_id'] = $personId; } - if($orgId) { + if ($orgId) { $params['org_id'] = $orgId; } return $this->curl->get('deals/find', $params); @@ -65,6 +66,7 @@ public function getByName($name, $personId=null, $orgId=null) * * @param array $data (id, start, limit) * @return array products + * @throws PipedriveMissingFieldError */ public function products(array $data) { @@ -73,14 +75,15 @@ public function products(array $data) throw new PipedriveMissingFieldError('You must include the "id" of the deal when getting products'); } - return $this->curl->get('deals/' . $data['id'] . '/products'); + return $this->curl->get('deals/' . $data['id'] . '/products', $data); } /** * Adds a deal * - * @param array $data deal detials - * @return array returns detials of the deal + * @param array $data deal details + * @return array returns details of the deal + * @throws PipedriveMissingFieldError */ public function add(array $data) { @@ -91,20 +94,20 @@ public function add(array $data) return $this->curl->post('deals', $data); } - + /** * Adds a product to a deal * - * @param int $dealId deal id - * @param array $data deal and product detials - * @return array returns detials of the deal-product + * @param int $dealId deal id + * @param array $data deal and product details + * @return array returns details of the deal-product * @throws PipedriveMissingFieldError */ public function addProduct($dealId, array $data) { //if there is no product_id set throw error as it is a required field if (!isset($data['product_id'])) { - throw new PipedriveMissingFieldError('You must include a "pdoruct_id" field when adding a product to a deal'); + throw new PipedriveMissingFieldError('You must include a "product_id" field when adding a product to a deal'); } //if there is no item_price set throw error as it is a required field if (!isset($data['item_price'])) { @@ -118,12 +121,49 @@ public function addProduct($dealId, array $data) return $this->curl->post('deals/' . $dealId . '/products', $data); } + + /** + * Update deal attached product. + * @param $dealId + * @param array $data + * @return array + * @throws PipedriveMissingFieldError + */ + public function updateProduct($dealId, array $data) + { + if (!isset($data['deal_product_id'])) { + throw new PipedriveMissingFieldError('You must include "deal_product_id" field when updating product.'); + } + + if (!isset($data['item_price'])) { + throw new PipedriveMissingFieldError('You must include "item_price" field when updating product.'); + } + + if (!isset($data['quantity'])) { + throw new PipedriveMissingFieldError('You must include "quantity" field when updating product'); + } + + return $this->curl->put('deals/' . $dealId . '/products/' . $data['deal_product_id'], $data); + } + + /** + * Delete attached product from deal. + * + * @param $dealId + * @param $productAttachmentId + * @return array + */ + public function deleteProduct($dealId, $productAttachmentId) + { + return $this->curl->delete('deals/' . $dealId . '/products/' . $productAttachmentId); + } + /** * Updates a deal * - * @param int $dealId pipedrives deal Id - * @param array $data new detials of deal - * @return array returns detials of a deal + * @param int $dealId pipedrives deal Id + * @param array $data new details of deal + * @return array returns details of a deal */ public function update($dealId, array $data = array()) { @@ -133,13 +173,133 @@ public function update($dealId, array $data = array()) /** * Moves deal to a new stage * - * @param int $dealId deal id - * @param int $stageId stage id - * @return array returns detials of the deal + * @param int $dealId deal id + * @param int $stageId stage id + * @return array returns details of the deal */ public function moveStage($dealId, $stageId) { return $this->curl->put('deals/' . $dealId, array('stage_id' => $stageId)); } + /** + * Delete single deal by deal id + * + * @param $dealId + * @return array + */ + + public function delete($dealId) + { + return $this->curl->delete('deals/' . $dealId); + } + + + /** + * Bulk delete deals + * + * @param $ids string Comma separated ids + * @return mixed + */ + public function bulkDelete($ids) + { + return $this->curl->bulkDelete('deals', array('ids' => $ids)); + } + + /** + * Get deals timeline + * + */ + + public function getDealsTimeline($params) + { + if (!isset($params['start_date'])) { + throw new PipedriveMissingFieldError('You must include "start_date" when getting deals timeline'); + } + if (!isset($params['interval']) || !in_array($params['interval'], ['day', 'week', 'month', 'quarter'])) { + throw new PipedriveMissingFieldError('You must include "interval" when getting deals timeline'); + } + if (!isset($params['amount'])) { + throw new PipedriveMissingFieldError('You must include "amount" when getting deals timeline'); + } + if (!isset($params['field_key'])) { + throw new PipedriveMissingFieldError('You must include "field_key" when getting deals timeline'); + } + + return $this->curl->get('deals/timeline', $params); + } + + /** + * Get all deals by filter id + * @param $params + * @return array + */ + public function getAllDeals($params) + { + return $this->curl->get('deals', $params); + } + + /** + * List all activities associated to deal. + * @param $dealId + * @param array $params + * @return array + */ + public function listActivities($dealId, $params = []) + { + return $this->curl->get('deals/' . $dealId . '/activities', $params); + } + + /** + * Add follower to a deal. + * @param $dealId + * @param $userId + * @return array + */ + public function addFollower($dealId, $userId) + { + return $this->curl->post('deals/' . $dealId . '/followers', [ + 'id' => $dealId, + 'user_id' => $userId + ]); + } + + /** + * Delete follower from a deal. + * @param $dealId + * @param $userId + * @return array + */ + public function deleteFollower($dealId, $userId) + { + return $this->curl->delete('deals/' . $dealId . '/followers/' . $userId); + } + + /** + * @param $dealId + * @return array + */ + public function listFollowers($dealId) + { + return $this->curl->get('deals/' . $dealId . '/followers'); + } + + /** + * @param $dealId + * @param array $params + * @return array + */ + public function flow($dealId, $params = []) + { + return $this->curl->get('deals/' . $dealId . '/flow', $params); + } + + /** + * @param array $params + * @return array + */ + public function summary($params = []) + { + return $this->curl->get('deals/summary', $params); + } } diff --git a/src/Benhawker/Pipedrive/Library/Filters.php b/src/Benhawker/Pipedrive/Library/Filters.php new file mode 100644 index 0000000..1d922cd --- /dev/null +++ b/src/Benhawker/Pipedrive/Library/Filters.php @@ -0,0 +1,108 @@ +curl = $master->curl(); + } + + /** + * Returns a filter + * + * @param int $id filter id + * @return array returns details of filter + */ + public function getById($id) + { + return $this->curl->get('filters/' . $id); + } + + /** + * Add new filter + * + * @param array $data filter details + * @return array returns details of filter + * @throws PipedriveMissingFieldError + */ + public function add(array $data) + { + //if there is no title set throw error as it is a required field + if (!isset($data['name']) || !isset($data['conditions'])) { + throw new PipedriveMissingFieldError('Missing required field name/conditions'); + } + + return $this->curl->post('filters', $data); + } + + /** + * Updates a deal + * + * @param $filterId + * @param array $data new details of filter + * @return array returns details of filter + * @internal param int $filterId + */ + public function update($filterId, array $data = array()) + { + return $this->curl->put('filters/' . $filterId, $data); + } + + + /** + * Delete single filter by filter id + * + * @param $filterId + * @return array + */ + + public function delete($filterId) + { + return $this->curl->delete('filters/' . $filterId); + } + + + /** + * Bulk delete filters + * + * @param string $ids comma separated ids + * @return mixed + */ + public function bulkDelete($ids) + { + return $this->curl->bulkDelete('filters', array('ids' => $ids)); + } + + /** + * Get all filters by filter type + * @param $type + * @return array + * @throws PipedriveMissingFieldError + * @internal param $params + */ + public function getAll($type) + { + $supportedTypes = ['deals', 'org', 'people', 'products', 'activity']; + if (!in_array($type, $supportedTypes)) { + throw new PipedriveMissingFieldError("Unrecognized filter type. Supported types are:" . implode(', ', $supportedTypes)); + } + return $this->curl->get('filters', ['type' => $type]); + } + +} diff --git a/src/Benhawker/Pipedrive/Library/Notes.php b/src/Benhawker/Pipedrive/Library/Notes.php index 4034a99..4716766 100644 --- a/src/Benhawker/Pipedrive/Library/Notes.php +++ b/src/Benhawker/Pipedrive/Library/Notes.php @@ -31,8 +31,9 @@ public function __construct(\Benhawker\Pipedrive\Pipedrive $master) /** * Adds a note * - * @param array $data note detials - * @return array returns detials of the note + * @param array $data note details + * @return array returns details of the note + * @throws PipedriveMissingFieldError */ public function add(array $data) { @@ -48,4 +49,26 @@ public function add(array $data) return $this->curl->post('notes', $data); } + + /** + * Get All Notes By Params + * + * @param array $params + * @return array + */ + public function getAll(array $params) + { + return $this->curl->get('notes/', $params); + } + + /** + * Get single note by ID + * + * @param $noteId + * @return array + */ + public function getOne($noteId) + { + return $this->curl->get('notes/' . $noteId); + } } diff --git a/src/Benhawker/Pipedrive/Library/Organizations.php b/src/Benhawker/Pipedrive/Library/Organizations.php index ac21d59..318dfdc 100644 --- a/src/Benhawker/Pipedrive/Library/Organizations.php +++ b/src/Benhawker/Pipedrive/Library/Organizations.php @@ -62,12 +62,13 @@ public function getAll(array $data = array()) { return $this->curl->get('organizations/', $data); } - + /** * Lists deals associated with a organization. * * @param array $data (id, start, limit) * @return array deals + * @throws PipedriveMissingFieldError */ public function deals(array $data) { @@ -75,15 +76,15 @@ public function deals(array $data) if (!isset($data['id'])) { throw new PipedriveMissingFieldError('You must include the "id" of the organization when getting deals'); } - return $this->curl->get('organizations/' . $data['id'] . '/deals'); + return $this->curl->get('organizations/' . $data['id'] . '/deals', $data); } /** * Updates an organization * * @param int $organizationId pipedrives organization Id - * @param array $data new detials of organization - * @return array returns detials of a organization + * @param array $data new details of organization + * @return array returns details of a organization */ public function update($organizationId, array $data = array()) { @@ -93,8 +94,9 @@ public function update($organizationId, array $data = array()) /** * Adds a organization * - * @param array $data organizations detials - * @return array returns detials of a organization + * @param array $data organizations details + * @return array returns details of a organization + * @throws PipedriveMissingFieldError */ public function add(array $data) { @@ -110,10 +112,88 @@ public function add(array $data) * Deletes an organization * * @param int $organizationId pipedrives organization Id - * @return array returns detials of a organization + * @return array returns details of a organization */ public function delete($organizationId) { return $this->curl->delete('organizations/' . $organizationId); } + + /** + * Bulk delete organizations + * + * @param $ids + * @return array + */ + public function bulkDelete($ids) + { + return $this->curl->bulkDelete('organizations', array('ids' => $ids)); + } + + + /** + * Get persons of organization + * + * @param $organizationId + * @return array + */ + + public function listPersons($organizationId) + { + return $this->curl->get('organizations/'.$organizationId.'/persons'); + } + + /** + * @param $organizationId + * @return array + */ + public function listFollowers($organizationId) + { + return $this->curl->get('organizations/' . $organizationId . '/followers'); + } + + /** + * Add follower to organization. + * @param $organizationId + * @param $userId + * @return array + */ + public function addFollower($organizationId, $userId) + { + return $this->curl->post('organizations/' . $organizationId . '/followers',[ + 'id' => $organizationId, + 'user_id' => $userId + ]); + } + + /** + * Delete organization follower. + * @param $organizationId + * @param $userId + * @return array + */ + public function deleteFollower($organizationId, $userId) + { + return $this->curl->delete('organizations/' . $organizationId . '/followers/' . $userId); + } + + /** + * @param array $params + * @return array + */ + public function summary($params = []) + { + return $this->curl->get('organizations/summary', $params); + } + + /** + * List activities associated with organization + * @param $organizationId + * @param array $data + * @return array + */ + public function listActivities($organizationId, array $data = []) + { + return $this->curl->get('organizations/' . $organizationId . '/activities', $data); + } } diff --git a/src/Benhawker/Pipedrive/Library/Persons.php b/src/Benhawker/Pipedrive/Library/Persons.php index e87f39b..4e355f1 100644 --- a/src/Benhawker/Pipedrive/Library/Persons.php +++ b/src/Benhawker/Pipedrive/Library/Persons.php @@ -30,8 +30,8 @@ public function __construct(\Benhawker\Pipedrive\Pipedrive $master) /** * Returns a person * - * @param int $id pipedrive persons id - * @return array returns detials of a person + * @param int $id pipedrive persons id + * @return array returns details of a person */ public function getById($id) { @@ -42,18 +42,31 @@ public function getById($id) * Returns a person / people * * @param string $name pipedrive persons name - * @return array returns detials of a person + * @return array returns details of a person */ public function getByName($name) { return $this->curl->get('persons/find', array('term' => $name)); } + /** + * + * Returns a person / people + * + * + * + * @param string $email pipedrive persons email + * + * @return array returns details of a person + * + */ + public function getByEmail($email) + { + return $this->curl->get('persons/find', array('term' => $email, 'search_by_email' => 1)); + } + + /** * Lists deals associated with a person. * * @param array $data (id, start, limit) * @return array deals + * @throws PipedriveMissingFieldError */ public function deals(array $data) { @@ -70,6 +83,7 @@ public function deals(array $data) * * @param array $data (id, start, limit) * @return array products + * @throws PipedriveMissingFieldError */ public function products(array $data) { @@ -84,9 +98,9 @@ public function products(array $data) /** * Updates a person * - * @param int $personId pipedrives person Id - * @param array $data new detials of person - * @return array returns detials of a person + * @param int $personId pipedrives person Id + * @param array $data new details of person + * @return array returns details of a person */ public function update($personId, array $data = array()) { @@ -97,7 +111,8 @@ public function update($personId, array $data = array()) * Adds a person * * @param array $data persons detials - * @return array returns detials of a person + * @return array returns details of a person + * @throws PipedriveMissingFieldError */ public function add(array $data) { @@ -109,14 +124,75 @@ public function add(array $data) return $this->curl->post('persons', $data); } + /** + * Get all persons by filter id + * @param $params + * @return array + */ + public function getAll($params) + { + return $this->curl->get('persons', $params); + } + /** * Deletes a person * - * @param int $personId pipedrives person Id - * @return array returns detials of a person + * @param int $personId pipedrives person Id + * @return array returns details of a person */ public function delete($personId) { return $this->curl->delete('persons/' . $personId); } + + public function listFollowers($personId) + { + return $this->curl->get('persons/' . $personId . '/followers'); + } + + /** + * @param $personId + * @param $followerId + * @return array + */ + public function addFollower($personId, $followerId) + { + return $this->curl->post('persons/' . $personId . '/followers', [ + 'id' => $personId, + 'user_id' => $followerId + ]); + } + + /** + * @param $personId + * @param $followerId + * @return array + */ + public function deleteFollower($personId, $followerId) + { + return $this->curl->delete('persons/' . $personId . '/followers/' . $followerId); + } + + /** + * @param array $params + * @return array + */ + public function summary($params = []) + { + return $this->curl->get('persons/summary', $params); + } + + /** + * @param array $params + * @return array + * @throws PipedriveMissingFieldError + */ + public function listActivities(array $params) + { + //if there is no id set throw error as it is a required field + if (!isset($data['id'])) { + throw new PipedriveMissingFieldError('You must include the "id" of the person when getting activities'); + } + return $this->curl->get('persons/' . $params['id'] . '/activities', $params); + } } diff --git a/src/Benhawker/Pipedrive/Library/Products.php b/src/Benhawker/Pipedrive/Library/Products.php index 8afca4b..f0a2660 100644 --- a/src/Benhawker/Pipedrive/Library/Products.php +++ b/src/Benhawker/Pipedrive/Library/Products.php @@ -1,6 +1,5 @@ curl = $master->curl(); } + /** + * @see https://developers.pipedrive.com/docs/api/v1/#!/Products/get_products + * @param $data + * @return array + */ + public function getAll($data) + { + return $this->curl->get('products', $data); + } + /** * Returns a product / products * @@ -42,4 +51,14 @@ public function getByName($name) return $this->curl->get('products/find', $params); } + /** + * @see https://developers.pipedrive.com/docs/api/v1/#!/Products/get_products_id + * @param $productId + * @return array + */ + public function getOne($productId) + { + return $this->curl->get('products/' . $productId); + } + } diff --git a/src/Benhawker/Pipedrive/Library/SearchResults.php b/src/Benhawker/Pipedrive/Library/SearchResults.php new file mode 100644 index 0000000..72d0e8c --- /dev/null +++ b/src/Benhawker/Pipedrive/Library/SearchResults.php @@ -0,0 +1,76 @@ +curl = $master->curl(); + } + + /** + * Performs a search across the account and returns SearchResults. + * @param array $data + * @return array + * @throws PipedriveApiError + * @throws PipedriveMissingFieldError + */ + public function search(array $data) + { + $this->validateSearchTerm($data, 2); + return $this->curl->get('searchResults', $data); + } + + /** + * Performs a search from a specific field's values. Results can be either the + * distinct values of the field (useful for searching autocomplete field values), + * or actual items IDs (deals, persons, organizations or products). Works only with the + * following field types: varchar, varchar_auto, double, address, text, phone, date. + * @param array $data + * @return array + * @throws PipedriveApiError + * @throws PipedriveMissingFieldError + */ + public function searchField(array $data) + { + $this->validateSearchTerm($data, 3); + return $this->curl->get('searchResults/field', $data); + } + + /** + * Validate that search query has term with min length + * @param array $data + * @param int $minLength + * @throws PipedriveApiError + * @throws PipedriveMissingFieldError + */ + private function validateSearchTerm(array $data, $minLength = 2) + { + if(!isset($data['term'])){ + throw new PipedriveMissingFieldError('You must include "term" in search method.'); + } else { + if(strlen($data['term']) < $minLength){ + throw new PipedriveApiError('Search term must be at least '. $minLength .' characters long'); + } + } + } +} \ No newline at end of file diff --git a/src/Benhawker/Pipedrive/Library/Stages.php b/src/Benhawker/Pipedrive/Library/Stages.php new file mode 100644 index 0000000..708083b --- /dev/null +++ b/src/Benhawker/Pipedrive/Library/Stages.php @@ -0,0 +1,63 @@ +curl = $master->curl(); + } + + /** + * If pipeline_id is ommited, + * all stages from all pipelines will be returned. + * + * @param null $pipeline_id + * @return array + */ + public function getAll($pipeline_id = null) + { + return $this->curl->get('stages', ['pipeline_id' => null]); + } + + /** + * Returns a stage + * + * @param int $id pipedrive stage id + * @return array returns details of a stage + */ + public function getById($id) + { + return $this->curl->get('stages/' . $id); + } + + /** + * Get all deals in stage + * + * @param $stage_id + * @param array $params + * @return array + */ + public function getDealsInStage($stage_id, $params = []) + { + return $this->curl->get('stages/' . $stage_id . '/deals', $params); + } + +} \ No newline at end of file diff --git a/src/Benhawker/Pipedrive/Pipedrive.php b/src/Benhawker/Pipedrive/Pipedrive.php index 1fe7b18..da11a3a 100644 --- a/src/Benhawker/Pipedrive/Pipedrive.php +++ b/src/Benhawker/Pipedrive/Pipedrive.php @@ -1,6 +1,10 @@ dealFields = new Library\DealFields($this); $this->organizations = new Library\Organizations($this); $this->products = new Library\Products($this); + $this->searchResults = new Library\SearchResults($this); + $this->filters = new Library\Filters($this); + $this->stages = new Stages($this); } /** @@ -155,6 +179,15 @@ public function deals() return $this->deals; } + /** + * Returns the Pipedrive Filters Object + * @return Library\Filters + */ + public function filters() + { + return $this->filters; + } + /** * Returns the Pipedrive Activities Object * @@ -188,7 +221,7 @@ public function dealFields() /** * Returns the Pipedrive Organizations Object * - * @return Organizations Object + * @return Organizations */ public function organizations() { @@ -198,10 +231,29 @@ public function organizations() /** * Returns the Pipedrive Products Object * - * @return Products Object + * @return Products */ public function products() { return $this->products; } + + /** + * Returns the Pipedrive SearchResults Object + * @return SearchResults + */ + public function searchResults() + { + return $this->searchResults; + } + + /** + * Returns the Pipedrive Stages Object + * @return Stages + */ + public function stages() + { + return $this->stages; + } + }