Skip to content

Commit

Permalink
Merge branch 'filament' into filamentExpenseResource
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurpar06 committed Oct 8, 2023
2 parents b557df2 + 67da786 commit 6ea7eef
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 38 deletions.
83 changes: 83 additions & 0 deletions app/Filament/Actions/ExportAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace App\Filament\Actions;

use App\Models\Enums\ImportExportType;
use App\Repositories\AircraftRepository;
use App\Repositories\AirportRepository;
use App\Repositories\ExpenseRepository;
use App\Repositories\FareRepository;
use App\Repositories\FlightRepository;
use App\Repositories\SubfleetRepository;
use App\Services\ExportService;
use Filament\Actions\Action;
use Filament\Actions\Concerns\CanCustomizeProcess;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class ExportAction extends Action
{
use CanCustomizeProcess;

public static function getDefaultName(): ?string
{
return 'export';
}

protected function setUp(): void
{
parent::setUp();

$this->label('Export to CSV');

$this->action(function (array $arguments): ?BinaryFileResponse {
if (!isset($arguments['resourceTitle']) || !$arguments['exportType']) {
$this->failure();
return null;
}

$exportSvc = app(ExportService::class);

$file_name = $arguments['resourceTitle'].'.csv';

switch ($arguments['exportType']) {
case ImportExportType::AIRCRAFT:
$data = app(AircraftRepository::class)->orderBy('registration')->get();
$path = $exportSvc->exportAircraft($data);
break;
case ImportExportType::AIRPORT:
$data = app(AirportRepository::class)->all();
$path = $exportSvc->exportAirports($data);
break;
case ImportExportType::EXPENSES:
$data = app(ExpenseRepository::class)->all();
$path = $exportSvc->exportExpenses($data);
break;
case ImportExportType::FARES:
$data = app(FareRepository::class)->all();
$path = $exportSvc->exportFares($data);
break;
case ImportExportType::FLIGHTS:
$data = app(FlightRepository::class)->orderBy('airline_id')->orderBy('flight_number')->orderBy('route_code')->orderBy('route_leg')->get();
$path = $exportSvc->exportFlights($data);
break;
case ImportExportType::SUBFLEETS:
$data = app(SubfleetRepository::class)->all();
$path = $exportSvc->exportSubfleets($data);
break;
}

$this->sendSuccessNotification();
return response()->download($path, $file_name, ['content-type' => 'text/csv'])->deleteFileAfterSend(true);
});

$this->successNotificationTitle('Data exported successfully');

$this->modalHeading('Export to CSV');

$this->modalSubmitActionLabel('Export');

$this->icon('heroicon-o-document-arrow-down');

$this->groupedIcon('heroicon-m-document-arrow-down');
}
}
94 changes: 94 additions & 0 deletions app/Filament/Actions/ImportAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace App\Filament\Actions;

use App\Models\Enums\ImportExportType;
use App\Services\ImportService;
use Filament\Actions\Action;
use Filament\Actions\Concerns\CanCustomizeProcess;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Toggle;
use Filament\Notifications\Notification;
use Illuminate\Support\Facades\Log;

class ImportAction extends Action
{
use CanCustomizeProcess;

public static function getDefaultName(): ?string
{
return 'import';
}

protected function setUp(): void
{
parent::setUp();

$this->label('Import from CSV');

$this->form([
FileUpload::make('importFile')->acceptedFileTypes(['text/csv'])->disk('local')->directory('import'),
Toggle::make('deletePrevious')->label('Delete Existing Data')->default(false),
]);

$this->action(function (array $data, array $arguments): void {
if (!isset($arguments['resourceTitle']) || !$arguments['importType']) {
$this->failure();
return;
}

$importSvc = app(ImportService::class);

$path = storage_path('app/'.$data['importFile']);
Log::info('Uploaded '.$arguments['resourceTitle'].' import file to '.$path);

switch ($arguments['importType']) {
case ImportExportType::AIRCRAFT:
$logs = $importSvc->importAircraft($path, $data['deletePrevious']);
break;
case ImportExportType::AIRPORT:
$logs = $importSvc->importAirports($path, $data['deletePrevious']);
break;
case ImportExportType::EXPENSES:
$logs = $importSvc->importExpenses($path, $data['deletePrevious']);
break;
case ImportExportType::FARES:
$logs = $importSvc->importFares($path, $data['deletePrevious']);
break;
case ImportExportType::FLIGHTS:
$logs = $importSvc->importFlights($path, $data['deletePrevious']);
break;
case ImportExportType::SUBFLEETS:
$logs = $importSvc->importSubfleets($path, $data['deletePrevious']);
break;
}

if (count($logs['errors']) > 0) {
Notification::make()
->title('There were '.count($logs['errors']).' errors importing '.$arguments['resourceTitle'])
->body(implode('<br>', $logs['errors']))
->persistent()
->actions([
\Filament\Notifications\Actions\Action::make('close')->label('Close')->close(),
])
->danger()
->send();
}

if (count($logs['success']) > 0) {
Notification::make()
->title(count($logs['success']).' '.$arguments['resourceTitle'].' imported successfully')
->success()
->send();
}
});

$this->modalHeading('Import from CSV');

$this->modalSubmitActionLabel('Import');

$this->icon('heroicon-o-document-arrow-up');

$this->groupedIcon('heroicon-m-document-arrow-up');
}
}
43 changes: 5 additions & 38 deletions app/Filament/Resources/FlightResource/Pages/ListFlights.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@

namespace App\Filament\Resources\FlightResource\Pages;

use App\Filament\Actions\ExportAction;
use App\Filament\Actions\ImportAction;
use App\Filament\Resources\FlightResource;
use App\Repositories\FlightRepository;
use App\Services\ExportService;
use App\Services\ImportService;
use App\Models\Enums\ImportExportType;
use Filament\Actions;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Toggle;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class ListFlights extends ListRecords
{
Expand All @@ -21,36 +16,8 @@ class ListFlights extends ListRecords
protected function getHeaderActions(): array
{
return [
Actions\Action::make('export')->label('Export to CSV')
->action(function (): BinaryFileResponse {
$exporter = app(ExportService::class);
$flightRepo = app(FlightRepository::class);

$where = [];
$file_name = 'flights.csv';
$flights = $flightRepo->where($where)->orderBy('airline_id')->orderBy('flight_number')->orderBy('route_code')->orderBy('route_leg')->get();

$path = $exporter->exportFlights($flights);

return response()->download($path, $file_name, ['content-type' => 'text/csv'])->deleteFileAfterSend(true);
})->after(function (): void {
Notification::make()
->title('Flights Exported')
->success()
->send();
}),
Actions\Action::make('import')->label('Import from CSV')
->form([
FileUpload::make('importFile')->acceptedFileTypes(['text/csv'])->disk('local')->directory('import'),
Toggle::make('delete')->label('Delete Previous Flights')->default(false),
])->action(function (array $data): void {
$importSvc = app(ImportService::class);

$path = storage_path('app/'.$data['importFile']);
Log::info('Uploaded airport import file to '.$path);

$importSvc->importFlights($path, $data['delete']);
}),
ExportAction::make('export')->arguments(['resourceTitle' => 'flights', 'exportType' => ImportExportType::FLIGHTS]),
ImportAction::make('import')->arguments(['resourceTitle' => 'flights', 'importType' => ImportExportType::FLIGHTS]),
Actions\CreateAction::make()->label('Add Flight'),
];
}
Expand Down

0 comments on commit 6ea7eef

Please sign in to comment.