2017-08-04 23:04:00 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Frontend;
|
|
|
|
|
2018-01-24 05:48:30 +08:00
|
|
|
use Log;
|
2017-12-14 00:56:26 +08:00
|
|
|
use App\Facades\Utils;
|
2018-01-24 05:48:30 +08:00
|
|
|
use App\Http\Requests\CreatePirepRequest;
|
2017-08-04 23:04:00 +08:00
|
|
|
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
|
|
|
|
2018-01-11 09:40:20 +08:00
|
|
|
use App\Models\Enums\PirepSource;
|
|
|
|
use App\Models\Enums\PirepState;
|
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
|
|
|
|
2018-01-11 09:40:20 +08:00
|
|
|
use App\Services\GeoService;
|
|
|
|
use App\Services\PIREPService;
|
|
|
|
use App\Services\UserService;
|
|
|
|
|
|
|
|
use App\Repositories\Criteria\WhereCriteria;
|
2017-08-17 07:18:47 +08:00
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Repositories\AirlineRepository;
|
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;
|
2018-01-11 09:40:20 +08:00
|
|
|
use App\Repositories\SubfleetRepository;
|
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,
|
|
|
|
$pirepRepo,
|
|
|
|
$airportRepo,
|
2017-12-04 05:29:34 +08:00
|
|
|
$pirepFieldRepo,
|
2017-12-21 01:28:21 +08:00
|
|
|
$geoSvc,
|
2018-01-11 09:40:20 +08:00
|
|
|
$pirepSvc,
|
|
|
|
$subfleetRepo,
|
|
|
|
$userSvc;
|
2017-12-02 01:11:03 +08:00
|
|
|
|
2017-08-17 07:18:47 +08:00
|
|
|
public function __construct(
|
|
|
|
AirlineRepository $airlineRepo,
|
|
|
|
PirepRepository $pirepRepo,
|
2017-12-01 10:28:45 +08:00
|
|
|
AirportRepository $airportRepo,
|
2017-12-04 05:29:34 +08:00
|
|
|
PirepFieldRepository $pirepFieldRepo,
|
2017-12-21 01:28:21 +08:00
|
|
|
GeoService $geoSvc,
|
2018-01-11 09:40:20 +08:00
|
|
|
SubfleetRepository $subfleetRepo,
|
|
|
|
PIREPService $pirepSvc,
|
|
|
|
UserService $userSvc
|
2017-12-01 10:28:45 +08:00
|
|
|
) {
|
2017-08-17 07:18:47 +08:00
|
|
|
$this->airlineRepo = $airlineRepo;
|
|
|
|
$this->pirepRepo = $pirepRepo;
|
2017-12-01 10:28:45 +08:00
|
|
|
$this->airportRepo = $airportRepo;
|
2018-01-11 09:40:20 +08:00
|
|
|
$this->subfleetRepo = $subfleetRepo;
|
2017-12-01 10:28:45 +08:00
|
|
|
$this->pirepFieldRepo = $pirepFieldRepo;
|
2017-12-21 01:28:21 +08:00
|
|
|
|
|
|
|
$this->geoSvc = $geoSvc;
|
2017-12-04 05:29:34 +08:00
|
|
|
$this->pirepSvc = $pirepSvc;
|
2018-01-11 09:40:20 +08:00
|
|
|
$this->userSvc = $userSvc;
|
2017-08-17 07:18:47 +08:00
|
|
|
}
|
|
|
|
|
2018-01-11 09:40:20 +08:00
|
|
|
/**
|
|
|
|
* Dropdown with aircraft grouped by subfleet
|
2018-01-20 01:46:30 +08:00
|
|
|
* @param null $user
|
|
|
|
* @return array
|
2018-01-11 09:40:20 +08:00
|
|
|
*/
|
2018-01-24 05:48:30 +08:00
|
|
|
public function aircraftList($user=null, $add_blank=false)
|
2018-01-11 09:40:20 +08:00
|
|
|
{
|
|
|
|
$aircraft = [];
|
2018-01-20 01:46:30 +08:00
|
|
|
$subfleets = $this->userSvc->getAllowableSubfleets($user);
|
2018-01-11 09:40:20 +08:00
|
|
|
|
2018-01-24 05:48:30 +08:00
|
|
|
if($add_blank) {
|
|
|
|
$aircraft[''] = '';
|
|
|
|
}
|
|
|
|
|
2018-01-11 09:40:20 +08:00
|
|
|
foreach ($subfleets as $subfleet) {
|
|
|
|
$tmp = [];
|
|
|
|
foreach ($subfleet->aircraft as $ac) {
|
|
|
|
$tmp[$ac->id] = $ac['name'] . ' - ' . $ac['registration'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$aircraft[$subfleet->name] = $tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $aircraft;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
* @throws \Prettus\Repository\Exceptions\RepositoryException
|
|
|
|
*/
|
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-05 00:34:01 +08:00
|
|
|
|
2018-01-03 23:54:32 +08:00
|
|
|
$where = [['user_id', $user->id]];
|
|
|
|
|
|
|
|
if(setting('pireps.hide_cancelled_pireps')) {
|
|
|
|
$where[] = ['state', '<>', PirepState::CANCELLED];
|
|
|
|
}
|
|
|
|
|
2017-12-05 00:34:01 +08:00
|
|
|
$this->pirepRepo->pushCriteria(new WhereCriteria($request, $where));
|
|
|
|
$pireps = $this->pirepRepo->orderBy('created_at', 'desc')->paginate();
|
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()
|
|
|
|
{
|
2018-01-11 09:40:20 +08:00
|
|
|
$user = Auth::user();
|
|
|
|
|
2017-08-17 07:18:47 +08:00
|
|
|
return $this->view('pireps.create', [
|
2018-01-24 05:48:30 +08:00
|
|
|
'airlines' => $this->airlineRepo->selectBoxList(true),
|
|
|
|
'aircraft' => $this->aircraftList($user, true),
|
|
|
|
'airports' => $this->airportRepo->selectBoxList(true),
|
|
|
|
'pirep_fields' => $this->pirepFieldRepo->all(),
|
|
|
|
'field_values' => [],
|
2017-08-17 07:18:47 +08:00
|
|
|
]);
|
2017-08-10 13:08:38 +08:00
|
|
|
}
|
|
|
|
|
2018-01-24 05:48:30 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param CreatePirepRequest $request
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
|
|
|
*/
|
|
|
|
public function store(CreatePirepRequest $request)
|
2017-08-10 13:08:38 +08:00
|
|
|
{
|
2017-08-24 03:19:05 +08:00
|
|
|
// Create the main PIREP
|
2018-01-24 05:48:30 +08:00
|
|
|
$pirep = new Pirep($request->all());
|
|
|
|
$pirep->user_id = Auth::user()->id;
|
|
|
|
|
|
|
|
# Make sure this isn't a duplicate
|
|
|
|
$dupe_pirep = $this->pirepSvc->findDuplicate($pirep);
|
|
|
|
if ($dupe_pirep !== false) {
|
|
|
|
flash()->error('This PIREP has already been filed.');
|
|
|
|
return redirect(route('frontend.pireps.create'))->withInput();
|
|
|
|
}
|
2017-08-24 03:19:05 +08:00
|
|
|
|
|
|
|
// Any special fields
|
2017-12-14 00:56:26 +08:00
|
|
|
$pirep->flight_time = ((int) Utils::hoursToMinutes($request['hours']))
|
|
|
|
+ ((int) $request['minutes']);
|
2017-08-24 03:19:05 +08:00
|
|
|
|
|
|
|
// The custom fields from the form
|
|
|
|
$custom_fields = [];
|
2018-01-24 05:48:30 +08:00
|
|
|
$pirep_fields = $this->pirepFieldRepo->all();
|
|
|
|
foreach ($pirep_fields as $field) {
|
|
|
|
if(!$request->filled($field->slug)) {
|
2017-08-24 03:19:05 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$custom_fields[] = [
|
2018-01-24 05:48:30 +08:00
|
|
|
'name' => $field->name,
|
|
|
|
'value' => $request->input($field->slug),
|
2017-12-20 10:19:36 +08:00
|
|
|
'source' => PirepSource::MANUAL
|
2017-08-24 03:19:05 +08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-01-24 05:48:30 +08:00
|
|
|
Log::info('PIREP Custom Fields', $custom_fields);
|
2017-12-04 05:29:34 +08:00
|
|
|
$pirep = $this->pirepSvc->create($pirep, $custom_fields);
|
2018-01-05 09:39:41 +08:00
|
|
|
$this->pirepSvc->saveRoute($pirep);
|
2017-08-10 13:08:38 +08:00
|
|
|
|
2018-01-24 05:48:30 +08:00
|
|
|
return redirect(route('frontend.pireps.show', ['id' => $pirep->id]));
|
2017-08-10 13:08:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function show($id)
|
|
|
|
{
|
2017-12-20 05:19:06 +08:00
|
|
|
#$pirep = Pirep::where('id', $id);
|
|
|
|
$pirep = $this->pirepRepo->find($id);
|
2017-12-21 01:28:21 +08:00
|
|
|
if (empty($pirep)) {
|
|
|
|
Flash::error('Pirep not found');
|
|
|
|
return redirect(route('frontend.pirep.index'));
|
|
|
|
}
|
|
|
|
|
2017-12-25 07:38:38 +08:00
|
|
|
$map_featuers = $this->geoSvc->pirepGeoJson($pirep);
|
2017-12-21 01:28:21 +08:00
|
|
|
|
2017-08-10 13:08:38 +08:00
|
|
|
return $this->view('pireps.show', [
|
|
|
|
'pirep' => $pirep,
|
2017-12-25 07:38:38 +08:00
|
|
|
'map_features' => $map_featuers,
|
2017-08-10 13:08:38 +08:00
|
|
|
]);
|
2017-08-09 09:34:36 +08:00
|
|
|
}
|
2017-08-04 23:04:00 +08:00
|
|
|
}
|