From 186bbca428a3af124408df21379107dd2fe55e33 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Wed, 10 Jan 2018 19:40:20 -0600 Subject: [PATCH] Restrict the aircraft to only show the user's #138 --- Makefile | 2 +- app/Database/seeds/sample.yml | 17 +++--- .../Controllers/Admin/PirepController.php | 45 ++++++++++---- .../Controllers/Frontend/PirepController.php | 61 +++++++++++++++---- 4 files changed, 93 insertions(+), 32 deletions(-) diff --git a/Makefile b/Makefile index 7e2fbd67..75fd7743 100644 --- a/Makefile +++ b/Makefile @@ -62,7 +62,7 @@ reload-db: @php artisan database:create --reset @php artisan migrate:refresh --seed @php artisan phpvms:import app/Database/seeds/sample.yml - @php artisan phpvms:navdata + #php artisan phpvms:navdata .PHONY: tests tests: test diff --git a/app/Database/seeds/sample.yml b/app/Database/seeds/sample.yml index 42224afd..3759d5f3 100644 --- a/app/Database/seeds/sample.yml +++ b/app/Database/seeds/sample.yml @@ -190,14 +190,11 @@ aircraft: name: Boeing 777-200 registration: NC20 tail_number: 20 - -#aircraft_rank: -# - aircraft_id: 1 -# rank_id: 1 -# - aircraft_id: 1 -# rank_id: 2 -# acars_pay: 100 -# manual_pay: 50 + - id: 3 + subfleet_id: 1 + name: Boeing 747-412 + registration: NS233 + tail_number: 1233 fares: - id: 1 @@ -253,6 +250,10 @@ subfleet_flight: - subfleet_id: 1 flight_id: flightid_1 +subfleet_rank: + - rank_id: 1 + subfleet_id: 1 + flights: - id: flightid_1 airline_id: 1 diff --git a/app/Http/Controllers/Admin/PirepController.php b/app/Http/Controllers/Admin/PirepController.php index c8ac642d..9bfc40cd 100644 --- a/app/Http/Controllers/Admin/PirepController.php +++ b/app/Http/Controllers/Admin/PirepController.php @@ -16,10 +16,13 @@ use App\Models\Enums\PirepState; use App\Http\Requests\CreatePirepRequest; use App\Http\Requests\UpdatePirepRequest; + use App\Repositories\AircraftRepository; use App\Repositories\AirlineRepository; use App\Repositories\AirportRepository; use App\Repositories\PirepRepository; +use App\Repositories\SubfleetRepository; + use App\Facades\Utils; @@ -27,34 +30,50 @@ class PirepController extends BaseController { private $airportRepo, $airlineRepo, - $pirepRepo, $aircraftRepo, - $pirepSvc; + $pirepSvc, + $pirepRepo, + $subfleetRepo; public function __construct( AirportRepository $airportRepo, AirlineRepository $airlineRepo, AircraftRepository $aircraftRepo, PirepRepository $pirepRepo, - PIREPService $pirepSvc + PIREPService $pirepSvc, + SubfleetRepository $subfleetRepo ) { $this->airportRepo = $airportRepo; $this->airlineRepo = $airlineRepo; $this->aircraftRepo = $aircraftRepo; $this->pirepRepo = $pirepRepo; $this->pirepSvc = $pirepSvc; + $this->subfleetRepo = $subfleetRepo; } - public function aircraftList() + /** + * Dropdown with aircraft grouped by subfleet + */ + public function aircraftList($user=null) { - $retval = []; - $all_aircraft = $this->aircraftRepo->all(); + $aircraft = []; - foreach ($all_aircraft as $ac) { - $retval[$ac->id] = $ac->subfleet->name.' - '.$ac->name.' ('.$ac->registration.')'; + if($user === null) { + $subfleets = $this->subfleetRepo->all(); + } else { + $subfleets = $this->userSvc->getAllowableSubfleets($user); } - return $retval; + foreach ($subfleets as $subfleet) { + $tmp = []; + foreach ($subfleet->aircraft as $ac) { + $tmp[$ac->id] = $ac['name'] . ' - ' . $ac['registration']; + } + + $aircraft[$subfleet->name] = $tmp; + } + + return $aircraft; } /** @@ -102,7 +121,11 @@ class PirepController extends BaseController */ public function create() { - return view('admin.pireps.create'); + return view('admin.pireps.create', [ + 'aircraft' => $this->aircraftList(), + 'airports' => $this->airportRepo->selectBoxList(), + 'airlines' => $this->airlineRepo->selectBoxList(), + ]); } /** @@ -160,9 +183,9 @@ class PirepController extends BaseController return view('admin.pireps.edit', [ 'pirep' => $pirep, + 'aircraft' => $this->aircraftList(), 'airports' => $this->airportRepo->selectBoxList(), 'airlines' => $this->airlineRepo->selectBoxList(), - 'aircraft' => $this->aircraftRepo->selectBoxList(), ]); } diff --git a/app/Http/Controllers/Frontend/PirepController.php b/app/Http/Controllers/Frontend/PirepController.php index c89f93f9..404139f1 100644 --- a/app/Http/Controllers/Frontend/PirepController.php +++ b/app/Http/Controllers/Frontend/PirepController.php @@ -3,54 +3,89 @@ namespace App\Http\Controllers\Frontend; use App\Facades\Utils; -use App\Models\Enums\PirepSource; -use App\Models\Enums\PirepState; -use App\Repositories\Criteria\WhereCriteria; -use App\Services\GeoService; -use App\Services\PIREPService; use Illuminate\Support\Facades\Auth; use Illuminate\Http\Request; +use App\Models\Enums\PirepSource; +use App\Models\Enums\PirepState; use App\Models\Pirep; use App\Models\PirepField; +use App\Services\GeoService; +use App\Services\PIREPService; +use App\Services\UserService; + +use App\Repositories\Criteria\WhereCriteria; use App\Http\Controllers\Controller; use App\Repositories\AirlineRepository; -use App\Repositories\AircraftRepository; use App\Repositories\AirportRepository; use App\Repositories\PirepRepository; use App\Repositories\PirepFieldRepository; +use App\Repositories\SubfleetRepository; class PirepController extends Controller { private $airlineRepo, - $aircraftRepo, $pirepRepo, $airportRepo, $pirepFieldRepo, $geoSvc, - $pirepSvc; + $pirepSvc, + $subfleetRepo, + $userSvc; public function __construct( AirlineRepository $airlineRepo, PirepRepository $pirepRepo, - AircraftRepository $aircraftRepo, AirportRepository $airportRepo, PirepFieldRepository $pirepFieldRepo, GeoService $geoSvc, - PIREPService $pirepSvc + SubfleetRepository $subfleetRepo, + PIREPService $pirepSvc, + UserService $userSvc ) { $this->airlineRepo = $airlineRepo; - $this->aircraftRepo = $aircraftRepo; $this->pirepRepo = $pirepRepo; $this->airportRepo = $airportRepo; + $this->subfleetRepo = $subfleetRepo; $this->pirepFieldRepo = $pirepFieldRepo; $this->geoSvc = $geoSvc; $this->pirepSvc = $pirepSvc; + $this->userSvc = $userSvc; } + /** + * Dropdown with aircraft grouped by subfleet + */ + public function aircraftList($user=null) + { + $aircraft = []; + + if ($user === null) { + $subfleets = $this->subfleetRepo->all(); + } else { + $subfleets = $this->userSvc->getAllowableSubfleets($user); + } + + 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 + */ public function index(Request $request) { $user = Auth::user(); @@ -72,10 +107,12 @@ class PirepController extends Controller public function create() { + $user = Auth::user(); + return $this->view('pireps.create', [ + 'aircraft' => $this->aircraftList($user), 'airports' => $this->airportRepo->selectBoxList(), 'airlines' => $this->airlineRepo->selectBoxList(), - 'aircraft' => $this->aircraftRepo->selectBoxList(), 'pirepfields' => $this->pirepFieldRepo->all(), 'fieldvalues' => [], ]);