Skip to content
This repository has been archived by the owner on Sep 28, 2020. It is now read-only.

Commit

Permalink
Added delete operation and added connection override
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-godfrey committed May 9, 2018
1 parent 272fe17 commit e91e754
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 9 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
53 changes: 45 additions & 8 deletions src/Vtiger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -125,7 +135,7 @@ protected function gettoken()
]
]);

// decode the response
// decode the response
$challenge = json_decode($response->getBody());

// If challenge failed
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand All @@ -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)
{
Expand All @@ -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',
Expand Down

0 comments on commit e91e754

Please sign in to comment.