phpvms/app/Http/Controllers/Frontend/SimBriefController.php
B.Fatih KOZ 984c1e0cec
SimBrief : Generate New Briefing (#986)
* Add SB Briefing Removal Route

Route will be called from simbrief.briefing.blade

* SimBrief Controller and Briefing Blade Update

Added the remove function to controller, which removes the simbrief ofp if no pirep_id is present, else it just nulls the flight_id for to be able to create a new SB pack

Also added the button to blade for it to work

* Style Fix

love styleci
2021-01-15 17:36:46 -05:00

190 lines
5.2 KiB
PHP

<?php
namespace App\Http\Controllers\Frontend;
use App\Exceptions\AssetNotFound;
use App\Models\SimBrief;
use App\Repositories\FlightRepository;
use App\Services\SimBriefService;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class SimBriefController
{
private $flightRepo;
private $simBriefSvc;
public function __construct(FlightRepository $flightRepo, SimBriefService $simBriefSvc)
{
$this->flightRepo = $flightRepo;
$this->simBriefSvc = $simBriefSvc;
}
/**
* Show the main OFP form
*
* @param Request $request
*
* @throws \Exception
*
* @return mixed
*/
public function generate(Request $request)
{
$flight_id = $request->input('flight_id');
$flight = $this->flightRepo->find($flight_id);
if (!$flight) {
flash()->error('Unknown flight');
return redirect(route('frontend.flights.index'));
}
$apiKey = setting('simbrief.api_key');
if (empty($apiKey)) {
flash()->error('Invalid SimBrief API key!');
return redirect(route('frontend.flights.index'));
}
$user = Auth::user();
$simbrief = SimBrief::select('id')->where([
'flight_id' => $flight_id,
'user_id' => $user->id,
])->first();
if ($simbrief) {
return redirect(route('frontend.simbrief.briefing', [$simbrief->id]));
}
return view('flights.simbrief_form', [
'flight' => $flight,
]);
}
/**
* Show the briefing
*
* @param string $id The OFP ID
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
*/
public function briefing($id)
{
$simbrief = SimBrief::find($id);
if (!$simbrief) {
flash()->error('SimBrief briefing not found');
return redirect(route('frontend.flights.index'));
}
return view('flights.simbrief_briefing', [
'simbrief' => $simbrief,
]);
}
/**
* Remove the flight_id from the SimBrief Briefing (to a create a new one)
* or if no pirep_id is attached to the briefing delete it completely
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function remove(Request $request)
{
$sb_pack = SimBrief::find($request->id);
if ($sb_pack) {
if (!$sb_pack->pirep_id) {
$sb_pack->delete();
} else {
$sb_pack->flight_id = null;
$sb_pack->save();
}
}
return redirect(route('frontend.flights.index'));
}
/**
* Create a prefile of this PIREP with a given OFP. Then redirect the
* user to the newly prefiled PIREP
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function prefile(Request $request)
{
$sb = SimBrief::find($request->id);
if (!$sb) {
return redirect(route('frontend.flights.index'));
}
// Redirect to the prefile page, with the flight_id and a simbrief_id
$rd = route('frontend.pireps.create').'?sb_id='.$sb->id;
return redirect($rd);
}
/**
* Cancel the SimBrief request
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function cancel(Request $request)
{
$sb = SimBrief::find($request->id);
if (!$sb) {
$sb->delete();
}
return redirect(route('frontend.simbrief.prefile', ['id' => $request->id]));
}
/**
* Check whether the OFP was generated. Pass in two items, the flight_id and ofp_id
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\JsonResponse
*/
public function check_ofp(Request $request)
{
$ofp_id = $request->input('ofp_id');
$flight_id = $request->input('flight_id');
$simbrief = $this->simBriefSvc->checkForOfp(Auth::user()->id, $ofp_id, $flight_id);
if ($simbrief === null) {
$error = new AssetNotFound(new Exception('Simbrief OFP not found'));
return $error->getResponse();
}
return response()->json([
'id' => $simbrief->id,
]);
}
/**
* Generate the API code
*
* @param \Illuminate\Http\Request $request
*
* @throws \Exception
*
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function api_code(Request $request)
{
$apiKey = setting('simbrief.api_key', null);
if (empty($apiKey)) {
flash()->error('Invalid SimBrief API key!');
return redirect(route('frontend.flights.index'));
}
$api_code = md5($apiKey.$request->input('api_req'));
return response()->json([
'api_code' => $api_code,
]);
}
}