From ad4259625cf28e4fc3d3bcbc46d551c9d63ef99d Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Mon, 19 Feb 2018 13:33:26 -0600 Subject: [PATCH] Add flight time to flight create/edit form --- .../Controllers/Admin/FlightController.php | 18 ++++++++++++-- .../Controllers/Admin/PirepController.php | 14 ++++++----- app/Support/Units/Time.php | 10 ++++++++ .../views/admin/flights/fields.blade.php | 24 ++++++++++++------- 4 files changed, 50 insertions(+), 16 deletions(-) diff --git a/app/Http/Controllers/Admin/FlightController.php b/app/Http/Controllers/Admin/FlightController.php index 6ab84f53..240245e8 100644 --- a/app/Http/Controllers/Admin/FlightController.php +++ b/app/Http/Controllers/Admin/FlightController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\Admin; +use App\Facades\Utils; use App\Models\Enums\FlightType; use App\Models\Flight; use App\Models\FlightFields; @@ -16,6 +17,7 @@ use App\Repositories\SubfleetRepository; use App\Services\FareService; use App\Services\FlightService; +use App\Support\Units\Time; use Illuminate\Http\Request; use Flash; use Response; @@ -121,7 +123,11 @@ class FlightController extends BaseController return redirect()->back()->withInput($request->all()); } - $input['active'] = get_truth_state($input['active']);; + $input['active'] = get_truth_state($input['active']); + + $time = new Time($input['minutes'], $input['hours']); + $input['flight_time'] = $time->getMinutes(); + $flight = $this->flightRepo->create($input); Flash::success('Flight saved successfully.'); @@ -155,12 +161,15 @@ class FlightController extends BaseController public function edit($id) { $flight = $this->flightRepo->findWithoutFail($id); - if (empty($flight)) { Flash::error('Flight not found'); return redirect(route('admin.flights.index')); } + $time = new Time($flight->flight_time); + $flight->hours = $time->hours; + $flight->minutes = $time->minutes; + $avail_subfleets = $this->getAvailSubfleets($flight); return view('admin.flights.edit', [ 'flight' => $flight, @@ -209,7 +218,12 @@ class FlightController extends BaseController return redirect()->back()->withInput($request->all()); } + $input['flight_time'] = Time::init( + $input['minutes'], + $input['hours'])->getMinutes(); + $input['active'] = get_truth_state($input['active']); + $this->flightRepo->update($input, $id); Flash::success('Flight updated successfully.'); diff --git a/app/Http/Controllers/Admin/PirepController.php b/app/Http/Controllers/Admin/PirepController.php index d4e49a90..4c4f72c4 100644 --- a/app/Http/Controllers/Admin/PirepController.php +++ b/app/Http/Controllers/Admin/PirepController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Admin; use App\Services\UserService; +use App\Support\Units\Time; use Log; use Flash; use Response; @@ -184,9 +185,9 @@ class PirepController extends BaseController return redirect(route('admin.pireps.index')); } - $hms = Utils::minutesToTimeParts($pirep->flight_time); - $pirep->hours = $hms['h']; - $pirep->minutes = $hms['m']; + $time = new Time($pirep->flight_time); + $pirep->hours = $time->hours; + $pirep->minutes = $time->minutes; # Can we modify? $read_only = false; @@ -208,6 +209,7 @@ class PirepController extends BaseController * @param UpdatePirepRequest $request * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector * @throws \Prettus\Validator\Exceptions\ValidatorException + * @throws \Exception */ public function update($id, UpdatePirepRequest $request) { @@ -224,9 +226,9 @@ class PirepController extends BaseController $attrs = $request->all(); # Fix the time - $hours = (int) $attrs['hours']; - $minutes = (int) $attrs['minutes']; - $attrs['flight_time'] = Utils::hoursToMinutes($hours) + $minutes; + $attrs['flight_time'] = Time::init( + $attrs['minutes'], + $attrs['hours'])->getMinutes(); $pirep = $this->pirepRepo->update($attrs, $id); diff --git a/app/Support/Units/Time.php b/app/Support/Units/Time.php index 8745e66c..7f151169 100644 --- a/app/Support/Units/Time.php +++ b/app/Support/Units/Time.php @@ -12,6 +12,16 @@ class Time implements Arrayable public $hours, $minutes; + /** + * @param $minutes + * @param $hours + * @return static + */ + public static function init($minutes, $hours) + { + return new Time($minutes, $hours); + } + /** * Pass just minutes to figure out how many hours * Or both hours and minutes diff --git a/resources/views/admin/flights/fields.blade.php b/resources/views/admin/flights/fields.blade.php index 25da81eb..943e657a 100644 --- a/resources/views/admin/flights/fields.blade.php +++ b/resources/views/admin/flights/fields.blade.php @@ -28,10 +28,6 @@ - -
@@ -64,25 +60,37 @@ SAME ROW
-
+
{!! Form::label('dpt_time', 'Departure Time:') !!} {!! Form::text('dpt_time', null, ['class' => 'form-control']) !!}

{{ $errors->first('dpt_time') }}

-
+
{!! Form::label('arr_time', 'Arrival Time:') !!} {!! Form::text('arr_time', null, ['class' => 'form-control']) !!}

{{ $errors->first('arr_time') }}

-
+
+ {!! Form::label('flight_time', 'Flight Time (hours & minutes):') !!} +
+ {!! Form::number('hours', null, ['class' => 'form-control', 'placeholder' => 'hours']) !!} +
+
+ {!! Form::number('minutes', null, ['class' => 'form-control', 'placeholder' => 'minutes']) !!} +

{{ $errors->first('hours') }}

+

{{ $errors->first('minutes') }}

+
+
+ +
{!! Form::label('level', 'Flight Level:') !!} {!! Form::text('level', null, ['class' => 'form-control']) !!}

{{ $errors->first('level') }}

-
+
{!! Form::label('distance', 'Distance:') !!} in miles {!! Form::text('distance', null, ['class' => 'form-control']) !!}

{{ $errors->first('distance') }}