Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filters to artwork #20

Merged
merged 3 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
35 changes: 35 additions & 0 deletions app/Http/Controllers/Twill/ArtworkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@
use A17\Twill\Services\Forms\Form;
use A17\Twill\Services\Listings\Columns\Boolean;
use A17\Twill\Services\Listings\Columns\Text;
use A17\Twill\Services\Listings\Filters\BelongsToFilter;
use A17\Twill\Services\Listings\Filters\BooleanFilter;
use A17\Twill\Services\Listings\Filters\QuickFilter;
use A17\Twill\Services\Listings\Filters\QuickFilters;
use A17\Twill\Services\Listings\Filters\TableFilters;
use A17\Twill\Services\Listings\TableColumns;
use App\Libraries\Api\Builders\ApiQueryBuilder;
use App\Models\Theme;
use App\Support\Forms\Fields\QueryArtwork;
use Exception;
use Facades\App\Libraries\DamsImageService;
Expand Down Expand Up @@ -130,6 +136,35 @@ protected function additionalIndexTableColumns(): TableColumns
->join(', ')));
}

public function quickFilters(): QuickFilters
{
return QuickFilters::make([
QuickFilter::make()
->label(twillTrans('twill::lang.listing.filter.all-items'))
->queryString('all')
->amount(fn () => $this->repository->getCountByStatusSlug('all')),
QuickFilter::make()
->label('Visible')
->queryString('visible')
->scope('active')
->amount(fn () => $this->repository->getCountVisible()),
QuickFilter::make()
->label('Hidden')
->queryString('hidden')
->scope('notActive')
->amount(fn () => $this->repository->getCountHidden()),
]);
}

public function filters(): TableFilters
{
return TableFilters::make([
BelongsToFilter::make()->field('themePrompts.theme')->label('Theme')->model(Theme::class),
BooleanFilter::make()->field('is_on_view')->label('On View'),
BooleanFilter::make()->field('published')->label('Published'),
]);
}

public function queryArtwork(Request $request, ApiQueryBuilder $api): JsonResponse
{
try {
Expand Down
12 changes: 12 additions & 0 deletions app/Http/Controllers/Twill/ThemeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use A17\Twill\Services\Forms\Form;
use A17\Twill\Services\Listings\Columns\Image;
use A17\Twill\Services\Listings\Columns\NestedData;
use A17\Twill\Services\Listings\Filters\QuickFilter;
use A17\Twill\Services\Listings\Filters\QuickFilters;
use A17\Twill\Services\Listings\TableColumns;

class ThemeController extends ModuleController
Expand Down Expand Up @@ -130,4 +132,14 @@ protected function additionalIndexTableColumns(): TableColumns

return $table;
}

public function quickFilters(): QuickFilters
{
return new QuickFilters([
QuickFilter::make()
->label(twillTrans('twill::lang.listing.filter.all-items'))
->queryString('all')
->amount(fn () => $this->repository->getCountByStatusSlug('all')),
]);
}
}
31 changes: 28 additions & 3 deletions app/Models/Artwork.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,34 @@ public function themePrompts(): HasManyThrough

public function scopeActive(Builder $query): void
{
$query
->published()
->where('is_on_view', true);
$query->published()->onView()->translated();
}

public function scopeNotActive(Builder $query): void
{
$query->where('published', false)
->orWhere->offView()
->orWhere->missingTranslations();
}

public function scopeTranslated(Builder $query): void
{
$query->whereDoesntHave('translations', fn (Builder $query) => $query->where('active', false));
}

public function scopeMissingTranslations(Builder $query): void
{
$query->whereHas('translations', fn (Builder $query) => $query->where('active', false));
}

public function scopeOnView(Builder $query): void
{
$query->where('is_on_view', true);
}

public function scopeOffView(Builder $query): void
{
$query->where('is_on_view', false);
}

/**
Expand Down
20 changes: 20 additions & 0 deletions app/Repositories/ArtworkRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,24 @@ public function prepareFieldsBeforeCreate(array $fields): array

return parent::prepareFieldsBeforeCreate([...$fields, ...$apiFields, ...$translatedFields]);
}

public function getCountVisible(): int
{
return $this->model->active()->count();
}

public function getCountHidden(): int
{
return $this->model->notActive()->count();
}

public function getCountOnView(): int
{
return $this->model->onView()->count();
}

public function getCountOffView(): int
{
return $this->model->offView()->count();
}
}