2017-06-12 00:36:16 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
use App\Http\Requests\CreateAirportRequest;
|
2018-03-23 06:17:37 +08:00
|
|
|
use App\Http\Requests\ImportRequest;
|
2017-06-12 00:36:16 +08:00
|
|
|
use App\Http\Requests\UpdateAirportRequest;
|
2018-03-20 09:50:40 +08:00
|
|
|
use App\Interfaces\Controller;
|
2018-03-06 12:10:00 +08:00
|
|
|
use App\Models\Airport;
|
|
|
|
use App\Models\Expense;
|
2017-06-12 00:36:16 +08:00
|
|
|
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;
|
2018-03-22 08:12:36 +08:00
|
|
|
use App\Services\ImportService;
|
2018-02-21 12:33:09 +08:00
|
|
|
use Flash;
|
2017-06-12 00:36:16 +08:00
|
|
|
use Illuminate\Http\Request;
|
2017-12-07 06:07:25 +08:00
|
|
|
use Jackiedo\Timezonelist\Facades\Timezonelist;
|
2018-03-21 08:17:11 +08:00
|
|
|
use Log;
|
2017-06-12 00:36:16 +08:00
|
|
|
use Response;
|
2018-03-21 08:17:11 +08:00
|
|
|
use Storage;
|
2017-06-12 00:36:16 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* Class AirportController
|
|
|
|
* @package App\Http\Controllers\Admin
|
|
|
|
*/
|
|
|
|
class AirportController extends Controller
|
2017-06-12 00:36:16 +08:00
|
|
|
{
|
2018-03-21 08:17:11 +08:00
|
|
|
private $airportRepo,
|
|
|
|
$importSvc;
|
2017-06-12 00:36:16 +08:00
|
|
|
|
2018-02-21 12:33:09 +08:00
|
|
|
/**
|
|
|
|
* @param AirportRepository $airportRepo
|
2018-03-22 08:12:36 +08:00
|
|
|
* @param ImportService $importSvc
|
2018-02-21 12:33:09 +08:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2018-03-21 08:17:11 +08:00
|
|
|
AirportRepository $airportRepo,
|
2018-03-22 08:12:36 +08:00
|
|
|
ImportService $importSvc
|
2018-02-21 12:33:09 +08:00
|
|
|
) {
|
2018-03-06 12:10:00 +08:00
|
|
|
$this->airportRepo = $airportRepo;
|
2018-03-21 08:17:11 +08:00
|
|
|
$this->importSvc = $importSvc;
|
2017-06-12 00:36:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a listing of the Airport.
|
|
|
|
* @param Request $request
|
|
|
|
* @return Response
|
2017-12-08 07:22:15 +08:00
|
|
|
* @throws \Prettus\Repository\Exceptions\RepositoryException
|
2017-06-12 00:36:16 +08:00
|
|
|
*/
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
2018-01-04 10:07:34 +08:00
|
|
|
$where = [];
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($request->has('icao')) {
|
2018-01-04 10:07:34 +08:00
|
|
|
$where['icao'] = $request->get('icao');
|
|
|
|
}
|
|
|
|
|
2018-03-06 12:10:00 +08:00
|
|
|
$this->airportRepo->pushCriteria(new WhereCriteria($request, $where));
|
|
|
|
$airports = $this->airportRepo
|
2018-03-20 09:50:40 +08:00
|
|
|
->orderBy('icao', 'asc')
|
|
|
|
->paginate();
|
2017-06-12 00:36:16 +08:00
|
|
|
|
2017-07-06 07:48:32 +08:00
|
|
|
return view('admin.airports.index', [
|
|
|
|
'airports' => $airports,
|
|
|
|
]);
|
2017-06-12 00:36:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new Airport.
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
2017-12-08 07:22:15 +08:00
|
|
|
return view('admin.airports.create', [
|
|
|
|
'timezones' => Timezonelist::toArray(),
|
|
|
|
]);
|
2017-06-12 00:36:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created Airport in storage.
|
|
|
|
* @param CreateAirportRequest $request
|
|
|
|
* @return Response
|
2017-12-08 07:22:15 +08:00
|
|
|
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
2017-06-12 00:36:16 +08:00
|
|
|
*/
|
|
|
|
public function store(CreateAirportRequest $request)
|
|
|
|
{
|
|
|
|
$input = $request->all();
|
2018-01-22 02:43:32 +08:00
|
|
|
$input['hub'] = get_truth_state($input['hub']);
|
2018-01-05 11:07:23 +08:00
|
|
|
|
2018-03-06 12:10:00 +08:00
|
|
|
$this->airportRepo->create($input);
|
2017-06-12 00:36:16 +08:00
|
|
|
|
|
|
|
Flash::success('Airport saved successfully.');
|
|
|
|
return redirect(route('admin.airports.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified Airport.
|
|
|
|
* @param int $id
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
2018-03-06 12:10:00 +08:00
|
|
|
$airport = $this->airportRepo->findWithoutFail($id);
|
2017-06-12 00:36:16 +08:00
|
|
|
|
|
|
|
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,
|
|
|
|
]);
|
2017-06-12 00:36:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified Airport.
|
|
|
|
* @param int $id
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function edit($id)
|
|
|
|
{
|
2018-03-06 12:10:00 +08:00
|
|
|
$airport = $this->airportRepo->findWithoutFail($id);
|
2017-06-12 00:36:16 +08:00
|
|
|
|
|
|
|
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', [
|
2017-12-07 06:07:25 +08:00
|
|
|
'timezones' => Timezonelist::toArray(),
|
2018-03-20 09:50:40 +08:00
|
|
|
'airport' => $airport,
|
2017-07-06 07:48:32 +08:00
|
|
|
]);
|
2017-06-12 00:36:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified Airport in storage.
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param int $id
|
2017-06-12 00:36:16 +08:00
|
|
|
* @param UpdateAirportRequest $request
|
|
|
|
* @return Response
|
2017-12-08 07:22:15 +08:00
|
|
|
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
2017-06-12 00:36:16 +08:00
|
|
|
*/
|
|
|
|
public function update($id, UpdateAirportRequest $request)
|
|
|
|
{
|
2018-03-06 12:10:00 +08:00
|
|
|
$airport = $this->airportRepo->findWithoutFail($id);
|
2017-06-12 00:36:16 +08:00
|
|
|
|
|
|
|
if (empty($airport)) {
|
|
|
|
Flash::error('Airport not found');
|
|
|
|
return redirect(route('admin.airports.index'));
|
|
|
|
}
|
|
|
|
|
2018-01-05 11:07:23 +08:00
|
|
|
$attrs = $request->all();
|
2018-01-22 02:43:32 +08:00
|
|
|
$attrs['hub'] = get_truth_state($attrs['hub']);
|
2018-01-05 13:22:31 +08:00
|
|
|
|
2018-03-06 12:10:00 +08:00
|
|
|
$this->airportRepo->update($attrs, $id);
|
2017-06-12 00:36:16 +08:00
|
|
|
|
|
|
|
Flash::success('Airport updated successfully.');
|
|
|
|
return redirect(route('admin.airports.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified Airport from storage.
|
|
|
|
* @param int $id
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
2018-03-06 12:10:00 +08:00
|
|
|
$airport = $this->airportRepo->findWithoutFail($id);
|
2017-06-12 00:36:16 +08:00
|
|
|
|
|
|
|
if (empty($airport)) {
|
|
|
|
Flash::error('Airport not found');
|
|
|
|
return redirect(route('admin.airports.index'));
|
|
|
|
}
|
|
|
|
|
2018-03-06 12:10:00 +08:00
|
|
|
$this->airportRepo->delete($id);
|
2017-06-12 00:36:16 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
|
|
|
|
* @throws \League\Csv\Exception
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-03-21 08:17:11 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
2018-03-23 06:17:37 +08:00
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
2018-03-21 08:17:11 +08:00
|
|
|
*/
|
|
|
|
public function import(Request $request)
|
|
|
|
{
|
|
|
|
$logs = [
|
|
|
|
'success' => [],
|
2018-03-23 06:17:37 +08:00
|
|
|
'errors' => [],
|
2018-03-21 08:17:11 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
if ($request->isMethod('post')) {
|
2018-03-23 06:17:37 +08:00
|
|
|
ImportRequest::validate($request);
|
2018-03-21 08:17:11 +08:00
|
|
|
$path = Storage::putFileAs(
|
2018-03-23 06:48:57 +08:00
|
|
|
'import', $request->file('csv_file'), 'import_airports.csv'
|
2018-03-21 08:17:11 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
$path = storage_path('app/'.$path);
|
2018-03-23 06:48:57 +08:00
|
|
|
Log::info('Uploaded airport import file to '.$path);
|
2018-03-21 08:17:11 +08:00
|
|
|
$logs = $this->importSvc->importAirports($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('admin.airports.import', [
|
|
|
|
'logs' => $logs,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-03-06 12:10:00 +08:00
|
|
|
/**
|
2018-03-23 06:17:37 +08:00
|
|
|
* @param Airport $airport
|
2018-03-06 12:10:00 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2018-03-23 06:17:37 +08:00
|
|
|
protected function return_expenses_view(Airport $airport)
|
2018-03-06 12:10:00 +08:00
|
|
|
{
|
|
|
|
$airport->refresh();
|
|
|
|
return view('admin.airports.expenses', [
|
|
|
|
'airport' => $airport,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Operations for associating ranks to the subfleet
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param $id
|
2018-03-06 12:10:00 +08:00
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
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;
|
2018-03-06 12:10:00 +08:00
|
|
|
$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
|
|
|
|
* @param Request $request
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2017-07-06 07:48:32 +08:00
|
|
|
public function fuel(Request $request)
|
|
|
|
{
|
|
|
|
$id = $request->id;
|
|
|
|
|
2018-03-06 12:10:00 +08:00
|
|
|
$airport = $this->airportRepo->findWithoutFail($id);
|
2017-07-06 07:48:32 +08:00
|
|
|
if (empty($airport)) {
|
|
|
|
Flash::error('Flight not found');
|
2018-03-20 09:50:40 +08:00
|
|
|
|
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();
|
|
|
|
}
|
2017-06-12 00:36:16 +08:00
|
|
|
}
|