Associate tags to tasks in BoardFormatter
This commit is contained in:
@@ -15,6 +15,7 @@ class BoardColumnFormatter extends BaseFormatter implements FormatterInterface
|
||||
protected $swimlaneId = 0;
|
||||
protected $columns = array();
|
||||
protected $tasks = array();
|
||||
protected $tags = array();
|
||||
|
||||
/**
|
||||
* Set swimlaneId
|
||||
@@ -55,6 +56,19 @@ class BoardColumnFormatter extends BaseFormatter implements FormatterInterface
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set tags
|
||||
*
|
||||
* @access public
|
||||
* @param array $tags
|
||||
* @return $this
|
||||
*/
|
||||
public function withTags(array $tags)
|
||||
{
|
||||
$this->tags = $tags;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply formatter
|
||||
*
|
||||
@@ -66,6 +80,7 @@ class BoardColumnFormatter extends BaseFormatter implements FormatterInterface
|
||||
foreach ($this->columns as &$column) {
|
||||
$column['tasks'] = BoardTaskFormatter::getInstance($this->container)
|
||||
->withTasks($this->tasks)
|
||||
->withTags($this->tags)
|
||||
->withSwimlaneId($this->swimlaneId)
|
||||
->withColumnId($column['id'])
|
||||
->format();
|
||||
|
||||
@@ -44,12 +44,14 @@ class BoardFormatter extends BaseFormatter implements FormatterInterface
|
||||
{
|
||||
$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();
|
||||
|
||||
$task_ids = array_column($tasks, 'id');
|
||||
$tags = $this->taskTagModel->getTagsByTasks($task_ids);
|
||||
|
||||
if (empty($swimlanes) || empty($columns)) {
|
||||
return array();
|
||||
}
|
||||
@@ -58,6 +60,7 @@ class BoardFormatter extends BaseFormatter implements FormatterInterface
|
||||
->withSwimlanes($swimlanes)
|
||||
->withColumns($columns)
|
||||
->withTasks($tasks)
|
||||
->withTags($tags)
|
||||
->format();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ class BoardSwimlaneFormatter extends BaseFormatter implements FormatterInterface
|
||||
protected $swimlanes = array();
|
||||
protected $columns = array();
|
||||
protected $tasks = array();
|
||||
protected $tags = array();
|
||||
|
||||
/**
|
||||
* Set swimlanes
|
||||
@@ -55,6 +56,19 @@ class BoardSwimlaneFormatter extends BaseFormatter implements FormatterInterface
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set tags
|
||||
*
|
||||
* @access public
|
||||
* @param array $tags
|
||||
* @return $this
|
||||
*/
|
||||
public function withTags(array $tags)
|
||||
{
|
||||
$this->tags = $tags;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply formatter
|
||||
*
|
||||
@@ -71,6 +85,7 @@ class BoardSwimlaneFormatter extends BaseFormatter implements FormatterInterface
|
||||
->withSwimlaneId($swimlane['id'])
|
||||
->withColumns($this->columns)
|
||||
->withTasks($this->tasks)
|
||||
->withTags($this->tags)
|
||||
->format();
|
||||
|
||||
$swimlane['nb_swimlanes'] = $nb_swimlanes;
|
||||
|
||||
@@ -13,9 +13,23 @@ use Kanboard\Core\Filter\FormatterInterface;
|
||||
class BoardTaskFormatter extends BaseFormatter implements FormatterInterface
|
||||
{
|
||||
protected $tasks = array();
|
||||
protected $tags = array();
|
||||
protected $columnId = 0;
|
||||
protected $swimlaneId = 0;
|
||||
|
||||
/**
|
||||
* Set tags
|
||||
*
|
||||
* @access public
|
||||
* @param array $tags
|
||||
* @return $this
|
||||
*/
|
||||
public function withTags(array $tags)
|
||||
{
|
||||
$this->tags = $tags;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set tasks
|
||||
*
|
||||
@@ -63,17 +77,19 @@ class BoardTaskFormatter extends BaseFormatter implements FormatterInterface
|
||||
*/
|
||||
public function format()
|
||||
{
|
||||
return array_values(array_filter($this->tasks, array($this, 'filterTasks')));
|
||||
$tasks = array_values(array_filter($this->tasks, array($this, 'filterTasks')));
|
||||
array_merge_relation($tasks, $this->tags, 'tags', 'id');
|
||||
return $tasks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Keep only tasks of the given column and swimlane
|
||||
*
|
||||
* @access public
|
||||
* @access protected
|
||||
* @param array $task
|
||||
* @return bool
|
||||
*/
|
||||
public function filterTasks(array $task)
|
||||
protected function filterTasks(array $task)
|
||||
{
|
||||
return $task['column_id'] == $this->columnId && $task['swimlane_id'] == $this->swimlaneId;
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ class TaskTagModel extends Base
|
||||
* @param integer $task_id
|
||||
* @return array
|
||||
*/
|
||||
public function getAll($task_id)
|
||||
public function getTagsByTask($task_id)
|
||||
{
|
||||
return $this->db->table(TagModel::TABLE)
|
||||
->columns(TagModel::TABLE.'.id', TagModel::TABLE.'.name')
|
||||
@@ -35,6 +35,28 @@ class TaskTagModel extends Base
|
||||
->findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all tags associated to a list of tasks
|
||||
*
|
||||
* @access public
|
||||
* @param integer[] $task_ids
|
||||
* @return array
|
||||
*/
|
||||
public function getTagsByTasks($task_ids)
|
||||
{
|
||||
if (empty($task_ids)) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$tags = $this->db->table(TagModel::TABLE)
|
||||
->columns(TagModel::TABLE.'.id', TagModel::TABLE.'.name', self::TABLE.'.task_id')
|
||||
->in(self::TABLE.'.task_id', $task_ids)
|
||||
->join(self::TABLE, 'tag_id', 'id')
|
||||
->findAll();
|
||||
|
||||
return array_column_index($tags, 'task_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get dictionary of tags
|
||||
*
|
||||
@@ -44,7 +66,7 @@ class TaskTagModel extends Base
|
||||
*/
|
||||
public function getList($task_id)
|
||||
{
|
||||
$tags = $this->getAll($task_id);
|
||||
$tags = $this->getTagsByTask($task_id);
|
||||
return array_column($tags, 'name', 'id');
|
||||
}
|
||||
|
||||
@@ -61,8 +83,8 @@ class TaskTagModel extends Base
|
||||
{
|
||||
$task_tags = $this->getList($task_id);
|
||||
|
||||
return $this->addTags($project_id, $task_id, $task_tags, $tags) &&
|
||||
$this->removeTags($task_id, $task_tags, $tags);
|
||||
return $this->associateTags($project_id, $task_id, $task_tags, $tags) &&
|
||||
$this->dissociateTags($task_id, $task_tags, $tags);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,7 +95,7 @@ class TaskTagModel extends Base
|
||||
* @param integer $tag_id
|
||||
* @return boolean
|
||||
*/
|
||||
public function associate($task_id, $tag_id)
|
||||
public function associateTag($task_id, $tag_id)
|
||||
{
|
||||
return $this->db->table(self::TABLE)->insert(array(
|
||||
'task_id' => $task_id,
|
||||
@@ -89,7 +111,7 @@ class TaskTagModel extends Base
|
||||
* @param integer $tag_id
|
||||
* @return boolean
|
||||
*/
|
||||
public function dissociate($task_id, $tag_id)
|
||||
public function dissociateTag($task_id, $tag_id)
|
||||
{
|
||||
return $this->db->table(self::TABLE)
|
||||
->eq('task_id', $task_id)
|
||||
@@ -97,12 +119,22 @@ class TaskTagModel extends Base
|
||||
->remove();
|
||||
}
|
||||
|
||||
private function addTags($project_id, $task_id, $task_tags, $tags)
|
||||
/**
|
||||
* Associate missing tags
|
||||
*
|
||||
* @access protected
|
||||
* @param integer $project_id
|
||||
* @param integer $task_id
|
||||
* @param array $task_tags
|
||||
* @param array $tags
|
||||
* @return bool
|
||||
*/
|
||||
protected function associateTags($project_id, $task_id, $task_tags, $tags)
|
||||
{
|
||||
foreach ($tags as $tag) {
|
||||
$tag_id = $this->tagModel->findOrCreateTag($project_id, $tag);
|
||||
|
||||
if (! isset($task_tags[$tag_id]) && ! $this->associate($task_id, $tag_id)) {
|
||||
if (! isset($task_tags[$tag_id]) && ! $this->associateTag($task_id, $tag_id)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -110,11 +142,20 @@ class TaskTagModel extends Base
|
||||
return true;
|
||||
}
|
||||
|
||||
private function removeTags($task_id, $task_tags, $tags)
|
||||
/**
|
||||
* Dissociate removed tags
|
||||
*
|
||||
* @access protected
|
||||
* @param integer $task_id
|
||||
* @param array $task_tags
|
||||
* @param array $tags
|
||||
* @return bool
|
||||
*/
|
||||
protected function dissociateTags($task_id, $task_tags, $tags)
|
||||
{
|
||||
foreach ($task_tags as $tag_id => $tag) {
|
||||
if (! in_array($tag, $tags)) {
|
||||
if (! $this->dissociate($task_id, $tag_id)) {
|
||||
if (! $this->dissociateTag($task_id, $tag_id)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,61 @@
|
||||
|
||||
use Kanboard\Core\Translator;
|
||||
|
||||
/**
|
||||
* Associate another dict to a dict based on a common key
|
||||
*
|
||||
* @param array $input
|
||||
* @param array $relations
|
||||
* @param string $relation
|
||||
* @param string $column
|
||||
*/
|
||||
function array_merge_relation(array &$input, array &$relations, $relation, $column)
|
||||
{
|
||||
foreach ($input as &$row) {
|
||||
if (isset($row[$column]) && isset($relations[$row[$column]])) {
|
||||
$row[$relation] = $relations[$row[$column]];
|
||||
} else {
|
||||
$row[$relation] = array();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create indexed array from a list of dict
|
||||
*
|
||||
* $input = [
|
||||
* ['k1' => 1, 'k2' => 2], ['k1' => 3, 'k2' => 4], ['k1' => 2, 'k2' => 5]
|
||||
* ]
|
||||
*
|
||||
* array_column_index($input, 'k1') will returns:
|
||||
*
|
||||
* [
|
||||
* 1 => [['k1' => 1, 'k2' => 2], ['k1' => 2, 'k2' => 5]],
|
||||
* 3 => [['k1' => 3, 'k2' => 4]],
|
||||
* ]
|
||||
*
|
||||
* @param array $input
|
||||
* @param string $column
|
||||
* @return array
|
||||
*/
|
||||
function array_column_index(array &$input, $column)
|
||||
{
|
||||
$result = array();
|
||||
|
||||
foreach ($input as &$row) {
|
||||
if (isset($row[$column])) {
|
||||
$result[$row[$column]][] = $row;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sum all values from a single column in the input array
|
||||
*
|
||||
* $input = [
|
||||
* ['column' => 2'], ['column' => 3']
|
||||
* ['column' => 2], ['column' => 3]
|
||||
* ]
|
||||
*
|
||||
* array_column_sum($input, 'column') returns 5
|
||||
|
||||
Reference in New Issue
Block a user