2017-08-04 23:04:00 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Frontend;
|
|
|
|
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2017-08-17 07:18:47 +08:00
|
|
|
use Illuminate\Http\Request;
|
2017-08-04 23:04:00 +08:00
|
|
|
|
2017-08-09 09:34:36 +08:00
|
|
|
use App\Models\Pirep;
|
2017-08-17 07:18:47 +08:00
|
|
|
use App\Models\PirepField;
|
2017-08-09 09:34:36 +08:00
|
|
|
|
2017-08-17 07:18:47 +08:00
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Repositories\AirlineRepository;
|
|
|
|
use App\Repositories\AircraftRepository;
|
2017-12-01 10:28:45 +08:00
|
|
|
use App\Repositories\AirportRepository;
|
2017-08-17 07:18:47 +08:00
|
|
|
use App\Repositories\PirepRepository;
|
2017-12-01 10:28:45 +08:00
|
|
|
use App\Repositories\PirepFieldRepository;
|
2017-08-04 23:04:00 +08:00
|
|
|
|
2017-08-17 07:18:47 +08:00
|
|
|
|
|
|
|
class PirepController extends Controller
|
2017-08-04 23:04:00 +08:00
|
|
|
{
|
2017-12-02 01:11:03 +08:00
|
|
|
private $airlineRepo,
|
|
|
|
$aircraftRepo,
|
|
|
|
$pirepRepo,
|
|
|
|
$airportRepo,
|
|
|
|
$pirepFieldRepo;
|
|
|
|
|
2017-08-17 07:18:47 +08:00
|
|
|
public function __construct(
|
|
|
|
AirlineRepository $airlineRepo,
|
|
|
|
PirepRepository $pirepRepo,
|
2017-12-01 10:28:45 +08:00
|
|
|
AircraftRepository $aircraftRepo,
|
|
|
|
AirportRepository $airportRepo,
|
|
|
|
PirepFieldRepository $pirepFieldRepo
|
|
|
|
) {
|
2017-08-17 07:18:47 +08:00
|
|
|
$this->airlineRepo = $airlineRepo;
|
|
|
|
$this->aircraftRepo = $aircraftRepo;
|
|
|
|
$this->pirepRepo = $pirepRepo;
|
2017-12-01 10:28:45 +08:00
|
|
|
$this->airportRepo = $airportRepo;
|
|
|
|
$this->pirepFieldRepo = $pirepFieldRepo;
|
2017-08-17 07:18:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function index(Request $request)
|
2017-08-04 23:04:00 +08:00
|
|
|
{
|
2017-08-09 09:34:36 +08:00
|
|
|
$user = Auth::user();
|
2017-12-01 10:28:45 +08:00
|
|
|
$pireps = $this->pirepRepo
|
|
|
|
->where('user_id', $user->id)
|
|
|
|
->orderBy('created_at', 'desc')
|
|
|
|
->get();
|
2017-08-09 09:34:36 +08:00
|
|
|
|
2017-08-04 23:04:00 +08:00
|
|
|
return $this->view('pireps.index', [
|
2017-08-09 09:34:36 +08:00
|
|
|
'user' => $user,
|
|
|
|
'pireps' => $pireps,
|
2017-08-04 23:04:00 +08:00
|
|
|
]);
|
|
|
|
}
|
2017-08-09 09:34:36 +08:00
|
|
|
|
|
|
|
public function create()
|
|
|
|
{
|
2017-08-24 03:19:05 +08:00
|
|
|
$aircraft = $this->aircraftList();
|
|
|
|
|
2017-08-17 07:18:47 +08:00
|
|
|
return $this->view('pireps.create', [
|
2017-12-02 01:11:03 +08:00
|
|
|
'airports' => $this->airportRepo->selectBoxList(),
|
2017-12-01 10:28:45 +08:00
|
|
|
'airlines' => $this->airlineRepo->all()->pluck('name', 'id'),
|
2017-12-02 01:11:03 +08:00
|
|
|
'aircraft' => $this->aircraftRepo->selectBoxList(),
|
2017-12-01 10:28:45 +08:00
|
|
|
'pirepfields' => $this->pirepFieldRepo->all(),
|
2017-08-17 07:18:47 +08:00
|
|
|
'fieldvalues' => [],
|
|
|
|
]);
|
2017-08-10 13:08:38 +08:00
|
|
|
}
|
|
|
|
|
2017-08-17 07:18:47 +08:00
|
|
|
public function store(Request $request)
|
2017-08-10 13:08:38 +08:00
|
|
|
{
|
2017-08-24 03:19:05 +08:00
|
|
|
$pirep_fields = $request->all();
|
|
|
|
|
|
|
|
// Create the main PIREP
|
|
|
|
$pirep = new Pirep($pirep_fields);
|
|
|
|
|
|
|
|
// Any special fields
|
|
|
|
$pirep->pilot()->associate(Auth::user());
|
|
|
|
$pirep->flight_time = ((int)$pirep_fields['hours'] * 60 * 60)
|
|
|
|
+ ((int)$pirep_fields['minutes'] * 60);
|
|
|
|
|
|
|
|
// The custom fields from the form
|
|
|
|
$custom_fields = [];
|
|
|
|
foreach($pirep_fields as $field_name => $field_val)
|
|
|
|
{
|
|
|
|
if (strpos($field_name, 'field_') === false) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$field_id = explode('field_', $field_name)[1];
|
|
|
|
$cfield = PirepField::find($field_id);
|
|
|
|
|
|
|
|
$custom_fields[] = [
|
|
|
|
'name' => $cfield->name,
|
|
|
|
'value' => $field_val,
|
|
|
|
'source' => config('enums.sources.MANUAL')
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$pirepSvc = app('\App\Services\PIREPService');
|
|
|
|
$pirep = $pirepSvc->create($pirep, $custom_fields);
|
2017-08-10 13:08:38 +08:00
|
|
|
|
2017-08-24 03:19:05 +08:00
|
|
|
//Flash::success('PIREP submitted successfully!');
|
|
|
|
return redirect(route('frontend.pireps.index'));
|
2017-08-10 13:08:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function show($id)
|
|
|
|
{
|
|
|
|
$pirep = Pirep::where('id', $id);
|
|
|
|
return $this->view('pireps.show', [
|
|
|
|
'pirep' => $pirep,
|
|
|
|
]);
|
2017-08-09 09:34:36 +08:00
|
|
|
}
|
2017-08-04 23:04:00 +08:00
|
|
|
}
|