API refactoring

This commit is contained in:
Frederic Guillot
2015-05-23 21:44:33 -04:00
parent c9ba525bab
commit e32f26d048
24 changed files with 1519 additions and 606 deletions

98
app/Api/Action.php Normal file
View File

@@ -0,0 +1,98 @@
<?php
namespace Api;
/**
* Action API controller
*
* @package api
* @author Frederic Guillot
*/
class Action extends Base
{
public function getAvailableActions()
{
return $this->action->getAvailableActions();
}
public function getAvailableActionEvents()
{
return $this->action->getAvailableEvents();
}
public function getCompatibleActionEvents($action_name)
{
return $this->action->getCompatibleEvents($action_name);
}
public function removeAction($action_id)
{
return $this->action->remove($action_id);
}
public function getActions($project_id)
{
$actions = $this->action->getAllByProject($project_id);
foreach ($actions as $index => $action) {
$params = array();
foreach($action['params'] as $param) {
$params[$param['name']] = $param['value'];
}
$actions[$index]['params'] = $params;
}
return $actions;
}
public function createAction($project_id, $event_name, $action_name, $params)
{
$values = array(
'project_id' => $project_id,
'event_name' => $event_name,
'action_name' => $action_name,
'params' => $params,
);
list($valid,) = $this->action->validateCreation($values);
if (! $valid) {
return false;
}
// Check if the action exists
$actions = $this->action->getAvailableActions();
if (! isset($actions[$action_name])) {
return false;
}
// Check the event
$action = $this->action->load($action_name, $project_id, $event_name);
if (! in_array($event_name, $action->getCompatibleEvents())) {
return false;
}
$required_params = $action->getActionRequiredParameters();
// Check missing parameters
foreach($required_params as $param => $value) {
if (! isset($params[$param])) {
return false;
}
}
// Check extra parameters
foreach($params as $param => $value) {
if (! isset($required_params[$param])) {
return false;
}
}
return $this->action->create($values);
}
}

22
app/Api/App.php Normal file
View File

@@ -0,0 +1,22 @@
<?php
namespace Api;
/**
* App API controller
*
* @package api
* @author Frederic Guillot
*/
class App extends Base
{
public function getTimezone()
{
return $this->config->get('application_timezone');
}
public function getVersion()
{
return APP_VERSION;
}
}

81
app/Api/Base.php Normal file
View File

@@ -0,0 +1,81 @@
<?php
namespace Api;
use Pimple\Container;
use JsonRPC\AuthenticationFailure;
use Symfony\Component\EventDispatcher\Event;
/**
* Base class
*
* @package api
* @author Frederic Guillot
*
* @property \Model\Board $board
* @property \Model\Config $config
* @property \Model\Comment $comment
* @property \Model\LastLogin $lastLogin
* @property \Model\Notification $notification
* @property \Model\Project $project
* @property \Model\ProjectPermission $projectPermission
* @property \Model\ProjectActivity $projectActivity
* @property \Model\ProjectAnalytic $projectAnalytic
* @property \Model\ProjectDailySummary $projectDailySummary
* @property \Model\Subtask $subtask
* @property \Model\Task $task
* @property \Model\TaskDuplication $taskDuplication
* @property \Model\TaskExport $taskExport
* @property \Model\TaskFinder $taskFinder
*/
abstract class Base
{
/**
* Container instance
*
* @access protected
* @var \Pimple\Container
*/
protected $container;
/**
* Constructor
*
* @access public
* @param \Pimple\Container $container
*/
public function __construct(Container $container)
{
$this->container = $container;
}
/**
* Load automatically models
*
* @access public
* @param string $name Model name
* @return mixed
*/
public function __get($name)
{
return $this->container[$name];
}
/**
* Check api credentials
*
* @access public
* @param string $username
* @param string $password
* @param string $class
* @param string $method
*/
public function authentication($username, $password, $class, $method)
{
$this->container['dispatcher']->dispatch('api.bootstrap', new Event);
if (! ($username === 'jsonrpc' && $password === $this->config->get('api_token'))) {
throw new AuthenticationFailure('Wrond credentials');
}
}
}

52
app/Api/Board.php Normal file
View File

