From 912cad5d3972525e06145f8247a8dcff4b4b4c85 Mon Sep 17 00:00:00 2001 From: Zach Garwood Date: Fri, 11 Aug 2023 16:10:41 -0500 Subject: [PATCH] Add "On View" quickfilter for Objects [MA-106] --- app/Http/Controllers/Twill/ArtworkController.php | 14 ++++++++++++++ app/Models/Api/Artwork.php | 12 ++++++++++++ 2 files changed, 26 insertions(+) diff --git a/app/Http/Controllers/Twill/ArtworkController.php b/app/Http/Controllers/Twill/ArtworkController.php index 8fe0fa5e..755f3151 100644 --- a/app/Http/Controllers/Twill/ArtworkController.php +++ b/app/Http/Controllers/Twill/ArtworkController.php @@ -12,9 +12,12 @@ use A17\Twill\Services\Forms\Options; use A17\Twill\Services\Listings\Columns\Boolean; use A17\Twill\Services\Listings\Columns\Text; +use A17\Twill\Services\Listings\Filters\QuickFilter; +use A17\Twill\Services\Listings\Filters\QuickFilters; use A17\Twill\Services\Listings\TableColumns; use App\Http\Controllers\Twill\Columns\ApiRelation; use App\Models\Api\Artwork; +use Illuminate\Contracts\Database\Query\Builder; class ArtworkController extends BaseApiController { @@ -32,6 +35,17 @@ protected function setUpController(): void $this->eagerLoadListingRelations(['gallery']); } + public function quickFilters(): QuickFilters + { + return $this->getDefaultQuickFilters() + ->add(QuickFilter::make() + ->queryString('is_on_view') + ->label('On View') + ->apply(fn (Builder $builder) => $builder->onView()) + ->amount(fn () => Artwork::query()->onView()->count())); + } + + protected function additionalIndexTableColumns(): TableColumns { return parent::additionalIndexTableColumns() diff --git a/app/Models/Api/Artwork.php b/app/Models/Api/Artwork.php index 65875486..fd17be96 100644 --- a/app/Models/Api/Artwork.php +++ b/app/Models/Api/Artwork.php @@ -29,4 +29,16 @@ public function gallery() { return $this->belongsToApi(\App\Models\Api\Gallery::class, 'gallery_id'); } + + public function scopeOnView($query) + { + return $query + ->rawSearch([ + 'bool' => [ + 'must' => [ + ['term' => ['is_on_view' => true]], + ], + ] + ]); + } }