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 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) (limited to 'app/Filter/TaskCommentFilter.php') 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'); + } } -- cgit v1.2.3