phpvms/app/Http/Controllers/Frontend/PirepController.php

185 lines
5.2 KiB
PHP
Raw Normal View History

2017-08-04 23:04:00 +08:00
<?php
namespace App\Http\Controllers\Frontend;
use Log;
use App\Facades\Utils;
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
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
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;
use App\Repositories\AirportRepository;
2017-08-17 07:18:47 +08:00
use App\Repositories\PirepRepository;
use App\Repositories\PirepFieldRepository;
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
{
private $airlineRepo,
$pirepRepo,
$airportRepo,
2017-12-04 05:29:34 +08:00
$pirepFieldRepo,
$geoSvc,
$pirepSvc,
$subfleetRepo,
$userSvc;
2017-08-17 07:18:47 +08:00
public function __construct(
AirlineRepository $airlineRepo,
PirepRepository $pirepRepo,
AirportRepository $airportRepo,
2017-12-04 05:29:34 +08:00
PirepFieldRepository $pirepFieldRepo,
GeoService $geoSvc,
SubfleetRepository $subfleetRepo,
PIREPService $pirepSvc,
UserService $userSvc
) {
2017-08-17 07:18:47 +08:00
$this->airlineRepo = $airlineRepo;
$this->pirepRepo = $pirepRepo;
$this->airportRepo = $airportRepo;
$this->subfleetRepo = $subfleetRepo;
$this->pirepFieldRepo = $pirepFieldRepo;
$this->geoSvc = $geoSvc;
2017-12-04 05:29:34 +08:00
$this->pirepSvc = $pirepSvc;
$this->userSvc = $userSvc;
2017-08-17 07:18:47 +08:00
}
/**
* Dropdown with aircraft grouped by subfleet
* @param null $user
* @return array
*/
public function aircraftList($user=null, $add_blank=false)
{
$aircraft = [];
$subfleets = $this->userSvc->getAllowableSubfleets($user);
if($add_blank) {
$aircraft[''] = '';
}
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();
2018-01-03 23:54:32 +08:00
$where = [['user_id', $user->id]];
if(setting('pireps.hide_cancelled_pireps')) {
$where[] = ['state', '<>', PirepState::CANCELLED];
}
$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()
{
$user = Auth::user();
2017-08-17 07:18:47 +08:00
return $this->view('pireps.create', [
'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
}
/**
*
* @param CreatePirepRequest $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function store(CreatePirepRequest $request)
2017-08-10 13:08:38 +08:00
{
// Create the main PIREP
$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();
}
// Any special fields
$pirep->flight_time = ((int) Utils::hoursToMinutes($request['hours']))
+ ((int) $request['minutes']);
// The custom fields from the form
$custom_fields = [];
$pirep_fields = $this->pirepFieldRepo->all();
foreach ($pirep_fields as $field) {
if(!$request->filled($field->slug)) {
continue;
}
$custom_fields[] = [
'name' => $field->name,
'value' => $request->input($field->slug),
'source' => PirepSource::MANUAL
];
}
Log::info('PIREP Custom Fields', $custom_fields);
2017-12-04 05:29:34 +08:00
$pirep = $this->pirepSvc->create($pirep, $custom_fields);
$this->pirepSvc->saveRoute($pirep);
2017-08-10 13:08:38 +08:00
return redirect(route('frontend.pireps.show', ['id' => $pirep->id]));
2017-08-10 13:08:38 +08:00
}
public function show($id)
{
#$pirep = Pirep::where('id', $id);
$pirep = $this->pirepRepo->find($id);
if (empty($pirep)) {
Flash::error('Pirep not found');
return redirect(route('frontend.pirep.index'));
}
$map_featuers = $this->geoSvc->pirepGeoJson($pirep);
2017-08-10 13:08:38 +08:00
return $this->view('pireps.show', [
'pirep' => $pirep,
'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
}