Add ranks from the subfleet edit page closes #160

This commit is contained in:
Nabeel Shahzad 2018-02-20 21:53:50 -06:00
parent 1c143f3b23
commit 14d04604cb
8 changed files with 165 additions and 18 deletions

View File

@ -41,9 +41,9 @@ class RankController extends BaseController
/** /**
* Display a listing of the Ranking. * Display a listing of the Ranking.
*
* @param Request $request * @param Request $request
* @return Response * @return Response
* @throws \Prettus\Repository\Exceptions\RepositoryException
*/ */
public function index(Request $request) public function index(Request $request)
{ {

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers\Admin; namespace App\Http\Controllers\Admin;
use App\Repositories\RankRepository;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Flash; use Flash;
use Prettus\Repository\Criteria\RequestCriteria; use Prettus\Repository\Criteria\RequestCriteria;
@ -24,7 +25,7 @@ use App\Services\FareService;
class SubfleetController extends BaseController class SubfleetController extends BaseController
{ {
/** @var SubfleetRepository */ /** @var SubfleetRepository */
private $aircraftRepo, $subfleetRepo, $fareRepo, $fareSvc; private $aircraftRepo, $rankRepo, $subfleetRepo, $fareRepo, $fareSvc;
/** /**
* SubfleetController constructor. * SubfleetController constructor.
@ -36,16 +37,35 @@ class SubfleetController extends BaseController
*/ */
public function __construct( public function __construct(
AircraftRepository $aircraftRepo, AircraftRepository $aircraftRepo,
RankRepository $rankRepo,
SubfleetRepository $subfleetRepo, SubfleetRepository $subfleetRepo,
FareRepository $fareRepo, FareRepository $fareRepo,
FareService $fareSvc FareService $fareSvc
) { ) {
$this->aircraftRepo = $aircraftRepo; $this->aircraftRepo = $aircraftRepo;
$this->rankRepo = $rankRepo;
$this->subfleetRepo = $subfleetRepo; $this->subfleetRepo = $subfleetRepo;
$this->fareRepo = $fareRepo; $this->fareRepo = $fareRepo;
$this->fareSvc = $fareSvc; $this->fareSvc = $fareSvc;
} }
/**
* Get the ranks that are available to the subfleet
* @param $subfleet
* @return array
*/
protected function getAvailRanks($subfleet)
{
$retval = [];
$all_ranks = $this->rankRepo->all();
$avail_ranks = $all_ranks->except($subfleet->ranks->modelKeys());
foreach ($avail_ranks as $rank) {
$retval[$rank->id] = $rank->name;
}
return $retval;
}
/** /**
* Get all the fares that haven't been assigned to a given subfleet * Get all the fares that haven't been assigned to a given subfleet
*/ */
@ -144,10 +164,13 @@ class SubfleetController extends BaseController
} }
$avail_fares = $this->getAvailFares($subfleet); $avail_fares = $this->getAvailFares($subfleet);
$avail_ranks = $this->getAvailRanks($subfleet);
return view('admin.subfleets.edit', [ return view('admin.subfleets.edit', [
'airlines' => Airline::all()->pluck('name', 'id'), 'airlines' => Airline::all()->pluck('name', 'id'),
'fuel_types' => FuelType::labels(), 'fuel_types' => FuelType::labels(),
'avail_fares' => $avail_fares, 'avail_fares' => $avail_fares,
'avail_ranks' => $avail_ranks,
'subfleet' => $subfleet, 'subfleet' => $subfleet,
]); ]);
} }
@ -202,6 +225,22 @@ class SubfleetController extends BaseController
return redirect(route('admin.subfleets.index')); return redirect(route('admin.subfleets.index'));
} }
/**
* @param Subfleet $subfleet
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
protected function return_ranks_view(Subfleet $subfleet)
{
$subfleet->refresh();
$avail_ranks = $this->getAvailRanks($subfleet);
return view('admin.subfleets.ranks', [
'subfleet' => $subfleet,
'avail_ranks' => $avail_ranks,
]);
}
/** /**
* @param Subfleet $subfleet * @param Subfleet $subfleet
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
@ -209,6 +248,7 @@ class SubfleetController extends BaseController
protected function return_fares_view(Subfleet $subfleet) protected function return_fares_view(Subfleet $subfleet)
{ {
$subfleet->refresh(); $subfleet->refresh();
$avail_fares = $this->getAvailFares($subfleet); $avail_fares = $this->getAvailFares($subfleet);
return view('admin.subfleets.fares', [ return view('admin.subfleets.fares', [
@ -218,13 +258,46 @@ class SubfleetController extends BaseController
} }
/** /**
* Operations for associating ranks to the subfleet
* @param $id
* @param Request $request * @param Request $request
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/ */
public function fares(Request $request) public function ranks($id, Request $request)
{ {
$id = $request->id; $subfleet = $this->subfleetRepo->findWithoutFail($id);
if (empty($subfleet)) {
return $this->return_ranks_view($subfleet);
}
if ($request->isMethod('get')) {
return $this->return_ranks_view($subfleet);
}
/**
* update specific rank data
*/
if ($request->isMethod('post')) {
$subfleet->ranks()->syncWithoutDetaching([$request->input('rank_id')]);
}
// dissassociate fare from teh aircraft
elseif ($request->isMethod('delete')) {
$subfleet->ranks()->detach($request->input('rank_id'));
}
$subfleet->save();
return $this->return_ranks_view($subfleet);
}
/**
* Operations on fares to the subfleet
* @param $id
* @param Request $request
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function fares($id, Request $request)
{
$subfleet = $this->subfleetRepo->findWithoutFail($id); $subfleet = $this->subfleetRepo->findWithoutFail($id);
if (empty($subfleet)) { if (empty($subfleet)) {
return $this->return_fares_view($subfleet); return $this->return_fares_view($subfleet);

View File

@ -17,6 +17,7 @@ Route::group([
# subfleet # subfleet
Route::resource('subfleets', 'SubfleetController'); Route::resource('subfleets', 'SubfleetController');
Route::match(['get', 'post', 'put', 'delete'], 'subfleets/{id}/fares', 'SubfleetController@fares'); Route::match(['get', 'post', 'put', 'delete'], 'subfleets/{id}/fares', 'SubfleetController@fares');
Route::match(['get', 'post', 'delete'], 'subfleets/{id}/ranks', 'SubfleetController@ranks');
# aircraft and fare associations # aircraft and fare associations
Route::resource('aircraft', 'AircraftController'); Route::resource('aircraft', 'AircraftController');

View File

@ -10,6 +10,12 @@
</div> </div>
</div> </div>
<div class="card border-blue-bottom">
<div class="content">
@include('admin.subfleets.ranks')
</div>
</div>
<div class="card border-blue-bottom"> <div class="card border-blue-bottom">
<div class="content"> <div class="content">
@include('admin.subfleets.fares') @include('admin.subfleets.fares')

View File

@ -10,15 +10,14 @@
</div> </div>
<br /> <br />
<table id="aircraft_fares" <table id="aircraft_fares"
class="table table-bordered table-hover dataTable" class="table table-hover dataTable">
role="grid" aria-describedby="aircraft_fares_info">
<thead> <thead>
<tr> <tr>
<th>name</th> <th>name</th>
<th>code</th> <th style="text-align: center;">code</th>
<th>capacity (default)</th> <th style="text-align: center;">capacity (default)</th>
<th>price (default)</th> <th style="text-align: center;">price (default)</th>
<th>cost (default)</th> <th style="text-align: center;">cost (default)</th>
<th></th> <th></th>
</tr> </tr>
</thead> </thead>
@ -27,12 +26,15 @@
<tr> <tr>
<td class="sorting_1">{!! $atf->name !!}</td> <td class="sorting_1">{!! $atf->name !!}</td>
<td style="text-align: center;">{!! $atf->code !!}</td> <td style="text-align: center;">{!! $atf->code !!}</td>
<td><a href="#" data-pk="{!! $atf->id !!}" data-name="capacity">{!! $atf->pivot->capacity !!}</a> <td style="text-align: center;">
<a href="#" data-pk="{!! $atf->id !!}" data-name="capacity">{!! $atf->pivot->capacity !!}</a>
<span class="small background-color-grey-light">({!! $atf->capacity !!})</span> <span class="small background-color-grey-light">({!! $atf->capacity !!})</span>
</td> </td>
<td><a href="#" data-pk="{!! $atf->id !!}" data-name="price">{!! $atf->pivot->price !!}</a> <td style="text-align: center;">
<a href="#" data-pk="{!! $atf->id !!}" data-name="price">{!! $atf->pivot->price !!}</a>
<span class="small background-color-grey-light">({!! $atf->price !!})</span></td> <span class="small background-color-grey-light">({!! $atf->price !!})</span></td>
<td><a href="#" data-pk="{!! $atf->id !!}" data-name="cost">{!! $atf->pivot->cost !!}</a> <td style="text-align: center;">
<a href="#" data-pk="{!! $atf->id !!}" data-name="cost">{!! $atf->pivot->cost !!}</a>
<span class="small background-color-grey-light">({!! $atf->cost!!})</span></td> <span class="small background-color-grey-light">({!! $atf->cost!!})</span></td>
<td style="text-align: right; width:3%;"> <td style="text-align: right; width:3%;">
{!! Form::open(['url' => '/admin/subfleets/'.$subfleet->id.'/fares', {!! Form::open(['url' => '/admin/subfleets/'.$subfleet->id.'/fares',

View File

@ -1,9 +1,4 @@
<div class="row"> <div class="row">
<div class="form-group col-sm-6">
{!! Form::label('airline_id', 'Airline:') !!}
{!! Form::select('airline_id', $airlines, null , ['class' => 'form-control select2']) !!}
<p class="text-danger">{{ $errors->first('airline_id') }}</p>
</div>
<div class="form-group col-sm-6"> <div class="form-group col-sm-6">
{!! Form::label('name', 'Name:') !!} {!! Form::label('name', 'Name:') !!}
@ -17,6 +12,12 @@
<p class="text-danger">{{ $errors->first('type') }}</p> <p class="text-danger">{{ $errors->first('type') }}</p>
</div> </div>
<div class="form-group col-sm-6">
{!! Form::label('airline_id', 'Airline:') !!}
{!! Form::select('airline_id', $airlines, null , ['class' => 'form-control select2']) !!}
<p class="text-danger">{{ $errors->first('airline_id') }}</p>
</div>
<div class="form-group col-sm-6"> <div class="form-group col-sm-6">
{!! Form::label('fuel_type', 'Fuel Type:') !!} {!! Form::label('fuel_type', 'Fuel Type:') !!}
{!! Form::select('fuel_type', $fuel_types, null , ['class' => 'form-control select2']) !!} {!! Form::select('fuel_type', $fuel_types, null , ['class' => 'form-control select2']) !!}

View File

@ -0,0 +1,58 @@
<div id="subfleet_ranks_wrapper" class="dataTables_wrapper form-inline dt-bootstrap">
<div class="header">
<h3>ranks</h3>
<p class="category">
<i class="icon fa fa-info">&nbsp;&nbsp;</i>
These ranks are allowed to fly aircraft in this subfleet
</p>
</div>
<br />
<table id="subfleet_ranks" class="table table-hover dataTable">
<thead>
<tr>
<th>name</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach($subfleet->ranks as $rank)
<tr>
<td class="sorting_1">{!! $rank->name !!}</td>
<td style="text-align: right; width:3%;">
{!! Form::open(['url' => '/admin/subfleets/'.$subfleet->id.'/ranks',
'method' => 'delete',
'class' => 'modify_rank'])
!!}
{!! Form::hidden('rank_id', $rank->id) !!}
{!! Form::button('<i class="fa fa-times"></i>',
['type' => 'submit',
'class' => 'btn btn-sm btn-danger btn-icon']) !!}
{!! Form::close() !!}
</td>
</tr>
@endforeach
</tbody>
</table>
<hr />
<div class="row">
<div class="col-xs-12">
<div class="text-right">
{!! Form::open(['url' => '/admin/subfleets/'.$subfleet->id.'/ranks',
'method' => 'post',
'class' => 'modify_rank form-inline'])
!!}
{!! Form::select('rank_id', $avail_ranks, null, [
'placeholder' => 'Select Rank',
'class' => 'ac-fare-dropdown form-control input-lg select2',
])
!!}
{!! Form::button('<i class="glyphicon glyphicon-plus"></i> add',
['type' => 'submit',
'class' => 'btn btn-success btn-s']) !!}
{!! Form::close() !!}
</div>
</div>
</div>
</div>
{{--</div></div>--}}

View File

@ -29,6 +29,12 @@ $(document).ready(function() {
setEditable(); setEditable();
}); });
$(document).on('submit', 'form.modify_rank', function (event) {
event.preventDefault();
console.log(event);
$.pjax.submit(event, '#subfleet_ranks_wrapper', {push: false});
});
$(document).on('pjax:complete', function() { $(document).on('pjax:complete', function() {
$(".select2").select2(); $(".select2").select2();
setEditable(); setEditable();