Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Allows you to duplicate a project in Asana and put it in another workspace
<?php
function asanaRequest($methodPath, $httpMethod = 'GET', $body = null) {
$apiKey = 'ASANA_API_KEY_HERE'; /// Get it from http://app.asana.com/-/account_api
$url = "https://app.asana.com/api/1.0/$methodPath";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ;
curl_setopt($ch, CURLOPT_USERPWD, $apiKey);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
if ($body)
{
if (!is_string($body))
{
$body = json_encode($body);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
}
$data = curl_exec($ch);
curl_close($ch);
$result = json_decode($data, true);
return $result;
}
function createTask($workspaceId, $projectId, $task) {
$data = array('data' => $task);
$result = asanaRequest("workspaces/$workspaceId/tasks", 'POST', $data);
if ($result['data'])
{
$newTask = $result['data'];
$newTaskId = $newTask['id'];
$data = array('data' => array('project' => $projectId));
$result = asanaRequest("tasks/$newTaskId/addProject", 'POST', $data);
return $newTask;
}
return $result;
}
function createSubtask($workspaceId, $projectId, $taskId, $task)
{
$data = array('data' => $task);
$result = asanaRequest("tasks/$taskId/subtasks", 'POST', $data);
if ($result['data'])
{
//$newTask = $result['data'];
//$newTaskId = $newTask['id'];
//$data = array('data' => array('project' => $projectId));
//$result = asanaRequest("tasks/$newTaskId/addProject", 'POST', $data);
//return $newTask;
}
return $result;
}
function copyTasks($fromProjectId, $toProjectId) {
$result = asanaRequest("projects/$toProjectId");
print_r($result);
if(!$result['data']) {
return;
}
$workspaceId = $result['data']['workspace']['id'];
$result = asanaRequest("projects/$fromProjectId/tasks?opt_pretty&opt_fields=name,due_on,assignee_status,notes,assignee");
$tasks = $result['data'];
for ($i = count($tasks) - 1; $i >= 0; $i--)
{
$task = $tasks[$i];
$newTask = $task;
unset($newTask['id']);
$newTask['assignee'] = $newTask['assignee']['id'];
foreach ($newTask as $key => $value)
{
if (empty($value))
{
unset($newTask[$key]);
}
}
$newTask = createTask($workspaceId, $toProjectId, $newTask);
if ($newTask['id'])
{
$taskId = $task['id'];
$result = asanaRequest("tasks/$taskId/stories");
$comments = array();
foreach ($result['data'] as $story)
{
$date = date('l M d, Y h:i A', strtotime($story['created_at']));
$comment = " ­\n" . $story['created_by']['name'] . ' on ' . $date . ":\n" . $story['text'];
$comments[] = $comment;
}
$comment = implode("\n----------------------", $comments);
$data = array('data' => array('text' => $comment));
$newTaskId = $newTask['id'];
//$result = asanaRequest("tasks/$newTaskId/stories", 'POST', $data);
$subtasks = asanaRequest("tasks/$taskId/subtasks?opt_pretty&opt_fields=name,due_on,assignee_status,notes,assignee");
print_r($subtasks);
if($subtasks['data']) {
$subtasks = $subtasks['data'];
for ($k = count($subtasks) - 1; $k >= 0; $k--) {
$subtask = $subtasks[$k];
$newSubtask = $subtask;
unset($newSubtask['id']);
$newSubtask['assignee'] = $newSubtask['assignee']['id'];
foreach ($newSubtask as $key => $value)
{
if (empty($value))
{
unset($newSubtask[$key]);
}
}
$newSubtask = createSubtask($workspaceId, $toProjectId, $newTaskId, $newSubtask);
}
}
}
}
}
//copyTasks(1234, 1234); /// Get those numbers from the URL of the project
//echo 'Finished!';
?>
@kirkouimet

This comment has been minimized.

Copy link
Owner Author

commented Jun 17, 2013

My only addition is the handling of first level subtask copying

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.