Split Board model into multiple classes

This commit is contained in:
Frederic Guillot
2016-02-20 15:08:18 -05:00
parent 8383252286
commit fc468088c3
39 changed files with 511 additions and 655 deletions

View File

@@ -150,8 +150,8 @@ class ActionParameter extends Base
case 'dest_column_id':
case 'dst_column_id':
case 'column_id':
$column = $this->board->getColumn($value);
return empty($column) ? false : $this->board->getColumnIdByTitle($project_id, $column['title']) ?: false;
$column = $this->column->getById($value);
return empty($column) ? false : $this->column->getColumnIdByTitle($project_id, $column['title']) ?: false;
case 'user_id':
case 'owner_id':
return $this->projectPermission->isAssignable($project_id, $value) ? $value : false;

View File

@@ -12,13 +12,6 @@ use PicoDb\Database;
*/
class Board extends Base
{
/**
* SQL table name
*
* @var string
*/
const TABLE = 'columns';
/**
* Get Kanboard default columns
*
@@ -73,7 +66,7 @@ class Board extends Base
'description' => $column['description'],
);
if (! $this->db->table(self::TABLE)->save($values)) {
if (! $this->db->table(Column::TABLE)->save($values)) {
return false;
}
}
@@ -91,7 +84,7 @@ class Board extends Base
*/
public function duplicate($project_from, $project_to)
{
$columns = $this->db->table(Board::TABLE)
$columns = $this->db->table(Column::TABLE)
->columns('title', 'task_limit', 'description')
->eq('project_id', $project_from)
->asc('position')
@@ -100,134 +93,6 @@ class Board extends Base
return $this->board->create($project_to, $columns);
}
/**
* Add a new column to the board
*
* @access public
* @param integer $project_id Project id
* @param string $title Column title
* @param integer $task_limit Task limit
* @param string $description Column description
* @return boolean|integer
*/
public function addColumn($project_id, $title, $task_limit = 0, $description = '')
{
$values = array(
'project_id' => $project_id,
'title' => $title,
'task_limit' => intval($task_limit),
'position' => $this->getLastColumnPosition($project_id) + 1,
'description' => $description,
);
return $this->persist(self::TABLE, $values);
}
/**
* Update a column
*
* @access public
* @param integer $column_id Column id
* @param string $title Column title
* @param integer $task_limit Task limit
* @param string $description Optional description
* @return boolean
*/
public function updateColumn($column_id, $title, $task_limit = 0, $description = '')
{
return $this->db->table(self::TABLE)->eq('id', $column_id)->update(array(
'title' => $title,
'task_limit' => intval($task_limit),
'description' => $description,
));
}
/**
* Get columns with consecutive positions
*
* If you remove a column, the positions are not anymore consecutives
*
* @access public
* @param integer $project_id
* @return array
*/
public function getNormalizedColumnPositions($project_id)
{
$columns = $this->db->hashtable(self::TABLE)->eq('project_id', $project_id)->asc('position')->getAll('id', 'position');
$position = 1;
foreach ($columns as $column_id => $column_position) {
$columns[$column_id] = $position++;
}
return $columns;
}
/**
* Save the new positions for a set of columns
*
* @access public
* @param array $columns Hashmap of column_id/column_position
* @return boolean
*/
public function saveColumnPositions(array $columns)
{
return $this->db->transaction(function (Database $db) use ($columns) {
foreach ($columns as $column_id => $position) {
if (! $db->table(Board::TABLE)->eq('id', $column_id)->update(array('position' => $position))) {
return false;
}
}
});
}
/**
* Move a column down, increment the column position value
*
* @access public
* @param integer $project_id Project id
* @param integer $column_id Column id
* @return boolean
*/
public function moveDown($project_id, $column_id)
{
$columns = $this->getNormalizedColumnPositions($project_id);
$positions = array_flip($columns);
if (isset($columns[$column_id]) && $columns[$column_id] < count($columns)) {
$position = ++$columns[$column_id];
$columns[$positions[$position]]--;
return $this->saveColumnPositions($columns);
}
return false;
}
/**
* Move a column up, decrement the column position value
*
* @access public
* @param integer $project_id Project id
* @param integer $column_id Column id
* @return boolean
*/
public function moveUp($project_id, $column_id)
{
$columns = $this->getNormalizedColumnPositions($project_id);
$positions = array_flip($columns);
if (isset($columns[$column_id]) && $columns[$column_id] > 1) {
$position = --$columns[$column_id];
$columns[$positions[$position]]++;
return $this->saveColumnPositions($columns);
}
return false;
}
/**
* Get all tasks sorted by columns and swimlanes
*
@@ -239,7 +104,7 @@ class Board extends Base
public function getBoard($project_id, $callback = null)
{
$swimlanes = $this->swimlane->getSwimlanes($project_id);
$columns = $this->getColumns($project_id);
$columns = $this->column->getAll($project_id);
$nb_columns = count($columns);
for ($i = 0, $ilen = count($swimlanes); $i < $ilen; $i++) {
@@ -307,131 +172,4 @@ class Board extends Base
return $prepend ? array(-1 => t('All columns')) + $listing : $listing;
}
/**
* Get the first column id for a given project
*
* @access public
* @param integer $project_id Project id
* @return integer
*/
public function getFirstColumn($project_id)
{
return $this->db->table(self::TABLE)->eq('project_id', $project_id)->asc('position')->findOneColumn('id');
}
/**
* Get the last column id for a given project
*
* @access public
* @param integer $project_id Project id
* @return integer
*/
public function getLastColumn($project_id)
{
return $this->db->table(self::TABLE)->eq('project_id', $project_id)->desc('position')->findOneColumn('id');
}
/**
* Get the list of columns sorted by position [ column_id => title ]
*
* @access public
* @param integer $project_id Project id
* @param boolean $prepend Prepend a default value
* @return array
*/
public function getColumnsList($project_id, $prepend = false)
{
$listing = $this->db->hashtable(self::TABLE)->eq('project_id', $project_id)->asc('position')->getAll('id', 'title');
return $prepend ? array(-1 => t('All columns')) + $listing : $listing;
}
/**
* Get all columns sorted by position for a given project
*
* @access public
* @param integer $project_id Project id
* @return array
*/
public function getColumns($project_id)
{
return $this->db->table(self::TABLE)->eq('project_id', $project_id)->asc('position')->findAll();
}
/**
* Get the number of columns for a given project
*
* @access public
* @param integer $project_id Project id
* @return integer
*/
public function countColumns($project_id)
{
return $this->db->table(self::TABLE)->eq('project_id', $project_id)->count();
}
/**
* Get a column by the id
*
* @access public
* @param integer $column_id Column id
* @return array
*/
public function getColumn($column_id)
{
return $this->db->table(self::TABLE)->eq('id', $column_id)->findOne();
}
/**
* Get a column id by the name
*
* @access public
* @param integer $project_id
* @param string $title
* @return integer
*/
public function getColumnIdByTitle($project_id, $title)
{
return (int) $this->db->table(self::TABLE)->eq('project_id', $project_id)->eq('title', $title)->findOneColumn('id');
}
/**
* Get a column title by the id
*
* @access public
* @param integer $column_id
* @return integer
*/
public function getColumnTitleById($column_id)
{
return $this->db->table(self::TABLE)->eq('id', $column_id)->findOneColumn('title');
}
/**
* Get the position of the last column for a given project
*
* @access public
* @param integer $project_id Project id
* @return integer
*/
public function getLastColumnPosition($project_id)
{
return (int) $this->db
->table(self::TABLE)
->eq('project_id', $project_id)
->desc('position')
->findOneColumn('position');
}
/**
* Remove a column and all tasks associated to this column
*
* @access public
* @param integer $column_id Column id
* @return boolean
*/
public function removeColumn($column_id)
{
return $this->db->table(self::TABLE)->eq('id', $column_id)->remove();
}
}

