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

203 lines
5.4 KiB
PHP
Raw Normal View History

2017-06-09 09:37:51 +08:00
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Requests\CreateAircraftRequest;
use App\Http\Requests\UpdateAircraftRequest;
use App\Interfaces\Controller;
use App\Models\Aircraft;
use App\Models\Enums\AircraftStatus;
use App\Models\Expense;
2018-02-21 12:33:09 +08:00
use App\Models\Subfleet;
2017-06-25 03:00:56 +08:00
use App\Repositories\AircraftRepository;
2017-06-09 09:37:51 +08:00
use Flash;
2018-02-21 12:33:09 +08:00
use Illuminate\Http\Request;
2017-06-09 09:37:51 +08:00
use Prettus\Repository\Criteria\RequestCriteria;
2017-12-02 22:29:53 +08:00
/**
* Class AircraftController
* @package App\Http\Controllers\Admin
*/
class AircraftController extends Controller
2017-06-09 09:37:51 +08:00
{
private $aircraftRepo;
2017-06-09 09:37:51 +08:00
2018-02-21 12:33:09 +08:00
/**
* AircraftController constructor.
* @param AircraftRepository $aircraftRepo
*/
2017-06-25 00:09:27 +08:00
public function __construct(
2017-12-02 22:29:53 +08:00
AircraftRepository $aircraftRepo
2017-06-25 00:09:27 +08:00
) {
$this->aircraftRepo = $aircraftRepo;
2017-06-09 09:37:51 +08:00
}
/**
* Display a listing of the Aircraft.
* @throws \Prettus\Repository\Exceptions\RepositoryException
2017-06-09 09:37:51 +08:00
*/
public function index(Request $request)
{
$this->aircraftRepo->pushCriteria(new RequestCriteria($request));
$aircraft = $this->aircraftRepo->orderBy('name', 'asc')->all();
2017-06-09 09:37:51 +08:00
2017-12-02 22:29:53 +08:00
return view('admin.aircraft.index', [
'aircraft' => $aircraft
2017-12-02 22:29:53 +08:00
]);
2017-06-09 09:37:51 +08:00
}
/**
* Show the form for creating a new Aircraft.
*/
public function create()
{
return view('admin.aircraft.create', [
'subfleets' => Subfleet::all()->pluck('name', 'id'),
'statuses' => AircraftStatus::select(true),
]);
2017-06-09 09:37:51 +08:00
}
/**
* Store a newly created Aircraft in storage.
* @throws \Prettus\Validator\Exceptions\ValidatorException
2017-06-09 09:37:51 +08:00
*/
public function store(CreateAircraftRequest $request)
{
2018-02-23 04:59:09 +08:00
$attrs = $request->all();
$aircraft = $this->aircraftRepo->create($attrs);
2017-06-09 09:37:51 +08:00
Flash::success('Aircraft saved successfully.');
2018-02-13 00:26:27 +08:00
return redirect(route('admin.aircraft.edit', ['id' => $aircraft->id]));
2017-06-09 09:37:51 +08:00
}
/**
* Display the specified Aircraft.
*/
public function show($id)
{
$aircraft = $this->aircraftRepo->findWithoutFail($id);
2017-06-09 09:37:51 +08:00
if (empty($aircraft)) {
Flash::error('Aircraft not found');
2017-06-09 09:37:51 +08:00
return redirect(route('admin.aircraft.index'));
}
return view('admin.aircraft.show', [
'aircraft' => $aircraft,
]);
2017-06-09 09:37:51 +08:00
}
/**
* Show the form for editing the specified Aircraft.
*/
public function edit($id)
{
$aircraft = $this->aircraftRepo->findWithoutFail($id);
2017-06-09 09:37:51 +08:00
if (empty($aircraft)) {
Flash::error('Aircraft not found');
2017-06-09 09:37:51 +08:00
return redirect(route('admin.aircraft.index'));
}
return view('admin.aircraft.edit', [
'subfleets' => Subfleet::all()->pluck('name', 'id'),
'statuses' => AircraftStatus::select(true),
2017-06-25 00:09:27 +08:00
'aircraft' => $aircraft,
]);
2017-06-09 09:37:51 +08:00
}
/**
* Update the specified Aircraft in storage.
* @throws \Prettus\Validator\Exceptions\ValidatorException
2017-06-09 09:37:51 +08:00
*/
public function update($id, UpdateAircraftRequest $request)
{
$aircraft = $this->aircraftRepo->findWithoutFail($id);
2017-06-09 09:37:51 +08:00
if (empty($aircraft)) {
Flash::error('Aircraft not found');
2017-06-09 09:37:51 +08:00
return redirect(route('admin.aircraft.index'));
}
2018-02-23 04:59:09 +08:00
$attrs = $request->all();
$this->aircraftRepo->update($attrs, $id);
2017-12-02 22:29:53 +08:00
2017-06-09 09:37:51 +08:00
Flash::success('Aircraft updated successfully.');
2017-06-09 09:37:51 +08:00
return redirect(route('admin.aircraft.index'));
}
/**
* Remove the specified Aircraft from storage.
*/
public function destroy($id)
{
$aircraft = $this->aircraftRepo->findWithoutFail($id);
2017-06-09 09:37:51 +08:00
if (empty($aircraft)) {
Flash::error('Aircraft not found');
2017-06-09 09:37:51 +08:00
return redirect(route('admin.aircraft.index'));
}
$this->aircraftRepo->delete($id);
2017-06-09 09:37:51 +08:00
Flash::success('Aircraft deleted successfully.');
2017-06-09 09:37:51 +08:00
return redirect(route('admin.aircraft.index'));
}
/**
* @param Aircraft|null $aircraft
* @return mixed
*/
protected function return_expenses_view(?Aircraft $aircraft)
{
$aircraft->refresh();
return view('admin.aircraft.expenses', [
'aircraft' => $aircraft,
]);
}
/**
* Operations for associating ranks to the subfleet
* @param $id
* @param Request $request
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @throws \Exception
*/
public function expenses($id, Request $request)
{
$aircraft = $this->aircraftRepo->findWithoutFail($id);
if (empty($aircraft)) {
return $this->return_expenses_view($aircraft);
}
if ($request->isMethod('get')) {
return $this->return_expenses_view($aircraft);
}
if ($request->isMethod('post')) {
$expense = new Expense($request->post());
$expense->ref_class = Aircraft::class;
$expense->ref_class_id = $aircraft->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($aircraft);
}
2017-06-09 09:37:51 +08:00
}