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

Commit

Permalink
Update to support offsets in search()
Browse files Browse the repository at this point in the history
Vtiger does not support the offset keyword in it's queries.

It instead supports limit [offset],[limit]. So the search() function has been modified to rewrite the query to the proper format for Vtiger so that the offset functions properly when sending a query to Vtiger.

Also, my php-fs-fixer added commas to the end of some of the arrays automatically, sorry about that.
  • Loading branch information
cjcox17 authored Oct 2, 2018
1 parent 210a8cb commit 20b8a0d
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions src/Vtiger.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function __construct()
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),
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',
6
Expand Down Expand Up @@ -153,8 +153,8 @@ protected function login($sessionData)
'form_params' => [
'operation' => 'login',
'username' => $this->username,
'accessKey' => $generatedKey
]
'accessKey' => $generatedKey,
],
]);
} catch (GuzzleException $e) {
throw VtigerError::init($this->vTigerErrors, 7, $e->getMessage());
Expand Down Expand Up @@ -231,8 +231,8 @@ protected function getToken()
$response = $this->guzzleClient->request('GET', $this->url, [
'query' => [
'operation' => 'getchallenge',
'username' => $this->username
]
'username' => $this->username,
],
]);
} catch (GuzzleException $e) {
throw VtigerError::init($this->vTigerErrors, 7, $e->getMessage());
Expand Down Expand Up @@ -279,8 +279,8 @@ protected function close($sessionId)
[
'query' => [
'operation' => 'logout',
'sessionName' => $sessionId
]
'sessionName' => $sessionId,
],
]
);
} catch (GuzzleException $e) {
Expand Down Expand Up @@ -308,8 +308,8 @@ public function query($query)
'query' => [
'operation' => 'query',
'sessionName' => $sessionId,
'query' => $query
]
'query' => $query,
],
]);
} catch (GuzzleException $e) {
throw VtigerError::init($this->vTigerErrors, 7, $e->getMessage());
Expand All @@ -332,6 +332,18 @@ public function search($query, $quote = true)
$queryString = preg_replace('/\?/', $binding, $queryString, 1);
}
}

//In the event there is a offset, append it to the front of the limit
//Vtiger does not support the offset keyword
$matchOffset = [];
$matchLimit = [];
if (preg_match('/(\s[o][f][f][s][e][t]) (\d*)/', $queryString, $matchOffset) && preg_match('/(\s[l][i][m][i][t]) (\d*)/', $queryString, $matchLimit)) {
$queryString = preg_replace('/(\s[o][f][f][s][e][t]) (\d*)/', '', $queryString);
$queryString = preg_replace('/(\s[l][i][m][i][t]) (\d*)/', '', $queryString);
$queryString = $queryString . ' limit ' . $matchOffset[2] . ',' . $matchLimit[2];
}

//Remove the backticks and add simicolon
$queryString = str_replace('`', '', $queryString) . ';';

return $this->query($queryString);
Expand All @@ -356,8 +368,8 @@ public function retrieve($id)
'query' => [
'operation' => 'retrieve',
'sessionName' => $sessionId,
'id' => $id
]
'id' => $id,
],
]);
} catch (GuzzleException $e) {
throw VtigerError::init($this->vTigerErrors, 7, $e->getMessage());
Expand Down Expand Up @@ -398,8 +410,8 @@ public function create($elem, $data)
'operation' => 'create',
'sessionName' => $sessionId,
'element' => $data,
'elementType' => $elem
]
'elementType' => $elem,
],
]);
} catch (GuzzleException $e) {
throw VtigerError::init($this->vTigerErrors, 7, $e->getMessage());
Expand Down Expand Up @@ -431,7 +443,7 @@ public function update($object)
'operation' => 'update',
'sessionName' => $sessionId,
'element' => json_encode($object),
]
],
]);
} catch (GuzzleException $e) {
throw VtigerError::init($this->vTigerErrors, 7, $e->getMessage());
Expand Down Expand Up @@ -461,8 +473,8 @@ public function delete($id)
'query' => [
'operation' => 'delete',
'sessionName' => $sessionId,
'id' => $id
]
'id' => $id,
],
]);
} catch (GuzzleException $e) {
throw VtigerError::init($this->vTigerErrors, 7, $e->getMessage());
Expand Down Expand Up @@ -494,8 +506,8 @@ public function describe($elementType)
'query' => [
'operation' => 'describe',
'sessionName' => $sessionId,
'elementType' => $elementType
]
'elementType' => $elementType,
],
]
);
} catch (GuzzleException $e) {
Expand Down Expand Up @@ -572,7 +584,7 @@ protected function _checkResponseStatusCode($response)
* @param \stdClass $processedData
*
* @throws VtigerError
*/
*/i
protected function _processResponseError($processedData)
{
if (!isset($processedData->error)) {
Expand Down

0 comments on commit 20b8a0d

Please sign in to comment.