#48 add custom fields/values to flights

This commit is contained in:
Nabeel Shahzad 2017-07-11 17:44:12 -05:00
parent 35133fe0e6
commit 5ffd152a43
8 changed files with 189 additions and 47 deletions

View File

@ -2,6 +2,9 @@
namespace App\Http\Controllers\Admin;
use App\Models\Airline;
use App\Models\FlightFields;
use App\Models\Airport;
use App\Http\Requests\CreateFlightRequest;
use App\Http\Requests\UpdateFlightRequest;
use App\Repositories\FlightRepository;
@ -101,7 +104,6 @@ class FlightController extends BaseController
$avail_subfleets = $this->getAvailSubfleets($flight);
return view('admin.flights.show', [
'flight' => $flight,
'fields' => $flight->fields(),
'avail_subfleets' => $avail_subfleets,
]);
}
@ -125,7 +127,8 @@ class FlightController extends BaseController
$avail_subfleets = $this->getAvailSubfleets($flight);
return view('admin.flights.edit', [
'flight' => $flight,
'fields' => $flight->fields(),
'airlines' => Airline::all()->pluck('name', 'id'),
'airports' => Airport::all()->pluck('icao', 'id'),
'avail_subfleets' => $avail_subfleets,
]);
}
@ -175,6 +178,49 @@ class FlightController extends BaseController
return redirect(route('admin.flights.index'));
}
protected function return_fields_view($flight)
{
$flight->refresh();
return view('admin.flights.flight_fields', [
'flight' => $flight,
]);
}
public function fields(Request $request)
{
print_r($request->toArray());
$id = $request->id;
$flight = $this->flightRepository->findWithoutFail($id);
if (empty($flight)) {
Flash::error('Flight not found');
return redirect(route('admin.flights.index'));
}
// add custom field to flight
if ($request->isMethod('post')) {
$field = new FlightFields;
$field->flight_id = $id;
$field->name = $request->name;
$field->value = $request->value;
$field->save();
}
elseif ($request->isMethod('put')) {
$field = FlightFields::where('id', $request->field_id)->first();
$field->value = $request->value;
$field->save();
// update the field value
}
// remove custom field from flight
elseif ($request->isMethod('delete')) {
FlightFields::destroy($request->field_id);
}
return $this->return_fields_view($flight);
}
protected function return_subfleet_view($flight)
{
$avail_subfleets = $this->getAvailSubfleets($flight);

View File

@ -44,6 +44,7 @@ class CreateFlightsTable extends Migration
$table->uuid('flight_id');
$table->string('name', 50);
$table->text('value');
$table->timestamps();
});
}

View File

@ -143,10 +143,10 @@ subfleet_fare:
subfleet_flight:
- subfleet_id: 1
flight_id: 1
flight_id: flightid_1
flights:
- id: 1
- id: flightid_1
airline_id: 1
flight_number: 100
dpt_airport_id: 1
@ -155,13 +155,13 @@ flights:
flight_fields:
- id: 1
flight_id: 1
flight_id: flightid_1
name: terminal
value: B
pireps:
- user_id: 1
flight_id: 1
flight_id: flightid_1
aircraft_id: 1
dpt_airport_id: 1
arr_airport_id: 2

View File

