-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.php
93 lines (74 loc) · 2.22 KB
/
task.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
<?php
include('config.php');
if (isset($token)) {
$client->setAccessToken($token);
$service = new Google_Service_Tasks($client);
$tasks = []; $tasklists = [];
if (isset($_GET['task'])) {
$task = json_decode($_GET['task']);
if (isset($task->id)) {
// Update existing event
$updateTask = $service->tasks->get($task->listId, $task->id);
$updateTask->setTitle($task->title);
$updateTask->setNotes($task->notes);
$updateTask->setDue($task->due);
$service->tasks->update($task->listId, $task->id, $updateTask);
} else {
$taskListId = $task->listId;
// Create new list
// $newTask = new Google_Service_Tasks_TaskList();
// $newTask->setTitle($task->title);
// $service->tasklists->insert($newTask);
// add task to given task list
$newTask = new Google_Service_Tasks_Task([
'title' => $task->title,
'notes' => $task->notes,
'due' => $task->due
]);
$service->tasks->insert($taskListId, $newTask);
}
}
if (isset($_GET['id'])) {
$service->tasks->delete($_GET['listId'], $_GET['id']);
}
echo json_encode(['tasks' => getTotalTasks(), 'lists' => $tasklists]);
}
function dateFormat($dateString) {
$date = new DateTime($dateString);
return $date->format("Y-m-d");
}
function getTotalTasks() {
global $service, $tasks, $tasklists;
$optParams = [
'maxResults' => 100,
];
$results = $service->tasklists->listTasklists($optParams);
if (count($results->getItems()) == 0) {
echo json_encode(['tasks' => []]);
die();
} else {
foreach ($results->getItems() as $tasklist) {
$taskListId = $tasklist->getId();
$tasklists[] = [
'value' => $taskListId,
'label' => $tasklist->getTitle()
];
if ($taskListId) {
$result_tasks = $service->tasks->listTasks($taskListId);
if (count($result_tasks) > 0) {
foreach ($result_tasks as $item) {
$_task = [
'listId' => $taskListId,
'id' => $item->id,
'title' => $item->title,
'notes' => $item->notes,
'due' => dateFormat($item->due)
];
$tasks[] = $_task;
}
}
}
}
}
return $tasks;
}