@@ -0,0 +1,52 @@
<?php
namespace Api;
/**
* Board API controller
*
* @package api
* @author Frederic Guillot
*/
class Board extends Base
{
public function getBoard($project_id)
{
return $this->board->getBoard($project_id);
}
public function getColumns($project_id)
{
return $this->board->getColumns($project_id);
}
public function getColumn($column_id)
{
return $this->board->getColumn($column_id);
}
public function moveColumnUp($project_id, $column_id)
{
return $this->board->moveUp($project_id, $column_id);
}
public function moveColumnDown($project_id, $column_id)
{
return $this->board->moveDown($project_id, $column_id);
}
public function updateColumn($column_id, $title, $task_limit = 0, $description = '')
{
return $this->board->updateColumn($column_id, $title, $task_limit, $description);
}
public function addColumn($project_id, $title, $task_limit = 0, $description = '')
{
return $this->board->addColumn($project_id, $title, $task_limit, $description);
}
public function removeColumn($column_id)
{
return $this->board->removeColumn($column_id);
}
}

49
app/Api/Category.php Normal file
View File

@@ -0,0 +1,49 @@
<?php
namespace Api;
/**
* Category API controller
*
* @package api
* @author Frederic Guillot
*/
class Category extends Base
{
public function getCategory($category_id)
{
return $this->category->getById($category_id);
}
public function getAllCategories($project_id)
{
return $this->category->getAll($project_id);
}
public function removeCategory($category_id)
{
return $this->category->remove($category_id);
}
public function createCategory($project_id, $name)
{
$values = array(
'project_id' => $project_id,
'name' => $name,
);
list($valid,) = $this->category->validateCreation($values);
return $valid ? $this->category->create($values) : false;
}
public function updateCategory($id, $name)
{
$values = array(
'id' => $id,
'name' => $name,
);
list($valid,) = $this->category->validateModification($values);
return $valid && $this->category->update($values);
}
}

51
app/Api/Comment.php Normal file
View File

@@ -0,0 +1,51 @@
<?php
namespace Api;
/**
* Comment API controller
*
* @package api
* @author Frederic Guillot
*/
class Comment extends Base
{
public function getComment($comment_id)
{
return $this->comment->getById($comment_id);
}
public function getAllComments($task_id)
{
return $this->comment->getAll($task_id);
}
public function removeComment($comment_id)
{
return $this->comment->remove($comment_id);
}
public function createComment($task_id, $user_id, $content)
{
$values = array(
'task_id' => $task_id,
'user_id' => $user_id,
'comment' => $content,
);
list($valid,) = $this->comment->validateCreation($values);
return $valid ? $this->comment->create($values) : false;
}
public function updateComment($id, $content)
{
$values = array(
'id' => $id,
'comment' => $content,
);
list($valid,) = $this->comment->validateModification($values);
return $valid && $this->comment->update($values);
}
}

111
app/Api/Link.php Normal file
View File

@@ -0,0 +1,111 @@
<?php
namespace Api;
/**
* Link API controller
*
* @package api
* @author Frederic Guillot
*/
class Link extends Base
{
/**
* Get a link by id
*
* @access public
* @param integer $link_id Link id
* @return array
*/
public function getLinkById($link_id)
{
return $this->link->getById($link_id);
}
/**
* Get a link by name
*
* @access public
* @param string $label
* @return array
*/
public function getLinkByLabel($label)
{
return $this->link->getByLabel($label);
}
/**
* Get the opposite link id
*
* @access public
* @param integer $link_id Link id
* @return integer
*/
public function getOppositeLinkId($link_id)
{
return $this->link->getOppositeLinkId($link_id);
}
/**
* Get all links
*
* @access public
* @return array
*/
public function getAllLinks()
{
return $this->link->getAll();
}
/**
* Create a new link label
*
* @access public
* @param string $label
* @param string $opposite_label
* @return boolean|integer
*/
public function createLink($label, $opposite_label = '')
{
$values = array(
'label' => $label,
'opposite_label' => $opposite_label,
);
list($valid,) = $this->link->validateCreation($values);
return $valid ? $this->link->create($label, $opposite_label) : false;
}
/**
* Update a link
*
* @access public
* @param integer $link_id
* @param integer $opposite_link_id
* @param string $label
* @return boolean
*/
public function updateLink($link_id, $opposite_link_id, $label)
{
$values = array(
'id' => $link_id,
'opposite_id' => $opposite_link_id,
'label' => $label,
);
list($valid,) = $this->link->validateModification($values);
return $valid && $this->link->update($values);
}
/**
* Remove a link a the relation to its opposite
*
* @access public
* @param integer $link_id
* @return boolean
*/
public function removeLink($link_id)
{
return $this->link->remove($link_id);
}
}

85
app/Api/Project.php Normal file
View File

