-
Notifications
You must be signed in to change notification settings - Fork 0
/
SlimTimer.php
467 lines (417 loc) · 12.2 KB
/
SlimTimer.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
<?php
/**
* A class to interact with the SlimTimer API
*
* @author Chris Lock <[email protected]>
* @see http://slimtimer.com/help/api
* @see https://github.com/catharsisjelly/SlimTimerPHP
*/
class SlimTimer
{
// The URL to use
const MAIN_URL = 'http://slimtimer.com/';
// This API key is mine, feel free to use your own if you want
const API_KEY = 'ae4b927814a4844633f7df27f555b7';
/**
* Class var to hold the curl handle
* @var resource
*/
private $_ch;
/**
* The user ID that is used for each request
* @var int
*/
private $_userID = null;
/**
* The Access token that is grabbed from the authentication method
* @var string
*/
private $_accessToken = null;
public function __construct()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/xml'
));
$this->_ch = $ch;
}
/**
* Set the user ID and Access token
*
* @param int $userId
* @param string $token
*/
public function setUserAndToken($userID, $token)
{
$this->_userID = $userID;
$this->_accessToken = $token;
}
/**
* Authenticate the user and grab a token id for them, returns a keyed array of the user-id & token
*
* @param string $email
* @param string $password
* @param bool $setToken
* @return array
*/
public function authenticate($email, $password, $setToken = false)
{
$params = array(
'user' => array(
'email' => $email,
'password' => $password
),
'api_key' => self::API_KEY
);
curl_setopt($this->_ch, CURLOPT_POST, 1);
curl_setopt($this->_ch, CURLOPT_URL, self::MAIN_URL.'/users/token');
curl_setopt($this->_ch, CURLOPT_POSTFIELDS, http_build_query($params));
$content=curl_exec($this->_ch);
$xml = simplexml_load_string($content);
if(!$xml)
return false;
if($setToken === true)
{
$this->_accessToken = (string) $xml->{'access-token'};
$this->_userID = (int) $xml->{'user-id'};
}
return array(
'user-id' => (int) $xml->{'user-id'},
'token' => (string) $xml->{'access-token'}
);
}
/**
* List the tasks in your list
*
* @param bool $showCompleted
* @param array $role an array of values from owner,coworker,reporter
* @param int $offset
* @return obj|false
*/
public function listTasks($showCompleted = true, array $role = array('owner','coworker'), $offset = null)
{
$string = array();
$params = array(
'api_key' => self::API_KEY,
'access_token' => $this->_accessToken,
'show_completed' => ($showCompleted ? 'yes' : 'no'),
'role' => implode(',', $role),
'offset' => (is_int($offset) ? $offset : 0)
);
curl_setopt($this->_ch, CURLOPT_URL, self::MAIN_URL.'/users/'.$this->_userID.'/tasks?'.http_build_query($params));
curl_setopt($this->_ch, CURLOPT_HTTPGET, 1);
$content = curl_exec($this->_ch);
return $this->_tidyXML($content);
}
/**
* Create a task
*
* @param string $name
* @param array $tags
* @param array $coworkers
* @param array $reporters
* @return obj|false
*/
public function createTask($name, array $tags = array(), array $coworkers = array(), array $reporters = array())
{
if(empty($name))
throw new Exception('name parameter cannot be null');
$params = array(
'api_key' => self::API_KEY,
'access_token' => $this->_accessToken,
'task' => array(
'name' => $name,
'tags' => implode(',', $tags),
'coworker_emails' => implode(',', $coworkers),
'reporter_emails' => implode(',', $reporters)
)
);
curl_setopt($this->_ch, CURLOPT_URL, self::MAIN_URL.'/users/'.$this->_userID.'/tasks');
curl_setopt($this->_ch, CURLOPT_POST, 1);
curl_setopt($this->_ch, CURLOPT_POSTFIELDS, http_build_query($params));
$content = curl_exec($this->_ch);
return $this->_tidyXML($content);
}
/**
* Update a task
*
* @param int $task_id
* @param string $name
* @param array $tags
* @param array $coworkers
* @param array $reporters
* @param string $completed A date/time in the format YYYY-MM-DD HH-MM-SS
* @return obj|false
*/
public function updateTask($task_id, $name = null, array $tags = array(), array $coworkers = array(), array $reporters = array(), $completed = null)
{
$params = array(
'api_key' => self::API_KEY,
'access_token' => $this->_accessToken,
'task' => array(
'name' => $name,
'tags' => implode(',', $tags),
'coworker_emails' => implode(',', $coworkers),
'reporter_emails' => implode(',', $reporters),
'completed_on' => $this->_checkDate($completed)
)
);
curl_setopt($this->_ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($this->_ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($this->_ch, CURLOPT_URL, self::MAIN_URL.'/users/'.$this->_userID.'/tasks/'.$task_id);
$content = curl_exec($this->_ch);
return $this->_tidyXML($content);
}
/**
* Helper function to easily and quickly complete a task, by default complete it now unless you specify a date
*
* @param int $task_id
* @param string $date
* @return obj|false
*/
public function completeTask($task_id, $date = null)
{
if(null === $date)
$date = date('Y-m-d H:i:s');
return $this->updateTask($task_id, null, array(), array(), array(), $date);
}
/**
* Show a task
*
* @param int $task_id
* @return obj|false
*/
public function showTask($task_id)
{
$params = array(
'api_key' => self::API_KEY,
'access_token' => $this->_accessToken,
);
curl_setopt($this->_ch, CURLOPT_HTTPGET, 1);
curl_setopt($this->_ch, CURLOPT_URL, self::MAIN_URL.'/users/'.$this->_userID.'/tasks/'.$task_id.'?'.http_build_query($params));
$content = curl_exec($this->_ch);
return $this->_tidyXML($content);
}
/**
* Delete a task
*
* @param int $task_id
* @return bool
*/
public function deleteTask($task_id)
{
$params = array(
'api_key' => self::API_KEY,
'access_token' => $this->_accessToken,
);
curl_setopt($this->_ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($this->_ch, CURLOPT_URL, self::MAIN_URL.'/users/'.$this->_userID.'/tasks/'.$task_id.'?'.http_build_query($params));
$content = curl_exec($this->_ch);
if(!$content)
return true;
return false;
}
/**
* List the times for any particular task
*
* @param int $task_id
* @param string $startDate
* @param string $endDate
* @param string $offset
* @return obj|false
*/
public function listTimesForTask($task_id, $startDate = null, $endDate = null, $offset = null)
{
// /users/user_id/time_entries
$params = array(
'api_key' => self::API_KEY,
'access_token' => $this->_accessToken,
'timeentry' => array(
'range_start' => $this->_checkDate($startDate),
'range_end' => $this->_checkDate($endDate),
'offset' => (is_int($offset) ? $offset : 0)
)
);
curl_setopt($this->_ch, CURLOPT_URL, self::MAIN_URL.'/users/'.$this->_userID.'/tasks/'.$task_id.'/time_entries?'.http_build_query($params));
curl_setopt($this->_ch, CURLOPT_HTTPGET, 1);
$content = curl_exec($this->_ch);
return $this->_tidyXML($content);
}
/**
* List the timesentries in the account
*
* @param string $startDate
* @param string $endDate
* @param int $offset
* @return obj|false
*/
public function listTimes($startDate = null, $endDate = null, $offset = null)
{
// /users/user_id/time_entries
$params = array(
'api_key' => self::API_KEY,
'access_token' => $this->_accessToken,
'timeentry' => array(
'range_start' => $this->_checkDate($startDate),
'range_end' => $this->_checkDate($endDate),
'offset' => (is_int($offset) ? $offset : 0)
)
);
curl_setopt($this->_ch, CURLOPT_URL, self::MAIN_URL.'/users/'.$this->_userID.'/time_entries?'.http_build_query($params));
curl_setopt($this->_ch, CURLOPT_HTTPGET, 1);
$content = curl_exec($this->_ch);
return $this->_tidyXML($content);
}
/**
* Create a time entry
*
* @param int $task_id
* @param int $duration
* @param string $startTime DEFAULT=now
* @param string $endTime
* @param array $tags
* @param string $comments
* @param string $progress
* @return obj|false
*/
public function createTime($task_id, $duration, $startTime = null, $endTime = null, array $tags = array(), $comments = "", $progress = false)
{
if($duration <= 0)
throw new Exception('Duration must be more than 0');
if($startTime === null)
$startTime = date('Y-m-d H:i:s');
$params = array(
'api_key' => self::API_KEY,
'access_token' => $this->_accessToken,
'time_entry' => array(
// required
'start_time' => $this->_checkDate($startTime),
'task_id' => (int) $task_id,
'duration_in_seconds' => (int) $duration,
// extra
'end_time' => $this->_checkDate($endTime),
'tags' => implode(',', $tags),
'comments' => $comments,
'in_progress' => $progress,
)
);
curl_setopt($this->_ch, CURLOPT_URL, self::MAIN_URL.'/users/'.$this->_userID.'/time_entries');
curl_setopt($this->_ch, CURLOPT_POST, 1);
curl_setopt($this->_ch, CURLOPT_POSTFIELDS, http_build_query($params));
$content = curl_exec($this->_ch);
return $this->_tidyXML($content);
}
/**
* Show a time entry
*
* @param int $time_id
* @return obj|false
*/
public function showTime($time_id)
{
$params = array(
'api_key' => self::API_KEY,
'access_token' => $this->_accessToken
);
curl_setopt($this->_ch, CURLOPT_URL, self::MAIN_URL.'/users/'.$this->_userID.'/time_entries/'.$time_id.'?'.http_build_query($params));
curl_setopt($this->_ch, CURLOPT_HTTPGET, 1);
$content = curl_exec($this->_ch);
return $this->_tidyXML($content);
}
/**
* Update a time entry
*
* @param int $time_id
* @param int $task_id
* @param int $duration
* @param string $startTime
* @param string $endTime
* @param array $tags
* @param string $comments
* @param string $progress
* @return obj|false
* @author chris
*/
public function updateTime($time_id, $task_id = null, $duration = null, $startTime = null, $endTime = null, array $tags = array(), $comments = "", $progress = false)
{
if($duration <= 0)
throw new Exception('Duration must be more than 0');
$params = array(
'api_key' => self::API_KEY,
'access_token' => $this->_accessToken,
'time_entry' => array(
// required
'start_time' => $this->_checkDate($startTime),
'task_id' => (int) $task_id,
'duration_in_seconds' => (int) $duration,
// extra
'end_time' => $this->_checkDate($endTime),
'tags' => implode(',', $tags),
'comments' => $comments,
'in_progress' => $progress,
)
);
if($duration)
$params['time_entry']['duration_in_seconds'] = (int) $duration;
if($task_id)
$params['time_entry']['task_id'] = $task_id;
curl_setopt($this->_ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($this->_ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($this->_ch, CURLOPT_URL, self::MAIN_URL.'/users/'.$this->_userID.'/time_entries/'.$time_id);
$content = curl_exec($this->_ch);
return $this->_tidyXML($content);
}
/**
* Delete a time entry
*
* @param int $time_id
* @return bool
*/
public function deleteTime($time_id)
{
$params = array(
'api_key' => self::API_KEY,
'access_token' => $this->_accessToken
);
curl_setopt($this->_ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($this->_ch, CURLOPT_URL, self::MAIN_URL.'/users/'.$this->_userID.'/time_entries/'.$time_id.'?'.http_build_query($params));
$content = curl_exec($this->_ch);
if(!$content)
return true;
return false;
}
/**
* Checks the formatting of the date, should be YYYY-MM-DD HH-MM-SS
*
* @param string $string
* @return string|false
*/
private function _checkDate($string)
{
if(!$string)
return false;
if($string && preg_match("/\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}/", $string))
return $string;
throw new Exception("Date format must be yyyy-mm-dd hh:mm:ss you provided {$string}");
}
/**
* Take the XML from the server, tidy it and return either an object or false
*
* @param string $content
* @return obj|false
*/
private function _tidyXML($content)
{
$content = preg_replace("/[\r\n\s]{2,}/", "", $content);
$xml = @simplexml_load_string($content);
if(!$xml)
return false;
return json_decode(json_encode($xml));
}
public function __unset($name)
{
curl_close($this->_ch);
}
}