Skip to content

Commit

Permalink
Merge pull request #6 from arthurpar06/filamentAirlineResource
Browse files Browse the repository at this point in the history
AirlineResource (wip)
  • Loading branch information
arthurpar06 authored Oct 7, 2023
2 parents 1ab4791 + 9f8331d commit 4c68740
Show file tree
Hide file tree
Showing 21 changed files with 353 additions and 95 deletions.
126 changes: 126 additions & 0 deletions app/Filament/Resources/AirlineResource.php
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> &nbsp;';
}
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 app/Filament/Resources/AirlineResource/Pages/CreateAirline.php
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 app/Filament/Resources/AirlineResource/Pages/EditAirline.php
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 app/Filament/Resources/AirlineResource/Pages/ListAirlines.php
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'),
];
}
}
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;
}),
]);
}
}
2 changes: 1 addition & 1 deletion app/Filament/Resources/FlightResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static function table(Table $table): Table
TextColumn::make('arr_time')->label('Arr Time'),
TextColumn::make('notes')->label('Notes'),
IconColumn::make('active')->label('Active')->color(fn ($record) => $record->active ? 'success' : 'danger')->icon(fn ($state) => $state ? 'heroicon-o-check-circle' : 'heroicon-o-x-circle'),
IconColumn::make('visible')->label('Visible')->color(fn ($record) => $record->visible ? 'success' : 'danger')->icon(fn ($record) => $record->visible ? 'heroicon-o-check-circle' : 'heroicon-o-x-circle')
IconColumn::make('visible')->label('Visible')->color(fn ($record) => $record->visible ? 'success' : 'danger')->icon(fn ($record) => $record->visible ? 'heroicon-o-check-circle' : 'heroicon-o-x-circle'),
])
->filters([
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
use App\Services\ExportService;
use App\Services\ImportService;
use Filament\Actions;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Toggle;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\ListRecords;
use Filament\Forms\Components\FileUpload;
use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

Expand Down
16 changes: 8 additions & 8 deletions app/Filament/Resources/PirepResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
use App\Models\Enums\PirepState;
use App\Models\Pirep;
use App\Repositories\UserRepository;
use App\Support\Units\Time;
use App\Services\PirepService;
use Filament\Tables\Actions\Action;
use App\Support\Units\Time;
use Filament\Forms;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Actions\Action;
use Filament\Tables\Actions\DeleteAction;
use Filament\Tables\Actions\EditAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Table;
use Filament\Tables\Actions\DeleteAction;
use Filament\Tables\Actions\EditAction;
use Illuminate\Database\Eloquent\Builder;

class PirepResource extends Resource
Expand Down Expand Up @@ -113,9 +113,9 @@ public static function form(Form $form): Form
->required()
->label('Planned Distance'),
Forms\Components\TextInput::make('planned_flight_time')
->required()
]),
])]);
->required(),
]),
])]);
}

public static function table(Table $table): Table
Expand All @@ -127,7 +127,7 @@ public static function table(Table $table): Table
TextColumn::make('dpt_airport_id')->label('DEP')->searchable(),
TextColumn::make('arr_airport_id')->label('ARR')->searchable(),
TextColumn::make('flight_time')->formatStateUsing(fn (int $state): string => Time::minutesToTimeString($state)),
TextColumn::make('aircraft')->formatStateUsing(fn (Pirep $record): string => $record->aircraft->registration .' - '. $record->aircraft->name),
TextColumn::make('aircraft')->formatStateUsing(fn (Pirep $record): string => $record->aircraft->registration.' - '.$record->aircraft->name),
TextColumn::make('source')->label('Filed Using')->formatStateUsing(fn (int $state): string => PirepSource::label($state)),
TextColumn::make('state')->badge()->color(fn (int $state): string => match ($state) {
PirepState::PENDING => 'warning',
Expand Down
1 change: 0 additions & 1 deletion app/Filament/Resources/PirepResource/Pages/ListPireps.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Filament\Actions\Action;
use Filament\Resources\Pages\ListRecords;
use Filament\Resources\Pages\ListRecords\Tab;

use Illuminate\Database\Eloquent\Builder;

class ListPireps extends ListRecords
Expand Down
Loading

0 comments on commit 4c68740

Please sign in to comment.