From eb64f268d3d6b1711a335df0e8b89ee6a923414d Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Fri, 23 Mar 2018 15:02:26 -0500 Subject: [PATCH] Fix flight field bugs --- .../Controllers/Admin/FlightController.php | 24 ++++++++++++------- app/Http/Kernel.php | 2 +- resources/views/admin/app.blade.php | 2 +- .../admin/flights/flight_fields.blade.php | 15 +++++++++++- .../views/admin/flights/scripts.blade.php | 16 ++++++++----- 5 files changed, 41 insertions(+), 18 deletions(-) diff --git a/app/Http/Controllers/Admin/FlightController.php b/app/Http/Controllers/Admin/FlightController.php index b50ea203..ab1a8fbc 100644 --- a/app/Http/Controllers/Admin/FlightController.php +++ b/app/Http/Controllers/Admin/FlightController.php @@ -375,7 +375,6 @@ class FlightController extends Controller protected function return_fields_view($flight) { $flight->refresh(); - return view('admin.flights.flight_fields', [ 'flight' => $flight, 'flight_fields' => $this->flightFieldRepo->all(), @@ -383,13 +382,12 @@ class FlightController extends Controller } /** + * @param $flight_id * @param Request $request * @return mixed */ - public function field_values(Request $request) + public function field_values($flight_id, Request $request) { - $flight_id = $request->id; - $flight = $this->flightRepo->findWithoutFail($flight_id); if (empty($flight)) { Flash::error('Flight not found'); @@ -398,18 +396,21 @@ class FlightController extends Controller // add custom field to flight if ($request->isMethod('post')) { + Log::info('Adding new flight field, flight: '.$flight_id, $request->input()); + $field = new FlightFieldValue; $field->flight_id = $flight_id; $field->name = $request->input('name'); $field->value = $request->input('value'); $field->save(); } elseif ($request->isMethod('put')) { - if(!$request->input('field_id')) { + Log::info('Updating flight field, flight: '.$flight_id, $request->input()); + $field = FlightFieldValue::where('name', $request->input('name'))->first(); + if(!$field) { + Log::info('Field not found, creating new'); $field = new FlightFieldValue(); $field->flight_id = $flight_id; $field->name = $request->input('name'); - } else { - $field = FlightFieldValue::where('id', $request->input('field_id'))->first(); } $field->value = $request->input('value'); @@ -417,7 +418,8 @@ class FlightController extends Controller // update the field value } // remove custom field from flight elseif ($request->isMethod('delete')) { - if($flight_id) { + Log::info('Deleting flight field, flight: '.$flight_id, $request->input()); + if($flight_id && $request->input('field_id')) { FlightFieldValue::destroy($request->input('field_id')); } } @@ -455,7 +457,11 @@ class FlightController extends Controller $fleetSvc = app(FleetService::class); // add aircraft to flight - $subfleet = $this->subfleetRepo->find($request->input('subfleet_id')); + $subfleet = $this->subfleetRepo->findWithoutFail($request->subfleet_id); + if(!$subfleet) { + return $this->return_subfleet_view($flight); + } + if ($request->isMethod('post')) { $fleetSvc->addSubfleetToFlight($subfleet, $flight); } // remove aircraft from flight diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 6b799490..3180a07d 100755 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -20,7 +20,7 @@ class Kernel extends HttpKernel \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, - \Spatie\Pjax\Middleware\FilterIfPjax::class, + //\Spatie\Pjax\Middleware\FilterIfPjax::class, ], 'api' => [ diff --git a/resources/views/admin/app.blade.php b/resources/views/admin/app.blade.php index dcdebc33..7dbd8fff 100644 --- a/resources/views/admin/app.blade.php +++ b/resources/views/admin/app.blade.php @@ -100,7 +100,7 @@ - +{{----}} diff --git a/resources/views/admin/flights/flight_fields.blade.php b/resources/views/admin/flights/flight_fields.blade.php index e06fc586..1f2c2f07 100644 --- a/resources/views/admin/flights/flight_fields.blade.php +++ b/resources/views/admin/flights/flight_fields.blade.php @@ -9,8 +9,21 @@ @endif - @foreach($flight_fields as $field) + @php + # + # A little nasty having logic like this in a template, but we need + # to filter out the field values that have already been shown, since + # they were values set because they had a FlightField parent + # + $shown = []; + @endphp + @foreach($flight_fields->concat($flight->field_values) as $field) @php + if(in_array($field->name, $shown, true)) { + continue; + } + + $shown[] = $field->name; $val_field = $flight->field_values->where('name', $field->name)->first(); @endphp diff --git a/resources/views/admin/flights/scripts.blade.php b/resources/views/admin/flights/scripts.blade.php index c9ea7540..7dc0856d 100644 --- a/resources/views/admin/flights/scripts.blade.php +++ b/resources/views/admin/flights/scripts.blade.php @@ -63,12 +63,16 @@ $(document).ready(function () { setEditable(); setFieldsEditable(); - /*const pjax = new Pjax({ - elements: 'form[action]', - selectors: ['.pjax_subfleet_form'], - switches: { - '#subfleet_flight_wrapper': Pjax.switches.replaceNode - } + /*new Pjax({ + elements: 'form[action].pjax_subfleet_form', + selectors: ['div#subfleet_flight_wrapper'], + history: false, + }); + + new Pjax({ + elements: 'form[action].pjax_flight_fields', + selectors: ['div#flight_fields_wrapper'], + history: false });*/ $(document).on('submit', 'form.pjax_flight_fields', function (event) {