-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathliquidplanner.php
executable file
·543 lines (499 loc) · 19.6 KB
/
liquidplanner.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Library to provide simple access to the Liquid Planner API
*
* The public methods are structured to mimic the API routes published
* by Liquid Planner at https://app.liquidplanner.com/api/help/urls.
* Method names follow the route sequence with the common leading
* elements generalised away, so for example this API route:
* api/workspaces/:workspace_id/tasks/:id/track_time
* is exposed as a public method called:
* tasks_track_time()
*
* Convenience methods for "create" and "delete" are also provided
* even though they are implied in the API routes and not explicitly
* named, so for example to create or delete tasks you can simply call:
* tasks_create()
* tasks_delete()
*
* @author Jonathan Oxer <[email protected]>
* @copyright 2011 Internet Vision Technologies <www.ivt.com.au>
* @version 2011-08-08
*/
class LiquidPlanner
{
private $email = '';
private $password = '';
private $serviceurl = '';
public $debug = false;
/**
* Constructor
*/
public function __construct($workspaceID, $email, $password)
{
$this->email = $email;
$this->password = $password;
$this->baseurl = "https://app.liquidplanner.com/api";
$this->serviceurl = $this->baseurl . "/workspaces/".$workspaceID;
}
/**
* Deletes a comment on a client from Liquid Planner
*
* Pass the ID of a comment in Liquid Planner into this method and it
* will be deleted from the workspace. The raw response from the
* web service is returned so you can examine the result.
*
* @param int $clientId the ID of the client in Liquid Planner
* @param int $commentId the ID of the comment in Liquid Planner
*
* @return string raw response from the API
*
* @access public
*/
public function clients_comments_delete($clientId, $commentId)
{
$url = $this->serviceurl.'/clients/'.$clientId.'/comments/'.$commentId;
$response = $this->lp_delete($url);
return($response);
}
/**
* Retrieves the specified task or a list of all tasks
*
* @param int $taskid ID of task.
* @param array Parameters to send such as date and count limiters.
*
* @return array Response from Liquid Planner
*
* @access public
*/
public function tasks($taskid=NULL, $params=array())
{
$url = $this->serviceurl.'/tasks'.($taskid ? '/'.$taskid : '').($params ? '?'.http_build_query($params) : '');
$response = $this->lp_get($url);
return($response);
}
/**
* Retrieves timesheets optionally filtered by parameters.
*
* @param array Parameters to send such as date and count limiters.
*
* @return array Response from Liquid Planner
*
* @access public
*/
public function timesheets($params=array())
{
$url = $this->serviceurl.'/timesheets/'.($params ? '?'.http_build_query($params) : '');
$response = $this->lp_get($url);
return($response);
}
/**
* Retrieves timesheet entries optionally filtered by parameters.
*
* @param array Parameters to send such as date and count limiters. Documentation here: http://www.liquidplanner.com/api-guide/technical-reference/filtering-timesheet-entries.html
*
* @return array Response from Liquid Planner
*
* @access public
*/
public function timesheet_entries($timesheetid=NULL, $params=array())
{
$url = $this->serviceurl.($timesheetid ? '/timesheets/'.$timesheetid : '').'/timesheet_entries'.($params ? '?'.http_build_query($params) : '');
$response = $this->lp_get($url);
return($response);
}
/**
* Creates a new task in Liquid Planner
*
* @param array $data values to apply to the newly created task
*
* @return array Response from Liquid Planner
*
* @access public
*/
public function tasks_create(array $data)
{
$encodedTask = json_encode(array('task' => $data));
$url = $this->serviceurl.'/tasks';
$response = $this->lp_post($url, $encodedTask);
return($response);
}
/**
* Deletes a task from Liquid Planner
*
* Pass the ID of a task in Liquid Planner into this method and it
* will be deleted from the workspace. The raw response from the
* web service is returned so you can examine the result.
*
* @param int $id the ID of the task in Liquid Planner
*
* @return string raw response from the API
*
* @access public
*/
public function tasks_delete($id)
{
$url = $this->serviceurl.'/tasks/'.$id;
$response = $this->lp_delete($url);
return($response);
}
/**
* Updates task time values, such as work completed and estimates
*
* @param array $data values to apply to the specified task
* @param int $taskid ID of Liquid Planner task to update
*
* @return array Response from Liquid Planner
*
* @access public
*/
public function tasks_track_time(array $data, $taskid)
{
$encodedTask = json_encode($data);
$url = $this->serviceurl.'/tasks/'.$taskid.'/track_time';
$response = $this->lp_post($url, $encodedTask);
return($response);
}
/**
* Creates a new comment on a task in Liquid Planner
*
* @param array $data values to apply to the newly created comment
* @param int $taskid ID of Liquid Planner task to link to comment
*
* @return array Response from Liquid Planner
*
* @access public
*/
public function tasks_comments_create(array $data, $taskid)
{
$encodedData = json_encode(array('comment' => $data));
$url = $this->serviceurl.'/tasks/'.$taskid.'/comments';
$response = $this->lp_post($url, $encodedData);
return($response);
}
/**
* Retrieves the logged in user's account information.
*
* @return array Response from Liquid Planner
*
* @access public
*/
public function account()
{
$url = $this->baseurl.'/account';
$response = $this->lp_get($url);
return($response);
}
/**
* Retrieves the current workspace details.
*
* @return array Response from Liquid Planner
*
* @access public
*/
public function workspace()
{
$url = $this->serviceurl;
$response = $this->lp_get($url);
return($response);
}
/**
* Retrieves the specified client or a list of clients
*
* @param int $clientid ID of client.
*
* @return array Response from Liquid Planner
*
* @access public
*/
public function clients($clientid=NULL)
{
$url = $this->serviceurl.'/clients'.($clientid ? '/'.$clientid : '');
$response = $this->lp_get($url);
return($response);
}
/**
* Creates a new client in Liquid Planner
*
* @param string $name name of this client
* @param string $description plain-text description of the client
* @param string $external_ref arbitrary string; use e.g. to store a reference ID from an external system
*
* @return array Response from Liquid Planner
*
* @access public
*/
public function clients_create($name, $description = '', $external_ref = '')
{
$encodedClient = json_encode(array('client' => array(
'name' => $name,
'description' => $description,
'external_reference' => $external_ref
)));
$url = $this->serviceurl.'/clients';
$response = $this->lp_post($url, $encodedClient);
return($response);
}
/**
* Gets a list of comments on a client from Liquid Planner
*
* @param int $clientid ID of Liquid Planner client to get comments from
* @param int $commentid ID of Liquid Planner client comment to get
*
* @return array Response from Liquid Planner
*
* @access public
*/
function clients_comments($clientid=NULL, $commentid=NULL)
{
$url = $this->serviceurl.'/clients/'.$clientid.'/comments'.($commentid ? '/'.$commentid : '');
echo $url;
return $this->lp_get($url);
}
/**
* Retrieves the specified member or a list of members
*
* @return array Response from Liquid Planner
*
* @access public
*/
public function members($memberid=NULL)
{
$url = $this->serviceurl.'/members'.($memberid? '/'.$memberid : '');
$response = $this->lp_get($url);
return($response);
}
/**
* Retrieves one member
*
* @return array Response from Liquid Planner
*
* @access public
*/
public function member($memberid)
{
return($this->members($memberid));
}
/**
* Creates a new project in Liquid Planner
*
* @param string $name name of this project
* @param int $client_id client ID associated with project
* @param int $parent_id parent ID associated with project
* @param string $description plain-text description of the project
* @param bool $is_done whether the project is done or not
* @param string $done_on date the project was done on
* @param string $external_reference
*
* @return array Response from Liquid Planner
*
* @access public
*/
public function projects_create($name, $client_id, $parent_id, $description = '', $is_done = false, $done_on = '', $external_reference = '')
{
$encodedClient = json_encode(array('project' => array(
'name' => $name,
'client_id' => $client_id,
'parent_id' => $parent_id,
'description' => $description,
'is_done' => $is_done,
'done_on' => $done_on,
'external_reference' => $external_reference
)));
$url = $this->serviceurl.'/projects';
$response = $this->lp_post($url, $encodedClient);
return($response);
}
/**
* Retrieves the specified project or a list of projects
*
* @param int $projectid ID of project
*
* @return array Response from Liquid Planner
*
* @access public
*/
public function projects($projectid=NULL)
{
$url = $this->serviceurl.'/projects'.($projectid ? '/'.$projectid : '');
$response = $this->lp_get($url);
return($response);
}
/**
* Retrieves the specified activity or a list of activities
*
* @param int $activityid ID of activity.
*
* @return array Response from Liquid Planner
*
* @access public
*/
function activities($activityid=NULL)
{
$url = $this->serviceurl.'/activities'.($activityid ? '/'.$activityid : '');
$response = $this->lp_get($url);
return($response);
}
/**
* Creates a new activity in Liquid Planner
*
* @param array $data Values to apply to the newly created activity
*
* @return array Response from Liquid Planner
*
* @access public
*/
public function activities_create(array $data)
{
return array("Not yet implemented - expected soon");
$encodedActivity = json_encode(array('activity' => $data));
$url = $this->serviceurl.'/activities';
$response = $this->lp_post($url, $encodedActivity);
return($response);
}
/**************************************************************/
function clients_dependencies(array $data, $id=NULL)
{ return array("Not yet implemented"); }
/**************************************************************/
/**
* Send data to the Liquid Planner API as a POST method with a
* JSON-encoded payload
*/
private function lp_post($url, $encodedTask)
{
/* Set up the CURL object and execute it */
$conn = curl_init();
curl_setopt($conn, CURLOPT_HEADER, false); // Suppress display of the response header
curl_setopt($conn, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); // Must submit as JSON
curl_setopt($conn, CURLOPT_RETURNTRANSFER, true); // Return result as a string
curl_setopt($conn, CURLOPT_POST, true); // Submit data as an HTTP POST
curl_setopt($conn, CURLOPT_POSTFIELDS, $encodedTask); // Set the POST field values
curl_setopt($conn, CURLOPT_ENCODING, ""); // Prevent GZIP compression of response from LP
curl_setopt($conn, CURLOPT_USERPWD, $this->email.":".$this->password); // Authenticate
curl_setopt($conn, CURLOPT_URL, $url); // Set the service URL
curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, false); // Accept any SSL certificate
$response = curl_exec($conn);
curl_close($conn);
/* The response is JSON, so decode it and return the result as an array */
$results = json_decode($response, true);
/* Check for Throttling from the API */
if((isset($results['type']) && $results['type'] == "Error") && (isset($results['error']) && $results['error'] == "Throttled"))
{
//We're being throttled. Wait the right amount of time and call it again.
$this->throttle_message($results);
sleep($this->get_wait_time($results['message']));
return $this->lp_post($url, $encodedTask);
}
return $results;
}
/**
* Send data to the Liquid Planner API as a PUT method with a
* JSON-encoded payload
*/
private function lp_put($url, $task)
{
/* Set up the CURL object and execute it */
$conn = curl_init();
curl_setopt($conn, CURLOPT_HEADER, false); // Suppress display of the response header
curl_setopt($conn, CURLOPT_RETURNTRANSFER, true); // Return result as a string
curl_setopt($conn, CURLOPT_POST, false); // Submit data as an HTTP POST
curl_setopt($conn, CURLOPT_CUSTOMREQUEST, 'PUT'); // Submit data as an HTTP POST
curl_setopt($conn, CURLOPT_POSTFIELDS, http_build_query($task)); // Set the POST field values
curl_setopt($conn, CURLOPT_ENCODING, ""); // Prevent GZIP compression of response from LP
curl_setopt($conn, CURLOPT_USERPWD, $this->email.":".$this->password); // Authenticate
curl_setopt($conn, CURLOPT_URL, $url); // Set the service URL
curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, false); // Accept any SSL certificate
$response = curl_exec($conn);
curl_close($conn);
/* The response is JSON, so decode it and return the result as an array */
$results = json_decode($response, true);
/* Check for Throttling from the API */
if((isset($results['type']) && $results['type'] == "Error") && (isset($results['error']) && $results['error'] == "Throttled"))
{
//We're being throttled. Wait the right amount of time and call it again.
$this->throttle_message($results);
sleep($this->get_wait_time($results['message']));
return $this->lp_put($url, $task);
}
return $results;
}
/**
* Send data to the Liquid Planner API as a GET method
*/
private function lp_get($url)
{
/* Set up the CURL object and execute it */
$conn = curl_init();
curl_setopt($conn, CURLOPT_HEADER, false); // Suppress display of the response header
curl_setopt($conn, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); // Must submit as JSON
curl_setopt($conn, CURLOPT_RETURNTRANSFER, true); // Return result as a string
curl_setopt($conn, CURLOPT_POST, false); // Submit data as an HTTP POST
curl_setopt($conn, CURLOPT_ENCODING, ""); // Prevent GZIP compression of response from LP
curl_setopt($conn, CURLOPT_USERPWD, $this->email.":".$this->password); // Authenticate
curl_setopt($conn, CURLOPT_URL, $url); // Set the service URL
curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, false); // Accept any SSL certificate
$response = curl_exec($conn);
curl_close($conn);
/* The response is JSON, so decode it and return the result as an array */
$results = json_decode($response, true);
/* Check for Throttling from the API */
if((isset($results['type']) && $results['type'] == "Error") && (isset($results['error']) && $results['error'] == "Throttled"))
{
//We're being throttled. Wait the right amount of time and call it again.
$this->throttle_message($results);
sleep($this->get_wait_time($results['message']));
return $this->lp_get($url);
}
return $results;
}
/**
* Send data to the Liquid Planner API as a DELETE method
*/
private function lp_delete($url)
{
/* Set up the CURL object and execute it */
$conn = curl_init();
curl_setopt($conn, CURLOPT_HEADER, false); // Suppress display of the response header
curl_setopt($conn, CURLOPT_RETURNTRANSFER, true); // Return result as a string
curl_setopt($conn, CURLOPT_CUSTOMREQUEST, "DELETE"); // Connect as an HTTP DELETE
curl_setopt($conn, CURLOPT_ENCODING, ""); // Prevent GZIP compression of response from LP
curl_setopt($conn, CURLOPT_USERPWD, $this->email.":".$this->password); // Authenticate
curl_setopt($conn, CURLOPT_URL, $url); // Set the service URL
curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, false); // Accept any SSL certificate
$response = curl_exec($conn);
curl_close($conn);
$results = json_decode($response, true);
/* Check for Throttling from the API */
if((isset($results['type']) && $results['type'] == "Error") && (isset($results['error']) && $results['error'] == "Throttled"))
{
//We're being throttled. Wait the right amount of time and call it again.
$this->throttle_message($results);
sleep($this->get_wait_time($results['message']));
return $this->lp_delete($url);
}
return $results;
}
private function throttle_message($results)
{
if($this->debug === true)
{
echo '<p class="throttled">API Throttling in effect. ' . $results['message'] . '</p>';
/* Clear the output buffer if it's turned on. */
if(ob_get_level() !== 0)
{
ob_flush();
flush();
}
}
}
private function get_wait_time($message)
{
$regexp = "/Try again in ([0-9]{1,}) seconds/";
preg_match($regexp, $message, $matches);
if(is_numeric($matches[1]))
return $matches[1] + 2;
else
return 15;
}
}