forked from nabeelio/phpvms
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from arthurpar06/filamentAirlineResource
AirlineResource (wip)
- Loading branch information
Showing
21 changed files
with
353 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
|
||
namespace App\Filament\Resources; | ||
|
||
use App\Filament\Resources\AirlineResource\Pages; | ||
use App\Filament\Resources\AirlineResource\RelationManagers\FilesRelationManager; | ||
use App\Models\Airline; | ||
use App\Models\File; | ||
use App\Services\FileService; | ||
use Filament\Forms\Components\Section; | ||
use Filament\Forms\Components\Select; | ||
use Filament\Forms\Components\TextInput; | ||
use Filament\Forms\Components\Toggle; | ||
use Filament\Forms\Form; | ||
use Filament\Resources\Resource; | ||
use Filament\Tables; | ||
use Filament\Tables\Columns\IconColumn; | ||
use Filament\Tables\Columns\TextColumn; | ||
use Filament\Tables\Table; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Database\Eloquent\SoftDeletingScope; | ||
use League\ISO3166\ISO3166; | ||
|
||
class AirlineResource extends Resource | ||
{ | ||
protected static ?string $model = Airline::class; | ||
|
||
protected static ?string $navigationGroup = 'config'; | ||
protected static ?int $navigationSort = 1; | ||
|
||
protected static ?string $navigationLabel = 'Airlines'; | ||
|
||
protected static ?string $navigationIcon = 'heroicon-o-building-office'; | ||
|
||
protected static ?string $recordTitleAttribute = 'name'; | ||
|
||
public static function form(Form $form): Form | ||
{ | ||
return $form | ||
->schema([ | ||
Section::make('Airline Informations')->schema([ | ||
TextInput::make('icao')->label('ICAO (3LD)')->required()->string()->length(3), | ||
TextInput::make('iata')->label('IATA (2LD)')->string()->length(2), | ||
TextInput::make('callsign')->label('Radio Callsign')->string()->maxLength(191), | ||
TextInput::make('name')->label('Name')->required()->string()->maxLength(50), | ||
TextInput::make('logo')->label('Logo URL')->string()->maxLength(255), | ||
Select::make('country') | ||
->options(collect((new ISO3166())->all())->mapWithKeys(fn ($item, $key) => [strtolower($item['alpha2']) => str_replace('&bnsp;', ' ', $item['name'])])) | ||
->searchable() | ||
->native(false), | ||
Toggle::make('active')->inline()->onColor('success')->onIcon('heroicon-m-check-circle')->offColor('danger')->offIcon('heroicon-m-x-circle'), | ||
])->columns(3), | ||
]); | ||
} | ||
|
||
public static function table(Table $table): Table | ||
{ | ||
return $table | ||
->columns([ | ||
TextColumn::make('Code')->formatStateUsing(function (Airline $record) { | ||
$html = ''; | ||
if (filled($record->country)) { | ||
$html .= '<span class="flag-icon flag-icon-'.$record->country.'"></span> '; | ||
} | ||
if (filled($record->iata)) { | ||
$html .= $record->iata.'/'; | ||
} | ||
return $html.$record->icao; | ||
})->html(), | ||
TextColumn::make('name')->label('Name')->searchable(), | ||
IconColumn::make('active')->label('Active')->color(fn ($record) => $record->active ? 'success' : 'danger')->icon(fn ($state) => $state ? 'heroicon-o-check-circle' : 'heroicon-o-x-circle'), | ||
]) | ||
->filters([ | ||
Tables\Filters\TrashedFilter::make(), | ||
]) | ||
->actions([ | ||
Tables\Actions\EditAction::make(), | ||
Tables\Actions\DeleteAction::make(), | ||
Tables\Actions\ForceDeleteAction::make()->before(function (Airline $record) { | ||
$record->files()->each(function (File $file) { | ||
app(FileService::class)->removeFile($file); | ||
}); | ||
}), | ||
Tables\Actions\RestoreAction::make(), | ||
]) | ||
->bulkActions([ | ||
Tables\Actions\BulkActionGroup::make([ | ||
Tables\Actions\DeleteBulkAction::make(), | ||
Tables\Actions\ForceDeleteBulkAction::make()->before(function (Collection $records) { | ||
$records->each(fn (Airline $record) => $record->files()->each(function (File $file) { | ||
app(FileService::class)->removeFile($file); | ||
})); | ||
}), | ||
Tables\Actions\RestoreBulkAction::make(), | ||
]), | ||
]) | ||
->emptyStateActions([ | ||
Tables\Actions\CreateAction::make()->label('Add Airline'), | ||
]); | ||
} | ||
|
||
public static function getEloquentQuery(): Builder | ||
{ | ||
return parent::getEloquentQuery() | ||
->withoutGlobalScopes([ | ||
SoftDeletingScope::class, | ||
]); | ||
} | ||
|
||
public static function getRelations(): array | ||
{ | ||
return [ | ||
FilesRelationManager::class, | ||
]; | ||
} | ||
|
||
public static function getPages(): array | ||
{ | ||
return [ | ||
'index' => Pages\ListAirlines::route('/'), | ||
'create' => Pages\CreateAirline::route('/create'), | ||
'edit' => Pages\EditAirline::route('/{record}/edit'), | ||
]; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
app/Filament/Resources/AirlineResource/Pages/CreateAirline.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace App\Filament\Resources\AirlineResource\Pages; | ||
|
||
use App\Filament\Resources\AirlineResource; | ||
use Filament\Resources\Pages\CreateRecord; | ||
|
||
class CreateAirline extends CreateRecord | ||
{ | ||
protected static string $resource = AirlineResource::class; | ||
} |
28 changes: 28 additions & 0 deletions
28
app/Filament/Resources/AirlineResource/Pages/EditAirline.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace App\Filament\Resources\AirlineResource\Pages; | ||
|
||
use App\Filament\Resources\AirlineResource; | ||
use App\Models\Airline; | ||
use App\Models\File; | ||
use App\Services\FileService; | ||
use Filament\Actions; | ||
use Filament\Resources\Pages\EditRecord; | ||
|
||
class EditAirline extends EditRecord | ||
{ | ||
protected static string $resource = AirlineResource::class; | ||
|
||
protected function getHeaderActions(): array | ||
{ | ||
return [ | ||
Actions\DeleteAction::make(), | ||
Actions\ForceDeleteAction::make()->before(function (Airline $record) { | ||
$record->files()->each(function (File $file) { | ||
app(FileService::class)->removeFile($file); | ||
}); | ||
}), | ||
Actions\RestoreAction::make(), | ||
]; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
app/Filament/Resources/AirlineResource/Pages/ListAirlines.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace App\Filament\Resources\AirlineResource\Pages; | ||
|
||
use App\Filament\Resources\AirlineResource; | ||
use Filament\Actions; | ||
use Filament\Resources\Pages\ListRecords; | ||
|
||
class ListAirlines extends ListRecords | ||
{ | ||
protected static string $resource = AirlineResource::class; | ||
|
||
protected function getHeaderActions(): array | ||
{ | ||
return [ | ||
Actions\CreateAction::make()->label('Add Airline'), | ||
]; | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
app/Filament/Resources/AirlineResource/RelationManagers/FilesRelationManager.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace App\Filament\Resources\AirlineResource\RelationManagers; | ||
|
||
use App\Models\File; | ||
use Filament\Forms\Components\FileUpload; | ||
use Filament\Forms\Components\TextInput; | ||
use Filament\Forms\Form; | ||
use Filament\Resources\RelationManagers\RelationManager; | ||
use Filament\Tables; | ||
use Filament\Tables\Columns\TextColumn; | ||
use Filament\Tables\Table; | ||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Support\Facades\Storage; | ||
use Illuminate\Support\Str; | ||
|
||
class FilesRelationManager extends RelationManager | ||
{ | ||
protected static string $relationship = 'files'; | ||
|
||
public function form(Form $form): Form | ||
{ | ||
return $form | ||
->schema([ | ||
TextInput::make('name')->required()->string()->maxLength(191), | ||
TextInput::make('description')->string()->maxLength(191), | ||
TextInput::make('url')->url()->requiredWithout('file'), | ||
FileUpload::make('file')->disk(config('filesystems.public_files'))->directory('files')->requiredWithout('url'), | ||
]); | ||
} | ||
|
||
public function table(Table $table): Table | ||
{ | ||
return $table->recordTitleAttribute('name') | ||
->columns([ | ||
TextColumn::make('name'), | ||
TextColumn::make('download_count')->label('Downloads'), | ||
]) | ||
->filters([ | ||
// | ||
]) | ||
->headerActions([ | ||
Tables\Actions\CreateAction::make()->label('Add File')->mutateFormDataUsing(function (array $data): array { | ||
if (!empty($data['url'])) { | ||
$data['path'] = $data['url']; | ||
} elseif (!empty($data['file'])) { | ||
$data['path'] = $data['file']; | ||
$data['disk'] = config('filesystems.public_files'); | ||
} | ||
|
||
return $data; | ||
}), | ||
]) | ||
->actions([ | ||
Tables\Actions\Action::make('download')->icon('heroicon-m-link')->label('Link to file') | ||
->action(fn (File $record) => Storage::disk($record->disk)->download($record->path, Str::kebab($record->name))) | ||
->visible(fn (File $record): bool => $record->disk && !str_contains($record->path, 'http') && Storage::disk($record->disk)->exists($record->path)), | ||
|
||
Tables\Actions\Action::make('view_file')->icon('heroicon-m-link')->label('Link to file') | ||
->url(fn (File $record): string => $record->path, shouldOpenInNewTab: true) | ||
->hidden(fn (File $record): bool => $record->disk && !str_contains($record->path, 'http') && Storage::disk($record->disk)->exists($record->path)), | ||
|
||
Tables\Actions\DeleteAction::make()->before(function (File $record) { | ||
if ($record->disk && !str_contains($record->path, 'http') && Storage::disk($record->disk)->exists($record->path)) { | ||
Storage::disk($record->disk)->delete($record->path); | ||
} | ||
}), | ||
]) | ||
->bulkActions([ | ||
Tables\Actions\BulkActionGroup::make([ | ||
Tables\Actions\DeleteBulkAction::make()->before(function (Collection $records) { | ||
$records->each(function (File $record) { | ||
if ($record->disk && !str_contains($record->path, 'http') && Storage::disk($record->disk)->exists($record->path)) { | ||
Storage::disk($record->disk)->delete($record->path); | ||
} | ||
}); | ||
}), | ||
]), | ||
]) | ||
->emptyStateActions([ | ||
Tables\Actions\CreateAction::make()->label('Add File')->mutateFormDataUsing(function (array $data): array { | ||
if (!empty($data['url'])) { | ||
$data['path'] = $data['url']; | ||
} elseif (!empty($data['file'])) { | ||
$data['path'] = $data['file']; | ||
$data['disk'] = config('filesystems.public_files'); | ||
} | ||
|
||
return $data; | ||
}), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.