Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Nov 10, 2023
1 parent 3d75e40 commit 22d0c1e
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 16 deletions.
10 changes: 3 additions & 7 deletions resources/views/media/filters.blade.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<div class="modal__filter">
<div class="search-form">
<input class="form-control search-form__control" type="text" placeholder="Search..." title="Search" />
<button type="button" class="search-form__submit">
<span class="sr-only">Search</span>
<x-root::icon name="search" class="search-form__icon" />
</button>
</div>
@foreach($filters as $filter)
@include($filter['template'], $filter)
@endforeach
</div>
67 changes: 66 additions & 1 deletion src/Fields/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

namespace Cone\Root\Fields;

use Closure;
use Cone\Root\Filters\Filter;
use Cone\Root\Filters\Filters;
use Cone\Root\Filters\MediaSearch;
use Cone\Root\Filters\RenderableFilter;
use Cone\Root\Filters\Search;
use Cone\Root\Http\Controllers\MediaController;
use Cone\Root\Models\Medium;
use Cone\Root\Traits\HasMedia;
use Cone\Root\Traits\RegistersRoutes;
use Cone\Root\Traits\ResolvesFilters;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
Expand All @@ -18,6 +25,7 @@

class Media extends File
{
use ResolvesFilters;
use RegistersRoutes {
RegistersRoutes::registerRoutes as __registerRoutes;
}
Expand All @@ -32,6 +40,11 @@ class Media extends File
*/
protected string $template = 'root::fields.media';

/**
* The filters resolver callback.
*/
protected ?Closure $filtersResolver = null;

/**
* Get the URI key.
*/
Expand Down Expand Up @@ -77,12 +90,55 @@ public function getModel(): Model
};
}

/**
* Define the filters for the object.
*/
public function filters(Request $request): array
{
return [
new MediaSearch(),
];
}

/**
* Set the filters resolver callback.
*/
public function withFilters(Closure $callback): static
{
$this->filtersResolver = $callback;

return $this;
}

/**
* Resolve the filters collection.
*/
public function resolveFilters(Request $request): Filters
{
if (is_null($this->filters)) {
$this->filters = new Filters($this->filters($request));

if (! is_null($this->filtersResolver)) {
$this->fields->register(
Arr::wrap(call_user_func_array($this->filtersResolver, [$request]))
);
}

$this->filters->each(function (Filter $filter) use ($request): void {
$this->resolveFilter($request, $filter);
});
}

return $this->filters;
}

/**
* Paginate the results.
*/
public function paginate(Request $request, Model $model): array
{
return $this->resolveRelatableQuery($request, $model)
return $this->resolveFilters($request)
->apply($request, $this->resolveRelatableQuery($request, $model))
->latest()
->paginate($request->input('per_page'))
->withQueryString()
Expand Down Expand Up @@ -203,6 +259,15 @@ public function toInput(Request $request, Model $model): array
]);
}, $data['options'] ?? []),
'url' => $this->getUri() ? $this->buildUri($request, $model) : null,
'filters' => $this->resolveFilters($request)
->authorized($request)
->renderable()
->map(function (RenderableFilter $filter) use ($request, $model): array {
return $filter->toField()
->removeAttribute('name')
->toInput($request, $this->getRelation($model)->getRelated());
})
->all(),
]);
}
}
31 changes: 31 additions & 0 deletions src/Filters/MediaSearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

Check warning on line 1 in src/Filters/MediaSearch.php

View workflow job for this annotation

GitHub Actions / 4️⃣ Coding Standards

Found violation(s) of type: no_unused_imports

Check warning on line 1 in src/Filters/MediaSearch.php

View workflow job for this annotation

GitHub Actions / 4️⃣ Coding Standards

Found violation(s) of type: blank_line_after_namespace

namespace Cone\Root\Filters;

use Cone\Root\Filters\SearchField;

class MediaSearch extends Search
{
/**
* The searchable attributes
*/
protected array $attributes = [
'file_name',
];

/**
* Create a new filter instance.
*/
public function __construct(array $attributes = ['file_name'])
{
$this->attributes = $attributes;
}

/**
* {@inheritdoc}
*/
public function getSearchableAttributes(): array
{
return array_fill_keys($this->attributes, null);
}
}
18 changes: 13 additions & 5 deletions src/Filters/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ public function __construct(Fields $fields)
*/
public function apply(Request $request, Builder $query, mixed $value): Builder
{
$attributes = $this->fields->mapWithKeys(static function (Field $field): array {
return [
$field->getModelAttribute() => $field instanceof Relation ? $field->getSearchableColumns() : null,
];
})->all();
$attributes = $this->getSearchableAttributes();

if (empty($value) || empty($attributes)) {
return $query;
Expand All @@ -59,6 +55,18 @@ public function apply(Request $request, Builder $query, mixed $value): Builder
});
}

/**
* Get the serachable attributes.
*/
public function getSearchableAttributes(): array
{
return $this->fields->mapWithKeys(static function (Field $field): array {
return [
$field->getModelAttribute() => $field instanceof Relation ? $field->getSearchableColumns() : null,
];
})->all();
}

/**
* {@inheritdoc}
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ abstract class Resource implements Arrayable, Form, Table
{
use AsForm;
use Authorizable;
use ResolvesActions;
use ResolvesFilters;
use RegistersRoutes {
RegistersRoutes::registerRoutes as __registerRoutes;
}
use ResolvesActions;
use ResolvesFilters;
use ResolvesWidgets;

/**
Expand Down Expand Up @@ -366,7 +366,7 @@ public function toCreate(Request $request): array
'method' => 'POST',
'fields' => $this->resolveFields($request)
->authorized($request, $model)
->visible('update')
->visible('create')
->mapToInputs($request, $model),
]);
}
Expand Down

0 comments on commit 22d0c1e

Please sign in to comment.