From 5ffd152a43fbd771184545ef03a84452f43593ef Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Tue, 11 Jul 2017 17:44:12 -0500 Subject: [PATCH] #48 add custom fields/values to flights --- .../Controllers/Admin/FlightController.php | 50 +++++++++++++- ...2017_06_17_214650_create_flights_table.php | 1 + database/seeds/dev.yml | 8 +-- resources/views/admin/flights/edit.blade.php | 52 +++++++++++---- .../views/admin/flights/fields.blade.php | 66 +++++++++++-------- .../admin/flights/flight_fields.blade.php | 56 ++++++++++++++++ .../views/admin/subfleets/fields.blade.php | 2 +- routes/admin.php | 1 + 8 files changed, 189 insertions(+), 47 deletions(-) create mode 100644 resources/views/admin/flights/flight_fields.blade.php diff --git a/app/Http/Controllers/Admin/FlightController.php b/app/Http/Controllers/Admin/FlightController.php index c4d4fc38..114c9ef9 100644 --- a/app/Http/Controllers/Admin/FlightController.php +++ b/app/Http/Controllers/Admin/FlightController.php @@ -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); diff --git a/database/migrations/2017_06_17_214650_create_flights_table.php b/database/migrations/2017_06_17_214650_create_flights_table.php index 49f00e1d..0a0e4291 100644 --- a/database/migrations/2017_06_17_214650_create_flights_table.php +++ b/database/migrations/2017_06_17_214650_create_flights_table.php @@ -44,6 +44,7 @@ class CreateFlightsTable extends Migration $table->uuid('flight_id'); $table->string('name', 50); $table->text('value'); + $table->timestamps(); }); } diff --git a/database/seeds/dev.yml b/database/seeds/dev.yml index d2d116a4..b8ebb262 100644 --- a/database/seeds/dev.yml +++ b/database/seeds/dev.yml @@ -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 diff --git a/resources/views/admin/flights/edit.blade.php b/resources/views/admin/flights/edit.blade.php index 7512ce6a..80f4d5d5 100644 --- a/resources/views/admin/flights/edit.blade.php +++ b/resources/views/admin/flights/edit.blade.php @@ -2,7 +2,7 @@ @section('content')
-

Edit {!! $flight->airline->name !!}{!! $flight->number !!}

+

Edit {!! $flight->airline->code !!}{!! $flight->flight_number !!}

@include('adminlte-templates::common.errors') @@ -17,6 +17,20 @@
+ +
+
+
+
+

custom fields

