1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LPTracker; |
4
|
|
|
|
5
|
|
|
use LPTracker\models\Comment; |
6
|
|
|
use LPTracker\models\Contact; |
7
|
|
|
use LPTracker\models\ContactField; |
8
|
|
|
use LPTracker\models\Custom; |
9
|
|
|
use LPTracker\models\Lead; |
10
|
|
|
use LPTracker\models\Project; |
11
|
|
|
use LPTracker\exceptions\LPTrackerSDKException; |
12
|
|
|
use LPTracker\authentication\AccessToken; |
13
|
|
|
use LPTracker\models\View; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class LPTracker |
17
|
|
|
* @package LPTracker |
18
|
|
|
*/ |
19
|
|
|
class LPTracker extends LPTrackerBase |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param $login |
24
|
|
|
* @param $password |
25
|
|
|
* @param string $serviceName |
26
|
|
|
* |
27
|
|
|
* @return AccessToken |
28
|
|
|
* @throws LPTrackerSDKException |
29
|
|
|
*/ |
30
|
|
|
public function login($login, $password, $serviceName = '') |
31
|
|
|
{ |
32
|
|
|
if (empty($login)) { |
33
|
|
|
throw new LPTrackerSDKException('Login is empty'); |
34
|
|
|
} |
35
|
|
|
if (empty($password)) { |
36
|
|
|
throw new LPTrackerSDKException('Password is empty'); |
37
|
|
|
} |
38
|
|
|
if (empty($serviceName)) { |
39
|
|
|
$serviceName = LPTrackerBase::DEFAULT_SERVICE_NAME; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$response = LPTrackerRequest::sendRequest('/login', [ |
43
|
|
|
'login' => $login, |
44
|
|
|
'password' => $password, |
45
|
|
|
'service' => $serviceName, |
46
|
|
|
'version' => LPTrackerBase::VERSION |
47
|
|
|
], 'POST', null, $this->address); |
48
|
|
|
|
49
|
|
|
$accessToken = new AccessToken($response['token']); |
50
|
|
|
|
51
|
|
|
return $accessToken; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @throws exceptions\LPTrackerResponseException |
57
|
|
|
* @throws exceptions\LPTrackerServerException |
58
|
|
|
*/ |
59
|
|
|
public function logout() |
60
|
|
|
{ |
61
|
|
|
LPTrackerRequest::sendRequest('/logout', [], 'POST', $this->token, $this->address); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return Project[] |
67
|
|
|
* @throws exceptions\LPTrackerResponseException |
68
|
|
|
* @throws exceptions\LPTrackerServerException |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
public function getProjectList() |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$response = LPTrackerRequest::sendRequest('/projects', [], 'GET', $this->token, $this->address); |
73
|
|
|
|
74
|
|
|
$result = []; |
75
|
|
|
foreach ($response as $projectData) { |
76
|
|
|
$result[] = new Project($projectData); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $result; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param $id |
85
|
|
|
* |
86
|
|
|
* @return Project |
87
|
|
|
* @throws exceptions\LPTrackerResponseException |
88
|
|
|
* @throws exceptions\LPTrackerServerException |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
public function getProject($id) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$url = '/project/'.$id; |
93
|
|
|
|
94
|
|
|
$response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address); |
95
|
|
|
|
96
|
|
|
$project = new Project($response); |
97
|
|
|
|
98
|
|
|
return $project; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param $project |
104
|
|
|
* |
105
|
|
|
* @return Custom[] |
106
|
|
|
* @throws exceptions\LPTrackerResponseException |
107
|
|
|
* @throws exceptions\LPTrackerServerException |
108
|
|
|
*/ |
109
|
|
|
public function getProjectCustoms($project) |
110
|
|
|
{ |
111
|
|
|
if ($project instanceof Project) { |
112
|
|
|
$project = $project->getId(); |
113
|
|
|
} else { |
114
|
|
|
$project = intval($project); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$url = '/project/'.$project.'/customs'; |
118
|
|
|
|
119
|
|
|
$response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address); |
120
|
|
|
|
121
|
|
|
$result = []; |
122
|
|
|
foreach ($response as $customData) { |
123
|
|
|
$result[] = new Custom($customData); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $result; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param $project |
132
|
|
|
* |
133
|
|
|
* @return ContactField[] |
134
|
|
|
* @throws exceptions\LPTrackerResponseException |
135
|
|
|
* @throws exceptions\LPTrackerServerException |
136
|
|
|
*/ |
137
|
|
|
public function getProjectFields($project) |
138
|
|
|
{ |
139
|
|
|
if ($project instanceof Project) { |
140
|
|
|
$project = $project->getId(); |
141
|
|
|
} else { |
142
|
|
|
$project = intval($project); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$url = '/project/'.$project.'/fields'; |
146
|
|
|
|
147
|
|
|
$response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address); |
148
|
|
|
|
149
|
|
|
$result = []; |
150
|
|
|
foreach ($response as $customData) { |
151
|
|
|
$result[] = new ContactField($customData); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $result; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param $project |
159
|
|
|
* @param $callbackUrl |
160
|
|
|
* |
161
|
|
|
* @throws exceptions\LPTrackerResponseException |
162
|
|
|
* @throws exceptions\LPTrackerServerException |
163
|
|
|
* @author Yuri Nazarenko / rezident <[email protected]> |
164
|
|
|
*/ |
165
|
|
|
public function setProjectCallbackUrl($project, $callbackUrl) |
166
|
|
|
{ |
167
|
|
|
if($project instanceof Project) { |
168
|
|
|
$project = $project->getId(); |
169
|
|
|
} else { |
170
|
|
|
$project = intval($project); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$url = '/project/' . $project . '/callback-url'; |
174
|
|
|
LPTrackerRequest::sendRequest($url, ['url' => $callbackUrl], 'PUT', $this->token, $this->address); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param $project |
180
|
|
|
* @param array $details |
181
|
|
|
* @param array $contactData |
182
|
|
|
* @param array $fields |
183
|
|
|
* |
184
|
|
|
* @return Contact |
185
|
|
|
* @throws LPTrackerSDKException |
186
|
|
|
*/ |
187
|
|
|
public function createContact( |
188
|
|
|
$project, |
189
|
|
|
array $details, |
190
|
|
|
array $contactData = [], |
191
|
|
|
array $fields = [] |
192
|
|
|
) { |
193
|
|
|
if (empty($details)) { |
194
|
|
|
throw new LPTrackerSDKException('Contact details can not be empty'); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
if ($project instanceof Project) { |
198
|
|
|
$project = $project->getId(); |
199
|
|
|
} else { |
200
|
|
|
$project = intval($project); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$contactData['project_id'] = $project; |
204
|
|
|
$contactData['details'] = $details; |
205
|
|
|
|
206
|
|
|
$fieldsArr = []; |
207
|
|
|
foreach ($fields as $fieldId => $fieldValue) { |
208
|
|
|
if ($fieldValue instanceof ContactField) { |
209
|
|
|
$fieldId = $fieldValue->getId(); |
210
|
|
|
$fieldValue = $fieldValue->getValue(); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
$fieldsArr[$fieldId] = $fieldValue; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
$contact = new Contact($contactData); |
217
|
|
|
$contact->validate(); |
218
|
|
|
|
219
|
|
|
$data = $contact->toArray(); |
220
|
|
|
$data['fields'] = $fieldsArr; |
221
|
|
|
|
222
|
|
|
$response = LPTrackerRequest::sendRequest('/contact', $data, 'POST', $this->token, $this->address); |
223
|
|
|
|
224
|
|
|
$resultContact = new Contact($response); |
225
|
|
|
|
226
|
|
|
return $resultContact; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param $contact |
232
|
|
|
* |
233
|
|
|
* @return Contact |
234
|
|
|
* @throws LPTrackerSDKException |
235
|
|
|
*/ |
236
|
|
View Code Duplication |
public function getContact($contact) |
|
|
|
|
237
|
|
|
{ |
238
|
|
|
if ($contact instanceof Contact) { |
239
|
|
|
$contact = $contact->getId(); |
240
|
|
|
} else { |
241
|
|
|
$contact = intval($contact); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
if ($contact <= 0) { |
245
|
|
|
throw new LPTrackerSDKException('Invalid contact ID'); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
$url = '/contact/'.$contact; |
249
|
|
|
|
250
|
|
|
$response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address); |
251
|
|
|
|
252
|
|
|
$resultContact = new Contact($response); |
253
|
|
|
|
254
|
|
|
return $resultContact; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param Contact $contact |
260
|
|
|
* |
261
|
|
|
* @return Contact |
262
|
|
|
* @throws LPTrackerSDKException |
263
|
|
|
*/ |
264
|
|
|
public function saveContact(Contact $contact) |
265
|
|
|
{ |
266
|
|
|
if ( ! $contact->validate()) { |
267
|
|
|
throw new LPTrackerSDKException('Invalid contact'); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
$data = $contact->toArray(); |
271
|
|
|
if ( ! empty($data['fields'])) { |
272
|
|
|
$fields = []; |
273
|
|
|
foreach ($data['fields'] as $field) { |
274
|
|
|
$fields[$field['id']] = $field['value']; |
275
|
|
|
} |
276
|
|
|
$data['fields'] = $fields; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
if ($contact->getId() > 0) { |
280
|
|
|
$url = '/contact/'.$contact->getId(); |
281
|
|
|
|
282
|
|
|
$response = LPTrackerRequest::sendRequest($url, $contact->toArray(), 'PUT', $this->token, $this->address); |
283
|
|
|
} else { |
284
|
|
|
$response = LPTrackerRequest::sendRequest('/contact', $contact->toArray(), 'POST', $this->token, |
285
|
|
|
$this->address); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
$resultContact = new Contact($response); |
289
|
|
|
|
290
|
|
|
return $resultContact; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @param $contactId |
296
|
|
|
* @param array $details |
297
|
|
|
* @param array $contactData |
298
|
|
|
* @param array $fields |
299
|
|
|
* |
300
|
|
|
* @return Contact |
301
|
|
|
* @throws LPTrackerSDKException |
302
|
|
|
*/ |
303
|
|
|
public function editContact( |
304
|
|
|
$contactId, |
305
|
|
|
array $details, |
306
|
|
|
array $contactData = [], |
307
|
|
|
array $fields = [] |
308
|
|
|
) { |
309
|
|
|
if (empty($details)) { |
310
|
|
|
throw new LPTrackerSDKException('Contact details can not be empty'); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
$contactData['id'] = $contactId; |
314
|
|
|
$contactData['details'] = $details; |
315
|
|
|
|
316
|
|
|
foreach ($fields as $fieldId => $fieldValue) { |
317
|
|
|
if ($fieldValue instanceof ContactField) { |
318
|
|
|
$fieldId = $fieldValue->getId(); |
319
|
|
|
$fieldValue = $fieldValue->getValue(); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
$contactData['fields'][] = [ |
323
|
|
|
'id' => $fieldId, |
324
|
|
|
'value' => $fieldValue |
325
|
|
|
]; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
$contact = new Contact($contactData); |
329
|
|
|
$contact->validate(); |
330
|
|
|
|
331
|
|
|
return $this->saveContact($contact); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* @param $project |
337
|
|
|
* @param array $searchOptions |
338
|
|
|
* |
339
|
|
|
* @return array |
340
|
|
|
* @throws LPTrackerSDKException |
341
|
|
|
*/ |
342
|
|
|
public function searchContacts($project, array $searchOptions = []) |
343
|
|
|
{ |
344
|
|
|
if ($project instanceof Project) { |
345
|
|
|
$project = $project->getId(); |
346
|
|
|
} |
347
|
|
|
$project = intval($project); |
348
|
|
|
if ($project <= 0) { |
349
|
|
|
throw new LPTrackerSDKException('Invalid project id'); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
$url = '/contact/search?'; |
353
|
|
|
|
354
|
|
|
$data = [ |
355
|
|
|
'project_id' => $project |
356
|
|
|
]; |
357
|
|
|
if (isset($searchOptions['email'])) { |
358
|
|
|
$data['email'] = $searchOptions['email']; |
359
|
|
|
} |
360
|
|
|
if (isset($searchOptions['phone'])) { |
361
|
|
|
$data['phone'] = $searchOptions['phone']; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
$url = $url.http_build_query($data); |
365
|
|
|
|
366
|
|
|
$response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address); |
367
|
|
|
|
368
|
|
|
$result = []; |
369
|
|
|
foreach ($response as $contactData) { |
370
|
|
|
$result[] = new Contact($contactData); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
return $result; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* @param $contact |
379
|
|
|
* |
380
|
|
|
* @return array |
381
|
|
|
* @throws LPTrackerSDKException |
382
|
|
|
*/ |
383
|
|
|
public function contactLeads($contact) |
384
|
|
|
{ |
385
|
|
|
if ($contact instanceof Contact) { |
386
|
|
|
$contact = $contact->getId(); |
387
|
|
|
} |
388
|
|
|
$contact = intval($contact); |
389
|
|
|
if ($contact <= 0) { |
390
|
|
|
throw new LPTrackerSDKException('Invalid contact id'); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
$url = '/contact/'.$contact.'/leads'; |
394
|
|
|
|
395
|
|
|
$response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address); |
396
|
|
|
|
397
|
|
|
$result = []; |
398
|
|
|
foreach ($response as $leadData) { |
399
|
|
|
$result[] = new Lead($leadData); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
return $result; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* @param $contact |
408
|
|
|
* @param $field |
409
|
|
|
* @param $newValue |
410
|
|
|
* |
411
|
|
|
* @return ContactField |
412
|
|
|
* @throws exceptions\LPTrackerResponseException |
413
|
|
|
* @throws exceptions\LPTrackerServerException |
414
|
|
|
*/ |
415
|
|
|
public function updateContactField($contact, $field, $newValue) |
416
|
|
|
{ |
417
|
|
|
if ($contact instanceof Contact) { |
418
|
|
|
$contact = $contact->getId(); |
419
|
|
|
} |
420
|
|
|
if ($field instanceof ContactField) { |
421
|
|
|
$field = $field->getId(); |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
$url = '/contact/'.$contact.'/field/'.$field; |
425
|
|
|
|
426
|
|
|
$data = [ |
427
|
|
|
'value' => $newValue |
428
|
|
|
]; |
429
|
|
|
|
430
|
|
|
$response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address); |
431
|
|
|
|
432
|
|
|
$contactField = new ContactField($response); |
433
|
|
|
|
434
|
|
|
return $contactField; |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* @param $project |
440
|
|
|
* @param array $viewData |
441
|
|
|
* |
442
|
|
|
* @return View |
443
|
|
|
* @throws LPTrackerSDKException |
444
|
|
|
*/ |
445
|
|
|
public function createView($project, array $viewData = []) |
446
|
|
|
{ |
447
|
|
|
if ($project instanceof Project) { |
448
|
|
|
$viewData['project_id'] = $project->getId(); |
449
|
|
|
} else { |
450
|
|
|
$viewData['project_id'] = intval($project); |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
$view = new View($viewData); |
454
|
|
|
if ( ! $view->validate()) { |
455
|
|
|
throw new LPTrackerSDKException('Invalid view data'); |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
$data = $view->toArray(); |
459
|
|
|
|
460
|
|
|
$response = LPTrackerRequest::sendRequest('/view', $data, 'POST', $this->token, $this->address); |
461
|
|
|
|
462
|
|
|
$resultView = new View($response); |
463
|
|
|
|
464
|
|
|
return $resultView; |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* @param $view |
470
|
|
|
* |
471
|
|
|
* @return View |
472
|
|
|
* @throws LPTrackerSDKException |
473
|
|
|
*/ |
474
|
|
View Code Duplication |
public function getView($view) |
|
|
|
|
475
|
|
|
{ |
476
|
|
|
if ($view instanceof View) { |
477
|
|
|
$view = $view->getId(); |
478
|
|
|
} else { |
479
|
|
|
$view = intval($view); |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
if ($view <= 0) { |
483
|
|
|
throw new LPTrackerSDKException('Invalid view ID'); |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
$url = '/view/'.$view; |
487
|
|
|
|
488
|
|
|
$response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address); |
489
|
|
|
|
490
|
|
|
$resultView = new View($response); |
491
|
|
|
|
492
|
|
|
return $resultView; |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
|
496
|
|
|
/** |
497
|
|
|
* @param View $view |
498
|
|
|
* |
499
|
|
|
* @return View |
500
|
|
|
* @throws LPTrackerSDKException |
501
|
|
|
*/ |
502
|
|
View Code Duplication |
public function saveView(View $view) |
|
|
|
|
503
|
|
|
{ |
504
|
|
|
if ( ! $view->validate()) { |
505
|
|
|
throw new LPTrackerSDKException('Invalid view'); |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
if ($view->getId() > 0) { |
509
|
|
|
$url = '/view/'.$view->getId(); |
510
|
|
|
|
511
|
|
|
$response = LPTrackerRequest::sendRequest($url, $view->toArray(), 'PUT', $this->token, $this->address); |
512
|
|
|
} else { |
513
|
|
|
$response = LPTrackerRequest::sendRequest('/view', $view->toArray(), 'POST', $this->token, $this->address); |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
$resultView = new View($response); |
517
|
|
|
|
518
|
|
|
return $resultView; |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
|
522
|
|
|
/** |
523
|
|
|
* @param $viewId |
524
|
|
|
* @param array $viewData |
525
|
|
|
* |
526
|
|
|
* @return View |
527
|
|
|
* @throws LPTrackerSDKException |
528
|
|
|
*/ |
529
|
|
|
public function editView($viewId, array $viewData = []) |
530
|
|
|
{ |
531
|
|
|
$viewData['id'] = $viewId; |
532
|
|
|
|
533
|
|
|
$view = new View($viewData); |
534
|
|
|
$view->validate(); |
535
|
|
|
|
536
|
|
|
return $this->saveView($view); |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
|
540
|
|
|
/** |
541
|
|
|
* @param $contact |
542
|
|
|
* @param array $leadData |
543
|
|
|
* @param array $options |
544
|
|
|
* |
545
|
|
|
* @return Lead |
546
|
|
|
* @throws LPTrackerSDKException |
547
|
|
|
*/ |
548
|
|
|
public function createLead($contact, array $leadData = [], array $options = []) |
549
|
|
|
{ |
550
|
|
|
if ($contact instanceof Contact) { |
551
|
|
|
$leadData['contact_id'] = $contact->getId(); |
552
|
|
|
} else { |
553
|
|
|
$leadData['contact_id'] = intval($contact); |
554
|
|
|
} |
555
|
|
|
|
556
|
|
|
$lead = new Lead($leadData); |
557
|
|
|
if ( ! $lead->validate()) { |
558
|
|
|
throw new LPTrackerSDKException('Invalid lead data'); |
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
if ( ! empty($lead->getView()) && empty($lead->getView()->getId())) { |
562
|
|
|
$contactModel = $this->getContact($contact); |
563
|
|
|
$viewData = $lead->getView()->toArray(); |
564
|
|
|
$lead->setView($this->createView($contactModel->getProjectId(), $viewData)); |
565
|
|
|
} |
566
|
|
|
|
567
|
|
|
$data = $lead->toArray(true); |
568
|
|
|
if (isset($options['callback'])) { |
569
|
|
|
$data['callback'] = $options['callback'] ? true : false; |
570
|
|
|
} |
571
|
|
|
if (isset($leadData['view_id'])) { |
572
|
|
|
$data['view_id'] = intval($leadData['view_id']); |
573
|
|
|
} elseif ( ! empty($lead->getView()) && ! empty($lead->getView()->getId())) { |
574
|
|
|
$data['view_id'] = $lead->getView()->getId(); |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
$response = LPTrackerRequest::sendRequest('/lead', $data, 'POST', $this->token, $this->address); |
578
|
|
|
|
579
|
|
|
$resultLead = new Lead($response); |
580
|
|
|
|
581
|
|
|
return $resultLead; |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
|
585
|
|
|
/** |
586
|
|
|
* @param $lead |
587
|
|
|
* |
588
|
|
|
* @return Lead |
589
|
|
|
* @throws LPTrackerSDKException |
590
|
|
|
*/ |
591
|
|
View Code Duplication |
public function getLead($lead) |
|
|
|
|
592
|
|
|
{ |
593
|
|
|
if ($lead instanceof Lead) { |
594
|
|
|
$lead = $lead->getId(); |
595
|
|
|
} else { |
596
|
|
|
$lead = intval($lead); |
597
|
|
|
} |
598
|
|
|
|
599
|
|
|
if ($lead <= 0) { |
600
|
|
|
throw new LPTrackerSDKException('Invalid lead ID'); |
601
|
|
|
} |
602
|
|
|
|
603
|
|
|
$url = '/lead/'.$lead; |
604
|
|
|
|
605
|
|
|
$response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address); |
606
|
|
|
|
607
|
|
|
$resultLead = new Lead($response); |
608
|
|
|
|
609
|
|
|
return $resultLead; |
610
|
|
|
} |
611
|
|
|
|
612
|
|
|
|
613
|
|
|
/** |
614
|
|
|
* @param Lead $lead |
615
|
|
|
* |
616
|
|
|
* @return Lead |
617
|
|
|
* @throws LPTrackerSDKException |
618
|
|
|
*/ |
619
|
|
View Code Duplication |
public function saveLead(Lead $lead) |
|
|
|
|
620
|
|
|
{ |
621
|
|
|
if ( ! $lead->validate()) { |
622
|
|
|
throw new LPTrackerSDKException('Invalid lead'); |
623
|
|
|
} |
624
|
|
|
|
625
|
|
|
if ($lead->getId() > 0) { |
626
|
|
|
$url = '/lead/'.$lead->getId(); |
627
|
|
|
|
628
|
|
|
$response = LPTrackerRequest::sendRequest($url, $lead->toArray(true), 'PUT', $this->token, $this->address); |
629
|
|
|
} else { |
630
|
|
|
$response = LPTrackerRequest::sendRequest('/lead', $lead->toArray(true), 'POST', $this->token, |
631
|
|
|
$this->address); |
632
|
|
|
} |
633
|
|
|
|
634
|
|
|
$resultLead = new Lead($response); |
635
|
|
|
|
636
|
|
|
return $resultLead; |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
|
640
|
|
|
/** |
641
|
|
|
* @param $leadId |
642
|
|
|
* @param array $leadData |
643
|
|
|
* |
644
|
|
|
* @return Lead |
645
|
|
|
* @throws LPTrackerSDKException |
646
|
|
|
*/ |
647
|
|
|
public function editLead($leadId, array $leadData = []) |
648
|
|
|
{ |
649
|
|
|
$leadData['id'] = $leadId; |
650
|
|
|
|
651
|
|
|
$lead = new Lead($leadData); |
652
|
|
|
$lead->validate(); |
653
|
|
|
|
654
|
|
|
return $this->saveLead($lead); |
655
|
|
|
} |
656
|
|
|
|
657
|
|
|
|
658
|
|
|
/** |
659
|
|
|
* @param $lead |
660
|
|
|
* @param string $category |
661
|
|
|
* @param string $purpose |
662
|
|
|
* @param float $sum |
663
|
|
|
* |
664
|
|
|
* @return Lead |
665
|
|
|
* @throws LPTrackerSDKException |
666
|
|
|
*/ |
667
|
|
|
public function addLeadPayment($lead, $category, $purpose, $sum) |
668
|
|
|
{ |
669
|
|
|
if ($lead instanceof Lead) { |
670
|
|
|
$lead = $lead->getId(); |
671
|
|
|
} else { |
672
|
|
|
$lead = intval($lead); |
673
|
|
|
} |
674
|
|
|
|
675
|
|
|
if (empty($category)) { |
676
|
|
|
throw new LPTrackerSDKException('Category can not be empty'); |
677
|
|
|
} |
678
|
|
|
if (empty($purpose)) { |
679
|
|
|
throw new LPTrackerSDKException('Purpose can not be empty'); |
680
|
|
|
} |
681
|
|
|
$sum = floatval($sum); |
682
|
|
|
|
683
|
|
|
if ($sum <= 0) { |
684
|
|
|
throw new LPTrackerSDKException('Invalid sum'); |
685
|
|
|
} |
686
|
|
|
|
687
|
|
|
if ($lead <= 0) { |
688
|
|
|
throw new LPTrackerSDKException('Invalid lead ID'); |
689
|
|
|
} |
690
|
|
|
|
691
|
|
|
$url = '/lead/'.$lead.'/payment'; |
692
|
|
|
|
693
|
|
|
$data = [ |
694
|
|
|
'category' => $category, |
695
|
|
|
'purpose' => $purpose, |
696
|
|
|
'sum' => $sum, |
697
|
|
|
]; |
698
|
|
|
|
699
|
|
|
$response = LPTrackerRequest::sendRequest($url, $data, 'POST', $this->token, $this->address); |
700
|
|
|
|
701
|
|
|
$resultLead = new Lead($response); |
702
|
|
|
|
703
|
|
|
return $resultLead; |
704
|
|
|
} |
705
|
|
|
|
706
|
|
|
|
707
|
|
|
/** |
708
|
|
|
* @param $lead |
709
|
|
|
* @param $newFunnelId |
710
|
|
|
* |
711
|
|
|
* @return Lead |
712
|
|
|
* @throws LPTrackerSDKException |
713
|
|
|
*/ |
714
|
|
View Code Duplication |
public function changeLeadFunnel($lead, $newFunnelId) |
|
|
|
|
715
|
|
|
{ |
716
|
|
|
if ($lead instanceof Lead) { |
717
|
|
|
$lead = $lead->getId(); |
718
|
|
|
} |
719
|
|
|
|
720
|
|
|
$url = '/lead/'.$lead.'/funnel'; |
721
|
|
|
|
722
|
|
|
$data = [ |
723
|
|
|
'funnel' => $newFunnelId |
724
|
|
|
]; |
725
|
|
|
|
726
|
|
|
$response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address); |
727
|
|
|
|
728
|
|
|
$resultLead = new Lead($response); |
729
|
|
|
|
730
|
|
|
return $resultLead; |
731
|
|
|
} |
732
|
|
|
|
733
|
|
|
|
734
|
|
|
/** |
735
|
|
|
* @param $lead |
736
|
|
|
* |
737
|
|
|
* @return Comment[] |
738
|
|
|
* @throws exceptions\LPTrackerResponseException |
739
|
|
|
* @throws exceptions\LPTrackerServerException |
740
|
|
|
*/ |
741
|
|
|
public function getLeadComments($lead) |
742
|
|
|
{ |
743
|
|
|
if ($lead instanceof Lead) { |
744
|
|
|
$lead = $lead->getId(); |
745
|
|
|
} else { |
746
|
|
|
$lead = intval($lead); |
747
|
|
|
} |
748
|
|
|
|
749
|
|
|
$url = '/lead/'.$lead.'/comments'; |
750
|
|
|
|
751
|
|
|
$response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address); |
752
|
|
|
|
753
|
|
|
$result = []; |
754
|
|
|
foreach ($response as $commentData) { |
755
|
|
|
$result[] = new Comment($commentData); |
756
|
|
|
} |
757
|
|
|
|
758
|
|
|
return $result; |
759
|
|
|
} |
760
|
|
|
|
761
|
|
|
|
762
|
|
|
/** |
763
|
|
|
* @param $lead |
764
|
|
|
* @param $text |
765
|
|
|
* |
766
|
|
|
* @return Comment |
767
|
|
|
* @throws exceptions\LPTrackerResponseException |
768
|
|
|
* @throws exceptions\LPTrackerServerException |
769
|
|
|
*/ |
770
|
|
View Code Duplication |
public function addCommentToLead($lead, $text) |
|
|
|
|
771
|
|
|
{ |
772
|
|
|
if ($lead instanceof Lead) { |
773
|
|
|
$lead = $lead->getId(); |
774
|
|
|
} else { |
775
|
|
|
$lead = intval($lead); |
776
|
|
|
} |
777
|
|
|
|
778
|
|
|
$url = '/lead/'.$lead.'/comment'; |
779
|
|
|
|
780
|
|
|
$data = [ |
781
|
|
|
'text' => $text |
782
|
|
|
]; |
783
|
|
|
|
784
|
|
|
$response = LPTrackerRequest::sendRequest($url, $data, 'POST', $this->token, $this->address); |
785
|
|
|
|
786
|
|
|
$comment = new Comment($response); |
787
|
|
|
|
788
|
|
|
return $comment; |
789
|
|
|
} |
790
|
|
|
|
791
|
|
|
/** |
792
|
|
|
* @param Lead|int $lead |
793
|
|
|
* @param Custom|int $custom |
794
|
|
|
* @param string $absolutePath |
795
|
|
|
* |
796
|
|
|
* @throws exceptions\LPTrackerResponseException |
797
|
|
|
* @throws exceptions\LPTrackerServerException |
798
|
|
|
* |
799
|
|
|
* @author Yuri Nazarenko / rezident <[email protected]> |
800
|
|
|
*/ |
801
|
|
|
public function addFileToLead($lead, $custom, $absolutePath) |
802
|
|
|
{ |
803
|
|
|
$leadId = $lead instanceof Lead |
804
|
|
|
? $lead->getId() |
805
|
|
|
: (int)$lead; |
806
|
|
|
|
807
|
|
|
$customId = $custom instanceof Custom |
808
|
|
|
? $custom->getId() |
809
|
|
|
: (int)$custom; |
810
|
|
|
|
811
|
|
|
$url = '/lead/' . $leadId . '/file'; |
812
|
|
|
$data = [ |
813
|
|
|
'name' => pathinfo($absolutePath, PATHINFO_BASENAME), |
814
|
|
|
'mime' => mime_content_type($absolutePath), |
815
|
|
|
'data' => base64_encode(file_get_contents($absolutePath)), |
816
|
|
|
'custom_field_id' => $customId |
817
|
|
|
]; |
818
|
|
|
|
819
|
|
|
LPTrackerRequest::sendRequest($url, $data, 'POST', $this->token, $this->address); |
820
|
|
|
} |
821
|
|
|
|
822
|
|
|
/** |
823
|
|
|
* @param Custom $custom |
824
|
|
|
* |
825
|
|
|
* @return Custom |
826
|
|
|
* @throws LPTrackerSDKException |
827
|
|
|
*/ |
828
|
|
|
public function saveLeadCustom(Custom $custom) |
829
|
|
|
{ |
830
|
|
|
if ( ! $custom->validate() || empty($custom->getLeadId())) { |
831
|
|
|
throw new LPTrackerSDKException('Invalid custom'); |
832
|
|
|
} |
833
|
|
|
|
834
|
|
|
$url = '/lead/'.$custom->getLeadId().'/custom/'.$custom->getId(); |
835
|
|
|
|
836
|
|
|
$data = [ |
837
|
|
|
'value' => $custom->getValue() |
838
|
|
|
]; |
839
|
|
|
|
840
|
|
|
$response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address); |
841
|
|
|
|
842
|
|
|
$resultCustom = new Custom($response); |
843
|
|
|
|
844
|
|
|
return $resultCustom; |
845
|
|
|
} |
846
|
|
|
|
847
|
|
|
|
848
|
|
|
/** |
849
|
|
|
* @param $lead |
850
|
|
|
* @param $custom |
851
|
|
|
* @param $newValue |
852
|
|
|
* |
853
|
|
|
* @return Custom |
854
|
|
|
* @throws LPTrackerSDKException |
855
|
|
|
*/ |
856
|
|
|
public function editLeadCustom($lead, $custom, $newValue) |
857
|
|
|
{ |
858
|
|
|
if ($lead instanceof Lead) { |
859
|
|
|
$lead = $lead->getId(); |
860
|
|
|
} else { |
861
|
|
|
$lead = intval($lead); |
862
|
|
|
} |
863
|
|
|
|
864
|
|
|
if ($custom instanceof Custom) { |
865
|
|
|
if (empty($newValue)) { |
866
|
|
|
$newValue = $custom->getValue(); |
867
|
|
|
} |
868
|
|
|
$custom = $custom->getId(); |
869
|
|
|
} else { |
870
|
|
|
$custom = intval($custom); |
871
|
|
|
} |
872
|
|
|
|
873
|
|
|
$customModel = new Custom([ |
874
|
|
|
'id' => $custom, |
875
|
|
|
'lead_id' => $lead, |
876
|
|
|
'value' => $newValue |
877
|
|
|
]); |
878
|
|
|
$customModel->validate(); |
879
|
|
|
|
880
|
|
|
return $this->saveLeadCustom($customModel); |
881
|
|
|
} |
882
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.