diff --git a/app/Http/Controllers/Admin/FlightController.php b/app/Http/Controllers/Admin/FlightController.php index d7155fe2..ae243c18 100644 --- a/app/Http/Controllers/Admin/FlightController.php +++ b/app/Http/Controllers/Admin/FlightController.php @@ -12,6 +12,7 @@ use App\Models\FlightFieldValue; use App\Repositories\AirlineRepository; use App\Repositories\AirportRepository; use App\Repositories\FareRepository; +use App\Repositories\FlightFieldRepository; use App\Repositories\FlightRepository; use App\Repositories\SubfleetRepository; use App\Services\FareService; @@ -32,25 +33,28 @@ class FlightController extends Controller $airportRepo, $fareRepo, $flightRepo, + $flightFieldRepo, $fareSvc, $flightSvc, $subfleetRepo; /** * FlightController constructor. - * @param AirlineRepository $airlineRepo - * @param AirportRepository $airportRepo - * @param FareRepository $fareRepo - * @param FlightRepository $flightRepo - * @param FareService $fareSvc - * @param FlightService $flightSvc - * @param SubfleetRepository $subfleetRepo + * @param AirlineRepository $airlineRepo + * @param AirportRepository $airportRepo + * @param FareRepository $fareRepo + * @param FlightRepository $flightRepo + * @param FlightFieldRepository $flightFieldRepository + * @param FareService $fareSvc + * @param FlightService $flightSvc + * @param SubfleetRepository $subfleetRepo */ public function __construct( AirlineRepository $airlineRepo, AirportRepository $airportRepo, FareRepository $fareRepo, FlightRepository $flightRepo, + FlightFieldRepository $flightFieldRepo, FareService $fareSvc, FlightService $flightSvc, SubfleetRepository $subfleetRepo @@ -59,6 +63,7 @@ class FlightController extends Controller $this->airportRepo = $airportRepo; $this->fareRepo = $fareRepo; $this->flightRepo = $flightRepo; + $this->flightFieldRepo = $flightFieldRepo; $this->fareSvc = $fareSvc; $this->flightSvc = $flightSvc; $this->subfleetRepo = $subfleetRepo; @@ -130,11 +135,12 @@ class FlightController extends Controller public function create() { return view('admin.flights.create', [ - 'flight' => null, - 'airlines' => $this->airlineRepo->selectBoxList(), - 'airports' => $this->airportRepo->selectBoxList(true, false), - 'alt_airports' => $this->airportRepo->selectBoxList(true), - 'flight_types' => FlightType::select(true), + 'flight' => null, + 'flight_fields' => $this->flightFieldRepo->all(), + 'airlines' => $this->airlineRepo->selectBoxList(), + 'airports' => $this->airportRepo->selectBoxList(true, false), + 'alt_airports' => $this->airportRepo->selectBoxList(true), + 'flight_types' => FlightType::select(true), ]); } @@ -163,7 +169,6 @@ class FlightController extends Controller $flights = $this->flightRepo->findWhere($where); if ($flights->count() > 0) { Flash::error('Duplicate flight with same number/code/leg found, please change to proceed'); - return redirect()->back()->withInput($request->all()); } @@ -175,7 +180,6 @@ class FlightController extends Controller $flight = $this->flightRepo->create($input); Flash::success('Flight saved successfully.'); - return redirect(route('admin.flights.edit', $flight->id)); } @@ -189,7 +193,6 @@ class FlightController extends Controller if (empty($flight)) { Flash::error('Flight not found'); - return redirect(route('admin.flights.index')); } @@ -197,6 +200,7 @@ class FlightController extends Controller return view('admin.flights.show', [ 'flight' => $flight, + 'flight_fields' => $this->flightFieldRepo->all(), 'avail_subfleets' => $avail_subfleets, ]); } @@ -221,6 +225,7 @@ class FlightController extends Controller return view('admin.flights.edit', [ 'flight' => $flight, + 'flight_fields' => $this->flightFieldRepo->all(), 'airlines' => $this->airlineRepo->selectBoxList(), 'airports' => $this->airportRepo->selectBoxList(), 'alt_airports' => $this->airportRepo->selectBoxList(true), @@ -242,7 +247,6 @@ class FlightController extends Controller if (empty($flight)) { Flash::error('Flight not found'); - return redirect(route('admin.flights.index')); } @@ -265,7 +269,6 @@ class FlightController extends Controller $flights = $this->flightRepo->findWhere($where); if ($flights->count() > 0) { Flash::error('Duplicate flight with same number/code/leg found, please change to proceed'); - return redirect()->back()->withInput($request->all()); } @@ -278,7 +281,6 @@ class FlightController extends Controller $this->flightRepo->update($input, $id); Flash::success('Flight updated successfully.'); - return redirect(route('admin.flights.index')); } @@ -293,14 +295,12 @@ class FlightController extends Controller if (empty($flight)) { Flash::error('Flight not found'); - return redirect(route('admin.flights.index')); } $this->flightSvc->deleteFlight($flight); Flash::success('Flight deleted successfully.'); - return redirect(route('admin.flights.index')); } @@ -313,7 +313,8 @@ class FlightController extends Controller $flight->refresh(); return view('admin.flights.flight_fields', [ - 'flight' => $flight, + 'flight' => $flight, + 'flight_fields' => $this->flightFieldRepo->all(), ]); } @@ -323,30 +324,38 @@ class FlightController extends Controller */ public function field_values(Request $request) { - $id = $request->id; + $flight_id = $request->id; - $flight = $this->flightRepo->findWithoutFail($id); + $flight = $this->flightRepo->findWithoutFail($flight_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 FlightFieldValue; - $field->flight_id = $id; - $field->name = $request->name; - $field->value = $request->value; + $field->flight_id = $flight_id; + $field->name = $request->input('name'); + $field->value = $request->input('value'); $field->save(); } elseif ($request->isMethod('put')) { - $field = FlightFieldValue::where('id', $request->field_id)->first(); - $field->value = $request->value; + if(!$request->input('field_id')) { + $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'); $field->save(); // update the field value } // remove custom field from flight elseif ($request->isMethod('delete')) { - FlightFieldValue::destroy($request->field_id); + if($flight_id) { + FlightFieldValue::destroy($request->input('field_id')); + } } return $this->return_fields_view($flight); @@ -376,7 +385,6 @@ class FlightController extends Controller $flight = $this->flightRepo->findWithoutFail($id); if (empty($flight)) { Flash::error('Flight not found'); - return redirect(route('admin.flights.index')); } @@ -400,8 +408,7 @@ class FlightController extends Controller $all_fares = $this->fareRepo->all(); $avail_fares = $all_fares->except($flight->fares->modelKeys()); foreach ($avail_fares as $fare) { - $retval[$fare->id] = $fare->name. - ' (base price: '.$fare->price.')'; + $retval[$fare->id] = $fare->name.' (base price: '.$fare->price.')'; } return $retval; diff --git a/app/Http/Controllers/Admin/FlightFieldController.php b/app/Http/Controllers/Admin/FlightFieldController.php new file mode 100644 index 00000000..341d6466 --- /dev/null +++ b/app/Http/Controllers/Admin/FlightFieldController.php @@ -0,0 +1,153 @@ +flightFieldRepo = $flightFieldRepository; + } + + /** + * Display a listing of the FlightField. + * @param Request $request + * @return Response + * @throws \Prettus\Repository\Exceptions\RepositoryException + */ + public function index(Request $request) + { + $this->flightFieldRepo->pushCriteria(new RequestCriteria($request)); + $fields = $this->flightFieldRepo->all(); + + return view('admin.flightfields.index', [ + 'fields' => $fields, + ]); + } + + /** + * Show the form for creating a new FlightField. + * @return Response + */ + public function create() + { + return view('admin.flightfields.create'); + } + + /** + * Store a newly created FlightField in storage. + * @param Request $request + * @return Response + * @throws \Prettus\Validator\Exceptions\ValidatorException + */ + public function store(Request $request) + { + $attrs = $request->all(); + $attrs['slug'] = str_slug($attrs['name']); + + $this->flightFieldRepo->create($attrs); + + Flash::success('Field added successfully.'); + return redirect(route('admin.flightfields.index')); + } + + /** + * Display the specified FlightField. + * @param int $id + * @return Response + */ + public function show($id) + { + $field = $this->flightFieldRepo->findWithoutFail($id); + + if (empty($field)) { + Flash::error('Flight field not found'); + return redirect(route('admin.flightfields.index')); + } + + return view('admin.flightfields.show', [ + 'field' => $field, + ]); + } + + /** + * Show the form for editing the specified FlightField. + * @param int $id + * @return Response + */ + public function edit($id) + { + $field = $this->flightFieldRepo->findWithoutFail($id); + + if (empty($field)) { + Flash::error('Field not found'); + return redirect(route('admin.flightfields.index')); + } + + return view('admin.flightfields.edit', [ + 'field' => $field, + ]); + } + + /** + * Update the specified FlightField in storage. + * @param $id + * @param Request $request + * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector + * @throws \Prettus\Validator\Exceptions\ValidatorException + */ + public function update($id, Request $request) + { + $field = $this->flightFieldRepo->findWithoutFail($id); + + if (empty($field)) { + Flash::error('FlightField not found'); + return redirect(route('admin.flightfields.index')); + } + + $attrs = $request->all(); + $attrs['slug'] = str_slug($attrs['name']); + $this->flightFieldRepo->update($attrs, $id); + + Flash::success('Field updated successfully.'); + return redirect(route('admin.flightfields.index')); + } + + /** + * Remove the specified FlightField from storage. + * @param int $id + * @return Response + */ + public function destroy($id) + { + $field = $this->flightFieldRepo->findWithoutFail($id); + + if (empty($field)) { + Flash::error('Field not found'); + return redirect(route('admin.flightfields.index')); + } + + $this->flightFieldRepo->delete($id); + + Flash::success('Field deleted successfully.'); + return redirect(route('admin.flightfields.index')); + } +} diff --git a/app/Models/FlightFieldValue.php b/app/Models/FlightFieldValue.php index 64d3a48d..2a6f11a2 100644 --- a/app/Models/FlightFieldValue.php +++ b/app/Models/FlightFieldValue.php @@ -6,6 +6,9 @@ use App\Interfaces\Model; /** * Class FlightFieldValue + * @property string flight_id + * @property string name + * @property string value * @package App\Models */ class FlightFieldValue extends Model diff --git a/app/Repositories/FlightFieldRepository.php b/app/Repositories/FlightFieldRepository.php new file mode 100644 index 00000000..df2cbe9b --- /dev/null +++ b/app/Repositories/FlightFieldRepository.php @@ -0,0 +1,25 @@ + 'like', + ]; + + /** + * @return string + */ + public function model(): string + { + return FlightField::class; + } +} diff --git a/app/Routes/admin.php b/app/Routes/admin.php index 1fcb2968..e82fd51d 100644 --- a/app/Routes/admin.php +++ b/app/Routes/admin.php @@ -35,6 +35,8 @@ Route::group([ Route::match(['get', 'post', 'put', 'delete'], 'flights/{id}/fields', 'FlightController@field_values'); Route::match(['get', 'post', 'put', 'delete'], 'flights/{id}/subfleets', 'FlightController@subfleets'); + Route::resource('flightfields', 'FlightFieldController'); + # pirep related routes Route::get('pireps/fares', 'PirepController@fares'); Route::get('pireps/pending', 'PirepController@pending'); diff --git a/resources/views/admin/flightfields/create.blade.php b/resources/views/admin/flightfields/create.blade.php new file mode 100644 index 00000000..e3126101 --- /dev/null +++ b/resources/views/admin/flightfields/create.blade.php @@ -0,0 +1,11 @@ +@extends('admin.app') +@section('title', 'Adding Field') +@section('content') +
+
+ {{ Form::open(['route' => 'admin.flightfields.store']) }} + @include('admin.flightfields.fields') + {{ Form::close() }} +
+
+@endsection diff --git a/resources/views/admin/flightfields/edit.blade.php b/resources/views/admin/flightfields/edit.blade.php new file mode 100644 index 00000000..636ed3b5 --- /dev/null +++ b/resources/views/admin/flightfields/edit.blade.php @@ -0,0 +1,11 @@ +@extends('admin.app') +@section('title', 'Editing ' . $field->name) +@section('content') +
+
+ {{ Form::model($field, ['route' => ['admin.flightfields.update', $field->id], 'method' => 'patch']) }} + @include('admin.flightfields.fields') + {{ Form::close() }} +
+
+@endsection diff --git a/resources/views/admin/flightfields/fields.blade.php b/resources/views/admin/flightfields/fields.blade.php new file mode 100644 index 00000000..9aecf4b3 --- /dev/null +++ b/resources/views/admin/flightfields/fields.blade.php @@ -0,0 +1,14 @@ +
+
+ {{ Form::label('name', 'Name:') }}  * + {{ Form::text('name', null, ['class' => 'form-control']) }} +

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

+
+
+
+ +
+ {{ Form::button('Save', ['type' => 'submit', 'class' => 'btn btn-success']) }} + Cancel +
+
diff --git a/resources/views/admin/flightfields/index.blade.php b/resources/views/admin/flightfields/index.blade.php new file mode 100644 index 00000000..f2d8902b --- /dev/null +++ b/resources/views/admin/flightfields/index.blade.php @@ -0,0 +1,18 @@ +@extends('admin.app') +@section('title', 'Flight Fields') +@section('actions') +
  • Add Field
  • +
  • + + + Add Flight +
  • +@endsection +@section('content') +
    +
    + @include('admin.flightfields.table') +
    +
    +@endsection + diff --git a/resources/views/admin/flightfields/table.blade.php b/resources/views/admin/flightfields/table.blade.php new file mode 100644 index 00000000..37651d60 --- /dev/null +++ b/resources/views/admin/flightfields/table.blade.php @@ -0,0 +1,34 @@ +
    + +
    + @component('admin.components.info') + Flights fields that can be filled out. You can still add other custom fields + directly in the flight, but this provides a template for all flights. + @endcomponent +
    + + + + + + + + @foreach($fields as $field) + + + + + @endforeach + +
    Name
    {{ $field->name }} + {{ Form::open(['route' => ['admin.flightfields.destroy', $field->id], 'method' => 'delete']) }} + + + + {{ Form::button('', + ['type' => 'submit', 'class' => 'btn btn-sm btn-danger btn-icon', + 'onclick' => "return confirm('Are you sure?')"]) }} + {{ Form::close() }} +
    +
    diff --git a/resources/views/admin/flights/flight_fields.blade.php b/resources/views/admin/flights/flight_fields.blade.php index e59c98ce..e06fc586 100644 --- a/resources/views/admin/flights/flight_fields.blade.php +++ b/resources/views/admin/flights/flight_fields.blade.php @@ -9,23 +9,27 @@ @endif - @foreach($flight->field_values as $field) + @foreach($flight_fields as $field) + @php + $val_field = $flight->field_values->where('name', $field->name)->first(); + @endphp {{ $field->name }} - {{ $field->value }} + + {{ $val_field['value'] ?? '' }} + {{ Form::open(['url' => '/admin/flights/'.$flight->id.'/fields', 'method' => 'delete', 'class' => 'pjax_form pjax_flight_fields' ]) }} - {{ Form::hidden('field_id', $field->id) }} + {{ Form::hidden('field_id', $val_field['id'] ?? null) }}
    {{ Form::button('', - ['type' => 'submit', - 'class' => 'btn btn-danger btn-xs']) - }} + ['type' => 'submit', 'class' => 'btn btn-sm btn-danger btn-icon', + 'onclick' => "return confirm('Are you sure?')"]) }}
    {{ Form::close() }} diff --git a/resources/views/admin/flights/index.blade.php b/resources/views/admin/flights/index.blade.php index 19793e65..919b8587 100644 --- a/resources/views/admin/flights/index.blade.php +++ b/resources/views/admin/flights/index.blade.php @@ -2,10 +2,11 @@ @section('title', 'Flights') @section('actions') +
  • Fields
  • - Add New + Add Flight
  • @endsection diff --git a/resources/views/admin/flights/scripts.blade.php b/resources/views/admin/flights/scripts.blade.php index 4cef09e6..707f70e8 100644 --- a/resources/views/admin/flights/scripts.blade.php +++ b/resources/views/admin/flights/scripts.blade.php @@ -1,8 +1,8 @@ @section('scripts')