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