|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of fof/doorman. |
| 5 | + * |
| 6 | + * Copyright (c) Reflar. |
| 7 | + * Copyright (c) FriendsOfFlarum. |
| 8 | + * |
| 9 | + * For the full copyright and license information, please view the LICENSE |
| 10 | + * file that was distributed with this source code. |
| 11 | + * |
| 12 | + */ |
| 13 | + |
| 14 | +namespace FoF\Doorman\Filter; |
| 15 | + |
| 16 | +use Flarum\Filter\FilterInterface; |
| 17 | +use Flarum\Filter\FilterState; |
| 18 | +use Flarum\Filter\ValidateFilterTrait; |
| 19 | +use Flarum\Search\AbstractRegexGambit; |
| 20 | +use Flarum\Search\SearchState; |
| 21 | +use Flarum\User\UserRepository; |
| 22 | +use Illuminate\Database\Query\Builder; |
| 23 | + |
| 24 | +class CreatedByFilterGambit extends AbstractRegexGambit implements FilterInterface |
| 25 | +{ |
| 26 | + use ValidateFilterTrait; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var \Flarum\User\UserRepository |
| 30 | + */ |
| 31 | + protected $users; |
| 32 | + |
| 33 | + /** |
| 34 | + * @param \Flarum\User\UserRepository $users |
| 35 | + */ |
| 36 | + public function __construct(UserRepository $users) |
| 37 | + { |
| 38 | + $this->users = $users; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * {@inheritdoc} |
| 43 | + */ |
| 44 | + public function getGambitPattern() |
| 45 | + { |
| 46 | + return 'created_by:(.+)'; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * {@inheritdoc} |
| 51 | + */ |
| 52 | + protected function conditions(SearchState $search, array $matches, $negate) |
| 53 | + { |
| 54 | + $this->constrain($search->getQuery(), $matches[1], $negate); |
| 55 | + } |
| 56 | + |
| 57 | + public function getFilterKey(): string |
| 58 | + { |
| 59 | + return 'created_by'; |
| 60 | + } |
| 61 | + |
| 62 | + public function filter(FilterState $filterState, $filterValue, bool $negate) |
| 63 | + { |
| 64 | + $this->constrain($filterState->getQuery(), $filterValue, $negate); |
| 65 | + } |
| 66 | + |
| 67 | + protected function constrain(Builder $query, $rawUsernames, $negate) |
| 68 | + { |
| 69 | + $usernames = $this->asStringArray($rawUsernames); |
| 70 | + |
| 71 | + $ids = $this->users->getIdsForUsernames($usernames); |
| 72 | + |
| 73 | + $query->whereIn('doorkeys.created_by', $ids, 'and', $negate); |
| 74 | + } |
| 75 | +} |
0 commit comments