-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathasana.php
580 lines (524 loc) · 18.3 KB
/
asana.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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
<?php
/**
*
* A PHP class that acts as wrapper for Asana API. Lets make things easy! :)
*
* Read Asana API documentation for fully use this class http://developer.asana.com/documentation/
*
* Version: 1.0
*
*/
class Asana {
private $timeout = 10;
private $debug = false;
private $advDebug = true; // Note that enabling advanced debug will include debugging information in the response possibly breaking up your code
private $asanaApiVersion = "1.0";
public $responseCode;
private $endPointUrl;
private $apiKey;
private $taskUrl;
private $userUrl;
private $projectsUrl;
private $workspaceUrl;
private $storiesUrl;
private $tagsUrl;
public function __construct($apiKey){
if(substr($apiKey, -1) != ":") $apiKey .= ":"; // If the API key is not ended by ":", we append it
$this->apiKey = $apiKey;
$this->endPointUrl = "https://app.asana.com/api/{$this->asanaApiVersion}/";
$this->taskUrl = $this->endPointUrl."tasks";
$this->userUrl = $this->endPointUrl."users";
$this->projectsUrl = $this->endPointUrl."projects";
$this->workspaceUrl = $this->endPointUrl."workspaces";
$this->storiesUrl = $this->endPointUrl."stories";
$this->tagsUrl = $this->endPointUrl."tags";
define("METHOD_POST", 1);
define("METHOD_PUT", 2);
define("METHOD_GET", 3);
}
/**
* **********************************
* User functions
* **********************************
*/
/**
* Returns the full user record for a single user.
* Call it without parameters to get the users info of the owner of the API key.
*
* @param string $userId
* @return string JSON or null
*/
public function getUserInfo($userId = null){
if(is_null($userId)) $userId = "me";
return $this->askAsana($this->userUrl."/{$userId}");
}
/**
* Returns the user records for all users in all workspaces you have access.
*
* @return string JSON or null
*/
public function getUsers(){
return $this->askAsana($this->userUrl);
}
/**
* **********************************
* Task functions
* **********************************
*/
/**
* Function to create a task.
* For assign or remove the task to a project, use the addProjectToTask and removeProjectToTask.
*
*
* @param array $data Array of data for the task following the Asana API documentation.
* Example:
*
* array(
* "workspace" => "1768",
* "name" => "Hello World!",
* "notes" => "This is a task for testing the Asana API :)",
* "assignee" => "176822166183",
* "followers" => array(
* "37136",
* "59083"
* )
* )
*
* @return string JSON or null
*/
public function createTask($data){
$data = array("data" => $data);
$data = json_encode($data);
return $this->askAsana($this->taskUrl, $data, METHOD_POST);
}
/**
* Returns task information
*
* @param string $taskId
* @return string JSON or null
*/
public function getTask($taskId){
return $this->askAsana($this->taskUrl."/{$taskId}");
}
/**
* Returns sub-task information
*
* @param string $taskId
* @return string JSON or null
*/
public function getSubTasks($taskId){
return $this->askAsana($this->taskUrl."/{$taskId}/subtasks");
}
/**
* Updates a task
*
* @param string $taskId
* @param array $data See, createTask function comments for proper parameter info.
* @return string JSON or null
*/
public function updateTask($taskId, $data){
$data = array("data" => $data);
$data = json_encode($data);
return $this->askAsana($this->taskUrl."/{$taskId}", $data, METHOD_PUT);
}
/**
* Returns the projects associated to the task.
*
* @param string $taskId
* @return string JSON or null
*/
public function getProjectsForTask($taskId){
return $this->askAsana($this->taskUrl."/{$taskId}/projects");
}
/**
* Adds a project to task. If successful, will return success and an empty data block.
*
* @param string $taskId
* @param string $projectId
* @return string JSON or null
*/
public function addProjectToTask($taskId, $projectId){
$data = array("data" => array("project" => $projectId));
$data = json_encode($data);
return $this->askAsana($this->taskUrl."/{$taskId}/addProject", $data, METHOD_POST);
}
/**
* Removes project from task. If successful, will return success and an empty data block.
*
* @param string $taskId
* @param string $projectId
* @return string JSON or null
*/
public function removeProjectToTask($taskId, $projectId){
$data = array("data" => array("project" => $projectId));
$data = json_encode($data);
return $this->askAsana($this->taskUrl."/{$taskId}/removeProject", $data, METHOD_POST);
}
/**
* Returns task by a given filter.
* For now (limited by Asana API), you may limit your query either to a specific project or to an assignee and workspace
*
* NOTE: As Asana API says, if you filter by assignee, you MUST specify a workspaceId and viceversa.
*
* @param array $filter The filter with optional values.
*
* array(
* "assignee" => "",
* "project" => 0,
* "workspace" => 0
* )
*
* @return string JSON or null
*/
public function getTasksByFilter($filter = array("assignee" => "", "project" => "", "workspace" => "")){
$url = "";
$filter = array_merge(array("assignee" => "", "project" => "", "workspace" => ""), $filter);
$url .= $filter["assignee"] != ""?"&assignee={$filter["assignee"]}":"";
$url .= $filter["project"] != ""?"&project={$filter["project"]}":"";
$url .= $filter["workspace"] != ""?"&workspace={$filter["workspace"]}":"";
if(strlen($url) > 0) $url = "?".substr($url, 1);
return $this->askAsana($this->taskUrl.$url);
}
/**
* Returns the list of stories associated with the object.
* As usual with queries, stories are returned in compact form.
* However, the compact form for stories contains more information by default than just the ID.
* There is presently no way to get a filtered set of stories.
*
* @param string $taskId
* @return string JSON or null
*/
public function getTaskStories($taskId){
return $this->askAsana($this->taskUrl."/{$taskId}/stories");
}
/**
* Adds a comment to a task.
* The comment will be authored by the authorized user, and timestamped when the server receives the request.
*
* @param string $taskId
* @param string $text
* @return string JSON or null
*/
public function commentOnTask($taskId, $text = ""){
$data = array(
"data" => array(
"text" => $text
)
);
$data = json_encode($data);
return $this->askAsana($this->taskUrl."/{$taskId}/stories", $data, METHOD_POST);
}
/**
* Adds a tag to a task. If successful, will return success and an empty data block.
*
* @param string $taskId
* @param string $tagId
* @return string JSON or null
*/
public function addTagToTask($taskId, $tagId){
$data = array("data" => array("tag" => $tagId));
$data = json_encode($data);
return $this->askAsana($this->taskUrl."/{$taskId}/addTag", $data, METHOD_POST);
}
/**
* Removes a tag from a task. If successful, will return success and an empty data block.
*
* @param string $taskId
* @param string $tagId
* @return string JSON or null
*/
public function removeTagFromTask($taskId, $tagId){
$data = array("data" => array("tag" => $tagId));
$data = json_encode($data);
return $this->askAsana($this->taskUrl."/{$taskId}/removeTag", $data, METHOD_POST);
}
/**
* **********************************
* Projects functions
* **********************************
*/
/**
* Function to create a project.
*
* @param array $data Array of data for the project following the Asana API documentation.
* Example:
*
* array(
* "workspace" => "1768",
* "name" => "Foo Project!",
* "notes" => "This is a test project"
* )
*
* @return string JSON or null
*/
public function createProject($data){
$data = array("data" => $data);
$data = json_encode($data);
return $this->askAsana($this->projectsUrl, $data, METHOD_POST);
}
/**
* Returns the full record for a single project.
*
* @param string $projectId
* @return string JSON or null
*/
public function getProject($projectId){
return $this->askAsana($this->projectsUrl."/{$projectId}");
}
/**
* Returns the projects in all workspaces containing archived ones or not.
*
* @param boolean $archived Return archived projects or not
* @param string $opt_fields Return results with optional parameters
*/
public function getProjects($archived = false, $opt_fields = ""){
$archived = $archived?"true":"false";
$opt_fields = ($opt_fields != "")?"&opt_fields={$opt_fields}":"";
return $this->askAsana($this->projectsUrl."?archived={$archived}{$opt_fields}");
}
/**
* Returns the projects in provided workspace containing archived ones or not.
*
* @param string $workspaceId
* @param boolean $archived Return archived projects or not
*/
public function getProjectsInWorkspace($workspaceId, $archived = false){
$archived = $archived?"true":"false";
return $this->askAsana($this->projectsUrl."?archived={$archived}&workspace={$workspaceId}");
}
/**
* This method modifies the fields of a project provided in the request, then returns the full updated record.
*
* @param string $projectId
* @param array $data An array containing fields to update, see Asana API if needed.
* Example: array("name" => "Test", "notes" => "It's a test project");
*
* @return string JSON or null
*/
public function updateProject($projectId, $data){
$data = array("data" => $data);
$data = json_encode($data);
return $this->askAsana($this->projectsUrl."/{$projectId}", $data, METHOD_PUT);
}
/**
* Returns all unarchived tasks of a given project
*
* @param string $projectId
*
* @return string JSON or null
*/
public function getProjectTasks($projectId){
return $this->askAsana($this->taskUrl."?project={$projectId}");
}
/**
* Returns the list of stories associated with the object.
* As usual with queries, stories are returned in compact form.
* However, the compact form for stories contains more
* information by default than just the ID.
* There is presently no way to get a filtered set of stories.
*
* @param string $projectId
* @return string JSON or null
*/
public function getProjectStories($projectId){
return $this->askAsana($this->projectsUrl."/{$projectId}/stories");
}
/**
* Adds a comment to a project
* The comment will be authored by the authorized user, and timestamped when the server receives the request.
*
* @param string $projectId
* @param string $text
* @return string JSON or null
*/
public function commentOnProject($projectId, $text = ""){
$data = array(
"data" => array(
"text" => $text
)
);
$data = json_encode($data);
return $this->askAsana($this->projectsUrl."/{$projectId}/stories", $data, METHOD_POST);
}
/**
* **********************************
* Tags functions
* **********************************
*/
/**
* Returns the full record for a single tag.
*
* @param string $tagId
* @return string JSON or null
*/
public function getTag($tagId){
return $this->askAsana($this->tagsUrl."/{$tagId}");
}
/**
* Returns the full record for all tags in all workspaces.
*
* @return string JSON or null
*/
public function getTags(){
return $this->askAsana($this->tagsUrl);
}
/**
* Modifies the fields of a tag provided in the request, then returns the full updated record.
*
* @param string $tagId
* @param array $data An array containing fields to update, see Asana API if needed.
* Example: array("name" => "Test", "notes" => "It's a test tag");
*
* @return string JSON or null
*/
public function updateTag($tagId, $data){
$data = array("data" => $data);
$data = json_encode($data);
return $this->askAsana($this->tagsUrl."/{$tagId}", $data, METHOD_PUT);
}
/**
* Create new tag provided in the request, then returns the full updated record.
*
* @param array $data An array containing fields to update, see Asana API if needed.
* Example: array("name" => "Test", "notes" => "It's a test tag");
*
* @return string JSON or null
*/
public function createTag( $data ) {
$data = array("data" => $data);
$data = json_encode($data);
return $this->askAsana($this->tagsUrl, $data, METHOD_POST);
}
/**
* Returns the list of all tasks with this tag. Tasks can have more than one tag at a time.
*
* @param string $tagId
* @return string JSON or null
*/
public function getTasksWithTag($tagId){
return $this->askAsana($this->tagsUrl."/{$tagId}/tasks");
}
/**
* **********************************
* Stories and comments functions
* **********************************
*/
/**
* Returns the full record for a single story.
*
* @param string $storyId
* @return string JSON or null
*/
public function getSingleStory($storyId){
return $this->askAsana($this->storiesUrl."/{$storyId}");
}
/**
* **********************************
* Workspaces functions
* **********************************
*/
/**
* Returns all the workspaces.
*
* @return string JSON or null
*/
public function getWorkspaces(){
return $this->askAsana($this->workspaceUrl);
}
/**
* Currently the only field that can be modified for a workspace is its name (as Asana API says).
* This method returns the complete updated workspace record.
*
* @param array $data
* Example: array("name" => "Test");
*
* @return string JSON or null
*/
public function updateWorkspace($workspaceId, $data = array("name" => "")){
$data = array("data" => $data);
$data = json_encode($data);
return $this->askAsana($this->workspaceUrl."/{$workspaceId}", $data, METHOD_PUT);
}
/**
* Returns tasks of all workspace assigned to someone.
* Note: As Asana API says, you must specify an assignee when querying for workspace tasks.
*
* @param string $workspaceId The id of the workspace
* @param string $assignee Can be "me" or user ID
*
* @return string JSON or null
*/
public function getWorkspaceTasks($workspaceId, $assignee = "me"){
return $this->askAsana($this->taskUrl."?workspace={$workspaceId}&assignee={$assignee}");
}
/**
* Returns tags of all workspace.
*
* @param string $workspaceId The id of the workspace
*
* @return string JSON or null
*/
public function getWorkspaceTags($workspaceId){
return $this->askAsana($this->workspaceUrl."/{$workspaceId}/tags");
}
/**
* Returns users of all workspace.
*
* @param string $workspaceId The id of the workspace
*
* @return string JSON or null
*/
public function getWorkspaceUsers($workspaceId){
return $this->askAsana($this->workspaceUrl."/{$workspaceId}/users");
}
/**
* This function communicates with Asana REST API.
* You don't need to call this function directly. It's only for inner class working.
*
* @param string $url
* @param string $data Must be a json string
* @param int $method See constants defined at the beginning of the class
* @return string JSON or null
*/
private function askAsana($url, $data = null, $method = METHOD_GET){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Don't print the result
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $this->timeout);
curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // Don't verify SSL connection
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // "" ""
curl_setopt($curl, CURLOPT_USERPWD, $this->apiKey);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); // Send as JSON
if($this->advDebug){
curl_setopt($curl, CURLOPT_HEADER, true); // Display headers
curl_setopt($curl, CURLOPT_VERBOSE, true); // Display communication with server
}
if($method == METHOD_POST){
curl_setopt($curl, CURLOPT_POST, true);
} else if($method == METHOD_PUT){
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
}
if(!is_null($data) && ($method == METHOD_POST || $method == METHOD_PUT)){
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
try {
$return = curl_exec($curl);
$this->responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if($this->debug || $this->advDebug){
echo "<pre>"; print_r(curl_getinfo($curl)); echo "</pre>";
}
} catch(Exception $ex){
if($this->debug || $this->advDebug){
echo "<br>cURL error num: ".curl_errno($curl);
echo "<br>cURL error: ".curl_error($curl);
}
echo "Error on cURL";
$return = null;
}
curl_close($curl);
return $return;
}
}