+
+ @include('admin.flights.flight_fields') +
+
+
+
+
+
@@ -33,17 +47,33 @@ @endsection @section('scripts') @endsection diff --git a/resources/views/admin/flights/fields.blade.php b/resources/views/admin/flights/fields.blade.php index 61e8dc1b..548d3c42 100644 --- a/resources/views/admin/flights/fields.blade.php +++ b/resources/views/admin/flights/fields.blade.php @@ -1,15 +1,21 @@ -
+
{!! Form::label('airline_id', 'Airline:') !!} - {!! Form::text('airline_id', null, ['class' => 'form-control']) !!} + {!! Form::select('airline_id', $airlines, null , ['class' => 'form-control select2']) !!}
-
+
{!! Form::label('flight_number', 'Flight Number:') !!} {!! Form::text('flight_number', null, ['class' => 'form-control']) !!}
+ +
+ {!! Form::label('active', 'Active:') !!} + {!! Form::checkbox('active', $flight->active, ['class' => 'form-control']) !!} +
+
{!! Form::label('route_code', 'Route Code:') !!} @@ -22,22 +28,42 @@ {!! Form::text('route_leg', null, ['class' => 'form-control']) !!}
+ + -
- {!! Form::label('dpt_airport_id', 'Dpt Airport Id:') !!} - {!! Form::text('dpt_airport_id', null, ['class' => 'form-control']) !!} +
+ {!! Form::label('dpt_airport_id', 'Departure Airport:') !!} + {!! Form::select('dpt_airport_id', $airports, null , ['class' => 'form-control select2']) !!}
-
- {!! Form::label('arr_airport_id', 'Arr Airport Id:') !!} - {!! Form::text('arr_airport_id', null, ['class' => 'form-control']) !!} +
+ {!! Form::label('arr_airport_id', 'Arrival Airport:') !!} + {!! Form::select('arr_airport_id', $airports, null , ['class' => 'form-control select2']) !!}
+
+ {!! Form::label('alt_airport_id', 'Alt Airport:') !!} + {!! Form::select('alt_airport_id', $airports, null , ['class' => 'form-control select2']) !!} +
+ + + +
- {!! 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']) !!} +
+ + +
+ {!! Form::label('arr_time', 'Arrival Time:') !!} + {!! Form::text('arr_time', null, ['class' => 'form-control']) !!}
@@ -46,30 +72,12 @@ {!! Form::text('route', null, ['class' => 'form-control']) !!}
- -
- {!! Form::label('dpt_time', 'Dpt Time:') !!} - {!! Form::text('dpt_time', null, ['class' => 'form-control']) !!} -
- - -
- {!! Form::label('arr_time', 'Arr Time:') !!} - {!! Form::text('arr_time', null, ['class' => 'form-control']) !!} -
-
{!! Form::label('notes', 'Notes:') !!} {!! Form::text('notes', null, ['class' => 'form-control']) !!}
- -
- {!! Form::label('active', 'Active:') !!} - {!! Form::text('active', null, ['class' => 'form-control']) !!} -
-
{!! Form::submit('Save', ['class' => 'btn btn-primary']) !!} diff --git a/resources/views/admin/flights/flight_fields.blade.php b/resources/views/admin/flights/flight_fields.blade.php new file mode 100644 index 00000000..e47ba8d8 --- /dev/null +++ b/resources/views/admin/flights/flight_fields.blade.php @@ -0,0 +1,56 @@ +
+ + + + + + + + @foreach($flight->fields as $field) + + + + + + @endforeach + +
NameValueActions
{!! $field->name !!} + {!! $field->value !!} + + {!! Form::open(['url' => '/admin/flights/'.$flight->id.'/fields', + 'method' => 'delete', + 'class' => 'pjax_form flight_fields' + ]) !!} + {!! Form::hidden('field_id', $field->id) !!} +
+ {!! Form::button('', + ['type' => 'submit', + 'class' => 'btn btn-danger btn-xs']) + !!} +
+ {!! Form::close() !!} +
+
+
+ {!! Form::open([ + 'url' => '/admin/flights/'.$flight->id.'/fields', + 'method' => 'post', + 'class' => 'pjax_form form-inline' + ]) + !!} + +
+ + {!! Form::label('name', 'Name:') !!} + {!! Form::text('name', null, ['class' => 'form-control']) !!} +    + {!! Form::label('value', 'Value:') !!} + {!! Form::text('value', null, ['class' => 'form-control']) !!} +    + {!! Form::button(' add', + ['type' => 'submit', + 'class' => 'btn btn-success btn-s']) !!} +
+ {!! Form::close() !!} +
+
diff --git a/resources/views/admin/subfleets/fields.blade.php b/resources/views/admin/subfleets/fields.blade.php index 120dab9c..486d246b 100644 --- a/resources/views/admin/subfleets/fields.blade.php +++ b/resources/views/admin/subfleets/fields.blade.php @@ -1,6 +1,6 @@
- {!! Form::label('airline_id', 'Airline Id:') !!} + {!! Form::label('airline_id', 'Airline:') !!} {!! Form::select('airline_id', $airlines, null , ['class' => 'form-control select2']) !!}
diff --git a/routes/admin.php b/routes/admin.php index f396897f..77e6aa11 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -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