Add aircraft export
This commit is contained in:
@@ -10,6 +10,7 @@ use App\Models\Enums\AircraftStatus;
|
||||
use App\Models\Expense;
|
||||
use App\Models\Subfleet;
|
||||
use App\Repositories\AircraftRepository;
|
||||
use App\Services\ExportService;
|
||||
use App\Services\ImportService;
|
||||
use Flash;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -152,6 +153,25 @@ class AircraftController extends Controller
|
||||
return redirect(route('admin.aircraft.index'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the flight exporter
|
||||
* @param Request $request
|
||||
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
|
||||
* @throws \League\Csv\Exception
|
||||
*/
|
||||
public function export(Request $request)
|
||||
{
|
||||
$exporter = app(ExportService::class);
|
||||
$aircraft = $this->aircraftRepo->all();
|
||||
|
||||
$path = $exporter->exportAircraft($aircraft);
|
||||
return response()
|
||||
->download($path, 'aircraft.csv', [
|
||||
'content-type' => 'text/csv',
|
||||
])
|
||||
->deleteFileAfterSend(true);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param Request $request
|
||||
|
||||
@@ -18,6 +18,7 @@ Route::group([
|
||||
Route::resource('awards', 'AwardController');
|
||||
|
||||
# aircraft and fare associations
|
||||
Route::get('aircraft/export', 'AircraftController@export')->name('aircraft.export');
|
||||
Route::match(['get', 'post'], 'aircraft/import', 'AircraftController@import')->name('aircraft.import');
|
||||
Route::match(['get', 'post', 'put', 'delete'], 'aircraft/{id}/expenses', 'AircraftController@expenses');
|
||||
Route::resource('aircraft', 'AircraftController');
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Services;
|
||||
use App\Interfaces\ImportExport;
|
||||
use App\Interfaces\Service;
|
||||
use App\Repositories\FlightRepository;
|
||||
use App\Services\ImportExport\AircraftExporter;
|
||||
use App\Services\ImportExport\FlightExporter;
|
||||
use Illuminate\Support\Collection;
|
||||
use League\Csv\CharsetConverter;
|
||||
@@ -68,11 +69,22 @@ class ExportService extends Service
|
||||
}
|
||||
|
||||
/**
|
||||
* Export all of the flights
|
||||
* @param Collection $flights
|
||||
* @param string $csv_file
|
||||
* Export all of the aircraft
|
||||
* @param Collection $aircraft
|
||||
* @return mixed
|
||||
* @throws \League\Csv\Exception
|
||||
* @throws \League\Csv\CannotInsertRecord
|
||||
*/
|
||||
public function exportAircraft($aircraft)
|
||||
{
|
||||
$exporter = new AircraftExporter();
|
||||
return $this->runExport($aircraft, $exporter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Export all of the flights
|
||||
* @param Collection $flights
|
||||
* @return mixed
|
||||
* @throws \League\Csv\CannotInsertRecord
|
||||
*/
|
||||
public function exportFlights($flights)
|
||||
{
|
||||
|
||||
43
app/Services/ImportExport/AircraftExporter.php
Normal file
43
app/Services/ImportExport/AircraftExporter.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ImportExport;
|
||||
|
||||
use App\Interfaces\ImportExport;
|
||||
use App\Models\Aircraft;
|
||||
use App\Models\Flight;
|
||||
|
||||
/**
|
||||
* The flight importer can be imported or export. Operates on rows
|
||||
*
|
||||
* @package App\Services\Import
|
||||
*/
|
||||
class AircraftExporter extends ImportExport
|
||||
{
|
||||
public $assetType = 'aircraft';
|
||||
|
||||
/**
|
||||
* Set the current columns and other setup
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
self::$columns = AircraftImporter::$columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Import a flight, parse out the different rows
|
||||
* @param Aircraft $aircraft
|
||||
* @return array
|
||||
*/
|
||||
public function export(Aircraft $aircraft): array
|
||||
{
|
||||
$ret = [];
|
||||
foreach(self::$columns as $column) {
|
||||
$ret[$column] = $aircraft->{$column};
|
||||
}
|
||||
|
||||
# Modify special fields
|
||||
$ret['subfleet'] = $aircraft->subfleet->type;
|
||||
|
||||
return $ret;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
@section('title', 'Aircraft')
|
||||
|
||||
@section('actions')
|
||||
<li><a href="{{ route('admin.aircraft.export') }}"><i class="ti-plus"></i>Export to CSV</a></li>
|
||||
<li><a href="{{ route('admin.aircraft.import') }}"><i class="ti-plus"></i>Import from CSV</a></li>
|
||||
<li><a href="{{ url('/admin/subfleets') }}"><i class="ti-files"></i>Subfleets</a></li>
|
||||
<li><a href="{{ route('admin.aircraft.create') }}"><i class="ti-plus"></i>New Aircraft</a></li>
|
||||
|
||||
Reference in New Issue
Block a user