-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: sort options not persisted after page reload (#35)
* fix: sort options not persisted after page reload * Apply fixes from StyleCI --------- Co-authored-by: StyleCI Bot <[email protected]>
- Loading branch information
1 parent
61ffa3c
commit 95f337e
Showing
4 changed files
with
102 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of flarumite/simple-discussion-views. | ||
* | ||
* Copyright (c) 2020 Flarumite. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Flarumite\DiscussionViews\Provider; | ||
|
||
use Flarum\Foundation\AbstractServiceProvider; | ||
|
||
class DiscussionViewsProvider extends AbstractServiceProvider | ||
{ | ||
public function register() | ||
{ | ||
$this->container->extend('flarum.forum.discussions.sortmap', function (array $map) { | ||
return array_merge($map, [ | ||
'popular' => '-view_count', | ||
'unpopular' => 'view_count', | ||
]); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of flarumite/simple-discussion-views. | ||
* | ||
* Copyright (c) 2020 Flarumite. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Flarumite\DiscussionViews\Search; | ||
|
||
use Flarum\Filter\FilterInterface; | ||
use Flarum\Filter\FilterState; | ||
use Flarum\Search\AbstractRegexGambit; | ||
use Flarum\Search\SearchState; | ||
use Flarum\User\User; | ||
use Illuminate\Database\Query\Builder; | ||
|
||
class PopularFilterGambit extends AbstractRegexGambit implements FilterInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getFilterKey(): string | ||
{ | ||
return 'popular'; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getGambitPattern() | ||
{ | ||
return 'is:popular'; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function filter(FilterState $filterState, string $filterValue, bool $negate) | ||
{ | ||
$this->sort($filterState->getQuery(), $filterState->getActor(), $negate); | ||
} | ||
|
||
protected function sort(Builder $query, User $actor, bool $negate) | ||
{ | ||
$query->orderBy('view_count', 'desc'); | ||
} | ||
|
||
/** | ||
* @param SearchState $search | ||
* @param array $matches | ||
* @param $negate | ||
*/ | ||
protected function conditions(SearchState $search, array $matches, $negate) | ||
{ | ||
$this->sort($search->getQuery(), $search->getActor(), $negate); | ||
} | ||
} |