1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wabel\Zoho\CRM; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Wabel\Zoho\CRM\Exception\ZohoCRMResponseException; |
7
|
|
|
use Wabel\Zoho\CRM\Request\Response; |
8
|
|
|
use GuzzleHttp\Psr7\Request; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Client for provide interface with Zoho CRM. |
12
|
|
|
* |
13
|
|
|
* TODO : Add comments (a lot) |
14
|
|
|
*/ |
15
|
|
|
class ZohoClient |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* URL for call request. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
const BASE_URI = 'https://crm.zoho.com/crm/private'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Token used for session of request. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $authtoken; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Instance of the client. |
33
|
|
|
* |
34
|
|
|
* @var Client |
35
|
|
|
*/ |
36
|
|
|
protected $zohoRestClient; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Format selected for get request. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $format; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Module selected for get request. |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $module; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Construct. |
54
|
|
|
* |
55
|
|
|
* @param string $authtoken Token for connection |
56
|
|
|
* @param Client $zohoRestClient Guzzl Client for connection [optional] |
57
|
|
|
*/ |
58
|
|
|
public function __construct($authtoken, Client $zohoRestClient = null) |
59
|
|
|
{ |
60
|
|
|
$this->authtoken = $authtoken; |
61
|
|
|
// Only XML format is supported for the time being |
62
|
|
|
$this->format = 'xml'; |
63
|
|
|
$this->zohoRestClient = $zohoRestClient ?: new Client(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Implements convertLead API method. |
68
|
|
|
* |
69
|
|
|
* @param $leadId |
70
|
|
|
* @param $data |
71
|
|
|
* @param array $params |
72
|
|
|
* |
73
|
|
|
* @return Response The Response object |
74
|
|
|
* |
75
|
|
|
* @throws ZohoCRMResponseException |
76
|
|
|
*/ |
77
|
|
|
public function convertLead($leadId, $data, $params = array()) |
78
|
|
|
{ |
79
|
|
|
$module = 'Leads'; |
80
|
|
|
$params['leadId'] = $leadId; |
81
|
|
|
$params['newFormat'] = 1; |
82
|
|
|
|
83
|
|
|
return $this->call($module, 'convertLead', $params, $data); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Implements getFields API method. |
88
|
|
|
* |
89
|
|
|
* @return Response The Response object |
90
|
|
|
*/ |
91
|
|
|
public function getFields($module) |
92
|
|
|
{ |
93
|
|
|
$params['newFormat'] = 1; |
|
|
|
|
94
|
|
|
|
95
|
|
|
return $this->call($module, 'getFields', array()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Implements deleteRecords API method. |
100
|
|
|
* |
101
|
|
|
* @param string $module |
102
|
|
|
* @param string $id Id of the record |
103
|
|
|
* |
104
|
|
|
* @return Response The Response object |
105
|
|
|
* |
106
|
|
|
* @throws ZohoCRMResponseException |
107
|
|
|
*/ |
108
|
|
|
public function deleteRecords($module, $id) |
109
|
|
|
{ |
110
|
|
|
$params = array( |
111
|
|
|
'id' => $id, |
112
|
|
|
'newFormat' => 1, |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
return $this->call($module, 'deleteRecords', $params); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Implements getRecordById API method. |
120
|
|
|
* |
121
|
|
|
* @param string $module The module to use |
122
|
|
|
* @param string $id Id of the record or a list of IDs separated by a semicolon |
123
|
|
|
* |
124
|
|
|
* @return Response The Response object |
125
|
|
|
* |
126
|
|
|
* @throws ZohoCRMResponseException |
127
|
|
|
*/ |
128
|
|
|
public function getRecordById($module, $id) |
129
|
|
|
{ |
130
|
|
|
if (strpos($id, ';') === false) { |
131
|
|
|
$params['id'] = $id; |
|
|
|
|
132
|
|
|
} else { |
133
|
|
|
$params['idlist'] = $id; |
|
|
|
|
134
|
|
|
} |
135
|
|
|
$params['newFormat'] = 1; |
136
|
|
|
|
137
|
|
|
return $this->call($module, 'getRecordById', $params); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Implements getRecords API method. |
142
|
|
|
* |
143
|
|
|
* @param $module |
144
|
|
|
* @param $sortColumnString |
145
|
|
|
* @param $sortOrderString |
146
|
|
|
* @param \DateTime $lastModifiedTime |
147
|
|
|
* @param $selectColumns |
148
|
|
|
* @param $fromIndex |
149
|
|
|
* @param $toIndex |
150
|
|
|
* |
151
|
|
|
* @return Response The Response object |
152
|
|
|
* |
153
|
|
|
* @throws ZohoCRMResponseException |
154
|
|
|
*/ |
155
|
|
|
public function getRecords($module, $sortColumnString = null, $sortOrderString = null, \DateTimeInterface $lastModifiedTime = null, $selectColumns = null, $fromIndex = null, $toIndex = null) |
156
|
|
|
{ |
157
|
|
|
$params['newFormat'] = 1; |
|
|
|
|
158
|
|
|
$params['version'] = 1; |
159
|
|
|
if ($selectColumns) { |
160
|
|
|
$params['selectColumns'] = $selectColumns; |
161
|
|
|
} |
162
|
|
|
if ($fromIndex) { |
163
|
|
|
$params['fromIndex'] = $fromIndex; |
164
|
|
|
} |
165
|
|
|
if ($toIndex) { |
166
|
|
|
$params['toIndex'] = $toIndex; |
167
|
|
|
} |
168
|
|
|
if ($sortColumnString) { |
169
|
|
|
$params['sortColumnString'] = $sortColumnString; |
170
|
|
|
} |
171
|
|
|
if ($sortOrderString) { |
172
|
|
|
$params['sortOrderString'] = $sortOrderString; |
173
|
|
|
} |
174
|
|
|
if ($lastModifiedTime) { |
175
|
|
|
$params['lastModifiedTime'] = $lastModifiedTime->format('Y-m-d H:i:s'); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $this->call($module, 'getRecords', $params); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Implements getDeletedRecordIds API method. |
183
|
|
|
|
184
|
|
|
* |
185
|
|
|
* @param string $module |
186
|
|
|
* @param \DateTimeInterface $lastModifiedTime |
187
|
|
|
* @param int $fromIndex |
188
|
|
|
* @param int $toIndex |
189
|
|
|
* |
190
|
|
|
* @return Response |
191
|
|
|
* |
192
|
|
|
* @throws ZohoCRMResponseException |
193
|
|
|
*/ |
194
|
|
|
public function getDeletedRecordIds($module, \DateTimeInterface $lastModifiedTime = null, $fromIndex = null, $toIndex = null) |
195
|
|
|
{ |
196
|
|
|
$params = []; |
197
|
|
|
if ($fromIndex) { |
|
|
|
|
198
|
|
|
$params['fromIndex'] = $fromIndex; |
199
|
|
|
} |
200
|
|
|
if ($toIndex) { |
|
|
|
|
201
|
|
|
$params['toIndex'] = $toIndex; |
202
|
|
|
} |
203
|
|
|
if ($lastModifiedTime) { |
204
|
|
|
$params['lastModifiedTime'] = $lastModifiedTime->format('Y-m-d H:i:s'); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return $this->call($module, 'getDeletedRecordIds', $params); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Implements getRecords API method. |
212
|
|
|
* |
213
|
|
|
* @param $module |
214
|
|
|
* @param $id |
215
|
|
|
* @param $parentModule |
216
|
|
|
* @param null $fromIndex |
217
|
|
|
* @param null $toIndex |
218
|
|
|
* |
219
|
|
|
* @return Response |
220
|
|
|
* |
221
|
|
|
* @throws ZohoCRMResponseException |
222
|
|
|
*/ |
223
|
|
|
public function getRelatedRecords($module, $id, $parentModule, $fromIndex = null, $toIndex = null) |
224
|
|
|
{ |
225
|
|
|
$params['id'] = $id; |
|
|
|
|
226
|
|
|
$params['parentModule'] = $parentModule; |
227
|
|
|
$params['newFormat'] = 1; |
228
|
|
|
if ($fromIndex) { |
229
|
|
|
$params['fromIndex'] = $fromIndex; |
230
|
|
|
} |
231
|
|
|
if ($toIndex) { |
232
|
|
|
$params['toIndex'] = $toIndex; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return $this->call($module, 'getRelatedRecords', $params); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Implements searchRecords API method. |
240
|
|
|
* |
241
|
|
|
* @param string $module |
242
|
|
|
* @param string $searchCondition |
243
|
|
|
* @param int $fromIndex |
244
|
|
|
* @param int $toIndex |
245
|
|
|
* @param \DateTime $lastModifiedTime |
246
|
|
|
* @param null $selectColumns |
247
|
|
|
* |
248
|
|
|
* @return Response |
249
|
|
|
* |
250
|
|
|
* @throws ZohoCRMResponseException |
251
|
|
|
*/ |
252
|
|
|
public function searchRecords($module, $searchCondition = null, $fromIndex = null, $toIndex = null, $lastModifiedTime = null, $selectColumns = null) |
253
|
|
|
{ |
254
|
|
|
if ($searchCondition) { |
|
|
|
|
255
|
|
|
$params['criteria'] = $searchCondition; |
|
|
|
|
256
|
|
|
} else { |
257
|
|
|
$params['criteria'] = '()'; |
|
|
|
|
258
|
|
|
} |
259
|
|
|
if ($fromIndex) { |
|
|
|
|
260
|
|
|
$params['fromIndex'] = $fromIndex; |
261
|
|
|
} |
262
|
|
|
if ($toIndex) { |
|
|
|
|
263
|
|
|
$params['toIndex'] = $toIndex; |
264
|
|
|
} |
265
|
|
|
if ($lastModifiedTime) { |
266
|
|
|
$params['lastModifiedTime'] = $lastModifiedTime->format('Y-m-d H:i:s'); |
267
|
|
|
} |
268
|
|
|
if ($selectColumns) { |
269
|
|
|
$params['selectColumns'] = $selectColumns; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
$params['newFormat'] = 1; |
273
|
|
|
|
274
|
|
|
return $this->call($module, 'searchRecords', $params); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Implements getUsers API method. |
279
|
|
|
* |
280
|
|
|
* @param string $type The type of users you want retrieve (among AllUsers, ActiveUsers, DeactiveUsers, AdminUsers and ActiveConfirmedAdmins) |
281
|
|
|
* |
282
|
|
|
* @return Response The array of Zoho Beans parsed from the response |
283
|
|
|
* |
284
|
|
|
* @throws ZohoCRMResponseException |
285
|
|
|
*/ |
286
|
|
|
public function getUsers($type = 'AllUsers') |
287
|
|
|
{ |
288
|
|
|
switch ($type) { |
289
|
|
|
case 'AllUsers': |
290
|
|
|
case 'ActiveUsers': |
291
|
|
|
case 'DeactiveUsers': |
292
|
|
|
case 'AdminUsers': |
293
|
|
|
case 'ActiveConfirmedAdmins': |
294
|
|
|
$params['type'] = $type; |
|
|
|
|
295
|
|
|
break; |
296
|
|
|
default : |
|
|
|
|
297
|
|
|
$params['type'] = 'AllUsers'; |
|
|
|
|
298
|
|
|
break; |
299
|
|
|
} |
300
|
|
|
$params['newFormat'] = 1; |
301
|
|
|
|
302
|
|
|
return $this->call('Users', 'getUsers', $params); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Implements insertRecords API method. |
307
|
|
|
* |
308
|
|
|
* @param $module |
309
|
|
|
* @param \SimpleXMLElement$xmlData |
310
|
|
|
* @param bool $wfTrigger |
311
|
|
|
* @param int $duplicateCheck |
312
|
|
|
* @param bool $isApproval |
313
|
|
|
* |
314
|
|
|
* @return Response |
315
|
|
|
* |
316
|
|
|
* @throws ZohoCRMResponseException |
317
|
|
|
*/ |
318
|
|
|
public function insertRecords($module, $xmlData, $wfTrigger = null, $duplicateCheck = null, $isApproval = null, $version = 4, $newFormat = 2) |
319
|
|
|
{ |
320
|
|
|
if ($wfTrigger) { |
321
|
|
|
$params['wfTrigger'] = 'true'; |
|
|
|
|
322
|
|
|
} |
323
|
|
|
if ($duplicateCheck) { |
|
|
|
|
324
|
|
|
$params['duplicateCheck'] = $duplicateCheck; |
|
|
|
|
325
|
|
|
} |
326
|
|
|
if ($isApproval) { |
327
|
|
|
$params['isApproval'] = 'true'; |
328
|
|
|
} |
329
|
|
|
$params['newFormat'] = $newFormat; |
330
|
|
|
$params['version'] = $version; |
331
|
|
|
|
332
|
|
|
return $this->call($module, 'insertRecords', $params, ['xmlData' => $xmlData->asXML()]); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Implements updateRecords API method. |
337
|
|
|
* |
338
|
|
|
* @param $module |
339
|
|
|
* @param \SimpleXMLElement $xmlData |
340
|
|
|
* @param string $id |
341
|
|
|
* @param bool $wfTrigger |
342
|
|
|
* |
343
|
|
|
* @return Response |
344
|
|
|
* |
345
|
|
|
* @throws ZohoCRMResponseException |
346
|
|
|
*/ |
347
|
|
|
public function updateRecords($module, $xmlData, $id = null, $wfTrigger = null, $version = 4, $newFormat = 2) |
348
|
|
|
{ |
349
|
|
|
$params['newFormat'] = $newFormat; |
|
|
|
|
350
|
|
|
$params['version'] = $version; |
351
|
|
|
if ($wfTrigger) { |
352
|
|
|
$params['wfTrigger'] = 'true'; |
353
|
|
|
} |
354
|
|
|
if ($id) { |
|
|
|
|
355
|
|
|
$params['id'] = $id; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
return $this->call($module, 'updateRecords', $params, ['xmlData' => $xmlData->asXML()]); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* Implements uploadFile API method. |
363
|
|
|
* |
364
|
|
|
* @param $module |
365
|
|
|
* @param $id |
366
|
|
|
* @param $content |
367
|
|
|
* |
368
|
|
|
* @return Response |
369
|
|
|
* |
370
|
|
|
* @throws ZohoCRMResponseException |
371
|
|
|
*/ |
372
|
|
|
public function uploadFile($module, $id, $content) |
373
|
|
|
{ |
374
|
|
|
$params['id'] = $id; |
|
|
|
|
375
|
|
|
$params['content'] = $content; |
376
|
|
|
|
377
|
|
|
return $this->call($module, 'uploadFile', $params); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Implements downloadFile API method. |
382
|
|
|
* |
383
|
|
|
* @param $module |
384
|
|
|
* @param $id |
385
|
|
|
* |
386
|
|
|
* @return Response |
387
|
|
|
* |
388
|
|
|
* @throws ZohoCRMResponseException |
389
|
|
|
*/ |
390
|
|
|
public function downloadFile($module, $id) |
391
|
|
|
{ |
392
|
|
|
$params['id'] = $id; |
|
|
|
|
393
|
|
|
|
394
|
|
|
return $this->call($module, 'downloadFile', $params); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* Returns a list of modules from Zoho. |
399
|
|
|
*/ |
400
|
|
|
public function getModules() |
401
|
|
|
{ |
402
|
|
|
return $this->call('Info', 'getModules', ['type' => 'api']); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* Get the body of the request |
408
|
|
|
* |
409
|
|
|
* @param array $params Params |
410
|
|
|
* @param Object $data Data |
411
|
|
|
* @return string |
412
|
|
|
*/ |
413
|
|
|
protected function getRequestBody($params, $data, $options) |
|
|
|
|
414
|
|
|
{ |
415
|
|
|
if($data){ |
416
|
|
|
return http_build_query($data, '', '&'); |
417
|
|
|
} |
418
|
|
|
return ''; |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* Make the call using the client. |
424
|
|
|
* |
425
|
|
|
* @param string $module The module to use |
426
|
|
|
* @param string $command Command to call |
427
|
|
|
* @param array $params Options |
|
|
|
|
428
|
|
|
* @param \SimpleXMLElement|string $data Data to send [optional] |
|
|
|
|
429
|
|
|
* @param array $options Options to add for configurations [optional] |
|
|
|
|
430
|
|
|
* |
431
|
|
|
* @return Response |
432
|
|
|
*/ |
433
|
|
|
public function call($module, $command, $getParams = array(), $postParams = array()) |
434
|
|
|
{ |
435
|
|
|
$getParams['authtoken'] = $this->authtoken; |
436
|
|
|
$getParams['scope'] = 'crmapi'; |
437
|
|
|
|
438
|
|
|
$uri = $this->getRequestURI($module, $command); |
439
|
|
|
|
440
|
|
|
// $params = $this->getRequestBody([], $postParams, []); |
|
|
|
|
441
|
|
|
// $stream = \GuzzleHttp\Psr7\stream_for($params); |
442
|
|
|
$response = $this->zohoRestClient->request('POST', $uri, ['query'=>$getParams+$postParams]); |
443
|
|
|
$zohoResponse = new Response((string)$response->getBody(), $module, $command); |
444
|
|
|
if ($zohoResponse->ifSuccess()) { |
445
|
|
|
return $zohoResponse; |
446
|
|
|
} else { |
447
|
|
|
throw new ZohoCRMResponseException($zohoResponse); |
448
|
|
|
} |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* Get the current request uri. |
453
|
|
|
* |
454
|
|
|
* @param $module The module to use |
455
|
|
|
* @param string $command Command for get uri |
456
|
|
|
* |
457
|
|
|
* @return string |
458
|
|
|
*/ |
459
|
|
|
protected function getRequestURI($module, $command) |
460
|
|
|
{ |
461
|
|
|
if (empty($module)) { |
462
|
|
|
throw new \RuntimeException('Zoho CRM module is not set.'); |
463
|
|
|
} |
464
|
|
|
$parts = array(self::BASE_URI, $this->format, $module, $command); |
465
|
|
|
|
466
|
|
|
return implode('/', $parts); |
467
|
|
|
} |
468
|
|
|
} |
469
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.