@@ -0,0 +1,85 @@
<?php
namespace Api;
/**
* Project API controller
*
* @package api
* @author Frederic Guillot
*/
class Project extends Base
{
public function getProjectById($project_id)
{
return $this->project->getById($project_id);
}
public function getProjectByName($name)
{
return $this->project->getByName($name);
}
public function getAllProjects()
{
return $this->project->getAll();
}
public function removeProject($project_id)
{
return $this->project->remove($project_id);
}
public function enableProject($project_id)
{
return $this->project->enable($project_id);
}
public function disableProject($project_id)
{
return $this->project->disable($project_id);
}
public function enableProjectPublicAccess($project_id)
{
return $this->project->enablePublicAccess($project_id);
}
public function disableProjectPublicAccess($project_id)
{
return $this->project->disablePublicAccess($project_id);
}
public function getProjectActivities(array $project_ids)
{
return $this->projectActivity->getProjects($project_ids);
}
public function getProjectActivity($project_id)
{
return $this->projectActivity->getProject($project_id);
}
public function createProject($name, $description = null)
{
$values = array(
'name' => $name,
'description' => $description
);
list($valid,) = $this->project->validateCreation($values);
return $valid ? $this->project->create($values) : false;
}
public function updateProject($id, $name, $description = null)
{
$values = array(
'id' => $id,
'name' => $name,
'description' => $description
);
list($valid,) = $this->project->validateModification($values);
return $valid && $this->project->update($values);
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace Api;
/**
* ProjectPermission API controller
*
* @package api
* @author Frederic Guillot
*/
class ProjectPermission extends Base
{
public function getMembers($project_id)
{
return $this->projectPermission->getMembers($project_id);
}
public function revokeUser($project_id, $user_id)
{
return $this->projectPermission->revokeMember($project_id, $user_id);
}
public function allowUser($project_id, $user_id)
{
return $this->projectPermission->addMember($project_id, $user_id);
}
}

64
app/Api/Subtask.php Normal file
View File

@@ -0,0 +1,64 @@
<?php
namespace Api;
/**
* Subtask API controller
*
* @package api
* @author Frederic Guillot
*/
class Subtask extends Base
{
public function getSubtask($subtask_id)
{
return $this->subtask->getById($subtask_id);
}
public function getAllSubtasks($task_id)
{
return $this->subtask->getAll($task_id);
}
public function removeSubtask($subtask_id)
{
return $this->subtask->remove($subtask_id);
}
public function createSubtask($task_id, $title, $user_id = 0, $time_estimated = 0, $time_spent = 0, $status = 0)
{
$values = array(
'title' => $title,
'task_id' => $task_id,
'user_id' => $user_id,
'time_estimated' => $time_estimated,
'time_spent' => $time_spent,
'status' => $status,
);
list($valid,) = $this->subtask->validateCreation($values);
return $valid ? $this->subtask->create($values) : false;
}
public function updateSubtask($id, $task_id, $title = null, $user_id = null, $time_estimated = null, $time_spent = null, $status = null)
{
$values = array(
'id' => $id,
'task_id' => $task_id,
'title' => $title,
'user_id' => $user_id,
'time_estimated' => $time_estimated,
'time_spent' => $time_spent,
'status' => $status,
);
foreach ($values as $key => $value) {
if (is_null($value)) {
unset($values[$key]);
}
}
list($valid,) = $this->subtask->validateApiModification($values);
return $valid && $this->subtask->update($values);
}
}

77
app/Api/Swimlane.php Normal file
View File

@@ -0,0 +1,77 @@
<?php
namespace Api;
/**
* Swimlane API controller
*
* @package api
* @author Frederic Guillot
*/
class Swimlane extends Base
{
public function getActiveSwimlanes($project_id)
{
return $this->swimlane->getSwimlanes($project_id);
}
public function getAllSwimlanes($project_id)
{
return $this->swimlane->getAll($project_id);
}
public function getSwimlaneById($swimlane_id)
{
return $this->swimlane->getById($swimlane_id);
}
public function getSwimlaneByName($project_id, $name)
{
return $this->swimlane->getByName($project_id, $name);
}
public function getSwimlane($swimlane_id)
{
return $this->swimlane->getById($swimlane_id);
}
public function getDefaultSwimlane($project_id)
{
return $this->swimlane->getDefault($project_id);
}
public function addSwimlane($project_id, $name)
{
return $this->swimlane->create($project_id, $name);
}
public function updateSwimlane($swimlane_id, $name)
{
return $this->swimlane->rename($swimlane_id, $name);
}
public function removeSwimlane($project_id, $swimlane_id)
{
return $this->swimlane->remove($project_id, $swimlane_id);
}
public function disableSwimlane($project_id, $swimlane_id)
{
return $this->swimlane->disable($project_id, $swimlane_id);
}
public function enableSwimlane($project_id, $swimlane_id)
{
return $this->swimlane->enable($project_id, $swimlane_id);
}
public function moveSwimlaneUp($project_id, $swimlane_id)
{
return $this->swimlane->moveUp($project_id, $swimlane_id);
}
public function moveSwimlaneDown($project_id, $swimlane_id)
{
return $this->swimlane->moveDown($project_id, $swimlane_id);
}
}

113
app/Api/Task.php Normal file
View File

@@ -0,0 +1,113 @@
<?php
namespace Api;
use Model\Task as TaskModel;
/**
* Task API controller
*
* @package api
* @author Frederic Guillot
*/
class Task extends Base
{
public function getTask($task_id)
{
return $this->taskFinder->getById($task_id);
}
public function getAllTasks($project_id, $status_id = TaskModel::STATUS_OPEN)
{
return $this->taskFinder->getAll($project_id, $status_id);
}
public function getOverdueTasks()
{
return $this->taskFinder->getOverdueTasks();
}
public function openTask($task_id)
{
return $this->taskStatus->open($task_id);
}
public function closeTask($task_id)
{
return $this->taskStatus->close($task_id);
}
public function removeTask($task_id)
{
return $this->task->remove($task_id);
}
public function moveTaskPosition($project_id, $task_id, $column_id, $position, $swimlane_id = 0)
{
return $this->taskPosition->movePosition($project_id, $task_id, $column_id, $position, $swimlane_id);
}
public function createTask($title, $project_id, $color_id = '', $column_id = 0, $owner_id = 0, $creator_id = 0,
$date_due = '', $description = '', $category_id = 0, $score = 0, $swimlane_id = 0,
$recurrence_status = 0, $recurrence_trigger = 0, $recurrence_factor = 0, $recurrence_timeframe = 0,
$recurrence_basedate = 0)
{
$values = array(
'title' => $title,
'project_id' => $project_id,
'color_id' => $color_id,
'column_id' => $column_id,
'owner_id' => $owner_id,
'creator_id' => $creator_id,
'date_due' => $date_due,
'description' => $description,
'category_id' => $category_id,
'score' => $score,
'swimlane_id' => $swimlane_id,
'recurrence_status' => $recurrence_status,
'recurrence_trigger' => $recurrence_trigger,
'recurrence_factor' => $recurrence_factor,
'recurrence_timeframe' => $recurrence_timeframe,
'recurrence_basedate' => $recurrence_basedate,
);
list($valid,) = $this->taskValidator->validateCreation($values);
return $valid ? $this->taskCreation->create($values) : false;
}
public function updateTask($id, $title = null, $project_id = null, $color_id = null, $column_id = null, $owner_id = null,
$creator_id = null, $date_due = null, $description = null, $category_id = null, $score = null,
$swimlane_id = null, $recurrence_status = null, $recurrence_trigger = null, $recurrence_factor = null,
$recurrence_timeframe = null, $recurrence_basedate = null)
{
$values = array(
'id' => $id,
'title' => $title,
'project_id' => $project_id,
'color_id' => $color_id,
'column_id' => $column_id,
'owner_id' => $owner_id,
'creator_id' => $creator_id,
'date_due' => $date_due,
'description' => $description,
'category_id' => $category_id,
'score' => $score,
'swimlane_id' => $swimlane_id,
'recurrence_status' => $recurrence_status,
'recurrence_trigger' => $recurrence_trigger,
'recurrence_factor' => $recurrence_factor,
'recurrence_timeframe' => $recurrence_timeframe,
'recurrence_basedate' => $recurrence_basedate,
);
foreach ($values as $key => $value) {
if (is_null($value)) {
unset($values[$key]);
}
}
list($valid) = $this->taskValidator->validateApiModification($values);
return $valid && $this->taskModification->update($values);
}
}

77
app/Api/TaskLink.php Normal file
View File

@@ -0,0 +1,77 @@
<?php
namespace Api;
/**
* TaskLink API controller
*
* @package api
* @author Frederic Guillot
*/
class TaskLink extends Base
{
/**
* Get a task link
*
* @access public
* @param integer $task_link_id Task link id
* @return array
*/
public function getTaskLinkById($task_link_id)
{
return $this->taskLink->getById($task_link_id);
}
/**
* Get all links attached to a task
*
* @access public
* @param integer $task_id Task id
* @return array
*/
public function getAllTaskLinks($task_id)
{
return $this->taskLink->getAll($task_id);
}
/**
* Create a new link
*
* @access public
* @param integer $task_id Task id
* @param integer $opposite_task_id Opposite task id
* @param integer $link_id Link id
* @return integer Task link id
*/
public function createTaskLink($task_id, $opposite_task_id, $link_id)
{
return $this->taskLink->create($task_id, $opposite_task_id, $link_id);
}
/**
* Update a task link
*
* @access public
* @param integer $task_link_id Task link id
* @param integer $task_id Task id
* @param integer $opposite_task_id Opposite task id
* @param integer $link_id Link id
* @return boolean
*/
public function updateTaskLink($task_link_id, $task_id, $opposite_task_id, $link_id)
{
return $this->taskLink->update($task_link_id, $task_id, $opposite_task_id, $link_id);
}
/**
* Remove a link between two tasks
*
* @access public
* @param integer $task_link_id
* @return boolean
*/
public function removeTaskLink($task_link_id)
{
return $this->taskLink->remove($task_link_id);
}
}

88
app/Api/User.php Normal file
View File

@@ -0,0 +1,88 @@
<?php
namespace Api;
use Auth\Ldap;
/**
* User API controller
*
* @package api
* @author Frederic Guillot
*/
class User extends Base
{
public function getUser($user_id)
{
return $this->user->getById($user_id);
}
public function getAllUsers()
{
return $this->user->getAll();
}
public function removeUser($user_id)
{
return $this->user->remove($user_id);
}
public function createUser($username, $password, $name = '', $email = '', $is_admin = 0, $default_project_id = 0)
{
$values = array(
'username' => $username,
'password' => $password,
'confirmation' => $password,
'name' => $name,
'email' => $email,
'is_admin' => $is_admin,
'default_project_id' => $default_project_id,
);
list($valid,) = $this->user->validateCreation($values);
return $valid ? $this->user->create($values) : false;
}
public function createLdapUser($username = '', $email = '', $is_admin = 0, $default_project_id = 0)
{
$ldap = new Ldap($this->container);
$user = $ldap->lookup($username, $email);
if (! $user) {
return false;
}
$values = array(
'username' => $user['username'],
'name' => $user['name'],
'email' => $user['email'],
'is_ldap_user' => 1,
'is_admin' => $is_admin,
'default_project_id' => $default_project_id,
);
return $this->user->create($values);
}
public function updateUser($id, $username = null, $name = null, $email = null, $is_admin = null, $default_project_id = null)
{
$values = array(
'id' => $id,
'username' => $username,
'name' => $name,
'email' => $email,
'is_admin' => $is_admin,
'default_project_id' => $default_project_id,
);
foreach ($values as $key => $value) {
if (is_null($value)) {
unset($values[$key]);
}
}
list($valid,) = $this->user->validateApiModification($values);
return $valid && $this->user->update($values);
}
}

View File

@@ -112,7 +112,7 @@ class Link extends Base
* @access public
* @param string $label
* @param string $opposite_label
* @return boolean
* @return boolean|integer
*/
public function create($label, $opposite_label = '')
{
@@ -123,38 +123,28 @@ class Link extends Base
return false;
}
$label_id = $this->db->getConnection()->getLastId();
if ($opposite_label !== '') {
$this->createOpposite($opposite_label);
$this->db
->table(self::TABLE)
->insert(array(
'label' => $opposite_label,
'opposite_id' => $label_id,
));
$this->db
->table(self::TABLE)
->eq('id', $label_id)
->update(array(
'opposite_id' => $this->db->getConnection()->getLastId()
));
}
$this->db->closeTransaction();
return true;
}
/**
* Create the opposite label (executed inside create() method)
*
* @access private
* @param string $label
*/
private function createOpposite($label)
{
$label_id = $this->db->getConnection()->getLastId();
$this->db
->table(self::TABLE)
->insert(array(
'label' => $label,
'opposite_id' => $label_id,
));
$this->db
->table(self::TABLE)
->eq('id', $label_id)
->update(array(
'opposite_id' => $this->db->getConnection()->getLastId()
));
return $label_id;
}
/**

View File

@@ -87,7 +87,7 @@ class Swimlane extends Base
return $this->db->table(self::TABLE)
->eq('project_id', $project_id)
->eq('name', $name)
->findAll();
->findOne();
}
/**