phpvms/app/Http/Controllers/Admin/AirportController.php

321 lines
8.1 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers\Admin;
2019-07-16 03:51:35 +08:00
use App\Contracts\Controller;
use App\Http\Requests\CreateAirportRequest;
2018-03-23 06:17:37 +08:00
use App\Http\Requests\ImportRequest;
use App\Http\Requests\UpdateAirportRequest;
use App\Models\Airport;
use App\Models\Expense;
use App\Repositories\AirportRepository;
2018-01-04 10:07:34 +08:00
use App\Repositories\Criteria\WhereCriteria;
2018-03-23 02:04:13 +08:00
use App\Services\ExportService;
use App\Services\ImportService;
use App\Support\Timezonelist;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Laracasts\Flash\Flash;
class AirportController extends Controller
{
2018-08-27 00:40:04 +08:00
private $airportRepo;
private $importSvc;
2018-02-21 12:33:09 +08:00
/**
* @param AirportRepository $airportRepo
* @param ImportService $importSvc
2018-02-21 12:33:09 +08:00
*/
public function __construct(
AirportRepository $airportRepo,
ImportService $importSvc
2018-02-21 12:33:09 +08:00
) {
$this->airportRepo = $airportRepo;
$this->importSvc = $importSvc;
}
/**
* Display a listing of the Airport.
2018-08-27 00:40:04 +08:00
*
* @param Request $request
2018-08-27 00:40:04 +08:00
*
* @throws \Prettus\Repository\Exceptions\RepositoryException
2018-08-27 00:40:04 +08:00
*
* @return mixed
*/
public function index(Request $request)
{
2018-01-04 10:07:34 +08:00
$where = [];
if ($request->has('icao')) {
2018-01-04 10:07:34 +08:00
$where['icao'] = $request->get('icao');
}
$this->airportRepo->pushCriteria(new WhereCriteria($request, $where));
$airports = $this->airportRepo
->orderBy('icao', 'asc')
->paginate();
2017-07-06 07:48:32 +08:00
return view('admin.airports.index', [
'airports' => $airports,
]);
}
/**
* Show the form for creating a new Airport.
2018-08-27 00:40:04 +08:00
*
* @return mixed
*/
public function create()
{
return view('admin.airports.create', [
'timezones' => Timezonelist::toArray(),
]);
}
/**
* Store a newly created Airport in storage.
2018-08-27 00:40:04 +08:00
*
* @param CreateAirportRequest $request
2018-08-27 00:40:04 +08:00
*
* @throws \Prettus\Validator\Exceptions\ValidatorException
2018-08-27 00:40:04 +08:00
*
* @return mixed
*/
public function store(CreateAirportRequest $request)
{
$input = $request->all();
$input['hub'] = get_truth_state($input['hub']);
$this->airportRepo->create($input);
Flash::success('Airport saved successfully.');
return redirect(route('admin.airports.index'));
}
/**
* Display the specified Airport.
2018-08-27 00:40:04 +08:00
*
* @param int $id
*
* @return mixed
*/
public function show($id)
{
$airport = $this->airportRepo->findWithoutFail($id);
if (empty($airport)) {
Flash::error('Airport not found');
return redirect(route('admin.airports.index'));
}
2017-07-06 07:48:32 +08:00
return view('admin.airports.show', [
'airport' => $airport,
]);
}
/**
* Show the form for editing the specified Airport.
2018-08-27 00:40:04 +08:00
*
* @param int $id
*
* @return mixed
*/
public function edit($id)
{
$airport = $this->airportRepo->findWithoutFail($id);
if (empty($airport)) {
Flash::error('Airport not found');
return redirect(route('admin.airports.index'));
}
2017-07-06 07:48:32 +08:00
return view('admin.airports.edit', [
'timezones' => Timezonelist::toArray(),
'airport' => $airport,
2017-07-06 07:48:32 +08:00
]);
}
/**
* Update the specified Airport in storage.
2018-08-27 00:40:04 +08:00
*
* @param int $id
* @param UpdateAirportRequest $request
2018-08-27 00:40:04 +08:00
*
* @throws \Prettus\Validator\Exceptions\ValidatorException
2018-08-27 00:40:04 +08:00
*
* @return mixed
*/
public function update($id, UpdateAirportRequest $request)
{
$airport = $this->airportRepo->findWithoutFail($id);
if (empty($airport)) {
Flash::error('Airport not found');
return redirect(route('admin.airports.index'));
}
$attrs = $request->all();
$attrs['hub'] = get_truth_state($attrs['hub']);
$this->airportRepo->update($attrs, $id);
Flash::success('Airport updated successfully.');
return redirect(route('admin.airports.index'));
}
/**
* Remove the specified Airport from storage.
2018-08-27 00:40:04 +08:00
*
* @param int $id
*
* @return mixed
*/
public function destroy($id)
{
$airport = $this->airportRepo->findWithoutFail($id);
if (empty($airport)) {
Flash::error('Airport not found');
return redirect(route('admin.airports.index'));
}
$this->airportRepo->delete($id);
Flash::success('Airport deleted successfully.');
return redirect(route('admin.airports.index'));
}
2017-07-06 07:48:32 +08:00
2018-03-23 02:04:13 +08:00
/**
* Run the airport exporter
2018-08-27 00:40:04 +08:00
*
2018-03-23 02:04:13 +08:00
* @param Request $request
2018-08-27 00:40:04 +08:00
*
2018-03-23 02:04:13 +08:00
* @throws \League\Csv\Exception
2018-08-27 00:40:04 +08:00
*
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
2018-03-23 02:04:13 +08:00
*/
public function export(Request $request)
{
$exporter = app(ExportService::class);
$airports = $this->airportRepo->all();
$path = $exporter->exportAirports($airports);
return response()
->download($path, 'airports.csv', [
'content-type' => 'text/csv',
])
->deleteFileAfterSend(true);
}
/**
* @param Request $request
2018-08-27 00:40:04 +08:00
*
2018-03-23 06:17:37 +08:00
* @throws \Illuminate\Validation\ValidationException
2018-08-27 00:40:04 +08:00
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function import(Request $request)
{
$logs = [
'success' => [],
2018-03-23 06:17:37 +08:00
'errors' => [],
];
if ($request->isMethod('post')) {
2018-03-23 06:17:37 +08:00
ImportRequest::validate($request);
$path = Storage::putFileAs(
2019-05-13 02:26:44 +08:00
'import',
$request->file('csv_file'),
'import_airports.csv'
);
$path = storage_path('app/'.$path);
2018-03-23 06:48:57 +08:00
Log::info('Uploaded airport import file to '.$path);
$logs = $this->importSvc->importAirports($path);
}
return view('admin.airports.import', [
'logs' => $logs,
]);
}
/**
2018-03-23 06:17:37 +08:00
* @param Airport $airport
2018-08-27 00:40:04 +08:00
*
* @return mixed
*/
2018-03-23 06:17:37 +08:00
protected function return_expenses_view(Airport $airport)
{
$airport->refresh();
return view('admin.airports.expenses', [
'airport' => $airport,
]);
}
/**
* Operations for associating ranks to the subfleet
2018-08-27 00:40:04 +08:00
*
* @param $id
* @param Request $request
2018-08-27 00:40:04 +08:00
*
* @throws \Exception
2018-08-27 00:40:04 +08:00
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function expenses($id, Request $request)
{
$airport = $this->airportRepo->findWithoutFail($id);
if (empty($airport)) {
return $this->return_expenses_view($airport);
}
if ($request->isMethod('get')) {
return $this->return_expenses_view($airport);
}
if ($request->isMethod('post')) {
$expense = new Expense($request->post());
2018-04-02 03:32:01 +08:00
$expense->ref_model = Airport::class;
$expense->ref_model_id = $airport->id;
$expense->save();
} elseif ($request->isMethod('put')) {
$expense = Expense::findOrFail($request->input('expense_id'));
$expense->{$request->name} = $request->value;
$expense->save();
} // dissassociate fare from teh aircraft
elseif ($request->isMethod('delete')) {
$expense = Expense::findOrFail($request->input('expense_id'));
$expense->delete();
}
return $this->return_expenses_view($airport);
}
2018-02-21 12:33:09 +08:00
/**
* Set fuel prices for this airport
2018-08-27 00:40:04 +08:00
*
2018-02-21 12:33:09 +08:00
* @param Request $request
2018-08-27 00:40:04 +08:00
*
2018-02-21 12:33:09 +08:00
* @return mixed
*/
2017-07-06 07:48:32 +08:00
public function fuel(Request $request)
{
$id = $request->id;
$airport = $this->airportRepo->findWithoutFail($id);
2017-07-06 07:48:32 +08:00
if (empty($airport)) {
Flash::error('Flight not found');
2017-07-06 07:48:32 +08:00
return redirect(route('admin.flights.index'));
}
// add aircraft to flight
if ($request->isMethod('put')) {
$airport->{$request->name} = $request->value;
}
$airport->save();
}
}