@ -2,7 +2,7 @@
@section('content')
<section class="content-header">
<h1>Edit {!! $flight->airline->name !!}{!! $flight->number !!}</h1>
<h1>Edit {!! $flight->airline->code !!}{!! $flight->flight_number !!}</h1>
</section>
<div class="content">
@include('adminlte-templates::common.errors')
@ -17,6 +17,20 @@
</div>
</div>
</div>
<div class="box box-primary">
<div class="box-body">
<div class="row">
<div class="col-xs-12">
<h3>custom fields</h3>
<div class="box-body">
@include('admin.flights.flight_fields')
</div>
</div>
</div>
</div>
</div>
<div class="box box-primary">
<div class="box-body">
<div class="row">
@ -34,11 +48,27 @@
@section('scripts')
<script>
$(document).ready(function () {
$('#flight_fields_wrapper a.inline').editable({
type: 'text',
mode: 'inline',
emptytext: '0',
url: '/admin/flights/{!! $flight->id !!}/fields',
ajaxOptions: {'type': 'put'},
params: function(params) {
return {
field_id: params.pk,
name: params.name,
value: params.value
}
}
});
$(".select2_dropdown").select2();
$(document).on('submit', 'form.pjax_form', function (event) {
event.preventDefault();
$.pjax.submit(event, '#subfleet_flight_wrapper', {push: false});
$.pjax.submit(event, '#flight_fields_wrapper', {push: false});
});
$(document).on('pjax:complete', function() {

View File

@ -1,15 +1,21 @@
<!-- Airline Id Field -->
<div class="form-group col-sm-6">
<div class="form-group col-sm-5">
{!! Form::label('airline_id', 'Airline:') !!}
{!! Form::text('airline_id', null, ['class' => 'form-control']) !!}
{!! Form::select('airline_id', $airlines, null , ['class' => 'form-control select2']) !!}
</div>
<!-- Flight Number Field -->
<div class="form-group col-sm-6">
<div class="form-group col-sm-5">
{!! Form::label('flight_number', 'Flight Number:') !!}
{!! Form::text('flight_number', null, ['class' => 'form-control']) !!}
</div>
<!-- Active Field -->
<div class="form-group col-sm-2">
{!! Form::label('active', 'Active:') !!}
{!! Form::checkbox('active', $flight->active, ['class' => 'form-control']) !!}
</div>
<!-- Route Code Field -->
<div class="form-group col-sm-6">
{!! Form::label('route_code', 'Route Code:') !!}
@ -22,22 +28,42 @@
{!! Form::text('route_leg', null, ['class' => 'form-control']) !!}
</div>
<!--
SAME ROW
-->
<!-- Dpt Airport Id Field -->
<div class="form-group col-sm-6">
{!! Form::label('dpt_airport_id', 'Dpt Airport Id:') !!}
{!! Form::text('dpt_airport_id', null, ['class' => 'form-control']) !!}
<div class="form-group col-sm-4">
{!! Form::label('dpt_airport_id', 'Departure Airport:') !!}
{!! Form::select('dpt_airport_id', $airports, null , ['class' => 'form-control select2']) !!}
</div>
<!-- Arr Airport Id Field -->
<div class="form-group col-sm-6">
{!! Form::label('arr_airport_id', 'Arr Airport Id:') !!}
{!! Form::text('arr_airport_id', null, ['class' => 'form-control']) !!}
<div class="form-group col-sm-4">
{!! Form::label('arr_airport_id', 'Arrival Airport:') !!}
{!! Form::select('arr_airport_id', $airports, null , ['class' => 'form-control select2']) !!}
</div>
<!-- Alt Airport Id Field -->
<div class="form-group col-sm-4">
{!! Form::label('alt_airport_id', 'Alt Airport:') !!}
{!! Form::select('alt_airport_id', $airports, null , ['class' => 'form-control select2']) !!}
</div>
<!--
END SAME ROW
-->
<!-- Dpt Time Field -->
<div class="form-group col-sm-6">
{!! Form::label('alt_airport_id', 'Alt Airport Id:') !!}
{!! Form::text('alt_airport_id', null, ['class' => 'form-control']) !!}
{!! Form::label('dpt_time', 'Departure Time:') !!}
{!! Form::text('dpt_time', null, ['class' => 'form-control']) !!}
</div>
<!-- Arr Time Field -->
<div class="form-group col-sm-6">
{!! Form::label('arr_time', 'Arrival Time:') !!}
{!! Form::text('arr_time', null, ['class' => 'form-control']) !!}
</div>
<!-- Route Field -->
@ -46,30 +72,12 @@
{!! Form::text('route', null, ['class' => 'form-control']) !!}
</div>
<!-- Dpt Time Field -->
<div class="form-group col-sm-6">
{!! Form::label('dpt_time', 'Dpt Time:') !!}
{!! Form::text('dpt_time', null, ['class' => 'form-control']) !!}
</div>
<!-- Arr Time Field -->
<div class="form-group col-sm-6">
{!! Form::label('arr_time', 'Arr Time:') !!}
{!! Form::text('arr_time', null, ['class' => 'form-control']) !!}
</div>
<!-- Notes Field -->
<div class="form-group col-sm-6">
{!! Form::label('notes', 'Notes:') !!}
{!! Form::text('notes', null, ['class' => 'form-control']) !!}
</div>
<!-- Active Field -->
<div class="form-group col-sm-6">
{!! Form::label('active', 'Active:') !!}
{!! Form::text('active', null, ['class' => 'form-control']) !!}
</div>
<!-- Submit Field -->
<div class="form-group col-sm-12">
{!! Form::submit('Save', ['class' => 'btn btn-primary']) !!}

View File

@ -0,0 +1,56 @@
<div id="flight_fields_wrapper">
<table class="table table-responsive" id="flight-fields-table">
<thead>
<th>Name</th>
<th>Value</th>
<th style="text-align: center;">Actions</th>
</thead>
<tbody>
@foreach($flight->fields as $field)
<tr>
<td>{!! $field->name !!}</td>
<td>
<a class="inline" href="#" data-pk="{!! $field->id !!}" data-name="{!! $field->name !!}">{!! $field->value !!}</a>
</td>
<td style="width: 10%; text-align: center;" class="form-inline">
{!! Form::open(['url' => '/admin/flights/'.$flight->id.'/fields',
'method' => 'delete',
'class' => 'pjax_form flight_fields'
]) !!}
{!! Form::hidden('field_id', $field->id) !!}
<div class='btn-group'>
{!! Form::button('<i class="glyphicon glyphicon-trash"></i>',
['type' => 'submit',
'class' => 'btn btn-danger btn-xs'])
!!}
</div>
{!! Form::close() !!}
</td>
</tr>
@endforeach
</tbody>
</table>
<hr/>
<div class="row pull-right">
{!! Form::open([
'url' => '/admin/flights/'.$flight->id.'/fields',
'method' => 'post',
'class' => 'pjax_form form-inline'
])
!!}
<div class="form-group col-xs-12 form-inline">
{!! Form::label('name', 'Name:') !!}
{!! Form::text('name', null, ['class' => 'form-control']) !!}
&nbsp;&nbsp;
{!! Form::label('value', 'Value:') !!}
{!! Form::text('value', null, ['class' => 'form-control']) !!}
&nbsp;&nbsp;
{!! Form::button('<i class="glyphicon glyphicon-plus"></i> add',
['type' => 'submit',
'class' => 'btn btn-success btn-s']) !!}
</div>
{!! Form::close() !!}
</div>
</div>

View File

@ -1,6 +1,6 @@
<!-- Airline Id Field -->
<div class="form-group col-sm-6">
{!! Form::label('airline_id', 'Airline Id:') !!}
{!! Form::label('airline_id', 'Airline:') !!}
{!! Form::select('airline_id', $airlines, null , ['class' => 'form-control select2']) !!}
</div>

View File

@ -24,6 +24,7 @@ Route::group([
# flights and aircraft associations
Route::resource('flights', 'FlightController');
Route::match(['get', 'post', 'put', 'delete'], 'flights/{id}/fields', 'FlightController@fields');
Route::match(['get', 'post', 'put', 'delete'], 'flights/{id}/subfleets', 'FlightController@subfleets');
# rankings