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

258 lines
6.5 KiB
PHP
Raw Normal View History

2017-06-22 01:10:25 +08:00
<?php
2017-06-22 02:44:30 +08:00
namespace App\Http\Controllers\Admin;
2017-06-22 01:10:25 +08:00
2019-07-16 03:51:35 +08:00
use App\Contracts\Controller;
2017-06-22 09:18:01 +08:00
use App\Http\Requests\CreateRankRequest;
use App\Http\Requests\UpdateRankRequest;
2017-06-22 02:44:30 +08:00
use App\Repositories\RankRepository;
2017-07-02 10:06:55 +08:00
use App\Repositories\SubfleetRepository;
use App\Services\FleetService;
2018-02-21 12:33:09 +08:00
use Cache;
use Illuminate\Http\Request;
use Laracasts\Flash\Flash;
2018-02-21 12:33:09 +08:00
use Prettus\Repository\Criteria\RequestCriteria;
2017-06-22 01:10:25 +08:00
class RankController extends Controller
2017-06-22 01:10:25 +08:00
{
2018-08-27 00:40:04 +08:00
private $fleetSvc;
private $rankRepository;
private $subfleetRepo;
2017-06-22 01:10:25 +08:00
2018-02-21 12:33:09 +08:00
/**
* RankController constructor.
2018-08-27 00:40:04 +08:00
*
* @param FleetService $fleetSvc
* @param RankRepository $rankingRepo
2018-02-21 12:33:09 +08:00
* @param SubfleetRepository $subfleetRepo
*/
2017-07-02 10:06:55 +08:00
public function __construct(
FleetService $fleetSvc,
2017-07-02 10:06:55 +08:00
RankRepository $rankingRepo,
SubfleetRepository $subfleetRepo
2018-01-01 04:00:50 +08:00
) {
$this->fleetSvc = $fleetSvc;
2017-06-22 02:44:30 +08:00
$this->rankRepository = $rankingRepo;
2017-07-02 10:06:55 +08:00
$this->subfleetRepo = $subfleetRepo;
}
2018-02-21 12:33:09 +08:00
/**
* Get the available subfleets for a rank
2018-08-27 00:40:04 +08:00
*
2018-02-21 12:33:09 +08:00
* @param $rank
2018-08-27 00:40:04 +08:00
*
2018-02-21 12:33:09 +08:00
* @return array
*/
2017-07-02 10:06:55 +08:00
protected function getAvailSubfleets($rank)
{
$retval = [];
$all_subfleets = $this->subfleetRepo->all();
$avail_subfleets = $all_subfleets->except($rank->subfleets->modelKeys());
foreach ($avail_subfleets as $subfleet) {
$retval[$subfleet->id] = $subfleet->name.
' (airline: '.$subfleet->airline->code.')';
}
return $retval;
2017-06-22 01:10:25 +08:00
}
/**
* Display a listing of the Ranking.
2018-08-27 00:40:04 +08:00
*
2017-06-22 01:10:25 +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
2017-06-22 01:10:25 +08:00
*/
public function index(Request $request)
{
2017-06-22 02:44:30 +08:00
$this->rankRepository->pushCriteria(new RequestCriteria($request));
$ranks = $this->rankRepository->all();
2017-06-22 01:10:25 +08:00
2017-07-02 10:06:55 +08:00
return view('admin.ranks.index', [
'ranks' => $ranks,
]);
2017-06-22 01:10:25 +08:00
}
/**
* Show the form for creating a new Ranking.
*
* @return mixed
2017-06-22 01:10:25 +08:00
*/
public function create()
{
2017-06-22 02:44:30 +08:00
return view('admin.ranks.create');
2017-06-22 01:10:25 +08:00
}
/**
* Store a newly created Ranking in storage.
2018-08-27 00:40:04 +08:00
*
2017-06-22 09:18:01 +08:00
* @param CreateRankRequest $request
2018-08-27 00:40:04 +08:00
*
2018-02-21 12:33:09 +08:00
* @throws \Prettus\Validator\Exceptions\ValidatorException
2018-08-27 00:40:04 +08:00
*
* @return mixed
2017-06-22 01:10:25 +08:00
*/
2017-07-02 10:06:55 +08:00
public function store(CreateRankRequest $request)
2017-06-22 01:10:25 +08:00
{
$input = $request->all();
2017-06-22 09:18:01 +08:00
$model = $this->rankRepository->create($input);
2017-06-22 01:10:25 +08:00
Flash::success('Ranking saved successfully.');
2017-07-05 04:43:47 +08:00
Cache::forget(config('cache.keys.RANKS_PILOT_LIST.key'));
2019-09-14 06:20:50 +08:00
return redirect(route('admin.ranks.edit', [$model->id]));
2017-06-22 01:10:25 +08:00
}
/**
* Display the specified Ranking.
2018-08-27 00:40:04 +08:00
*
* @param int $id
*
* @return mixed
2017-06-22 01:10:25 +08:00
*/
public function show($id)
{
2017-06-22 02:44:30 +08:00
$rank = $this->rankRepository->findWithoutFail($id);
2017-06-22 01:10:25 +08:00
2017-06-22 02:44:30 +08:00
if (empty($rank)) {
2017-06-22 01:10:25 +08:00
Flash::error('Ranking not found');
2017-06-22 02:44:30 +08:00
return redirect(route('admin.ranks.index'));
2017-06-22 01:10:25 +08:00
}
2017-07-02 10:06:55 +08:00
return view('admin.ranks.show', [
2018-08-27 00:40:04 +08:00
'rank' => $rank,
2017-07-02 10:06:55 +08:00
]);
2017-06-22 01:10:25 +08:00
}
/**
* Show the form for editing the specified Ranking.
2018-08-27 00:40:04 +08:00
*
* @param int $id
*
* @return mixed
2017-06-22 01:10:25 +08:00
*/
public function edit($id)
{
2017-06-22 02:44:30 +08:00
$rank = $this->rankRepository->findWithoutFail($id);
2017-06-22 01:10:25 +08:00
2017-06-22 02:44:30 +08:00
if (empty($rank)) {
2017-06-22 01:10:25 +08:00
Flash::error('Ranking not found');
2017-06-22 02:44:30 +08:00
return redirect(route('admin.ranks.index'));
2017-06-22 01:10:25 +08:00
}
2017-07-02 10:06:55 +08:00
$avail_subfleets = $this->getAvailSubfleets($rank);
2017-07-02 10:06:55 +08:00
return view('admin.ranks.edit', [
'rank' => $rank,
2017-07-02 10:06:55 +08:00
'avail_subfleets' => $avail_subfleets,
]);
2017-06-22 01:10:25 +08:00
}
/**
* Update the specified Ranking in storage.
2018-08-27 00:40:04 +08:00
*
* @param int $id
2017-06-22 09:18:01 +08:00
* @param UpdateRankRequest $request
2018-08-27 00:40:04 +08:00
*
2018-02-21 12:33:09 +08:00
* @throws \Prettus\Validator\Exceptions\ValidatorException
2018-08-27 00:40:04 +08:00
*
* @return mixed
2017-06-22 01:10:25 +08:00
*/
2017-06-22 09:18:01 +08:00
public function update($id, UpdateRankRequest $request)
2017-06-22 01:10:25 +08:00
{
2017-06-22 02:44:30 +08:00
$rank = $this->rankRepository->findWithoutFail($id);
2017-06-22 01:10:25 +08:00
2017-06-22 02:44:30 +08:00
if (empty($rank)) {
2017-06-22 01:10:25 +08:00
Flash::error('Ranking not found');
2017-06-22 02:44:30 +08:00
return redirect(route('admin.ranks.index'));
2017-06-22 01:10:25 +08:00
}
2017-06-22 02:44:30 +08:00
$rank = $this->rankRepository->update($request->all(), $id);
2017-07-05 04:43:47 +08:00
Cache::forget(config('cache.keys.RANKS_PILOT_LIST.key'));
2017-06-22 01:10:25 +08:00
Flash::success('Ranking updated successfully.');
2017-06-22 02:44:30 +08:00
return redirect(route('admin.ranks.index'));
2017-06-22 01:10:25 +08:00
}
/**
* Remove the specified Ranking from storage.
2018-08-27 00:40:04 +08:00
*
* @param int $id
*
* @return mixed
2017-06-22 01:10:25 +08:00
*/
public function destroy($id)
{
2017-06-22 02:44:30 +08:00
$rank = $this->rankRepository->findWithoutFail($id);
2017-06-22 01:10:25 +08:00
2017-06-22 02:44:30 +08:00
if (empty($rank)) {
2017-06-22 01:10:25 +08:00
Flash::error('Ranking not found');
2017-06-22 02:44:30 +08:00
return redirect(route('admin.ranks.index'));
2017-06-22 01:10:25 +08:00
}
2017-06-22 02:44:30 +08:00
$this->rankRepository->delete($id);
2017-06-22 01:10:25 +08:00
Flash::success('Ranking deleted successfully.');
2017-06-22 02:44:30 +08:00
return redirect(route('admin.ranks.index'));
2017-06-22 01:10:25 +08:00
}
2017-07-02 10:06:55 +08:00
2018-02-21 12:33:09 +08:00
/**
* @param $rank
2018-08-27 00:40:04 +08:00
*
2018-02-21 12:33:09 +08:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
2017-07-02 10:06:55 +08:00
protected function return_subfleet_view($rank)
{
$avail_subfleets = $this->getAvailSubfleets($rank);
2017-07-02 10:06:55 +08:00
return view('admin.ranks.subfleets', [
'rank' => $rank,
2017-07-02 10:06:55 +08:00
'avail_subfleets' => $avail_subfleets,
]);
}
2018-02-21 12:33:09 +08:00
/**
* Subfleet operations on a rank
2018-08-27 00:40:04 +08:00
*
* @param $id
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
*/
public function subfleets($id, Request $request)
2017-07-02 10:06:55 +08:00
{
$rank = $this->rankRepository->findWithoutFail($id);
if (empty($rank)) {
Flash::error('Rank not found!');
return redirect(route('admin.ranks.index'));
}
// add aircraft to flight
if ($request->isMethod('post')) {
$subfleet = $this->subfleetRepo->find($request->input('subfleet_id'));
$this->fleetSvc->addSubfleetToRank($subfleet, $rank);
} elseif ($request->isMethod('put')) {
$override = [];
$override[$request->name] = $request->value;
$subfleet = $this->subfleetRepo->find($request->input('subfleet_id'));
$this->fleetSvc->addSubfleetToRank($subfleet, $rank);
} // remove aircraft from flight
2017-07-02 10:06:55 +08:00
elseif ($request->isMethod('delete')) {
$subfleet = $this->subfleetRepo->find($request->input('subfleet_id'));
$this->fleetSvc->removeSubfleetFromRank($subfleet, $rank);
2017-07-02 10:06:55 +08:00
}
return $this->return_subfleet_view($rank);
}
2017-06-22 01:10:25 +08:00
}