summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRafael de Camargo <rafaelcamargo@trt15.jus.br>2019-05-14 02:34:53 -0300
committerfguillot <fred@kanboard.net>2019-05-13 22:34:52 -0700
commitd5f6317608376bf55351185a76bb3d8817af1e2d (patch)
tree477f5d51545a0e5b7644e1c3b894c41a5a30faf1
parent702379a55093aa62bae7dcd49a657397a21fd6b5 (diff)
Do not show duplicated results when multiple comments match
-rw-r--r--app/Filter/TaskCommentFilter.php45
-rw-r--r--app/ServiceProvider/FilterProvider.php6
-rw-r--r--tests/units/Filter/TaskCommentFilterTest.php3
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
@@ -15,6 +16,14 @@ use Kanboard\Model\TaskModel;
class TaskCommentFilter extends BaseFilter implements FilterInterface
{
/**
+ * Database object
+ *
+ * @access private
+ * @var Database
+ */
+ private $db;
+
+ /**
* Get search attribute
*
* @access public
@@ -26,6 +35,19 @@ class TaskCommentFilter extends BaseFilter implements FilterInterface
}
/**
+ * Set database object
+ *
+ * @access public
+ * @param Database $db
+ * @return $this
+ */
+ public function setDatabase(Database $db)
+ {
+ $this->db = $db;
+ return $this;
+ }
+
+ /**
* Apply filter
*
* @access public
@@ -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();