Add flight fields for templated fields on edit flight #213

This commit is contained in:
Nabeel Shahzad
2018-03-20 13:47:47 -05:00
parent 485c6e86bb
commit 25a299fb74
13 changed files with 337 additions and 53 deletions
+40 -33
View File
@@ -12,6 +12,7 @@ use App\Models\FlightFieldValue;
use App\Repositories\AirlineRepository; use App\Repositories\AirlineRepository;
use App\Repositories\AirportRepository; use App\Repositories\AirportRepository;
use App\Repositories\FareRepository; use App\Repositories\FareRepository;
use App\Repositories\FlightFieldRepository;
use App\Repositories\FlightRepository; use App\Repositories\FlightRepository;
use App\Repositories\SubfleetRepository; use App\Repositories\SubfleetRepository;
use App\Services\FareService; use App\Services\FareService;
@@ -32,25 +33,28 @@ class FlightController extends Controller
$airportRepo, $airportRepo,
$fareRepo, $fareRepo,
$flightRepo, $flightRepo,
$flightFieldRepo,
$fareSvc, $fareSvc,
$flightSvc, $flightSvc,
$subfleetRepo; $subfleetRepo;
/** /**
* FlightController constructor. * FlightController constructor.
* @param AirlineRepository $airlineRepo * @param AirlineRepository $airlineRepo
* @param AirportRepository $airportRepo * @param AirportRepository $airportRepo
* @param FareRepository $fareRepo * @param FareRepository $fareRepo
* @param FlightRepository $flightRepo * @param FlightRepository $flightRepo
* @param FareService $fareSvc * @param FlightFieldRepository $flightFieldRepository
* @param FlightService $flightSvc * @param FareService $fareSvc
* @param SubfleetRepository $subfleetRepo * @param FlightService $flightSvc
* @param SubfleetRepository $subfleetRepo
*/ */
public function __construct( public function __construct(
AirlineRepository $airlineRepo, AirlineRepository $airlineRepo,
AirportRepository $airportRepo, AirportRepository $airportRepo,
FareRepository $fareRepo, FareRepository $fareRepo,
FlightRepository $flightRepo, FlightRepository $flightRepo,
FlightFieldRepository $flightFieldRepo,
FareService $fareSvc, FareService $fareSvc,
FlightService $flightSvc, FlightService $flightSvc,
SubfleetRepository $subfleetRepo SubfleetRepository $subfleetRepo
@@ -59,6 +63,7 @@ class FlightController extends Controller
$this->airportRepo = $airportRepo; $this->airportRepo = $airportRepo;
$this->fareRepo = $fareRepo; $this->fareRepo = $fareRepo;
$this->flightRepo = $flightRepo; $this->flightRepo = $flightRepo;
$this->flightFieldRepo = $flightFieldRepo;
$this->fareSvc = $fareSvc; $this->fareSvc = $fareSvc;
$this->flightSvc = $flightSvc; $this->flightSvc = $flightSvc;
$this->subfleetRepo = $subfleetRepo; $this->subfleetRepo = $subfleetRepo;
@@ -130,11 +135,12 @@ class FlightController extends Controller
public function create() public function create()
{ {
return view('admin.flights.create', [ return view('admin.flights.create', [
'flight' => null, 'flight' => null,
'airlines' => $this->airlineRepo->selectBoxList(), 'flight_fields' => $this->flightFieldRepo->all(),
'airports' => $this->airportRepo->selectBoxList(true, false), 'airlines' => $this->airlineRepo->selectBoxList(),
'alt_airports' => $this->airportRepo->selectBoxList(true), 'airports' => $this->airportRepo->selectBoxList(true, false),
'flight_types' => FlightType::select(true), 'alt_airports' => $this->airportRepo->selectBoxList(true),
'flight_types' => FlightType::select(true),
]); ]);
} }
@@ -163,7 +169,6 @@ class FlightController extends Controller
$flights = $this->flightRepo->findWhere($where); $flights = $this->flightRepo->findWhere($where);
if ($flights->count() > 0) { if ($flights->count() > 0) {
Flash::error('Duplicate flight with same number/code/leg found, please change to proceed'); Flash::error('Duplicate flight with same number/code/leg found, please change to proceed');
return redirect()->back()->withInput($request->all()); return redirect()->back()->withInput($request->all());
} }
@@ -175,7 +180,6 @@ class FlightController extends Controller
$flight = $this->flightRepo->create($input); $flight = $this->flightRepo->create($input);
Flash::success('Flight saved successfully.'); Flash::success('Flight saved successfully.');
return redirect(route('admin.flights.edit', $flight->id)); return redirect(route('admin.flights.edit', $flight->id));
} }
@@ -189,7 +193,6 @@ class FlightController extends Controller
if (empty($flight)) { if (empty($flight)) {
Flash::error('Flight not found'); Flash::error('Flight not found');
return redirect(route('admin.flights.index')); return redirect(route('admin.flights.index'));
} }
@@ -197,6 +200,7 @@ class FlightController extends Controller
return view('admin.flights.show', [ return view('admin.flights.show', [
'flight' => $flight, 'flight' => $flight,
'flight_fields' => $this->flightFieldRepo->all(),
'avail_subfleets' => $avail_subfleets, 'avail_subfleets' => $avail_subfleets,
]); ]);
} }
@@ -221,6 +225,7 @@ class FlightController extends Controller
return view('admin.flights.edit', [ return view('admin.flights.edit', [
'flight' => $flight, 'flight' => $flight,
'flight_fields' => $this->flightFieldRepo->all(),
'airlines' => $this->airlineRepo->selectBoxList(), 'airlines' => $this->airlineRepo->selectBoxList(),
'airports' => $this->airportRepo->selectBoxList(), 'airports' => $this->airportRepo->selectBoxList(),
'alt_airports' => $this->airportRepo->selectBoxList(true), 'alt_airports' => $this->airportRepo->selectBoxList(true),
@@ -242,7 +247,6 @@ class FlightController extends Controller
if (empty($flight)) { if (empty($flight)) {
Flash::error('Flight not found'); Flash::error('Flight not found');
return redirect(route('admin.flights.index')); return redirect(route('admin.flights.index'));
} }
@@ -265,7 +269,6 @@ class FlightController extends Controller
$flights = $this->flightRepo->findWhere($where); $flights = $this->flightRepo->findWhere($where);
if ($flights->count() > 0) { if ($flights->count() > 0) {
Flash::error('Duplicate flight with same number/code/leg found, please change to proceed'); Flash::error('Duplicate flight with same number/code/leg found, please change to proceed');
return redirect()->back()->withInput($request->all()); return redirect()->back()->withInput($request->all());
} }
@@ -278,7 +281,6 @@ class FlightController extends Controller
$this->flightRepo->update($input, $id); $this->flightRepo->update($input, $id);
Flash::success('Flight updated successfully.'); Flash::success('Flight updated successfully.');
return redirect(route('admin.flights.index')); return redirect(route('admin.flights.index'));
} }
@@ -293,14 +295,12 @@ class FlightController extends Controller
if (empty($flight)) { if (empty($flight)) {
Flash::error('Flight not found'); Flash::error('Flight not found');
return redirect(route('admin.flights.index')); return redirect(route('admin.flights.index'));
} }
$this->flightSvc->deleteFlight($flight); $this->flightSvc->deleteFlight($flight);
Flash::success('Flight deleted successfully.'); Flash::success('Flight deleted successfully.');
return redirect(route('admin.flights.index')); return redirect(route('admin.flights.index'));
} }
@@ -313,7 +313,8 @@ class FlightController extends Controller
$flight->refresh(); $flight->refresh();
return view('admin.flights.flight_fields', [ 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) 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)) { if (empty($flight)) {
Flash::error('Flight not found'); Flash::error('Flight not found');
return redirect(route('admin.flights.index')); return redirect(route('admin.flights.index'));
} }
// add custom field to flight // add custom field to flight
if ($request->isMethod('post')) { if ($request->isMethod('post')) {
$field = new FlightFieldValue; $field = new FlightFieldValue;
$field->flight_id = $id; $field->flight_id = $flight_id;
$field->name = $request->name; $field->name = $request->input('name');
$field->value = $request->value; $field->value = $request->input('value');
$field->save(); $field->save();
} elseif ($request->isMethod('put')) { } elseif ($request->isMethod('put')) {
$field = FlightFieldValue::where('id', $request->field_id)->first(); if(!$request->input('field_id')) {
$field->value = $request->value; $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(); $field->save();
// update the field value // update the field value
} // remove custom field from flight } // remove custom field from flight
elseif ($request->isMethod('delete')) { elseif ($request->isMethod('delete')) {
FlightFieldValue::destroy($request->field_id); if($flight_id) {
FlightFieldValue::destroy($request->input('field_id'));
}
} }
return $this->return_fields_view($flight); return $this->return_fields_view($flight);
@@ -376,7 +385,6 @@ class FlightController extends Controller
$flight = $this->flightRepo->findWithoutFail($id); $flight = $this->flightRepo->findWithoutFail($id);
if (empty($flight)) { if (empty($flight)) {
Flash::error('Flight not found'); Flash::error('Flight not found');
return redirect(route('admin.flights.index')); return redirect(route('admin.flights.index'));
} }
@@ -400,8 +408,7 @@ class FlightController extends Controller
$all_fares = $this->fareRepo->all(); $all_fares = $this->fareRepo->all();
$avail_fares = $all_fares->except($flight->fares->modelKeys()); $avail_fares = $all_fares->except($flight->fares->modelKeys());
foreach ($avail_fares as $fare) { foreach ($avail_fares as $fare) {
$retval[$fare->id] = $fare->name. $retval[$fare->id] = $fare->name.' (base price: '.$fare->price.')';
' (base price: '.$fare->price.')';
} }
return $retval; return $retval;
@@ -0,0 +1,153 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Interfaces\Controller;
use App\Repositories\FlightFieldRepository;
use Flash;
use Illuminate\Http\Request;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;
/**
* Class FlightFieldController
* @package App\Http\Controllers\Admin
*/
class FlightFieldController extends Controller
{
private $flightFieldRepo;
/**
* FlightFieldController constructor.
* @param FlightFieldRepository $flightFieldRepository
*/
public function __construct(
FlightFieldRepository $flightFieldRepository
) {
$this->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'));
}
}
+3
View File
@@ -6,6 +6,9 @@ use App\Interfaces\Model;
/** /**
* Class FlightFieldValue * Class FlightFieldValue
* @property string flight_id
* @property string name
* @property string value
* @package App\Models * @package App\Models
*/ */
class FlightFieldValue extends Model class FlightFieldValue extends Model
@@ -0,0 +1,25 @@
<?php
namespace App\Repositories;
use App\Interfaces\Repository;
use App\Models\FlightField;
/**
* Class FlightFieldRepository
* @package App\Repositories
*/
class FlightFieldRepository extends Repository
{
protected $fieldSearchable = [
'name' => 'like',
];
/**
* @return string
*/
public function model(): string
{
return FlightField::class;
}
}
+2
View File
@@ -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}/fields', 'FlightController@field_values');
Route::match(['get', 'post', 'put', 'delete'], 'flights/{id}/subfleets', 'FlightController@subfleets'); Route::match(['get', 'post', 'put', 'delete'], 'flights/{id}/subfleets', 'FlightController@subfleets');
Route::resource('flightfields', 'FlightFieldController');
# pirep related routes # pirep related routes
Route::get('pireps/fares', 'PirepController@fares'); Route::get('pireps/fares', 'PirepController@fares');
Route::get('pireps/pending', 'PirepController@pending'); Route::get('pireps/pending', 'PirepController@pending');
@@ -0,0 +1,11 @@
@extends('admin.app')
@section('title', 'Adding Field')
@section('content')
<div class="card border-blue-bottom">
<div class="content">
{{ Form::open(['route' => 'admin.flightfields.store']) }}
@include('admin.flightfields.fields')
{{ Form::close() }}
</div>
</div>
@endsection
@@ -0,0 +1,11 @@
@extends('admin.app')
@section('title', 'Editing ' . $field->name)
@section('content')
<div class="card border-blue-bottom">
<div class="content">
{{ Form::model($field, ['route' => ['admin.flightfields.update', $field->id], 'method' => 'patch']) }}
@include('admin.flightfields.fields')
{{ Form::close() }}
</div>
</div>
@endsection
@@ -0,0 +1,14 @@
<div class="row">
<div class="form-group col-sm-6">
{{ Form::label('name', 'Name:') }}&nbsp;&nbsp;<span class="required">*</span>
{{ Form::text('name', null, ['class' => 'form-control']) }}
<p class="text-danger">{{ $errors->first('name') }}</p>
</div>
</div>
<div class="row">
<!-- Submit Field -->
<div class="form-group col-sm-12">
{{ Form::button('Save', ['type' => 'submit', 'class' => 'btn btn-success']) }}
<a href="{{ route('admin.flightfields.index') }}" class="btn btn-default">Cancel</a>
</div>
</div>
@@ -0,0 +1,18 @@
@extends('admin.app')
@section('title', 'Flight Fields')
@section('actions')
<li><a href="{{ route('admin.flightfields.create') }}"><i class="ti-plus"></i>Add Field</a></li>
<li>
<a href="{{ route('admin.flights.create') }}">
<i class="ti-plus"></i>
Add Flight</a>
</li>
@endsection
@section('content')
<div class="card border-blue-bottom">
<div class="content">
@include('admin.flightfields.table')
</div>
</div>
@endsection
@@ -0,0 +1,34 @@
<div class="content table-responsive table-full-width">
<div class="header">
@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
</div>
<table class="table table-hover table-responsive" id="pirepFields-table">
<thead>
<th>Name</th>
<th></th>
</thead>
<tbody>
@foreach($fields as $field)
<tr>
<td>{{ $field->name }}</td>
<td class="text-right">
{{ Form::open(['route' => ['admin.flightfields.destroy', $field->id], 'method' => 'delete']) }}
<a href="{{ route('admin.flightfields.edit', [$field->id]) }}"
class='btn btn-sm btn-success btn-icon'>
<i class="fas fa-pencil-alt"></i></a>
{{ Form::button('<i class="fa fa-times"></i>',
['type' => 'submit', 'class' => 'btn btn-sm btn-danger btn-icon',
'onclick' => "return confirm('Are you sure?')"]) }}
{{ Form::close() }}
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
@@ -9,23 +9,27 @@
</thead> </thead>
@endif @endif
<tbody> <tbody>
@foreach($flight->field_values as $field) @foreach($flight_fields as $field)
@php
$val_field = $flight->field_values->where('name', $field->name)->first();
@endphp
<tr> <tr>
<td>{{ $field->name }}</td> <td>{{ $field->name }}</td>
<td> <td>
<a class="inline" href="#" data-pk="{{ $field->id }}" data-name="{{ $field->name }}">{{ $field->value }}</a> <a class="inline" href="#" data-pk="{{ $val_field['id'] ?? '' }}" data-name="{{ $field->name }}">
{{ $val_field['value'] ?? '' }}
</a>
</td> </td>
<td style="width: 10%; text-align: center;" class="form-inline"> <td style="width: 10%; text-align: center;" class="form-inline">
{{ Form::open(['url' => '/admin/flights/'.$flight->id.'/fields', {{ Form::open(['url' => '/admin/flights/'.$flight->id.'/fields',
'method' => 'delete', 'method' => 'delete',
'class' => 'pjax_form pjax_flight_fields' 'class' => 'pjax_form pjax_flight_fields'
]) }} ]) }}
{{ Form::hidden('field_id', $field->id) }} {{ Form::hidden('field_id', $val_field['id'] ?? null) }}
<div class='btn-group'> <div class='btn-group'>
{{ Form::button('<i class="fa fa-times"></i>', {{ Form::button('<i class="fa fa-times"></i>',
['type' => 'submit', ['type' => 'submit', 'class' => 'btn btn-sm btn-danger btn-icon',
'class' => 'btn btn-danger btn-xs']) 'onclick' => "return confirm('Are you sure?')"]) }}
}}
</div> </div>
{{ Form::close() }} {{ Form::close() }}
</td> </td>
@@ -2,10 +2,11 @@
@section('title', 'Flights') @section('title', 'Flights')
@section('actions') @section('actions')
<li><a href="{{ route('admin.flightfields.index') }}"><i class="ti-plus"></i>Fields</a></li>
<li> <li>
<a href="{{ route('admin.flights.create') }}"> <a href="{{ route('admin.flights.create') }}">
<i class="ti-plus"></i> <i class="ti-plus"></i>
Add New</a> Add Flight</a>
</li> </li>
@endsection @endsection
+14 -13
View File
@@ -1,8 +1,8 @@
@section('scripts') @section('scripts')
<script> <script>
function setEditable() { const setEditable = () =>
{
const api_key = $('meta[name="api-key"]').attr('content'); const api_key = $('meta[name="api-key"]').attr('content');
const csrf_token = $('meta[name="csrf-token"]').attr('content'); const csrf_token = $('meta[name="csrf-token"]').attr('content');
@@ -28,19 +28,17 @@ function setEditable() {
} }
} }
}); });
} };
$(document).ready(function () { const setFieldsEditable = () =>
{
const csrf_token = $('meta[name="csrf-token"]').attr('content');
const api_key = $('meta[name="api-key"]').attr('content'); const api_key = $('meta[name="api-key"]').attr('content');
const csrf_token = $('meta[name="csrf-token"]').attr('content');
setEditable();
$('#flight_fields_wrapper a.inline').editable({ $('#flight_fields_wrapper a.inline').editable({
type: 'text', type: 'text',
mode: 'inline', mode: 'inline',
emptytext: '0', emptytext: 'no value',
url: '{{ url('/admin/flights/'.$flight->id.'/fields') }}', url: '{{ url('/admin/flights/'.$flight->id.'/fields') }}',
ajaxOptions: { ajaxOptions: {
type: 'post', type: 'post',
@@ -58,29 +56,32 @@ $(document).ready(function () {
} }
} }
}); });
};
$(document).ready(function () {
setEditable();
setFieldsEditable();
$(document).on('submit', 'form.pjax_flight_fields', function (event) { $(document).on('submit', 'form.pjax_flight_fields', function (event) {
event.preventDefault(); event.preventDefault();
$.pjax.submit(event, '#flight_fields_wrapper', {push: false}); $.pjax.submit(event, '#flight_fields_wrapper', {push: false});
setEditable();
}); });
$(document).on('submit', 'form.pjax_subfleet_form', function (event) { $(document).on('submit', 'form.pjax_subfleet_form', function (event) {
event.preventDefault(); event.preventDefault();
$.pjax.submit(event, '#subfleet_flight_wrapper', {push: false}); $.pjax.submit(event, '#subfleet_flight_wrapper', {push: false});
setEditable();
}); });
$(document).on('submit', 'form.pjax_fares_form', function (event) { $(document).on('submit', 'form.pjax_fares_form', function (event) {
event.preventDefault(); event.preventDefault();
console.log(event);
$.pjax.submit(event, '#flight_fares_wrapper', {push: false}); $.pjax.submit(event, '#flight_fares_wrapper', {push: false});
setEditable();
}); });
$(document).on('pjax:complete', function () { $(document).on('pjax:complete', function () {
$(".select2").select2(); $(".select2").select2();
setEditable(); setEditable();
setFieldsEditable();
}); });
}); });
</script> </script>