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

218 lines
5.8 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
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;
2018-02-21 12:33:09 +08:00
use Cache;
use Flash;
use Illuminate\Http\Request;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;
2017-06-22 01:10:25 +08:00
2017-06-22 02:44:30 +08:00
class RankController extends BaseController
2017-06-22 01:10:25 +08:00
{
2017-06-22 02:44:30 +08:00
/** @var RankRepository */
2017-07-02 10:06:55 +08:00
private $rankRepository, $subfleetRepo;
2017-06-22 01:10:25 +08:00
2018-02-21 12:33:09 +08:00
/**
* RankController constructor.
* @param RankRepository $rankingRepo
* @param SubfleetRepository $subfleetRepo
*/
2017-07-02 10:06:55 +08:00
public function __construct(
RankRepository $rankingRepo,
SubfleetRepository $subfleetRepo
2018-01-01 04:00:50 +08:00
) {
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
* @param $rank
* @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.
* @param Request $request
* @return Response
* @throws \Prettus\Repository\Exceptions\RepositoryException
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 Response
*/
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.
2017-06-22 09:18:01 +08:00
* @param CreateRankRequest $request
2017-06-22 01:10:25 +08:00
* @return Response
2018-02-21 12:33:09 +08:00
* @throws \Prettus\Validator\Exceptions\ValidatorException
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'));
2017-07-03 04:03:25 +08:00
return redirect(route('admin.ranks.edit', ['id' => $model->id]));
2017-06-22 01:10:25 +08:00
}
/**
* Display the specified Ranking.
* @param int $id
* @return Response
*/
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', [
'rank' => $rank
]);
2017-06-22 01:10:25 +08:00
}
/**
* Show the form for editing the specified Ranking.
* @param int $id
* @return Response
*/
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);
return view('admin.ranks.edit', [
'rank' => $rank,
'avail_subfleets' => $avail_subfleets,
]);
2017-06-22 01:10:25 +08:00
}
/**
* Update the specified Ranking in storage.
2018-02-21 12:33:09 +08:00
* @param int $id
2017-06-22 09:18:01 +08:00
* @param UpdateRankRequest $request
2017-06-22 01:10:25 +08:00
* @return Response
2018-02-21 12:33:09 +08:00
* @throws \Prettus\Validator\Exceptions\ValidatorException
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.
* @param int $id
* @return Response
*/
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
* @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);
return view('admin.ranks.subfleets', [
'rank' => $rank,
'avail_subfleets' => $avail_subfleets,
]);
}
2018-02-21 12:33:09 +08:00
/**
* Subfleet operations on a rank
* @param $id
* @param Request $request
* @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')) {
$rank->subfleets()->syncWithoutDetaching([$request->subfleet_id]);
}
// remove aircraft from flight
elseif ($request->isMethod('delete')) {
$rank->subfleets()->detach($request->subfleet_id);
}
return $this->return_subfleet_view($rank);
}
2017-06-22 01:10:25 +08:00
}