-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.class.php
472 lines (413 loc) · 10.2 KB
/
api.class.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
<?php
/**
* Wrapper for Wunderlist2 API
* api.class.php
* Purpose: communicate with Wunderlist2 API
*
* @requires base.class.php
* @author Joshua de Gier
* @version 1.02 26/07/2013
*/
require_once('base.class.php');
class Wunderlist extends Wunderbase
{
public $authtoken = false;
protected $api_url = 'https://api.wunderlist.com';
private $tasks = false;
private $lists = false;
private $listTasks = false;
/**
* construct the Wunderlist API and save the authentication token for later use
*
* @param string $email
* @param string $password
*
* @throws Exception if any of the parameters are empty or authentication fails
* @return void
*/
public function __construct($email, $password)
{
// Check for email
if( $email == "" )
{
throw new Exception( "E-mail parameter empty", 1001 );
}
// Check for password
if( $password == "" )
{
throw new Exception( "Password parameter empty", 1002 );
}
$data = array(
'email' => $email,
'password' => $password
);
$return = $this->call('/login', 'post', $data);
if($return != false)
{
$this->authtoken = $return['token'];
}
else
{
throw new Exception( "Authentication failed", 3001 );
}
}
/**
* get the details of the currently logged in user
*
* @return array Returns an array with the data of the user
*/
public function me()
{
$return = $this->call('/me', 'get', array());
return $return;
}
/**
* get all tasks connected to the account
*
* @param bool $completed
* @return array Returns an array with all tasks
*/
public function getTasks($completed = false)
{
$return = $this->call('/me/tasks', 'get', array());
// Put subtasks within their parents
$aOrdened = $subtasks = array();
foreach($return as $task)
{
if(($completed == true) || ($completed == false && $task['completed_at'] == NULL))
{
if( $task['parent_id'] == NULL )
{
$aOrdened[ $task['id'] ] = $task;
$aOrdened[ $task['id'] ]['subtasks'] = array();
}
else
{
$subtasks[] = $task;
}
}
}
// Insert subtasks
foreach($subtasks as $subtask)
{
$aOrdened[ $subtask['parent_id'] ]['subtasks'][] = $subtask;
}
// Put tasks in variable
$this->tasks = $aOrdened;
return $aOrdened;
}
/**
* get all lists connected to the account
*
* @return array Returns an array with all lists
*/
public function getLists()
{
$return = $this->call('/me/lists', 'get', array());
// Create a useable array
$aOrdened = array();
foreach($return as $list)
{
$aOrdened[ $list['id'] ] = $list;
}
$aOrdened[ 'inbox' ] = 'Inbox'; // Inbox is not returned from API call.
$this->lists = $aOrdened;
return $aOrdened;
}
/**
* get all tasks belonging to a specified task list
*
* @param string $list_id
* @param bool $completed
*
* @throws Exception if any of the parameters are empty
* @return array Returns an array with tasks belonging to $list_id
*/
public function getTasksByList($list_id, $completed = false)
{
// Check data
if( $list_id == "" )
{
throw new Exception( "list_id parameter empty", 1003 );
}
// Get all lists
if($this->lists == false)
{
$this->getLists();
}
// Get all tasks
if($this->tasks == false)
{
$this->getTasks();
}
// Build an associative array with all tasks
if($this->listsTasks == false)
{
// Loop lists
foreach($this->lists as $list)
{
$this->listTasks[ $list['id'] ] = array(
'details' => $list,
'tasks' => array()
);
}
// Loop tasks
foreach($this->tasks as $task)
{
// Check for completed state
if(($completed == true) || ($completed == false && $task['completed_at'] == NULL))
{
$this->listTasks[ $task['list_id'] ]['tasks'][] = $task;
}
}
}
// Return the list
if( array_key_exists( $list_id, $this->listTasks ))
{
return $this->listTasks[ $list_id ];
} else {
throw new Exception( "List not found", 2001 );
}
}
/**
* get reminders that are set
*
* @return array Returns an array containing the reminders
*/
public function getReminders()
{
// Get all tasks
if($this->tasks == false)
{
$this->getTasks();
}
// Get the reminders
$reminders = $this->call('/me/reminders', 'get', array());
// Loop reminders to add task data
foreach($reminders as $key => $data)
{
$reminders[$key]['task'] = $this->tasks[ $data['task_id'] ];
}
// Return the reminders
return $reminders;
}
/**
* Add a list to the Wunderlist account
*
* @param string $title
*
* @throws Exception if any of the parameters are empty
* @return array Returns the array with the added list
*/
public function addList($title)
{
// Check title parameter
if( $title == "" )
{
throw new Exception( "title parameter empty", 1004 );
}
else
{
return $this->call('/me/lists', 'post', array('title' => $title));
}
}
/**
* Add a task to a list
*
* @param string $title
* @param string $list_id
* @param date $due_date (format "YYYY-mm-dd")
* @param bool $starred
*
* @throws Exception if any of the parameters are empty
* @return array Returns the array with the added task
*/
public function addTask($title, $list_id, $due_date='', $starred=false)
{
// Check title parameter
if( $title == "" )
{
throw new Exception( "title parameter empty", 1004 );
}
// Check list_id parameter
if( $list_id == "" )
{
throw new Exception( "list_id parameter empty", 1003 );
}
// Set data for the call
$data = array(
'title' => $title,
'list_id' => $list_id,
'starred' => $starred == true ? 1 : 0
);
// Add due date of found
if( $due_date != "" )
{
if( !strtotime($due_date) )
{
throw new Exception( "due_date parameter invalid", 1006 );
}
$data['due_date'] = date("Y-m-d", strtotime($due_date));
}
return $this->call('/me/tasks', 'post', $data);
}
/**
* Add a note to a task
*
* @param string $task_id
* @param string $note
*
* @throws Exception if any of the parameters are empty
* @return array Returns an array with the task details
*/
public function addNoteToTask($task_id, $note)
{
// Check title parameter
if( $task_id == "" )
{
throw new Exception( "task_id parameter empty", 1005 );
}
// Check list_id parameter
if( $note == "" )
{
throw new Exception( "note parameter empty", 1007 );
}
// Set data for the call
$data = array(
'note' => $note
);
return $this->call('/'.$task_id, 'put', $data);
}
/**
* set a due date for a task, optional: recurring
*
* @param string $task_id
* @param date $due_date
* @param bool $recurring
* @param int $recurring_interval_num
* @param string $recurring_interval_type
*
* @throws Exception if any of the parameters are empty
* @return array Returns an array with the task details
*/
public function addDueDateToTask($task_id, $due_date, $recurring = false, $interval_num = 0, $interval_type = 'none')
{
switch($interval_type)
{
case 'days':$interval_type='day';break;
case 'weeks':$interval_type='week';break;
case 'months':$interval_type='month';break;
case 'years':$interval_type='year';break;
default:$interval_type=false;break;
}
// Check title parameter
if( $task_id == "" )
{
throw new Exception( "task_id parameter empty", 1005 );
}
// Check for a valid due_date
if( $due_date == "" || !strtotime($due_date) )
{
throw new Exception(" due_date parameter invalid", 1006 );
}
// Set the data
$data = array(
'due_date' => date("Y-m-d", strtotime($due_date))
);
// If the task is recurring, set the interval
if( $recurring )
{
if( !$interval_type || intval($interval_num) == 0 )
{
throw new Exception( "Invalid recurring profile", 1008 );
}
else
{
$data['recurrence_count'] = intval($interval_num);
$data['recurrence_type'] = $interval_type;
}
}
return $this->call('/'.$task_id, 'put', $data);
}
/**
* add a reminder to a task
*
* @param string $task_id
* @param date $reminder_date
*
* @throws Exception if any of the parameters are empty
* @return array Returns an array with the task detaild
*/
public function addReminderToTask($task_id, $reminder_date)
{
// Check title parameter
if( $task_id == "" )
{
throw new Exception( "task_id parameter empty", 1005 );
}
// Check reminder_date
if( $reminder_date == "" || !strtotime($reminder_date))
{
throw new Exception( "reminder_date parameter invalid", 1009 );
}
// Set data for the call
$data = array(
'task_id' => $task_id,
'date' => date("Y-m-d\TH:i:s\Z", strtotime($reminder_date))
);
return $this->call('/me/reminders', 'post', $data);
}
/**
* delete a task
*
* @param string $task_id
*
* @throws Exception if any of the parameters are empty
* @return bool
*/
public function deleteTask($task_id)
{
// Check title parameter
if( $task_id == "" )
{
throw new Exception( "task_id parameter empty", 1005 );
}
$return = $this->call('/'.$task_id, 'delete', array());
// If delete was succesfull an empty array is return
if( is_array($return) && count($array) == 0)
{
return true;
}
else
{
return false;
}
}
/**
* delete a list
*
* @param string $list_id
*
* @throws Exception if any of the parameters are empty
* @return bool
*/
public function deleteList($list_id)
{
// Check data
if( $list_id == "" )
{
throw new Exception( "list_id parameter empty", 1003 );
}
$return = $this->call('/'.$list_id, 'delete', array());
// If delete was succesfull an empty array is return
if( is_array($return) && count($array) == 0)
{
return true;
}
else
{
return false;
}
}
}
?>