View File

@@ -17,6 +17,83 @@ class Column extends Base
*/
const TABLE = 'columns';
/**
* Get a column by the id
*
* @access public
* @param integer $column_id Column id
* @return array
*/
public function getById($column_id)
{
return $this->db->table(self::TABLE)->eq('id', $column_id)->findOne();
}
/**
* Get the first column id for a given project
*
* @access public
* @param integer $project_id Project id
* @return integer
*/
public function getFirstColumnId($project_id)
{
return $this->db->table(self::TABLE)->eq('project_id', $project_id)->asc('position')->findOneColumn('id');
}
/**
* Get the last column id for a given project
*
* @access public
* @param integer $project_id Project id
* @return integer
*/
public function getLastColumnId($project_id)
{
return $this->db->table(self::TABLE)->eq('project_id', $project_id)->desc('position')->findOneColumn('id');
}
/**
* Get the position of the last column for a given project
*
* @access public
* @param integer $project_id Project id
* @return integer
*/
public function getLastColumnPosition($project_id)
{
return (int) $this->db
->table(self::TABLE)
->eq('project_id', $project_id)
->desc('position')
->findOneColumn('position');
}
/**
* Get a column id by the name
*
* @access public
* @param integer $project_id
* @param string $title
* @return integer
*/
public function getColumnIdByTitle($project_id, $title)
{
return (int) $this->db->table(self::TABLE)->eq('project_id', $project_id)->eq('title', $title)->findOneColumn('id');
}
/**
* Get a column title by the id
*
* @access public
* @param integer $column_id
* @return integer
*/
public function getColumnTitleById($column_id)
{
return $this->db->table(self::TABLE)->eq('id', $column_id)->findOneColumn('title');
}
/**
* Get all columns sorted by position for a given project
*
@@ -29,6 +106,74 @@ class Column extends Base
return $this->db->table(self::TABLE)->eq('project_id', $project_id)->asc('position')->findAll();
}
/**
* Get the list of columns sorted by position [ column_id => title ]
*
* @access public
* @param integer $project_id Project id
* @param boolean $prepend Prepend a default value
* @return array
*/
public function getList($project_id, $prepend = false)
{
$listing = $this->db->hashtable(self::TABLE)->eq('project_id', $project_id)->asc('position')->getAll('id', 'title');
return $prepend ? array(-1 => t('All columns')) + $listing : $listing;
}
/**
* Add a new column to the board
*
* @access public
* @param integer $project_id Project id
* @param string $title Column title
* @param integer $task_limit Task limit
* @param string $description Column description
* @return boolean|integer
*/
public function create($project_id, $title, $task_limit = 0, $description = '')
{
$values = array(
'project_id' => $project_id,
'title' => $title,
'task_limit' => intval($task_limit),
'position' => $this->getLastColumnPosition($project_id) + 1,
'description' => $description,
);
return $this->persist(self::TABLE, $values);
}
/**
* Update a column
*
* @access public
* @param integer $column_id Column id
* @param string $title Column title
* @param integer $task_limit Task limit
* @param string $description Optional description
* @return boolean
*/
public function update($column_id, $title, $task_limit = 0, $description = '')
{
return $this->db->table(self::TABLE)->eq('id', $column_id)->update(array(
'title' => $title,
'task_limit' => intval($task_limit),
'description' => $description,
));
}
/**
* Remove a column and all tasks associated to this column
*
* @access public
* @param integer $column_id Column id
* @return boolean
*/
public function remove($column_id)
{
return $this->db->table(self::TABLE)->eq('id', $column_id)->remove();
}
/**
* Change column position
*

View File

@@ -241,7 +241,7 @@ class Project extends Base
{
$stats = array();
$stats['nb_active_tasks'] = 0;
$columns = $this->board->getColumns($project_id);
$columns = $this->column->getAll($project_id);
$column_stats = $this->board->getColumnStats($project_id);
foreach ($columns as &$column) {
@@ -265,7 +265,7 @@ class Project extends Base
*/
public function getColumnStats(array &$project)
{
$project['columns'] = $this->board->getColumns($project['id']);
$project['columns'] = $this->column->getAll($project['id']);
$stats = $this->board->getColumnStats($project['id']);
foreach ($project['columns'] as &$column) {

View File

@@ -84,7 +84,7 @@ class ProjectDailyColumnStats extends Base
*/
public function getAggregatedMetrics($project_id, $from, $to, $field = 'total')
{
$columns = $this->board->getColumnsList($project_id);
$columns = $this->column->getList($project_id);
$metrics = $this->getMetrics($project_id, $from, $to);
return $this->buildAggregate($metrics, $columns, $field);
}

View File

@@ -48,7 +48,7 @@ class TaskAnalytic extends Base
public function getTimeSpentByColumn(array $task)
{
$result = array();
$columns = $this->board->getColumnsList($task['project_id']);
$columns = $this->column->getList($task['project_id']);
$sums = $this->transition->getTimeSpentByTask($task['id']);
foreach ($columns as $column_id => $column_title) {

View File

@@ -56,7 +56,7 @@ class TaskCreation extends Base
$this->resetFields($values, array('date_started', 'creator_id', 'owner_id', 'swimlane_id', 'date_due', 'score', 'category_id', 'time_estimated'));
if (empty($values['column_id'])) {
$values['column_id'] = $this->board->getFirstColumn($values['project_id']);
$values['column_id'] = $this->column->getFirstColumnId($values['project_id']);
}
if (empty($values['color_id'])) {

View File

@@ -64,7 +64,7 @@ class TaskDuplication extends Base
if ($values['recurrence_status'] == Task::RECURRING_STATUS_PENDING) {
$values['recurrence_parent'] = $task_id;
$values['column_id'] = $this->board->getFirstColumn($values['project_id']);
$values['column_id'] = $this->column->getFirstColumnId($values['project_id']);
$this->calculateRecurringTaskDueDate($values);
$recurring_task_id = $this->save($task_id, $values);
@@ -181,12 +181,12 @@ class TaskDuplication extends Base
// Check if the column exists for the destination project
if ($values['column_id'] > 0) {
$values['column_id'] = $this->board->getColumnIdByTitle(
$values['column_id'] = $this->column->getColumnIdByTitle(
$values['project_id'],
$this->board->getColumnTitleById($values['column_id'])
$this->column->getColumnTitleById($values['column_id'])
);
$values['column_id'] = $values['column_id'] ?: $this->board->getFirstColumn($values['project_id']);
$values['column_id'] = $values['column_id'] ?: $this->column->getFirstColumnId($values['project_id']);
}
return $values;

View File

@@ -469,7 +469,7 @@ class TaskFilter extends Base
$this->query->beginOr();
foreach ($values as $project) {
$this->query->ilike(Board::TABLE.'.title', $project);
$this->query->ilike(Column::TABLE.'.title', $project);
}
$this->query->closeOr();

View File

@@ -38,14 +38,14 @@ class TaskFinder extends Base
Task::TABLE.'.time_spent',
Task::TABLE.'.time_estimated',
Project::TABLE.'.name AS project_name',
Board::TABLE.'.title AS column_name',
Column::TABLE.'.title AS column_name',
User::TABLE.'.username AS assignee_username',
User::TABLE.'.name AS assignee_name'
)
->eq(Task::TABLE.'.is_active', $is_active)
->in(Project::TABLE.'.id', $project_ids)
->join(Project::TABLE, 'id', 'project_id')
->join(Board::TABLE, 'id', 'column_id', Task::TABLE)
->join(Column::TABLE, 'id', 'column_id', Task::TABLE)
->join(User::TABLE, 'id', 'owner_id', Task::TABLE);
}
@@ -129,15 +129,15 @@ class TaskFinder extends Base
User::TABLE.'.name AS assignee_name',
Category::TABLE.'.name AS category_name',
Category::TABLE.'.description AS category_description',
Board::TABLE.'.title AS column_name',
Board::TABLE.'.position AS column_position',
Column::TABLE.'.title AS column_name',
Column::TABLE.'.position AS column_position',
Swimlane::TABLE.'.name AS swimlane_name',
Project::TABLE.'.default_swimlane',
Project::TABLE.'.name AS project_name'
)
->join(User::TABLE, 'id', 'owner_id', Task::TABLE)
->join(Category::TABLE, 'id', 'category_id', Task::TABLE)
->join(Board::TABLE, 'id', 'column_id', Task::TABLE)
->join(Column::TABLE, 'id', 'column_id', Task::TABLE)
->join(Swimlane::TABLE, 'id', 'swimlane_id', Task::TABLE)
->join(Project::TABLE, 'id', 'project_id', Task::TABLE);
}

View File

@@ -111,7 +111,7 @@ class TaskImport extends Base
}
if (! empty($row['column'])) {
$values['column_id'] = $this->board->getColumnIdByTitle($this->projectId, $row['column']);
$values['column_id'] = $this->column->getColumnIdByTitle($this->projectId, $row['column']);
}
if (! empty($row['category'])) {

View File

@@ -81,17 +81,17 @@ class TaskLink extends Base
Task::TABLE.'.owner_id AS task_assignee_id',
User::TABLE.'.username AS task_assignee_username',
User::TABLE.'.name AS task_assignee_name',
Board::TABLE.'.title AS column_title',
Column::TABLE.'.title AS column_title',
Project::TABLE.'.name AS project_name'
)
->eq(self::TABLE.'.task_id', $task_id)
->join(Link::TABLE, 'id', 'link_id')
->join(Task::TABLE, 'id', 'opposite_task_id')
->join(Board::TABLE, 'id', 'column_id', Task::TABLE)
->join(Column::TABLE, 'id', 'column_id', Task::TABLE)
->join(User::TABLE, 'id', 'owner_id', Task::TABLE)
->join(Project::TABLE, 'id', 'project_id', Task::TABLE)
->asc(Link::TABLE.'.id')
->desc(Board::TABLE.'.position')
->desc(Column::TABLE.'.position')
->desc(Task::TABLE.'.is_active')
->asc(Task::TABLE.'.position')
->asc(Task::TABLE.'.id')

View File

@@ -76,8 +76,8 @@ class Transition extends Base
->eq('task_id', $task_id)
->desc('date')
->join(User::TABLE, 'id', 'user_id')
->join(Board::TABLE.' as src', 'id', 'src_column_id', self::TABLE, 'src')
->join(Board::TABLE.' as dst', 'id', 'dst_column_id', self::TABLE, 'dst')
->join(Column::TABLE.' as src', 'id', 'src_column_id', self::TABLE, 'src')
->join(Column::TABLE.' as dst', 'id', 'dst_column_id', self::TABLE, 'dst')
->findAll();
}
@@ -118,8 +118,8 @@ class Transition extends Base
->desc('date')
->join(Task::TABLE, 'id', 'task_id')
->join(User::TABLE, 'id', 'user_id')
->join(Board::TABLE.' as src', 'id', 'src_column_id', self::TABLE, 'src')
->join(Board::TABLE.' as dst', 'id', 'dst_column_id', self::TABLE, 'dst')
->join(Column::TABLE.' as src', 'id', 'src_column_id', self::TABLE, 'src')
->join(Column::TABLE.' as dst', 'id', 'dst_column_id', self::TABLE, 'dst')
->findAll();
}