Skip to content

Commit

Permalink
Adding Classes for the Portfolio Board endpoints (#45)
Browse files Browse the repository at this point in the history
* Add Portfolio Board Model

Add the Model for the /portfolio/boards endpoints for the Teamwork API
https://developer.teamwork.com/projects/portfolio-boards/boards-in-portfolio-view

* Add Portfolio Column Model

Add the Model for the /portfolio/columns endpoints for the Teamwork API
https://developer.teamwork.com/projects/portfolio-boards/columns-inside-a-portfolio-board

gatAll / insert use this end point: /portfolio/boards/{boardId}/columns

* Change getAll to getAllForBoard in Column

The getAll method wasn't really correct, as there isn't a get all method.

You can only get all columns for a given board (the ID of which was
already being passed in), so added ForBoard to the function name

* Add Portfolio Card Model

Add the Model for the /portfolio/cards endpoints for the Teamwork API

Some use special endpoints:
- getAllForColumn: portfolio/columns/{columnId}/cards
- insert: portfolio/columns/{columnId}/cards (this adds a project as a card on the given column)
- update - portfolio/cards/{cardId}/move (this moves the card from one column to another)
  • Loading branch information
AlanHolmes authored and simonschaufi committed Jan 28, 2019
1 parent 482997b commit 29b2006
Show file tree
Hide file tree
Showing 8 changed files with 816 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/Portfolio/Board.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace TeamWorkPm\Portfolio;

use TeamWorkPm\Model;

class Board extends Model
{
public function init()
{
$this->parent = 'board';
$this->action = 'portfolio/boards';

$this->fields = [
'canEdit' => [
'required' => false,
'attributes' => ['type' => 'boolean'],
],

'name' => [
'required' => false,
'attributes' => ['type' => 'string'],
],

'displayOrder' => [
'required' => false,
'attributes' => ['type' => 'string'],
],

'description' => [
'required' => false,
'attributes' => ['type' => 'string'],
],

'deletedDate' => [
'required' => false,
'attributes' => ['type' => 'string'],
],

'id' => [
'required' => false,
'attributes' => ['type' => 'string'],
],

'dateCreated' => [
'required' => false,
'attributes' => ['type' => 'string'],
],

'color' => [
'required' => false,
'attributes' => ['type' => 'string'],
],

'deleted' => [
'required' => false,
'attributes' => ['type' => 'boolean'],
],
];
}

/**
* Get all the Portfolio Boards
* GET /portfolio/boards
*
* @return \TeamWorkPm\Response\Model
* @throws \TeamWorkPm\Exception
*/
public function getAll()
{
return $this->rest->get("$this->action");
}
}
117 changes: 117 additions & 0 deletions src/Portfolio/Card.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace TeamWorkPm\Portfolio;

use TeamWorkPm\Exception;
use TeamWorkPm\Model;

class Card extends Model
{
public function init()
{
$this->parent = 'card';
$this->action = 'portfolio/cards';

$this->fields = [
'projectId' => [
'required' => false,
'attributes' => ['type' => 'string'],
],

// These are only used by the update method
'cardId' => [
'required' => false,
'sibling' => true,
'attributes' => ['type' => 'string'],
],

'columnId' => [
'required' => false,
'sibling' => true, // Dont nest under 'Card'
'attributes' => ['type' => 'string'],
],

'oldColumnId' => [
'required' => false,
'sibling' => true, // Dont nest under 'Card'
'attributes' => ['type' => 'string'],
],

'positionAfterId' => [
'required' => false,
'sibling' => true, // Dont nest under 'Card'
'attributes' => ['type' => 'integer'],
],
];
}

/**
* Get all the Columns for a Portfolio Column
* GET /portfolio/columns/{columnId}/cards
*
* @param int $columnId
*
* @return \TeamWorkPm\Response\Model
* @throws \TeamWorkPm\Exception
*/
public function getAllForColumn($columnId)
{
$columnId = (int) $columnId;
if ($columnId <= 0) {
throw new Exception('Invalid param columnId');
}

return $this->rest->get("portfolio/columns/$columnId/cards");
}

/**
* Adds a project to the given board
*
* @param array $data
*
* @return int
*/
public function insert(array $data)
{
$columnId = empty($data['columnId']) ? 0 : (int) $data['columnId'];
if ($columnId <= 0) {
throw new Exception('Required field columnId');
}
unset($data['columnId']);

if (empty($data['projectId'])) {
throw new Exception('Required field projectId');
}

return $this->rest->post("portfolio/columns/$columnId/cards", $data);
}

/**
* Moves the given card from one board to another
*
* @param array $data
*
* @return bool
* @throws \TeamWorkPm\Exception
*/
public function update(array $data)
{
$cardId = empty($data['id']) ? 0: (int) $data['id'];
if ($cardId <= 0) {
throw new Exception('Required field id');
}
$data['cardId'] = $data['id'];
unset($data['id']);

if (empty($data['columnId'])) {
throw new Exception('Required field columnId');
}

if (empty($data['oldColumnId'])) {
throw new Exception('Required field oldColumnId');
}

return $this->rest->put("$this->action/$cardId/move", $data);
}

}
112 changes: 112 additions & 0 deletions src/Portfolio/Column.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace TeamWorkPm\Portfolio;

use TeamWorkPm\Exception;
use TeamWorkPm\Model;

class Column extends Model
{
public function init()
{
$this->parent = 'column';
$this->action = 'portfolio/columns';

$this->fields = [
'name' => [
'required' => false,
'attributes' => ['type' => 'string'],
],

'displayOrder' => [
'required' => false,
'attributes' => ['type' => 'string'],
],

'sortOrder' => [
'required' => false,
'attributes' => ['type' => 'string'],
],

'deletedDate' => [
'required' => false,
'attributes' => ['type' => 'string'],
],

'dateUpdated' => [
'required' => false,
'attributes' => ['type' => 'string'],
],

'hasTriggers' => [
'required' => false,
'attributes' => ['type' => 'boolean'],
],

'sort' => [
'required' => false,
'attributes' => ['type' => 'string'],
],

'canEdit' => [
'required' => false,
'attributes' => ['type' => 'boolean'],
],

'id' => [
'required' => false,
'attributes' => ['type' => 'string'],
],

'dateCreated' => [
'required' => false,
'attributes' => ['type' => 'string'],
],

'color' => [
'required' => false,
'attributes' => ['type' => 'string'],
],

'deleted' => [
'required' => false,
'attributes' => ['type' => 'boolean'],
],
];
}

/**
* Get all the Columns for a Portfolio Board
* GET /portfolio/boards/{boardId}/columns
*
* @param int $boardId
*
* @return \TeamWorkPm\Response\Model
* @throws \TeamWorkPm\Exception
*/
public function getAllForBoard($boardId)
{
$boardId = (int) $boardId;
if ($boardId <= 0) {
throw new Exception('Invalid param boardId');
}

return $this->rest->get("portfolio/boards/$boardId/columns");
}

/**
* @param array $data
*
* @return int
*/
public function insert(array $data)
{
$boardId = empty($data['board_id']) ? 0 : (int) $data['board_id'];
if ($boardId <= 0) {
throw new Exception('Required field board_id');
}
unset($data['board_id']);

return $this->rest->post("portfolio/boards/$boardId/columns", $data);
}
}
5 changes: 5 additions & 0 deletions src/Response/JSON.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public function parse($data, array $headers)
preg_match('!projects/(\d+)/notebooks!', $headers['X-Action'])
) {
$source = [];
} elseif (
isset($source->cards) &&
preg_match('!portfolio/columns/(\d+)/cards!', $headers['X-Action'])
) {
$source = $source->cards;
} else {
$source = current($source);
}
Expand Down
Loading

0 comments on commit 29b2006

Please sign in to comment.