1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Vtiger Web Services PHP Client Library |
5
|
|
|
* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2015, Zhmayev Yaroslav <[email protected]> |
9
|
|
|
* |
10
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
11
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
12
|
|
|
* in the Software without restriction, including without limitation the rights |
13
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
14
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
15
|
|
|
* furnished to do so, subject to the following conditions: |
16
|
|
|
* |
17
|
|
|
* The above copyright notice and this permission notice shall be included in |
18
|
|
|
* all copies or substantial portions of the Software. |
19
|
|
|
* |
20
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
21
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
22
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
23
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
24
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
25
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
26
|
|
|
* THE SOFTWARE. |
27
|
|
|
* |
28
|
|
|
* @author Zhmayev Yaroslav <[email protected]> |
29
|
|
|
* @copyright 2015-2016 Zhmayev Yaroslav |
30
|
|
|
* @license The MIT License (MIT) |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
namespace Salaros\Vtiger\VTWSCLib; |
34
|
|
|
|
35
|
|
|
use GuzzleHttp\Client; |
36
|
|
|
use GuzzleHttp\Exception\RequestException; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Vtiger Web Services PHP Client |
40
|
|
|
* |
41
|
|
|
* Class WSClient |
42
|
|
|
* @package Salaros\Vtiger\VTWSCLib |
43
|
|
|
*/ |
44
|
|
|
class WSClient |
45
|
|
|
{ |
46
|
|
|
// HTTP Client instance |
47
|
|
|
protected $httpClient = null; |
48
|
|
|
|
49
|
|
|
// Service URL to which client connects to |
50
|
|
|
protected $serviceBaseURL = 'webservice.php'; |
51
|
|
|
|
52
|
|
|
// Webservice login validity |
53
|
|
|
private $serviceServerTime = false; |
54
|
|
|
private $serviceExpireTime = false; |
55
|
|
|
private $serviceToken = false; |
56
|
|
|
|
57
|
|
|
// Webservice user credentials |
58
|
|
|
private $userName = false; |
59
|
|
|
private $accessKey = false; |
60
|
|
|
|
61
|
|
|
// Webservice login credentials |
62
|
|
|
private $userID = false; |
63
|
|
|
private $sessionName = false; |
64
|
|
|
|
65
|
|
|
// Vtiger CRM and WebServices API version |
66
|
|
|
private $apiVersion = false; |
67
|
|
|
private $vtigerVersion = false; |
68
|
|
|
|
69
|
|
|
// Last operation error information |
70
|
|
|
protected $lastErrorMessage = null; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Class constructor |
74
|
|
|
* @param string $url The URL of the remote WebServices server |
75
|
|
|
*/ |
76
|
|
|
public function __construct($url) |
77
|
|
|
{ |
78
|
|
|
if (!preg_match('/^https?:\/\//i', $url)) { |
79
|
|
|
$url = sprintf('http://%s', $url); |
80
|
|
|
} |
81
|
|
|
if (strripos($url, '/') != (strlen($url)-1)) { |
82
|
|
|
$url .= '/'; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// Gets target URL for WebServices API requests |
86
|
|
|
$this->httpClient = new Client([ |
87
|
|
|
'base_uri' => $url |
88
|
|
|
]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Check if server response contains an error, therefore the requested operation has failed |
93
|
|
|
* @access private |
94
|
|
|
* @param array $jsonResult Server response object to check for errors |
95
|
|
|
* @return boolean True if response object contains an error |
96
|
|
|
*/ |
97
|
|
|
private function checkForError(array $jsonResult) |
98
|
|
|
{ |
99
|
|
|
if (isset($jsonResult['success']) && (bool)$jsonResult['success'] === true) { |
100
|
|
|
$this->lastErrorMessage = null; |
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$this->lastErrorMessage = new WSClientError( |
105
|
|
|
$jsonResult['error']['message'], |
106
|
|
|
$jsonResult['error']['code'] |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
return true; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Checks and performs a login operation if requried and repeats login if needed |
114
|
|
|
* @access private |
115
|
|
|
*/ |
116
|
|
|
private function checkLogin() |
117
|
|
|
{ |
118
|
|
|
if (time() <= $this->serviceExpireTime) { |
119
|
|
|
return; |
120
|
|
|
} |
121
|
|
|
$this->login($this->userName, $this->accessKey); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Sends HTTP request to VTiger web service API endpoint |
126
|
|
|
* @access private |
127
|
|
|
* @param array $requestData HTTP request data |
128
|
|
|
* @param string $method HTTP request method (GET, POST etc) |
129
|
|
|
* @return array Returns request result object (null in case of failure) |
130
|
|
|
*/ |
131
|
|
|
private function sendHttpRequest(array $requestData, $method = 'POST') |
132
|
|
|
{ |
133
|
|
|
try { |
134
|
|
|
switch ($method) { |
135
|
|
|
case 'GET': |
136
|
|
|
$response = $this->httpClient->get($this->serviceBaseURL, ['query' => $requestData]); |
137
|
|
|
break; |
138
|
|
|
case 'POST': |
139
|
|
|
$response = $this->httpClient->post($this->serviceBaseURL, ['form_params' => $requestData]); |
140
|
|
|
break; |
141
|
|
|
default: |
142
|
|
|
$this->lastErrorMessage = new WSClientError("Unknown request type {$method}"); |
143
|
|
|
return null; |
144
|
|
|
} |
145
|
|
|
} catch (RequestException $ex) { |
146
|
|
|
$this->lastError = new WSClientError( |
|
|
|
|
147
|
|
|
$ex->getMessage(), |
148
|
|
|
$ex->getCode() |
149
|
|
|
); |
150
|
|
|
return null; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$jsonRaw = $response->getBody(); |
154
|
|
|
$jsonObj = json_decode($jsonRaw, true); |
155
|
|
|
|
156
|
|
|
return (!is_array($jsonObj) || $this->checkForError($jsonObj)) |
157
|
|
|
? null |
158
|
|
|
: $jsonObj['result']; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Gets a challenge token from the server and stores for future requests |
163
|
|
|
* @access private |
164
|
|
|
* @param string $username VTiger user name |
165
|
|
|
* @return booleanReturns false in case of failure |
166
|
|
|
*/ |
167
|
|
|
private function passChallenge($username) |
168
|
|
|
{ |
169
|
|
|
$getdata = [ |
170
|
|
|
'operation' => 'getchallenge', |
171
|
|
|
'username' => $username |
172
|
|
|
]; |
173
|
|
|
$result = $this->sendHttpRequest($getdata, 'GET'); |
174
|
|
|
|
175
|
|
|
if (!is_array($result) || !isset($result['token'])) { |
176
|
|
|
return false; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$this->serviceServerTime = $result['serverTime']; |
180
|
|
|
$this->serviceExpireTime = $result['expireTime']; |
181
|
|
|
$this->serviceToken = $result['token']; |
182
|
|
|
|
183
|
|
|
return true; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Login to the server using username and VTiger access key token |
188
|
|
|
* @access public |
189
|
|
|
* @param string $username VTiger user name |
190
|
|
|
* @param string $accessKey VTiger access key token (visible on user profile/settings page) |
191
|
|
|
* @return boolean Returns true if login operation has been successful |
192
|
|
|
*/ |
193
|
|
|
public function login($username, $accessKey) |
194
|
|
|
{ |
195
|
|
|
// Do the challenge before loggin in |
196
|
|
|
if ($this->passChallenge($username) === false) { |
197
|
|
|
return false; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$postdata = [ |
201
|
|
|
'operation' => 'login', |
202
|
|
|
'username' => $username, |
203
|
|
|
'accessKey' => md5($this->serviceToken.$accessKey) |
204
|
|
|
]; |
205
|
|
|
|
206
|
|
|
$result = $this->sendHttpRequest($postdata); |
207
|
|
|
if (!$result || !is_array($result)) { |
|
|
|
|
208
|
|
|
return false; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
// Backuping logged in user credentials |
212
|
|
|
$this->userName = $username; |
|
|
|
|
213
|
|
|
$this->accessKey = $accessKey; |
|
|
|
|
214
|
|
|
|
215
|
|
|
// Session data |
216
|
|
|
$this->sessionName = $result['sessionName']; |
217
|
|
|
$this->userID = $result['userId']; |
218
|
|
|
|
219
|
|
|
// Vtiger CRM and WebServices API version |
220
|
|
|
$this->apiVersion = $result['version']; |
221
|
|
|
$this->vtigerVersion = $result['vtigerVersion']; |
222
|
|
|
|
223
|
|
|
return true; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Allows you to login using username and password instead of access key (works on some VTige forks) |
228
|
|
|
* @access public |
229
|
|
|
* @param string $username VTiger user name |
230
|
|
|
* @param string $password VTiger password (used to access CRM using the standard login page) |
231
|
|
|
* @param string $accessKey This parameter will be filled with user's VTiger access key |
232
|
|
|
* @return boolean Returns true if login operation has been successful |
233
|
|
|
*/ |
234
|
|
|
public function loginPassword($username, $password, &$accessKey = null) |
235
|
|
|
{ |
236
|
|
|
// Do the challenge before loggin in |
237
|
|
|
if ($this->passChallenge($username) === false) { |
238
|
|
|
return false; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
$postdata = [ |
242
|
|
|
'operation' => 'login_pwd', |
243
|
|
|
'username' => $username, |
244
|
|
|
'password' => $password |
245
|
|
|
]; |
246
|
|
|
|
247
|
|
|
$result = $this->sendHttpRequest($postdata); |
248
|
|
|
if (!$result || !is_array($result) || count($result) !== 1) { |
|
|
|
|
249
|
|
|
return false; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
$accessKey = array_key_exists('accesskey', $result) |
253
|
|
|
? $result['accesskey'] |
254
|
|
|
: $result[0]; |
255
|
|
|
|
256
|
|
|
return $this->login($username, $accessKey); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Gets last operation error, if any |
261
|
|
|
* @access public |
262
|
|
|
* @return WSClientError The error object |
263
|
|
|
*/ |
264
|
|
|
public function getLastError() |
265
|
|
|
{ |
266
|
|
|
return $this->lastErrorMessage; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Returns the client library version. |
271
|
|
|
* @access public |
272
|
|
|
* @return string Client library version |
273
|
|
|
*/ |
274
|
|
|
public function getVersion() |
275
|
|
|
{ |
276
|
|
|
global $wsclient_version; |
|
|
|
|
277
|
|
|
return $wsclient_version; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Lists all the Vtiger entity types available through the API |
282
|
|
|
* @access public |
283
|
|
|
* @return array List of entity types |
284
|
|
|
*/ |
285
|
|
|
public function getTypes() |
286
|
|
|
{ |
287
|
|
|
// Perform re-login if required. |
288
|
|
|
$this->checkLogin(); |
289
|
|
|
|
290
|
|
|
$getdata = [ |
291
|
|
|
'operation' => 'listtypes', |
292
|
|
|
'sessionName' => $this->sessionName |
293
|
|
|
]; |
294
|
|
|
|
295
|
|
|
$result = $this->sendHttpRequest($getdata, 'GET'); |
296
|
|
|
$modules = $result['types']; |
297
|
|
|
|
298
|
|
|
$result = array(); |
299
|
|
|
foreach ($modules as $moduleName) { |
300
|
|
|
$result[$moduleName] = ['name' => $moduleName]; |
301
|
|
|
} |
302
|
|
|
return $result; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Get the type information about a given VTiger entity type. |
307
|
|
|
* @access public |
308
|
|
|
* @param string $moduleName Name of the module / entity type |
309
|
|
|
* @return array Result object |
310
|
|
|
*/ |
311
|
|
|
public function getType($moduleName) |
312
|
|
|
{ |
313
|
|
|
// Perform re-login if required. |
314
|
|
|
$this->checkLogin(); |
315
|
|
|
|
316
|
|
|
$getdata = [ |
317
|
|
|
'operation' => 'describe', |
318
|
|
|
'sessionName' => $this->sessionName, |
319
|
|
|
'elementType' => $moduleName |
320
|
|
|
]; |
321
|
|
|
|
322
|
|
|
return $this->sendHttpRequest($getdata, 'GET'); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Gets the entity ID prepended with module / entity type ID |
327
|
|
|
* @access private |
328
|
|
|
* @param string $moduleName Name of the module / entity type |
329
|
|
|
* @param string $entityID Numeric entity ID |
330
|
|
|
* @return boolean|string Returns false if it is not possible to retrieve module / entity type ID |
331
|
|
|
*/ |
332
|
|
|
private function getTypedID($moduleName, $entityID) |
333
|
|
|
{ |
334
|
|
|
if (stripos((string)$entityID, 'x') !== false) { |
335
|
|
|
return $entityID; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
if (empty($entityID) || intval($entityID) < 1) { |
339
|
|
|
throw new \Exception('Entity ID must be a valid number'); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
$type = $this->getType($moduleName); |
343
|
|
|
if (!$type || !array_key_exists('idPrefix', $type)) { |
|
|
|
|
344
|
|
|
$errorMessage = sprintf("The following module is not installed: %s", $moduleName); |
345
|
|
|
$this->lastErrorMessage = new WSClientError($errorMessage); |
346
|
|
|
return false; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
return "{$type['idPrefix']}x{$entityID}"; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Invokes custom operation (defined in vtiger_ws_operation table) |
354
|
|
|
* @access public |
355
|
|
|
* @param string $operation Name of the webservice to invoke |
356
|
|
|
* @param array [$params = null] Parameter values to operation |
357
|
|
|
* @param string [$method = 'POST'] HTTP request method (GET, POST etc) |
358
|
|
|
* @return array Result object |
359
|
|
|
*/ |
360
|
|
|
public function invokeOperation($operation, array $params = null, $method = 'POST') |
361
|
|
|
{ |
362
|
|
View Code Duplication |
if (!empty($params) || !is_array($params) || !$this->isAssocArray($params)) { |
363
|
|
|
$this->lastErrorMessage = new WSClientError( |
364
|
|
|
"You have to specified a list of operation parameters, |
365
|
|
|
but apparently it's not an associative array ('prop' => value), you must fix it!" |
366
|
|
|
); |
367
|
|
|
return false; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
// Perform re-login if required |
371
|
|
|
$this->checkLogin(); |
372
|
|
|
|
373
|
|
|
$requestData = [ |
374
|
|
|
'operation' => $operation, |
375
|
|
|
'sessionName' => $this->sessionName |
376
|
|
|
]; |
377
|
|
|
|
378
|
|
|
if (!empty($params) && is_array($params)) { |
379
|
|
|
$requestData = array_merge($requestData, $params); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
return $this->sendHttpRequest($requestData, $method); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* VTiger provides a simple query language for fetching data. |
387
|
|
|
* This language is quite similar to select queries in SQL. |
388
|
|
|
* There are limitations, the queries work on a single Module, |
389
|
|
|
* embedded queries are not supported, and does not support joins. |
390
|
|
|
* But this is still a powerful way of getting data from Vtiger. |
391
|
|
|
* Query always limits its output to 100 records, |
392
|
|
|
* Client application can use limit operator to get different records. |
393
|
|
|
* @access public |
394
|
|
|
* @param string $query SQL-like expression |
395
|
|
|
* @return array Query results |
396
|
|
|
*/ |
397
|
|
|
public function query($query) |
398
|
|
|
{ |
399
|
|
|
// Perform re-login if required. |
400
|
|
|
$this->checkLogin(); |
401
|
|
|
|
402
|
|
|
// Make sure the query ends with ; |
403
|
|
|
$query = (strripos($query, ';') != strlen($query)-1) |
404
|
|
|
? trim($query .= ';') |
405
|
|
|
: trim($query); |
406
|
|
|
|
407
|
|
|
$getdata = [ |
408
|
|
|
'operation' => 'query', |
409
|
|
|
'sessionName' => $this->sessionName, |
410
|
|
|
'query' => $query |
411
|
|
|
]; |
412
|
|
|
|
413
|
|
|
return $this->sendHttpRequest($getdata, 'GET'); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* Retrieves an entity by ID |
418
|
|
|
* @param string $moduleName The name of the module / entity type |
419
|
|
|
* @param string $entityID The ID of the entity to retrieve |
420
|
|
|
* @return boolean Entity data |
421
|
|
|
*/ |
422
|
|
View Code Duplication |
public function entityRetrieveByID($moduleName, $entityID) |
|
|
|
|
423
|
|
|
{ |
424
|
|
|
// Perform re-login if required. |
425
|
|
|
$this->checkLogin(); |
426
|
|
|
|
427
|
|
|
// Preprend so-called moduleid if needed |
428
|
|
|
$entityID = $this->getTypedID($moduleName, $entityID); |
429
|
|
|
|
430
|
|
|
$getdata = [ |
431
|
|
|
'operation' => 'retrieve', |
432
|
|
|
'sessionName' => $this->sessionName, |
433
|
|
|
'id' => $entityID |
434
|
|
|
]; |
435
|
|
|
|
436
|
|
|
return $this->sendHttpRequest($getdata, 'GET'); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* Uses VTiger queries to retrieve the entity matching a list of constraints |
441
|
|
|
* @param string $moduleName The name of the module / entity type |
442
|
|
|
* @param array $params Data used to find a matching entry |
443
|
|
|
* @return array $select The list of fields to select (defaults to SQL-like '*' - all the fields) |
444
|
|
|
* @return int The matching record |
445
|
|
|
*/ |
446
|
|
|
public function entityRetrieve($moduleName, array $params, array $select = []) |
447
|
|
|
{ |
448
|
|
|
$records = $this->entitiesRetrieve($moduleName, $params, $select, 1); |
449
|
|
|
if (false === $records || !isset($records[0])) { |
450
|
|
|
return false; |
|
|
|
|
451
|
|
|
} |
452
|
|
|
return $records[0]; |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* Retrieves the ID of the entity matching a list of constraints + prepends '<module_id>x' string to it |
457
|
|
|
* @param string $moduleName The name of the module / entity type |
458
|
|
|
* @param array $params Data used to find a matching entry |
459
|
|
|
* @return int Type ID (a numeric ID + '<module_id>x') |
460
|
|
|
*/ |
461
|
|
|
public function entityRetrieveTypeID($moduleName, array $params) |
462
|
|
|
{ |
463
|
|
|
$records = $this->entitiesRetrieve($moduleName, $params, ['id'], 1); |
464
|
|
|
if (false === $records || !isset($records[0]['id']) || empty($records[0]['id'])) { |
465
|
|
|
return false; |
|
|
|
|
466
|
|
|
} |
467
|
|
|
return $records[0]['id']; |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* Retrieve a numeric ID of the entity matching a list of constraints |
472
|
|
|
* @param string $moduleName The name of the module / entity type |
473
|
|
|
* @param array $params Data used to find a matching entry |
474
|
|
|
* @return int Numeric ID |
475
|
|
|
*/ |
476
|
|
|
public function entityRetrieveID($moduleName, array $params) |
477
|
|
|
{ |
478
|
|
|
$entityID = $this->entityRetrieveTypeID($moduleName, $params); |
479
|
|
|
$entityIDParts = explode('x', $entityID, 2); |
480
|
|
|
return (is_array($entityIDParts) && count($entityIDParts) === 2) |
481
|
|
|
? $entityIDParts[1] |
482
|
|
|
: -1; |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
/** |
486
|
|
|
* Creates an entity for the giving module |
487
|
|
|
* @param string $moduleName Name of the module / entity type for which the entry has to be created |
488
|
|
|
* @param array $params Entity data |
489
|
|
|
* @return array Entity creation results |
490
|
|
|
*/ |
491
|
|
|
public function entityCreate($moduleName, array $params) |
492
|
|
|
{ |
493
|
|
|
if (!$this->checkParams($params, 'be able to create an entity')) { |
494
|
|
|
return false; |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
// Perform re-login if required. |
498
|
|
|
$this->checkLogin(); |
499
|
|
|
|
500
|
|
|
// Assign record to logged in user if not specified |
501
|
|
|
if (!isset($params['assigned_user_id'])) { |
502
|
|
|
$params['assigned_user_id'] = $this->userID; |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
$postdata = [ |
506
|
|
|
'operation' => 'create', |
507
|
|
|
'sessionName' => $this->sessionName, |
508
|
|
|
'elementType' => $moduleName, |
509
|
|
|
'element' => json_encode($params) |
510
|
|
|
]; |
511
|
|
|
|
512
|
|
|
return $this->sendHttpRequest($postdata); |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
/** |
516
|
|
|
* Updates an entity |
517
|
|
|
* @param string $moduleName The name of the module / entity type |
518
|
|
|
* @param array $params Entity data |
519
|
|
|
* @return array Entity update result |
520
|
|
|
*/ |
521
|
|
|
public function entityUpdate($moduleName, array $params) |
522
|
|
|
{ |
523
|
|
|
if (!$this->checkParams($params, 'be able to update the entity(ies)')) { |
524
|
|
|
return false; |
525
|
|
|
} |
526
|
|
|
|
527
|
|
|
// Fail if no ID was supplied |
528
|
|
|
if (!array_key_exists('id', $params) || empty($params['id'])) { |
529
|
|
|
$this->lastErrorMessage = new WSClientError( |
530
|
|
|
"The list of contraints must contain a valid ID" |
531
|
|
|
); |
532
|
|
|
return false; |
533
|
|
|
} |
534
|
|
|
|
535
|
|
|
// Perform re-login if required. |
536
|
|
|
$this->checkLogin(); |
537
|
|
|
|
538
|
|
|
// Preprend so-called moduleid if needed |
539
|
|
|
$params['id'] = $this->getTypedID($moduleName, $params['id']); |
540
|
|
|
|
541
|
|
|
// Check if the entity exists + retrieve its data so it can be used below |
542
|
|
|
$entityData = $this->entityRetrieve($moduleName, [ 'id' => $params['id'] ]); |
543
|
|
|
if ($entityData === false && !is_array($entityData)) { |
544
|
|
|
$this->lastErrorMessage = new WSClientError("Such entity doesn't exist, so it cannot be updated"); |
545
|
|
|
return false; |
546
|
|
|
} |
547
|
|
|
|
548
|
|
|
// The new data overrides the existing one needed to provide |
549
|
|
|
// mandatory field values to WS 'update' operation |
550
|
|
|
$params = array_merge( |
551
|
|
|
$entityData, |
552
|
|
|
$params |
553
|
|
|
); |
554
|
|
|
|
555
|
|
|
$postdata = [ |
556
|
|
|
'operation' => 'update', |
557
|
|
|
'sessionName' => $this->sessionName, |
558
|
|
|
'elementType' => $moduleName, |
559
|
|
|
'element' => json_encode($params) |
560
|
|
|
]; |
561
|
|
|
|
562
|
|
|
return $this->sendHttpRequest($postdata); |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
/** |
566
|
|
|
* Provides entity removal functionality |
567
|
|
|
* @param string $moduleName The name of the module / entity type |
568
|
|
|
* @param string $entityID The ID of the entity to delete |
569
|
|
|
* @return array Removal status object |
570
|
|
|
*/ |
571
|
|
View Code Duplication |
public function entityDelete($moduleName, $entityID) |
|
|
|
|
572
|
|
|
{ |
573
|
|
|
// Perform re-login if required. |
574
|
|
|
$this->checkLogin(); |
575
|
|
|
|
576
|
|
|
// Preprend so-called moduleid if needed |
577
|
|
|
$entityID = $this->getTypedID($moduleName, $entityID); |
578
|
|
|
|
579
|
|
|
$postdata = [ |
580
|
|
|
'operation' => 'delete', |
581
|
|
|
'sessionName' => $this->sessionName, |
582
|
|
|
'id' => $entityID |
583
|
|
|
]; |
584
|
|
|
|
585
|
|
|
return $this->sendHttpRequest($postdata); |
586
|
|
|
} |
587
|
|
|
|
588
|
|
|
/** |
589
|
|
|
* Retrieves multiple records using module name and a set of constraints |
590
|
|
|
* @param string $moduleName The name of the module / entity type |
591
|
|
|
* @param array $params Data used to find matching entries |
592
|
|
|
* @return array $select The list of fields to select (defaults to SQL-like '*' - all the fields) |
593
|
|
|
* @return int $limit limit the list of entries to N records (acts like LIMIT in SQL) |
594
|
|
|
* @return bool|array The array containing matching entries or false if nothing was found |
595
|
|
|
*/ |
596
|
|
|
public function entitiesRetrieve($moduleName, array $params, array $select = [], $limit = 0) |
597
|
|
|
{ |
598
|
|
|
if (!$this->checkParams($params, 'be able to retrieve entity(ies)')) { |
599
|
|
|
return false; |
600
|
|
|
} |
601
|
|
|
|
602
|
|
|
// Perform re-login if required. |
603
|
|
|
$this->checkLogin(); |
604
|
|
|
|
605
|
|
|
// Builds the query |
606
|
|
|
$query = $this->buildQuery($moduleName, $params, $select, $limit); |
607
|
|
|
|
608
|
|
|
// Run the query |
609
|
|
|
$records = $this->query($query); |
610
|
|
|
if (false === $records || !is_array($records) || (count($records) <= 0)) { |
611
|
|
|
return false; |
612
|
|
|
} |
613
|
|
|
|
614
|
|
|
return $records; |
615
|
|
|
} |
616
|
|
|
|
617
|
|
|
/** |
618
|
|
|
* Sync will return a sync result object containing details of changes after modifiedTime |
619
|
|
|
* @param int [$modifiedTime = null] The date of the first change |
620
|
|
|
* @param string [$moduleName = null] The name of the module / entity type |
621
|
|
|
* @return array Sync result object |
622
|
|
|
*/ |
623
|
|
|
public function entitiesSync($modifiedTime = null, $moduleName = null) |
624
|
|
|
{ |
625
|
|
|
// Perform re-login if required. |
626
|
|
|
$this->checkLogin(); |
627
|
|
|
|
628
|
|
|
$modifiedTime = (empty($modifiedTime)) |
629
|
|
|
? strtotime('today midnight') |
630
|
|
|
: intval($modifiedTime); |
631
|
|
|
|
632
|
|
|
$requestData = [ |
633
|
|
|
'operation' => 'sync', |
634
|
|
|
'sessionName' => $this->sessionName, |
635
|
|
|
'modifiedTime' => $modifiedTime |
636
|
|
|
]; |
637
|
|
|
|
638
|
|
|
if (!empty($moduleName)) { |
639
|
|
|
$requestData['elementType'] = $moduleName; |
640
|
|
|
} |
641
|
|
|
|
642
|
|
|
return $this->sendHttpRequest($requestData, true); |
|
|
|
|
643
|
|
|
} |
644
|
|
|
|
645
|
|
|
/** |
646
|
|
|
* Builds the query using the supplied parameters |
647
|
|
|
* @param string $moduleName The name of the module / entity type |
648
|
|
|
* @param array $params Data used to find matching entries |
649
|
|
|
* @return array $select The list of fields to select (defaults to SQL-like '*' - all the fields) |
650
|
|
|
* @return int $limit limit the list of entries to N records (acts like LIMIT in SQL) |
651
|
|
|
* @return string The query build out of the supplied parameters |
652
|
|
|
*/ |
653
|
|
|
private function buildQuery($moduleName, array $params, array $select = [], $limit = 0) |
654
|
|
|
{ |
655
|
|
|
$criteria = array(); |
656
|
|
|
$select=(empty($select)) ? '*' : implode(',', $select); |
657
|
|
|
$query=sprintf("SELECT %s FROM $moduleName WHERE ", $select); |
658
|
|
|
foreach ($params as $param => $value) { |
659
|
|
|
$criteria[] = "{$param} LIKE '{$value}'"; |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
$query.=implode(" AND ", $criteria); |
663
|
|
|
if (intval($limit) > 0) { |
664
|
|
|
$query.=sprintf(" LIMIT %s", intval($limit)); |
665
|
|
|
} |
666
|
|
|
return $query; |
667
|
|
|
} |
668
|
|
|
|
669
|
|
|
/** |
670
|
|
|
* Checks if if params holds valid entity data/search constraints, otherwise returns false |
671
|
|
|
* @param array $params Array holding entity data/search constraints |
672
|
|
|
* @return boolean Returns true if params holds valid entity data/search constraints, otherwise returns false |
673
|
|
|
*/ |
674
|
|
|
private function checkParams(array $params, $paramsPurpose) |
675
|
|
|
{ |
676
|
|
View Code Duplication |
if (empty($params) || !is_array($params) || !$this->isAssocArray($params)) { |
|
|
|
|
677
|
|
|
$this->lastErrorMessage = new WSClientError(sprintf( |
678
|
|
|
"You have to specify at least one search parameter (prop => value) in order to %s", |
679
|
|
|
$paramsPurpose |
680
|
|
|
)); |
681
|
|
|
return false; |
682
|
|
|
} |
683
|
|
|
return true; |
684
|
|
|
} |
685
|
|
|
|
686
|
|
|
/** |
687
|
|
|
* A helper method, used to check in an array is associative or not |
688
|
|
|
* @param string Array to test |
689
|
|
|
* @return boolean Returns true in a given array is associative and false if it's not |
690
|
|
|
*/ |
691
|
|
|
private function isAssocArray(array $array) |
692
|
|
|
{ |
693
|
|
|
foreach (array_keys($array) as $key) { |
694
|
|
|
if (!is_int($key)) { |
695
|
|
|
return true; |
696
|
|
|
} |
697
|
|
|
} |
698
|
|
|
return false; |
699
|
|
|
} |
700
|
|
|
} |
701
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: