Skip to content
Merged
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
14 changes: 14 additions & 0 deletions app/Concerns/Searchable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace App\Concerns;

use Laravel\Scout\Searchable as ScoutSearchable;

trait Searchable
{
use ScoutSearchable;

abstract public static function typesenseModelSettings(): array;
}
37 changes: 37 additions & 0 deletions app/Console/Commands/ScoutDeleteAllIndexesCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace App\Console\Commands;

use Illuminate\Console\Command;

class ScoutDeleteAllIndexesCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'scout:delete-all-indexes';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Delete all indexes';

/**
* Execute the console command.
*/
public function handle(): int
{
app('searchable-models')
->each(function (string $model): void {
$this->call('scout:delete-index', ['name' => (new $model)->searchableAs()]);
});

return self::SUCCESS;
}
}
38 changes: 38 additions & 0 deletions app/Console/Commands/ScoutRebuildCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace App\Console\Commands;

use Illuminate\Console\Command;

class ScoutRebuildCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'scout:rebuild';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Rebuilds the search index for all searchable models';

/**
* Execute the console command.
*/
public function handle(): int
{
app('searchable-models')
->each(function (string $model): void {
$this->call('scout:flush', ['model' => $model]);
$this->call('scout:import', ['model' => $model]);
});

return self::SUCCESS;
}
}
2 changes: 1 addition & 1 deletion app/Filament/Shelter/Resources/BeneficiaryResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class BeneficiaryResource extends Resource
{
protected static ?string $model = Beneficiary::class;

protected static ?string $navigationIcon = 'heroicon-o-user-group';
protected static ?string $navigationIcon = 'heroicon-o-user';

protected static ?string $recordTitleAttribute = 'name';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace App\Filament\Shelter\Resources\BeneficiaryResource\Schemas;

use App\Models\Group as GroupModel;
use App\Models\Request;
use App\Models\Shelter;
use Filament\Facades\Filament;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\DatePicker;
Expand All @@ -16,7 +18,7 @@
use Filament\Forms\Components\TextInput;
use Filament\Forms\Get;
use Filament\Forms\Set;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Builder;

class StayForm
{
Expand Down Expand Up @@ -57,6 +59,21 @@ public static function getSchema(?int $beneficiary_id = null): array
->rows(5),
]),

Checkbox::make('has_group')
->label(__('app.field.has_group'))
->columnSpanFull()
->live(),

Select::make('group_id')
->label(__('app.field.group'))
->visible(fn (Get $get) => $get('has_group'))
->columnSpanFull()
->searchable()
->preload()
->required()
->getSearchResultsUsing(fn (string $search) => static::getGroupOptions(Filament::getTenant(), $search))
->options(fn () => static::getGroupOptions(Filament::getTenant())),

Checkbox::make('has_request')
->label(__('app.field.has_request'))
->columnSpanFull()
Expand All @@ -69,19 +86,8 @@ public static function getSchema(?int $beneficiary_id = null): array
->searchable()
->preload()
->required()
->getSearchResultsUsing(
fn (string $search) => Request::query()
->whereAllocatable()
->where('shelter_id', Filament::getTenant()->getKey())
->where(function ($query) use ($search) {
$query->whereLike('id', Str::remove('#', $search) . '%')
->orWhereLike('beneficiary->name', "%{$search}%");
})
->get()
->pluck('title', 'id')
)
->getOptionLabelUsing(fn ($value) => Request::find($value)->title()),

->getSearchResultsUsing(fn (string $search) => static::getRequestOptions(Filament::getTenant(), $search))
->options(fn () => static::getRequestOptions(Filament::getTenant())),
]),
];
}
Expand All @@ -106,4 +112,50 @@ public static function getEndDateGroup(): Group
}),
]);
}

protected static function getGroupOptions(Shelter $shelter, ?string $search = null): array
{
if (filled($search)) {
$options = GroupModel::search($search)
->query(
fn (Builder $query) => $query
->whereBelongsTo($shelter)
->limit(50)
)
->where('shelter_id', $shelter->id)
->get();
} else {
$options = GroupModel::query()
->whereBelongsTo($shelter)
->limit(50)
->get();
}

return $options
->pluck('title', 'id')
->all();
}

protected static function getRequestOptions(Shelter $shelter, ?string $search = null): array
{
if (filled($search)) {
$options = Request::search($search)
->query(
fn (Builder $query) => $query
->whereBelongsTo($shelter)
->limit(50)
)
->where('shelter_id', $shelter->id)
->get();
} else {
$options = Request::query()
->whereBelongsTo($shelter)
->limit(50)
->get();
}

return $options
->pluck('title', 'id')
->all();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Filament\Shelter\Resources\BeneficiaryResource\Schemas;

use App\Filament\Shelter\Resources\GroupResource;
use App\Filament\Shelter\Resources\RequestResource;
use App\Models\Stay;
use Carbon\Carbon;
Expand All @@ -23,10 +24,10 @@ public static function getSchema(): array
TextEntry::make('id')
->label(__('app.field.id'))
->prefix('#'),

TextEntry::make('created_at')
->label(__('app.field.created_at'))
->dateTime(),

]),

Section::make(__('app.stay.details'))
Expand Down Expand Up @@ -62,6 +63,14 @@ public static function getSchema(): array
'record' => $record->request_id,
]))
->color('primary'),

TextEntry::make('group.title')
->visible(fn (Stay $record) => $record->has_group)
->label(__('app.field.group'))
->url(fn (Stay $record) => GroupResource::getUrl('view', [
'record' => $record->group_id,
]))
->color('primary'),
]),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Filament\Shelter\Resources\BeneficiaryResource;
use App\Filament\Shelter\Resources\BeneficiaryResource\Schemas\StayForm;
use App\Filament\Shelter\Resources\GroupResource;
use App\Filament\Shelter\Resources\RequestResource;
use App\Models\Beneficiary;
use App\Models\Stay;
Expand Down Expand Up @@ -59,6 +60,21 @@ public function table(Table $table): Table
->sortable()
->shrink(),

TextColumn::make('group.title')
->label(__('app.field.group'))
->url(function (Stay $record) {
if (blank($record->group_id)) {
return null;
}

return GroupResource::getUrl('view', [
'record' => $record->group_id,
]);
})
->color('primary')
->wrap()
->shrink(),

TextColumn::make('request.title')
->label(__('app.field.request'))
->url(function (Stay $record) {
Expand Down
106 changes: 106 additions & 0 deletions app/Filament/Shelter/Resources/GroupResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

declare(strict_types=1);

namespace App\Filament\Shelter\Resources;

use App\Filament\Shelter\Resources\GroupResource\Pages;
use App\Filament\Shelter\Resources\GroupResource\RelationManagers\StaysRelationManager;
use App\Filament\Shelter\Resources\GroupResource\Schemas\GroupForm;
use App\Filament\Shelter\Resources\GroupResource\Schemas\GroupInfolist;
use App\Models\Group;
use Filament\Forms\Form;
use Filament\Infolists\Infolist;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

class GroupResource extends Resource
{
protected static ?string $model = Group::class;

protected static ?string $navigationIcon = 'heroicon-o-user-group';

protected static ?string $recordTitleAttribute = 'name';

protected static bool $isScopedToTenant = true;

public static function getNavigationGroup(): ?string
{
return __('app.navigation.activity');
}

public static function getModelLabel(): string
{
return __('app.group.label.singular');
}

public static function getPluralModelLabel(): string
{
return __('app.group.label.plural');
}

public static function form(Form $form): Form
{
return $form
->columns(1)
->schema(GroupForm::getSchema());
}

public static function infolist(Infolist $infolist): Infolist
{
return $infolist
->columns(1)
->schema(GroupInfolist::getSchema());
}

public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('id')
->label(__('app.field.id'))
->prefix('#')
->sortable()
->shrink(),

TextColumn::make('name')
->label(__('app.field.group_name'))
->searchable()
->sortable(),

TextColumn::make('stays_count')
->label(__('app.field.group_members'))
->counts('stays')
->sortable(),
])
->filters([
//
])
->actions([
Tables\Actions\ActionGroup::make([
Tables\Actions\ViewAction::make(),
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
]),
]);
}

public static function getRelations(): array
{
return [
StaysRelationManager::class,
];
}

public static function getPages(): array
{
return [
'index' => Pages\ListGroups::route('/'),
'create' => Pages\CreateGroup::route('/create'),
'view' => Pages\ViewGroup::route('/{record}'),
'edit' => Pages\EditGroup::route('/{record}/edit'),
];
}
}
Loading
Loading