-
-
Notifications
You must be signed in to change notification settings - Fork 86
/
TagGambit.php
51 lines (44 loc) · 1.58 KB
/
TagGambit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php namespace Flarum\Tags\Gambits;
use Flarum\Tags\TagRepository;
use Flarum\Core\Search\Search;
use Flarum\Core\Search\RegexGambit;
use Illuminate\Database\Query\Expression;
class TagGambit extends RegexGambit
{
protected $pattern = 'tag:(.+)';
/**
* @var \Flarum\Tags\TagRepository
*/
protected $tags;
/**
* @param \Flarum\Tags\TagRepository $tags
*/
public function __construct(TagRepository $tags)
{
$this->tags = $tags;
}
protected function conditions(Search $search, array $matches, $negate)
{
$slugs = explode(',', trim($matches[1], '"'));
// TODO: implement $negate
$search->getQuery()->where(function ($query) use ($slugs) {
foreach ($slugs as $slug) {
if ($slug === 'untagged') {
$query->orWhereNotExists(function ($query) {
$query->select(app('flarum.db')->raw(1))
->from('discussions_tags')
->where('discussions.id', new Expression('discussion_id'));
});
} else {
$id = $this->tags->getIdForSlug($slug);
$query->orWhereExists(function ($query) use ($id) {
$query->select(app('flarum.db')->raw(1))
->from('discussions_tags')
->where('discussions.id', new Expression('discussion_id'))
->where('tag_id', $id);
});
}
}
});
}
}