Skip to content

Commit

Permalink
Use normal search if scout disabled (#1617)
Browse files Browse the repository at this point in the history
there are few places in the panel is using scout search for searchable
Select field.

this PR is to cater for disabled scout search

---------

Co-authored-by: Glenn Jacobs <[email protected]>
  • Loading branch information
wychoong and glennjacobs authored Jul 4, 2024
1 parent 3a65c22 commit 5c50fd7
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public function moveAction()
->model(Collection::class)
->searchable()
->getSearchResultsUsing(static function (Forms\Components\Select $component, string $search): array {
return Collection::search($search)
return get_search_builder(Collection::class, $search)
->get()
->mapWithKeys(fn (Collection $record): array => [$record->getKey() => $record->breadcrumb->push($record->translateAttribute('name'))->join(' > ')])
->all();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function getDefaultHeaderActions(): array
->model(Collection::class)
->searchable()
->getSearchResultsUsing(static function (Forms\Components\Select $component, string $search) use ($record): array {
return Collection::search($search)
return get_search_builder(Collection::class, $search)
->get()
->reject(
fn ($result) => $result->isDescendantOf($record)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ public function table(Table $table): Table
->label('Product')
->required()
->searchable(true)
->getSearchResultsUsing(static function (Forms\Components\Select $component, string $search): array {
return Product::search($search)
->getSearchResultsUsing(static function (Forms\Components\Select $component, string $search, ManageCollectionProducts $livewire): array {
$relationModel = $livewire->getRelationship()->getRelated()::class;

return get_search_builder($relationModel, $search)
->get()
->mapWithKeys(fn (Product $record): array => [$record->getKey() => $record->translateAttribute('name')])
->all();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function table(Table $table): Table
Forms\Components\MorphToSelect\Type::make(Product::class)
->titleAttribute('name.en')
->getSearchResultsUsing(static function (Forms\Components\Select $component, string $search): array {
return Product::search($search)
return get_search_builder(Product::class, $search)
->get()
->mapWithKeys(fn (Product $record): array => [$record->getKey() => $record->attr('name')])
->all();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function table(Table $table): Table
Forms\Components\MorphToSelect\Type::make(Product::class)
->titleAttribute('name.en')
->getSearchResultsUsing(static function (Forms\Components\Select $component, string $search): array {
return Product::search($search)
return get_search_builder(Product::class, $search)
->get()
->mapWithKeys(fn (Product $record): array => [$record->getKey() => $record->attr('name')])
->all();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function table(Table $table): Table
Forms\Components\MorphToSelect\Type::make(Product::class)
->titleAttribute('name.en')
->getSearchResultsUsing(static function (Forms\Components\Select $component, string $search): array {
return Product::search($search)
return get_search_builder(Product::class, $search)
->get()
->mapWithKeys(fn (Product $record): array => [$record->getKey() => $record->attr('name')])
->all();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function table(Table $table): Table
Forms\Components\MorphToSelect\Type::make(ProductVariant::class)
->titleAttribute('sku')
->getSearchResultsUsing(static function (Forms\Components\Select $component, string $search): array {
$products = Product::search($search)
$products = get_search_builder(Product::class, $search)
->get();

return ProductVariant::whereIn('product_id', $products->pluck('id'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function form(Form $form): Form
->required()
->searchable(true)
->getSearchResultsUsing(static function (Forms\Components\Select $component, string $search): array {
return Product::search($search)
return get_search_builder(Product::class, $search)
->get()
->mapWithKeys(fn (Product $record): array => [$record->getKey() => $record->translateAttribute('name')])
->all();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ public function table(Table $table): Table
->recordSelect(
function (Forms\Components\Select $select) {
return $select->placeholder('Select a collection') // TODO: needs translation
->getSearchResultsUsing(static function (Forms\Components\Select $component, string $search): array {
return Collection::search($search)
->getSearchResultsUsing(static function (Forms\Components\Select $component, string $search, ManageProductCollections $livewire): array {
$relationModel = $livewire->getRelationship()->getRelated()::class;

return get_search_builder($relationModel, $search)
->get()
->mapWithKeys(fn (Collection $record): array => [$record->getKey() => $record->breadcrumb->push($record->translateAttribute('name'))->join(' > ')])
->all();
Expand Down
61 changes: 61 additions & 0 deletions packages/admin/src/helpers.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<?php

use Illuminate\Database\Connection;
use Illuminate\Database\Eloquent\Builder;
use Lunar\Base\Traits\Searchable;
use Lunar\DataTypes\Price;
use Lunar\FieldTypes\TranslatedText;
use Lunar\Models\Attribute;

use function Filament\Support\generate_search_term_expression;

if (! function_exists('price')) {
function price($value, $currency, $unitQty = 1)
Expand Down Expand Up @@ -66,3 +72,58 @@ function db_date($column, $format, $alias = null)
return $select;
}
}

if (! function_exists('get_search_builder')) {

function get_search_builder($model, $search): Laravel\Scout\Builder|Builder
{
$scoutEnabled = config('lunar.panel.scout_enabled', false);
$isScoutSearchable = in_array(Searchable::class, class_uses_recursive($model));

if (
$scoutEnabled &&
$isScoutSearchable
) {
return $model::search($search);
} else {
$query = $model::query();

/** @var Connection $databaseConnection */
$databaseConnection = $query->getConnection();

$search = generate_search_term_expression($search, true, $databaseConnection);

foreach (explode(' ', $search) as $searchWord) {
$query->where(function (Builder $query) use ($model, $searchWord) {
$attributes = Attribute::whereAttributeType($model)
->whereSearchable(true)
->get();

$searchableAttributes = [];

foreach ($attributes as $attribute) {
if ($attribute->type == TranslatedText::class) {
array_push($searchableAttributes, 'attribute_data->'.$attribute->handle.'->value');
}
}

$isFirst = true;

foreach ($searchableAttributes as $searchAttribute) {
$whereClause = $isFirst ? 'where' : 'orWhere';

$query->{$whereClause}(
$searchAttribute,
'like',
"%{$searchWord}%",
);

$isFirst = false;
}
});
}

return $query;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function form(Form $form): Form
fn (Model $record) => $record->purchasable->attr('name')
)
->getSearchResultsUsing(static function (Forms\Components\Select $component, string $search): array {
return Product::search($search)
return get_search_builder(Product::class, $search)
->get()
->mapWithKeys(fn (Product $record): array => [$record->getKey() => $record->translateAttribute('name')])
->all();
Expand Down

0 comments on commit 5c50fd7

Please sign in to comment.