Skip to content

Commit

Permalink
Merge pull request #68 from nickbreslin/master
Browse files Browse the repository at this point in the history
createSection() method and example
  • Loading branch information
ajimix authored Oct 26, 2018
2 parents 673e5b0 + 215a869 commit 75c3837
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
32 changes: 32 additions & 0 deletions asana.php
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,38 @@ public function getWorkspaceTypeahead($workspaceId, $type, $query, $count = 1, a
* **********************************
*/

/**
* Creates a section associated with a project.
* More about sections (@see https://asana.com/developers/api-reference/sections)
*
* @param string $projectId
* @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 section for testing the Asana API :)",
* "assignee" => "176822166183",
* "followers" => array(
* "37136",
* "59083"
* )
* )
* @param array $opts Array of options to pass
* (@see https://asana.com/developers/documentation/getting-started/input-output-options)
*
* @return string JSON or null
*/
public function createSection($projectId, $data, array $opts = array())
{
$data = array('data' => $data);
$data = json_encode($data);
$options = http_build_query($opts);

return $this->askAsana($this->projectsUrl . '/' . $projectId . '/sections?' . $options, $data, ASANA_METHOD_POST);
}

/**
* Returns the full record for a single section.
*
Expand Down
55 changes: 55 additions & 0 deletions examples/section-creation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
require_once('../asana.php');

// See class comments and Asana API for full info
$asana = new Asana(array('personalAccessToken' => 'xxxxxxxxxxxxxxxxxxxxx')); // Create a personal access token in Asana or use OAuth

$workspaceId = 'XXXXXXXXXXXXXXXXXXX'; // The workspace where we want to create our task, take a look at getWorkspaces() method.
$projectId = 'XXXXXXXXXXXXXXXXXXX'; // the project id where we want to add our section, take a look at getProjects() method.

// A section is a subdivision of a project that groups tasks together.
// It is also just a task with a name that ends with a colon.
// These are two ways to create a section, shown below.
//
// More about sections: https://asana.com/developers/api-reference/sections

//
// One method to create a section is to create a task with specific properties.
// We've added a colon to the end of the task name and added a project to it's membership properties.
$asana->createTask(array(
'workspace' => $workspaceId, // Workspace ID
'name' => 'Section created by createTask:', // Name of task
'assignee' => '[email protected]', // Assign task to...
"memberships" => [
[
"project" => $projectId
]
]
));

/*
// Alternatively, we can use the createSection() method.
// We identify the project as the first parameter and the data for the section in an array for the second parameter.
//
// Note: The section name does not need to include a colon, Asana will add this when the section is created.
$asana->createSection($projectId, array(
'workspace' => $workspaceId, // Workspace ID
'name' => 'Section created by createSection', // Name of section
'assignee' => '[email protected]' // Assign task to...
));
*/


// As Asana API documentation says, when a task is created, 201 response code is sent back so...
if ($asana->hasError()) {
echo 'Error while trying to connect to Asana, response code: ' . $asana->responseCode;
return;
}

$result = $asana->getData();

if (isset($result->id)) {
echo $result->id; // Here we have the id of the task/section that have been created
}

0 comments on commit 75c3837

Please sign in to comment.