forked from sad0vnikov/asana-time-track
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsanaApi.php
168 lines (133 loc) · 6.12 KB
/
AsanaApi.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
<?php
/**
* AsanaApi-Class connect to the asana api
* some helper functions e.g. getWorkspaces, getProjects,...
*
* @author codelovers
* @author codelovers.de
* @version 1.0 (2012_07_07)
* @package asana_track_time
*/
class AsanaApi {
// ##############################################################################################
// CLASS VARIABLES & CONSTANTS
// ##############################################################################################
// variables
private $apiKey, $uri, $workspaceUri, $responseCode;
// set constants
const PUT_METHOD = 2;
const GET_METHOD = 3;
// ##############################################################################################
// CONSTRUCTOR
// ##############################################################################################
public function __construct($apiKey){
// : away, append it
if(substr($apiKey, -1) != ":"){
$apiKey .= ":";
}
// initialize needed values
$this->apiKey = $apiKey;
$this->uri = "https://api.asana.com/api/1.0/";
$this->workspaceUri = $this->uri."workspaces"; //
$this->projectUri = $this->uri."projects";
$this->taskUri = $this->uri."tasks";
$this->userUri = $this->uri."users";
}
// ##############################################################################################
// SETTER & GETTER & HELPER METHODS
// ##############################################################################################
public function getUserId(){
$resultJson = json_decode($this->apiRequest($this->userUri.'/me'));
return $resultJson->data->id;
}
public function getResponseCode(){
return $this->responseCode;
}
public function getWorkspaces(){
return $this->apiRequest($this->workspaceUri);
}
public function getTasks($workspaceId){
return $this->apiRequest($this->workspaceUri.'/'.$workspaceId.'/tasks?assignee=me');
}
public function getOneTask($taskId){
$resultJson = json_decode($this->apiRequest($this->taskUri.'/'.$taskId));
$castIntoArray = array();
if(array_key_exists(0, $resultJson->data->projects)) {
$castIntoArray = (array)$resultJson->data->projects[0];
}
elseif(is_object($resultJson->data->parent)){
$castIntoArray = array(
'id' => $resultJson->data->parent->id,
'name' => 'PARENT TASK: '.$resultJson->data->parent->name
);
}
else{
$castIntoArray = array(
'id' => $resultJson->data->id,
'name' => 'NO PROJECT'
);
}
$array = array ( 'completed' => $resultJson->data->completed,
'assignee' => $resultJson->data->assignee->id,
'projects' => $castIntoArray
);
return $array;
}
public function updateTask($taskId, $workedHours, $workedMinutes, $estimatedHours, $estimatedMinutes, $taskName){
$data = array( "name" => $taskName ." [ET: " . $estimatedHours . "h " . $estimatedMinutes . "m] [WT: " . $workedHours . "h " . $workedMinutes . "m]");
$data = array("data" => $data);
$data = json_encode($data);
return $this->apiRequest($this->taskUri.'/'.$taskId , $data, self::PUT_METHOD);
}
public function getEstimatedAndWorkedTime($taskName){
$estimatedTimeHours = 0;
$estimatedTimeMinutes = 0;
$workedTimeHours = 0;
$workedTimeMinutes = 0;
$workedTime = 0;
$pattern = "/\[ET\: (\d+)h (\d+)m\] \[WT\: (\d+)h (\d+)m\]$/";
if(preg_match($pattern, $taskName, $matches)) {
// estimated time
$estimatedTimeHours = $matches[1];
$estimatedTimeMinutes = $matches[2];
// worked time
$workedTimeHours = $matches[3];
$workedTimeMinutes = $matches[4];
// worked time in sec
$workedTime = $workedTimeHours * 60 * 60 * 1000;
$workedTime += $workedTimeMinutes * 60 * 1000;
$taskName = preg_replace($pattern, "", $taskName);
}
$array = array ( 'taskName' => $taskName,
'estimatedHours' => $estimatedTimeHours,
'estimatedMinutes' => $estimatedTimeMinutes,
'workedHours' => $workedTimeHours,
'workedMinutes' => $workedTimeMinutes,
'workedTimeSec' => $workedTime
);
return $array;
}
// ##############################################################################################
// ASK ASANA API AND RETURN DATA
// ##############################################################################################
private function apiRequest($url, $givenData = null, $method = self::GET_METHOD){
// ask asana api and return data
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_USERPWD, $this->apiKey);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // don´t print json-string
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); // Send as JSON
if($method == self::PUT_METHOD){
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $givenData);
}
$data = curl_exec($curl);
// set responsCode, needed in index.php
$this->responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
return $data;
}
}