From d5f6317608376bf55351185a76bb3d8817af1e2d Mon Sep 17 00:00:00 2001 From: Rafael de Camargo Date: Tue, 14 May 2019 02:34:53 -0300 Subject: Do not show duplicated results when multiple comments match --- app/Filter/TaskCommentFilter.php | 45 ++++++++++++++++++++++++++-- app/ServiceProvider/FilterProvider.php | 6 ++-- tests/units/Filter/TaskCommentFilterTest.php | 3 ++ 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/app/Filter/TaskCommentFilter.php b/app/Filter/TaskCommentFilter.php index 52db5581..1bb230e9 100644 --- a/app/Filter/TaskCommentFilter.php +++ b/app/Filter/TaskCommentFilter.php @@ -5,6 +5,7 @@ namespace Kanboard\Filter; use Kanboard\Core\Filter\FilterInterface; use Kanboard\Model\CommentModel; use Kanboard\Model\TaskModel; +use PicoDb\Database; /** * Filter tasks by comment @@ -14,6 +15,14 @@ use Kanboard\Model\TaskModel; */ class TaskCommentFilter extends BaseFilter implements FilterInterface { + /** + * Database object + * + * @access private + * @var Database + */ + private $db; + /** * Get search attribute * @@ -25,6 +34,19 @@ class TaskCommentFilter extends BaseFilter implements FilterInterface return array('comment'); } + /** + * Set database object + * + * @access public + * @param Database $db + * @return $this + */ + public function setDatabase(Database $db) + { + $this->db = $db; + return $this; + } + /** * Apply filter * @@ -33,9 +55,28 @@ class TaskCommentFilter extends BaseFilter implements FilterInterface */ public function apply() { - $this->query->ilike(CommentModel::TABLE.'.comment', '%'.$this->value.'%'); - $this->query->join(CommentModel::TABLE, 'task_id', 'id', TaskModel::TABLE); + $task_ids = $this->getTaskIdsWithGivenComment(); + + if (empty($task_ids)) { + $task_ids = array(-1); + } + + $this->query->in(TaskModel::TABLE.'.id', $task_ids); return $this; } + + /** + * Get task ids having this comment + * + * @access public + * @return array + */ + protected function getTaskIdsWithGivenComment() + { + return $this->db + ->table(CommentModel::TABLE) + ->ilike(CommentModel::TABLE.'.comment', '%'.$this->value.'%') + ->findAllByColumn(CommentModel::TABLE.'.task_id'); + } } diff --git a/app/ServiceProvider/FilterProvider.php b/app/ServiceProvider/FilterProvider.php index 392c6c92..5f90e400 100644 --- a/app/ServiceProvider/FilterProvider.php +++ b/app/ServiceProvider/FilterProvider.php @@ -148,7 +148,9 @@ class FilterProvider implements ServiceProviderInterface ) ->withFilter(new TaskPriorityFilter()) ->withFilter(new TaskColumnFilter()) - ->withFilter(new TaskCommentFilter()) + ->withFilter(TaskCommentFilter::getInstance() + ->setDatabase($c['db']) + ) ->withFilter(TaskCreationDateFilter::getInstance() ->setDateParser($c['dateParser']) ) @@ -186,7 +188,7 @@ class FilterProvider implements ServiceProviderInterface ) ->withFilter(TaskMovedDateRangeFilter::getInstance() ->setDateParser($c['dateParser']) - ) + ) ->withFilter(new TaskProjectFilter()) ->withFilter(new TaskReferenceFilter()) ->withFilter(new TaskScoreFilter()) diff --git a/tests/units/Filter/TaskCommentFilterTest.php b/tests/units/Filter/TaskCommentFilterTest.php index 95ffb32f..e6e20cbe 100644 --- a/tests/units/Filter/TaskCommentFilterTest.php +++ b/tests/units/Filter/TaskCommentFilterTest.php @@ -21,8 +21,10 @@ class TaskCommentFilterTest extends Base $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); $this->assertEquals(1, $taskCreation->create(array('title' => 'Test', 'project_id' => 1))); $this->assertEquals(1, $commentModel->create(array('task_id' => 1, 'user_id' => 1, 'comment' => 'This is a test'))); + $this->assertEquals(2, $commentModel->create(array('task_id' => 1, 'user_id' => 1, 'comment' => 'This is another test'))); $filter = new TaskCommentFilter(); + $filter->setDatabase($this->container['db']); $filter->withQuery($query); $filter->withValue('test'); $filter->apply(); @@ -43,6 +45,7 @@ class TaskCommentFilterTest extends Base $this->assertEquals(1, $commentModel->create(array('task_id' => 1, 'user_id' => 1, 'comment' => 'This is a test'))); $filter = new TaskCommentFilter(); + $filter->setDatabase($this->container['db']); $filter->withQuery($query); $filter->withValue('foobar'); $filter->apply(); -- cgit v1.2.3