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

Commit

Permalink
-Add query builder around query function, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
cjcox17 committed Jun 27, 2018
1 parent 544b933 commit e7a7eb0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ Use the Vtiger webservice (REST) API from within Laravel for the following opera
- Retrieve
- Update
- Delete
- Search
- Query
- Describe

See [Third Party App Integration (REST APIs)](http://community.vtiger.com/help/vtigercrm/developers/third-party-app-integration.html)
See [Third Party App Integration (REST APIs)](http://community.vtiger.com/help/vtigercrm/developers/third-party-app-integration.html)

## Installation, Configuration and Usage

Expand Down Expand Up @@ -46,15 +47,13 @@ See [Third Party App Integration (REST APIs)](http://community.vtiger.com/help/v
- In your application, edit *config/vtiger.php* and replace the following array values
- Set the url to the https://{DOMAIN_NAME}/webservice.php
- Set the username and accesskey with your CRM username and access key.
- Set the session drive to either file or reddis
- Set persistconnection to false if you want a fresh login with each request
|key |value |
|-----------------|-------------------------------------|
|url |http://www.example.com/webservice.php|
|username |API |
|accesskey |irGsy9HB0YOZdEA |
|sessiondriver |file |
|persistconnection|true |
|max_retries |10 |
Expand All @@ -70,7 +69,7 @@ use Vtiger;

#### Create

To insert a record into the CRM, first create an array of data to insert. Don't forget the added the id of the `assigned_user_id` (i.e. '4x12') otherwise the insert will fail as `assigned_user_id` is a mandatory field.
To insert a record into the CRM, first create an array of data to insert. Don't forget the added the id of the `assigned_user_id` (i.e. '4x12') otherwise the insert will fail as `assigned_user_id` is a mandatory field.
```php
$data = array(
'assigned_user_id' => '',
Expand Down Expand Up @@ -122,6 +121,25 @@ $obj = Vtiger::retrieve($id);
var_dump($obj);
```

#### Search

This function is a sql query builder wrapped around the query function.
```php
$search = [];
$search[] = [
'firstname' => ['=', 'John'],
'lastname' => ['=', 'Smith'],
'login_date' => ['>=', '00-00-00 00:00:00'],
];

$obj = Vtiger::search('Leads', $search);

//loop over result
foreach($obj->result as $result) {
// do something
}
```

#### Query

To use the [Query Operation](http://community.vtiger.com/help/vtigercrm/developers/third-party-app-integration.html#query-operation), you first need to create a SQL query.
Expand Down
23 changes: 23 additions & 0 deletions src/Vtiger.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Psr\Http\Message\ResponseInterface;
use Config;
use Cache;
use DB;

/**
* Laravel wrapper for the VTgier API
Expand Down Expand Up @@ -57,6 +58,7 @@ public function __construct()
1 => new VtigerErrorElement('API request did not complete correctly - Response code: ', 1),
2 => new VtigerErrorElement('Success property not set on VTiger response', 2),
3 => new VtigerErrorElement('Error property not set on VTiger response when success is false', 3),
4 => new VtigerErrorElement('There are no search fields in the array', 4),
5 => new VtigerErrorElement('Could not complete login request within ' . $this->maxRetries . ' tries', 5),
6 => new VtigerErrorElement(
'Could not complete get token request within ' . $this->maxRetries . ' tries',
Expand Down Expand Up @@ -318,6 +320,27 @@ public function query($query)
return $this->_processResult($response);
}

public function search($module, $array)
{
if (empty($array) || !$module) {
throw VtigerError::init($this->vTigerErrors, 4);
}

$query = DB::table($module);
foreach ($array as $item) {
$query->where(key($item), $item[key($item)][0], $item[key($item)][1]);
}

$bindings = $query->getBindings();
$queryString = $query->toSQL();

foreach ($bindings as $binding) {
$queryString = preg_replace('/\?/', DB::connection()->getPdo()->quote($binding), $queryString, 1);
}

return $this->query($queryString);
}

/**
* Retreive a record from the VTiger API
* Format of id must be {moudler_code}x{item_id}, e.g 4x12
Expand Down

0 comments on commit e7a7eb0

Please sign in to comment.