Use BoardFormatter to generate the board
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace Kanboard\Api;
|
||||
|
||||
use Kanboard\Formatter\BoardFormatter;
|
||||
|
||||
/**
|
||||
* Board API controller
|
||||
*
|
||||
@@ -13,6 +15,10 @@ class BoardApi extends BaseApi
|
||||
public function getBoard($project_id)
|
||||
{
|
||||
$this->checkProjectPermission($project_id);
|
||||
return $this->boardModel->getBoard($project_id);
|
||||
|
||||
return BoardFormatter::getInstance($this->container)
|
||||
->withProjectId($project_id)
|
||||
->withQuery($this->taskFinderModel->getExtendedQuery())
|
||||
->format();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,7 +134,7 @@ class BoardAjaxController extends BaseController
|
||||
'board_highlight_period' => $this->configModel->get('board_highlight_period'),
|
||||
'swimlanes' => $this->taskLexer
|
||||
->build($this->userSession->getFilters($project_id))
|
||||
->format(BoardFormatter::getInstance($this->container)->setProjectId($project_id))
|
||||
->format(BoardFormatter::getInstance($this->container)->withProjectId($project_id))
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,11 @@ class BoardViewController extends BaseController
|
||||
|
||||
$this->response->html($this->helper->layout->app('board/view_public', array(
|
||||
'project' => $project,
|
||||
'swimlanes' => $this->boardModel->getBoard($project['id']),
|
||||
'swimlanes' => BoardFormatter::getInstance($this->container)
|
||||
->withProjectId($project['id'])
|
||||
->withQuery($this->taskFinderModel->getExtendedQuery())
|
||||
->format()
|
||||
,
|
||||
'title' => $project['name'],
|
||||
'description' => $project['description'],
|
||||
'no_layout' => true,
|
||||
@@ -59,7 +63,7 @@ class BoardViewController extends BaseController
|
||||
'board_highlight_period' => $this->configModel->get('board_highlight_period'),
|
||||
'swimlanes' => $this->taskLexer
|
||||
->build($search)
|
||||
->format(BoardFormatter::getInstance($this->container)->setProjectId($project['id']))
|
||||
->format(BoardFormatter::getInstance($this->container)->withProjectId($project['id']))
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,8 +43,7 @@ abstract class BaseFilter
|
||||
*/
|
||||
public static function getInstance($value = null)
|
||||
{
|
||||
$self = new static($value);
|
||||
return $self;
|
||||
return new static($value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
namespace Kanboard\Formatter;
|
||||
|
||||
use Kanboard\Core\Base;
|
||||
use Kanboard\Core\Filter\FormatterInterface;
|
||||
use PicoDb\Table;
|
||||
use Pimple\Container;
|
||||
|
||||
/**
|
||||
* Class BaseFormatter
|
||||
@@ -22,12 +22,25 @@ abstract class BaseFormatter extends Base
|
||||
*/
|
||||
protected $query;
|
||||
|
||||
/**
|
||||
* Get object instance
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @param Container $container
|
||||
* @return static
|
||||
*/
|
||||
public static function getInstance(Container $container)
|
||||
{
|
||||
return new static($container);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set query
|
||||
*
|
||||
* @access public
|
||||
* @param Table $query
|
||||
* @return FormatterInterface
|
||||
* @return $this
|
||||
*/
|
||||
public function withQuery(Table $query)
|
||||
{
|
||||
|
||||
79
app/Formatter/BoardColumnFormatter.php
Normal file
79
app/Formatter/BoardColumnFormatter.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace Kanboard\Formatter;
|
||||
|
||||
use Kanboard\Core\Filter\FormatterInterface;
|
||||
|
||||
/**
|
||||
* Board Column Formatter
|
||||
*
|
||||
* @package formatter
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class BoardColumnFormatter extends BaseFormatter implements FormatterInterface
|
||||
{
|
||||
protected $swimlaneId = 0;
|
||||
protected $columns = array();
|
||||
protected $tasks = array();
|
||||
|
||||
/**
|
||||
* Set swimlaneId
|
||||
*
|
||||
* @access public
|
||||
* @param integer $swimlaneId
|
||||
* @return $this
|
||||
*/
|
||||
public function withSwimlaneId($swimlaneId)
|
||||
{
|
||||
$this->swimlaneId = $swimlaneId;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set columns
|
||||
*
|
||||
* @access public
|
||||
* @param array $columns
|
||||
* @return $this
|
||||
*/
|
||||
public function withColumns(array $columns)
|
||||
{
|
||||
$this->columns = $columns;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set tasks
|
||||
*
|
||||
* @access public
|
||||
* @param array $tasks
|
||||
* @return $this
|
||||
*/
|
||||
public function withTasks(array $tasks)
|
||||
{
|
||||
$this->tasks = $tasks;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply formatter
|
||||
*
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function format()
|
||||
{
|
||||
foreach ($this->columns as &$column) {
|
||||
$column['tasks'] = BoardTaskFormatter::getInstance($this->container)
|
||||
->withTasks($this->tasks)
|
||||
->withSwimlaneId($this->swimlaneId)
|
||||
->withColumnId($column['id'])
|
||||
->format();
|
||||
|
||||
$column['nb_tasks'] = count($column['tasks']);
|
||||
$column['score'] = (int) array_column_sum($column['tasks'], 'score');
|
||||
}
|
||||
|
||||
return $this->columns;
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@ class BoardFormatter extends BaseFormatter implements FormatterInterface
|
||||
* @param integer $projectId
|
||||
* @return $this
|
||||
*/
|
||||
public function setProjectId($projectId)
|
||||
public function withProjectId($projectId)
|
||||
{
|
||||
$this->projectId = $projectId;
|
||||
return $this;
|
||||
@@ -42,15 +42,22 @@ class BoardFormatter extends BaseFormatter implements FormatterInterface
|
||||
*/
|
||||
public function format()
|
||||
{
|
||||
$swimlanes = $this->swimlaneModel->getSwimlanes($this->projectId);
|
||||
$columns = $this->columnModel->getAll($this->projectId);
|
||||
|
||||
$tasks = $this->query
|
||||
->eq(TaskModel::TABLE.'.project_id', $this->projectId)
|
||||
->asc(TaskModel::TABLE.'.position')
|
||||
->findAll();
|
||||
|
||||
return $this->boardModel->getBoard($this->projectId, function ($project_id, $column_id, $swimlane_id) use ($tasks) {
|
||||
return array_filter($tasks, function (array $task) use ($column_id, $swimlane_id) {
|
||||
return $task['column_id'] == $column_id && $task['swimlane_id'] == $swimlane_id;
|
||||
});
|
||||
});
|
||||
if (empty($swimlanes) || empty($columns)) {
|
||||
return array();
|
||||
}
|
||||
|
||||
return BoardSwimlaneFormatter::getInstance($this->container)
|
||||
->withSwimlanes($swimlanes)
|
||||
->withColumns($columns)
|
||||
->withTasks($tasks)
|
||||
->format();
|
||||
}
|
||||
}
|
||||
|
||||
105
app/Formatter/BoardSwimlaneFormatter.php
Normal file
105
app/Formatter/BoardSwimlaneFormatter.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
namespace Kanboard\Formatter;
|
||||
|
||||
use Kanboard\Core\Filter\FormatterInterface;
|
||||
|
||||
/**
|
||||
* Board Swimlane Formatter
|
||||
*
|
||||
* @package formatter
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class BoardSwimlaneFormatter extends BaseFormatter implements FormatterInterface
|
||||
{
|
||||
protected $swimlanes = array();
|
||||
protected $columns = array();
|
||||
protected $tasks = array();
|
||||
|
||||
/**
|
||||
* Set swimlanes
|
||||
*
|
||||
* @access public
|
||||
* @param array $swimlanes
|
||||
* @return $this
|
||||
*/
|
||||
public function withSwimlanes($swimlanes)
|
||||
{
|
||||
$this->swimlanes = $swimlanes;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set columns
|
||||
*
|
||||
* @access public
|
||||
* @param array $columns
|
||||
* @return $this
|
||||
*/
|
||||
public function withColumns($columns)
|
||||
{
|
||||
$this->columns = $columns;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set tasks
|
||||
*
|
||||
* @access public
|
||||
* @param array $tasks
|
||||
* @return $this
|
||||
*/
|
||||
public function withTasks(array $tasks)
|
||||
{
|
||||
$this->tasks = $tasks;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply formatter
|
||||
*
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function format()
|
||||
{
|
||||
$nb_swimlanes = count($this->swimlanes);
|
||||
$nb_columns = count($this->columns);
|
||||
|
||||
foreach ($this->swimlanes as &$swimlane) {
|
||||
$swimlane['columns'] = BoardColumnFormatter::getInstance($this->container)
|
||||
->withSwimlaneId($swimlane['id'])
|
||||
->withColumns($this->columns)
|
||||
->withTasks($this->tasks)
|
||||
->format();
|
||||
|
||||
$swimlane['nb_swimlanes'] = $nb_swimlanes;
|
||||
$swimlane['nb_columns'] = $nb_columns;
|
||||
$swimlane['nb_tasks'] = array_column_sum($swimlane['columns'], 'nb_tasks');
|
||||
$swimlane['score'] = array_column_sum($swimlane['columns'], 'score');
|
||||
|
||||
$this->calculateStatsByColumnAcrossSwimlanes($swimlane['columns']);
|
||||
}
|
||||
|
||||
return $this->swimlanes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate stats for each column acrosss all swimlanes
|
||||
*
|
||||
* @access protected
|
||||
* @param array $columns
|
||||
*/
|
||||
protected function calculateStatsByColumnAcrossSwimlanes(array $columns)
|
||||
{
|
||||
foreach ($columns as $columnIndex => $column) {
|
||||
if (! isset($this->swimlanes[0]['columns'][$columnIndex]['column_nb_tasks'])) {
|
||||
$this->swimlanes[0]['columns'][$columnIndex]['column_nb_tasks'] = 0;
|
||||
$this->swimlanes[0]['columns'][$columnIndex]['column_score'] = 0;
|
||||
}
|
||||
|
||||
$this->swimlanes[0]['columns'][$columnIndex]['column_nb_tasks'] += $column['nb_tasks'];
|
||||
$this->swimlanes[0]['columns'][$columnIndex]['column_score'] += $column['score'];
|
||||
}
|
||||
}
|
||||
}
|
||||
80
app/Formatter/BoardTaskFormatter.php
Normal file
80
app/Formatter/BoardTaskFormatter.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace Kanboard\Formatter;
|
||||
|
||||
use Kanboard\Core\Filter\FormatterInterface;
|
||||
|
||||
/**
|
||||
* Board Task Formatter
|
||||
*
|
||||
* @package formatter
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class BoardTaskFormatter extends BaseFormatter implements FormatterInterface
|
||||
{
|
||||
protected $tasks = array();
|
||||
protected $columnId = 0;
|
||||
protected $swimlaneId = 0;
|
||||
|
||||
/**
|
||||
* Set tasks
|
||||
*
|
||||
* @access public
|
||||
* @param array $tasks
|
||||
* @return $this
|
||||
*/
|
||||
public function withTasks(array $tasks)
|
||||
{
|
||||
$this->tasks = $tasks;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set columnId
|
||||
*
|
||||
* @access public
|
||||
* @param integer $columnId
|
||||
* @return $this
|
||||
*/
|
||||
public function withColumnId($columnId)
|
||||
{
|
||||
$this->columnId = $columnId;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set swimlaneId
|
||||
*
|
||||
* @access public
|
||||
* @param integer $swimlaneId
|
||||
* @return $this
|
||||
*/
|
||||
public function withSwimlaneId($swimlaneId)
|
||||
{
|
||||
$this->swimlaneId = $swimlaneId;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply formatter
|
||||
*
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function format()
|
||||
{
|
||||
return array_values(array_filter($this->tasks, array($this, 'filterTasks')));
|
||||
}
|
||||
|
||||
/**
|
||||
* Keep only tasks of the given column and swimlane
|
||||
*
|
||||
* @access public
|
||||
* @param array $task
|
||||
* @return bool
|
||||
*/
|
||||
public function filterTasks(array $task)
|
||||
{
|
||||
return $task['column_id'] == $this->columnId && $task['swimlane_id'] == $this->swimlaneId;
|
||||
}
|
||||
}
|
||||
@@ -93,66 +93,6 @@ class BoardModel extends Base
|
||||
return $this->boardModel->create($project_to, $columns);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all tasks sorted by columns and swimlanes
|
||||
*
|
||||
* @access public
|
||||
* @param integer $project_id
|
||||
* @param callable $callback
|
||||
* @return array
|
||||
*/
|
||||
public function getBoard($project_id, $callback = null)
|
||||
{
|
||||
$swimlanes = $this->swimlaneModel->getSwimlanes($project_id);
|
||||
$columns = $this->columnModel->getAll($project_id);
|
||||
$nb_columns = count($columns);
|
||||
|
||||
for ($i = 0, $ilen = count($swimlanes); $i < $ilen; $i++) {
|
||||
$swimlanes[$i]['columns'] = $columns;
|
||||
$swimlanes[$i]['nb_columns'] = $nb_columns;
|
||||
$swimlanes[$i]['nb_tasks'] = 0;
|
||||
$swimlanes[$i]['nb_swimlanes'] = $ilen;
|
||||
|
||||
for ($j = 0; $j < $nb_columns; $j++) {
|
||||
$column_id = $columns[$j]['id'];
|
||||
$swimlane_id = $swimlanes[$i]['id'];
|
||||
|
||||
if (! isset($swimlanes[0]['columns'][$j]['nb_column_tasks'])) {
|
||||
$swimlanes[0]['columns'][$j]['nb_column_tasks'] = 0;
|
||||
$swimlanes[0]['columns'][$j]['total_score'] = 0;
|
||||
}
|
||||
|
||||
$swimlanes[$i]['columns'][$j]['tasks'] = $callback === null ? $this->taskFinderModel->getTasksByColumnAndSwimlane($project_id, $column_id, $swimlane_id) : $callback($project_id, $column_id, $swimlane_id);
|
||||
$swimlanes[$i]['columns'][$j]['nb_tasks'] = count($swimlanes[$i]['columns'][$j]['tasks']);
|
||||
$swimlanes[$i]['columns'][$j]['score'] = $this->getColumnSum($swimlanes[$i]['columns'][$j]['tasks'], 'score');
|
||||
$swimlanes[$i]['nb_tasks'] += $swimlanes[$i]['columns'][$j]['nb_tasks'];
|
||||
$swimlanes[0]['columns'][$j]['nb_column_tasks'] += $swimlanes[$i]['columns'][$j]['nb_tasks'];
|
||||
$swimlanes[0]['columns'][$j]['total_score'] += $swimlanes[$i]['columns'][$j]['score'];
|
||||
}
|
||||
}
|
||||
|
||||
return $swimlanes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the sum of the defined field for a list of tasks
|
||||
*
|
||||
* @access public
|
||||
* @param array $tasks
|
||||
* @param string $field
|
||||
* @return integer
|
||||
*/
|
||||
public function getColumnSum(array &$tasks, $field)
|
||||
{
|
||||
$sum = 0;
|
||||
|
||||
foreach ($tasks as $task) {
|
||||
$sum += $task[$field];
|
||||
}
|
||||
|
||||
return $sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the total of tasks per column
|
||||
*
|
||||
|
||||
@@ -152,26 +152,6 @@ class TaskFinderModel extends Base
|
||||
->join(ProjectModel::TABLE, 'id', 'project_id', TaskModel::TABLE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all tasks shown on the board (sorted by position)
|
||||
*
|
||||
* @access public
|
||||
* @param integer $project_id Project id
|
||||
* @param integer $column_id Column id
|
||||
* @param integer $swimlane_id Swimlane id
|
||||
* @return array
|
||||
*/
|
||||
public function getTasksByColumnAndSwimlane($project_id, $column_id, $swimlane_id = 0)
|
||||
{
|
||||
return $this->getExtendedQuery()
|
||||
->eq(TaskModel::TABLE.'.project_id', $project_id)
|
||||
->eq(TaskModel::TABLE.'.column_id', $column_id)
|
||||
->eq(TaskModel::TABLE.'.swimlane_id', $swimlane_id)
|
||||
->eq(TaskModel::TABLE.'.is_active', TaskModel::STATUS_OPEN)
|
||||
->asc(TaskModel::TABLE.'.position')
|
||||
->findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all tasks for a given project and status
|
||||
*
|
||||
|
||||
@@ -18,9 +18,9 @@
|
||||
</div>
|
||||
<?php endif ?>
|
||||
|
||||
<?php if ($swimlane['nb_swimlanes'] > 1 && ! empty($column['nb_column_tasks'])): ?>
|
||||
<?php if ($swimlane['nb_swimlanes'] > 1 && ! empty($column['column_nb_tasks'])): ?>
|
||||
<span title="<?= t('Total number of tasks in this column across all swimlanes') ?>" class="board-column-header-task-count">
|
||||
(<span><?= $column['nb_column_tasks'] ?></span>)
|
||||
(<span><?= $column['column_nb_tasks'] ?></span>)
|
||||
</span>
|
||||
<?php endif ?>
|
||||
|
||||
|
||||
@@ -2,6 +2,32 @@
|
||||
|
||||
use Kanboard\Core\Translator;
|
||||
|
||||
/**
|
||||
* Sum all values from a single column in the input array
|
||||
*
|
||||
* $input = [
|
||||
* ['column' => 2'], ['column' => 3']
|
||||
* ]
|
||||
*
|
||||
* array_column_sum($input, 'column') returns 5
|
||||
*
|
||||
* @param array $input
|
||||
* @param string $column
|
||||
* @return double
|
||||
*/
|
||||
function array_column_sum(array &$input, $column)
|
||||
{
|
||||
$sum = 0.0;
|
||||
|
||||
foreach ($input as &$row) {
|
||||
if (isset($row[$column])) {
|
||||
$sum += (float) $row[$column];
|
||||
}
|
||||
}
|
||||
|
||||
return $sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build version number from git-archive output
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user