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

View File

@@ -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;

View File

@@ -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'));
}
}

View File

@@ -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

View File

@@ -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;
}
}

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}/subfleets', 'FlightController@subfleets');
Route::resource('flightfields', 'FlightFieldController');
# pirep related routes
Route::get('pireps/fares', 'PirepController@fares');
Route::get('pireps/pending', 'PirepController@pending');

View File

@@ -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

View File

@@ -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

View File

@@ -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>

View File

@@ -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

View File

@@ -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>

View File

@@ -9,23 +9,27 @@
</thead>
@endif
<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>
<td>{{ $field->name }}</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 style="width: 10%; text-align: center;" class="form-inline">
{{ 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) }}
<div class='btn-group'>
{{ Form::button('<i class="fa fa-times"></i>',
['type' => 'submit',
'class' => 'btn btn-danger btn-xs'])
}}
['type' => 'submit', 'class' => 'btn btn-sm btn-danger btn-icon',
'onclick' => "return confirm('Are you sure?')"]) }}
</div>
{{ Form::close() }}
</td>

View File

@@ -2,10 +2,11 @@
@section('title', 'Flights')
@section('actions')
<li><a href="{{ route('admin.flightfields.index') }}"><i class="ti-plus"></i>Fields</a></li>
<li>
<a href="{{ route('admin.flights.create') }}">
<i class="ti-plus"></i>
Add New</a>
Add Flight</a>
</li>
@endsection

View File

@@ -1,8 +1,8 @@
@section('scripts')
<script>
function setEditable() {
const setEditable = () =>
{
const api_key = $('meta[name="api-key"]').attr('content');
const csrf_token = $('meta[name="csrf-token"]').attr('content');
@@ -28,19 +28,17 @@ function setEditable() {
}
}
});
}
};
$(document).ready(function () {
const csrf_token = $('meta[name="csrf-token"]').attr('content');
const setFieldsEditable = () =>
{
const api_key = $('meta[name="api-key"]').attr('content');
setEditable();
const csrf_token = $('meta[name="csrf-token"]').attr('content');
$('#flight_fields_wrapper a.inline').editable({
type: 'text',
mode: 'inline',
emptytext: '0',
emptytext: 'no value',
url: '{{ url('/admin/flights/'.$flight->id.'/fields') }}',
ajaxOptions: {
type: 'post',
@@ -58,29 +56,32 @@ $(document).ready(function () {
}
}
});
};
$(document).ready(function () {
setEditable();
setFieldsEditable();
$(document).on('submit', 'form.pjax_flight_fields', function (event) {
event.preventDefault();
$.pjax.submit(event, '#flight_fields_wrapper', {push: false});
setEditable();
});
$(document).on('submit', 'form.pjax_subfleet_form', function (event) {
event.preventDefault();
$.pjax.submit(event, '#subfleet_flight_wrapper', {push: false});
setEditable();
});
$(document).on('submit', 'form.pjax_fares_form', function (event) {
event.preventDefault();
console.log(event);
$.pjax.submit(event, '#flight_fares_wrapper', {push: false});
setEditable();
});
$(document).on('pjax:complete', function () {
$(".select2").select2();
setEditable();
setFieldsEditable();
});
});
</script>