mirror of
https://github.com/flarum/core.git
synced 2025-07-18 15:21:16 +02:00
Add an event to modify search queries
This commit is contained in:
16
framework/core/src/Core/Events/SearchWillBePerformed.php
Normal file
16
framework/core/src/Core/Events/SearchWillBePerformed.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php namespace Flarum\Core\Events;
|
||||
|
||||
use Flarum\Core\Search\SearcherInterface;
|
||||
|
||||
class SearchWillBePerformed
|
||||
{
|
||||
public $searcher;
|
||||
|
||||
public $criteria;
|
||||
|
||||
public function __construct(SearcherInterface $searcher, $criteria)
|
||||
{
|
||||
$this->searcher = $searcher;
|
||||
$this->criteria = $criteria;
|
||||
}
|
||||
}
|
@@ -5,6 +5,7 @@ use Flarum\Core\Search\SearcherInterface;
|
||||
use Flarum\Core\Search\GambitManager;
|
||||
use Flarum\Core\Repositories\DiscussionRepositoryInterface;
|
||||
use Flarum\Core\Repositories\PostRepositoryInterface;
|
||||
use Flarum\Core\Events\SearchWillBePerformed;
|
||||
|
||||
class DiscussionSearcher implements SearcherInterface
|
||||
{
|
||||
@@ -46,7 +47,7 @@ class DiscussionSearcher implements SearcherInterface
|
||||
|
||||
public function query()
|
||||
{
|
||||
return $this->query;
|
||||
return $this->query->getQuery();
|
||||
}
|
||||
|
||||
public function search(DiscussionSearchCriteria $criteria, $count = null, $start = 0, $load = [])
|
||||
@@ -58,10 +59,10 @@ class DiscussionSearcher implements SearcherInterface
|
||||
|
||||
$total = $this->query->count();
|
||||
|
||||
$sort = $criteria->sort;
|
||||
if (empty($sort)) {
|
||||
$sort = $this->defaultSort;
|
||||
if (empty($criteria->sort)) {
|
||||
$criteria->sort = $this->defaultSort;
|
||||
}
|
||||
$sort = $criteria->sort;
|
||||
if (is_array($sort)) {
|
||||
foreach ($sort as $id) {
|
||||
$this->query->orderByRaw('id != '.(int) $id);
|
||||
@@ -78,6 +79,8 @@ class DiscussionSearcher implements SearcherInterface
|
||||
$this->query->take($count + 1);
|
||||
}
|
||||
|
||||
event(new SearchWillBePerformed($this, $criteria));
|
||||
|
||||
$discussions = $this->query->get();
|
||||
|
||||
if ($count > 0 && $areMoreResults = $discussions->count() > $count) {
|
||||
|
@@ -34,7 +34,7 @@ class UserSearcher implements SearcherInterface
|
||||
|
||||
public function query()
|
||||
{
|
||||
return $this->query;
|
||||
return $this->query->getQuery();
|
||||
}
|
||||
|
||||
public function search(UserSearchCriteria $criteria, $count = null, $start = 0, $load = [])
|
||||
|
Reference in New Issue
Block a user