Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/Http/Middleware/HandleGlobalFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Code16\Sharp\Filters\GlobalFilters\GlobalFilters;
use Code16\Sharp\Filters\GlobalRequiredFilter;
use Code16\Sharp\Http\Context\SharpBreadcrumb;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;

Expand All @@ -14,26 +15,36 @@ public function __construct(private GlobalFilters $globalFiltersHandler) {}

public function handle(Request $request, Closure $next)
{
if ($filterKey = $request->route('filterKey')) {
$filterKeys = explode(GlobalFilters::$valuesUrlSeparator, $filterKey);
$globalFilterValue = $request->route('filterKey');
if (! $globalFilterValue && $request->wantsJson()) {
if ($urlToParse = $request->header(SharpBreadcrumb::CURRENT_PAGE_URL_HEADER) ?: request()->query('current_page_url')) {
$globalFilterValue = str($urlToParse)
->after($request->host().'/'.sharp()->config()->get('custom_url_segment').'/')
->before('/');
}
}

if ($globalFilterValue) {
$globalFilterValues = explode(GlobalFilters::$valuesUrlSeparator, $globalFilterValue);

if ($this->globalFiltersHandler->isEnabled()) {
$globalFilters = $this->globalFiltersHandler->getFilters();
if (count($filterKeys) !== count($globalFilters)) {
if (count($globalFilterValues) !== count($globalFilters)) {
return redirect()->route('code16.sharp.home', [
'filterKey' => sharp()->context()->globalFilterUrlSegmentValue(),
]);
}

collect($globalFilters)
->each(fn (GlobalRequiredFilter $globalFilter, int $index) => $globalFilter
->setCurrentValue($filterKeys[$index])
->setCurrentValue($globalFilterValues[$index])
);

if (! $request->wantsJson()
if (sharp()->context()->globalFilterUrlSegmentValue() !== $globalFilterValue
&& ! $request->wantsJson()
&& $request->isMethod('GET')
&& sharp()->context()->globalFilterUrlSegmentValue() !== $filterKey
) {
// Filter value is invalid, redirect to homepage
return redirect()->route('code16.sharp.home', [
'filterKey' => sharp()->context()->globalFilterUrlSegmentValue(),
]);
Expand Down
43 changes: 43 additions & 0 deletions tests/Http/GlobalFilterRoutesTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<?php

use Code16\Sharp\EntityList\Commands\EntityCommand;
use Code16\Sharp\Form\Fields\SharpFormTextField;
use Code16\Sharp\Http\Context\SharpBreadcrumb;
use Code16\Sharp\Tests\Fixtures\Entities\DashboardEntity;
use Code16\Sharp\Tests\Fixtures\Entities\PersonEntity;
use Code16\Sharp\Tests\Fixtures\Entities\SinglePersonEntity;
use Code16\Sharp\Tests\Fixtures\Sharp\PersonList;
use Code16\Sharp\Utils\Fields\FieldsContainer;

beforeEach(function () {
sharp()->config()->declareEntity(PersonEntity::class);
Expand Down Expand Up @@ -76,3 +81,41 @@
expect(sharp()->context()->globalFilterValue('test1'))->toEqual('one');
expect(sharp()->context()->globalFilterValue('test2'))->toEqual('two');
});

it('sets the current filterKey according to the URL in an API case', function () {
fakeGlobalFilter();

fakeListFor('person', new class() extends PersonList
{
protected function getEntityCommands(): ?array
{
return [
'cmd' => new class() extends EntityCommand
{
public function label(): ?string
{
return 'entity';
}

public function buildFormFields(FieldsContainer $formFields): void
{
$formFields->addField(SharpFormTextField::make('name'));
}

public function execute(array $data = []): array {}
},
];
}
});

$this
->getJson(
route('code16.sharp.api.list.command.entity.form', ['person', 'cmd']),
headers: [
SharpBreadcrumb::CURRENT_PAGE_URL_HEADER => url('/sharp/one/s-list/person'),
]
)
->assertOk();

expect(sharp()->context()->globalFilterValue('test'))->toEqual('one');
});
Loading