From e91e754991dfb532bf03bbad084afc16c5d52084 Mon Sep 17 00:00:00 2001 From: "DESKTOP-9DOKON5\\Adam" Date: Wed, 9 May 2018 10:07:47 +0100 Subject: [PATCH] Added delete operation and added connection override --- README.md | 26 ++++++++++++++++++++++++- src/Vtiger.php | 53 ++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 70 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index a8ec7c3..71dbfa1 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ See [Third Party App Integration (REST APIs)](http://community.vtiger.com/help/v 1. In order to install the Vtiger package in your Laravel project, just run the composer require command from your terminal: ``` - composer require "clystnet/vtiger ~1.1" + composer require "clystnet/vtiger ~1.2" ``` > *If you are using Laravel 5.5 you don’t need to do steps 2 and 3.* @@ -47,6 +47,12 @@ See [Third Party App Integration (REST APIs)](http://community.vtiger.com/help/v > Because I've experienced problems getting the sessionid from the CRM when multiple users are accessing the CRM at the same time, the solution was to store the sessionid into a file within Laravel application. > Instead of getting the token from the database for each request using the webservice API, a check is made against the expiry time in the file. If the expiry time has expired, a token is requested from the CRM and file is updated with the new token and updated expiry time. +### Overriding default configuration +If you need to use a different connection to the default configuration, you can add the `connection()` method and pass parameters like so and chain the following operation +``` +Vtiger::connection($url, $username, $accesskey)->query($query); +``` + ### Usage In your controller include the Vtiger package @@ -113,6 +119,24 @@ foreach($obj->result as $result) { } ``` +#### Delete + +To delete an record, you need the id of the record you want to delete (i.e. '4x12'). +``` +$id = '4x12'; + +$obj = Vtiger::delete($id); +``` + +#### Describe + +To describe an element, you need the module name you want to describe (i.e. 'Contacts'). +``` +$element = 'Contacts'; + +$obj = Vtiger::describe($element); +``` + ## Contributing Please report any issue you find in the issues page. Pull requests are more than welcome. diff --git a/src/Vtiger.php b/src/Vtiger.php index 4905994..1e2e8c1 100644 --- a/src/Vtiger.php +++ b/src/Vtiger.php @@ -26,7 +26,17 @@ public function __construct() { $this->retry = true; $this->maxretry = 10; - } + } + + // user can pass other connection parameters to override the contructor + public function connection($url, $username, $accesskey) + { + $this->url = $url; + $this->username = $username; + $this->accesskey = $accesskey; + + return $this; + } protected function sessionid() { @@ -125,7 +135,7 @@ protected function gettoken() ] ]); - // decode the response + // decode the response $challenge = json_decode($response->getBody()); // If challenge failed @@ -146,7 +156,7 @@ protected function gettoken() protected function close($sessionid) { - // send a request using a database query to get back any user with the email from the form POST request + // send a request to close current connection $response = $this->client->request('GET', $this->url, [ 'query' => [ 'operation' => 'logout', @@ -169,7 +179,7 @@ public function query($query) } for($i = 0; (!isset($data->success) && $i < 10); $i++) { - // send a request using a database query to get back any user with the email from the form POST request + // send a request using a database query to get back any matching records $response = $this->client->request('GET', $this->url, [ 'query' => [ 'operation' => 'query', @@ -196,7 +206,7 @@ public function retrieve($id) } for($i = 0; (!isset($data->success) && $i < 10); $i++) { - // send a request using a database query to get back any user with the email from the form POST request + // send a request to retrieve a record $response = $this->client->request('GET', $this->url, [ 'query' => [ 'operation' => 'retrieve', @@ -223,7 +233,7 @@ public function create($elem, $data) } for($i = 0; (!isset($data->success) && $i < 10); $i++) { - // send a request using a database query to get back any user with the email from the form POST request + // send a request to create a record $response = $this->client->request('POST', $this->url, [ 'form_params' => [ 'operation' => 'create', @@ -251,7 +261,7 @@ public function update($object) } for($i = 0; (!isset($data->success) && $i < 10); $i++) { - // send a request using a database query to get back any user with the email from the form POST request + // send a request to update a record $response = $this->client->request('POST', $this->url, [ 'form_params' => [ 'operation' => 'update', @@ -268,6 +278,33 @@ public function update($object) return (isset($data->success)) ? $data : false; } + + public function delete($id) + { + $sessionid = self::sessionid(); + + if (isset($sessionid->success)) { + return $sessionid->message; + } + + for ($i = 0; (!isset($data->success) && $i < 10); $i++) { + // send a request to delete a record + $response = $this->client->request('GET', $this->url, [ + 'query' => [ + 'operation' => 'delete', + 'sessionName' => $sessionid, + 'id' => $id + ] + ]); + + // decode the response + $data = json_decode($response->getBody()->getContents()); + } + + self::close($sessionid); + + return (isset($data->success)) ? $data : false; + } public function describe($elementType) { @@ -278,7 +315,7 @@ public function describe($elementType) } for ($i = 0; (!isset($data->success) && $i < 10); $i++) { - // send a request to describe a module (which returns a list of available fields) for a Vtiger module + // send a request to describe a module (which returns a list of available fields) for a Vtiger module $response = $this->client->request('GET', $this->url, [ 'query' => [ 'operation' => 'describe',