From 7a34756188ab193dd30468d413aa085514f7e8ea Mon Sep 17 00:00:00 2001 From: Nabeel S Date: Wed, 23 Oct 2019 12:01:31 -0400 Subject: [PATCH] Issue fixes (#413) * Auto lookup missing airports closes #404 * Ensure flight ICAOs are capitalized closes #404 * Update htaccess in root closes #412 * Update htaccess in root closes #412 * StyleCI fix --- .htaccess | 13 ++---- app/Database/seeds/settings.yml | 7 +++ .../Controllers/Admin/FlightController.php | 3 +- app/Models/Observers/FlightObserver.php | 29 ++++++++++++ app/Providers/AppServiceProvider.php | 3 ++ app/Services/AirportService.php | 44 +++++++++++++++++++ app/Services/FlightService.php | 6 +++ app/Services/ImportExport/FlightImporter.php | 11 +++-- app/Services/Installer/MigrationService.php | 5 +-- composer.lock | 2 +- index.php | 5 ++- public/.htaccess | 5 ++- .../views/admin/flights/fields.blade.php | 2 +- resources/views/admin/menu.blade.php | 2 +- 14 files changed, 113 insertions(+), 24 deletions(-) create mode 100644 app/Models/Observers/FlightObserver.php diff --git a/.htaccess b/.htaccess index b0112ed5..cef54daa 100755 --- a/.htaccess +++ b/.htaccess @@ -4,8 +4,8 @@ Options -Indexes RewriteEngine On # Handle Authorization Header -RewriteCond %{HTTP:Authorization} ^(.*) -RewriteRule .* - [e=HTTP_AUTHORIZATION:%1] +RewriteCond %{HTTP:Authorization} . +RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] # Deny all these files/folders RedirectMatch 403 ^/.git/.*?$ @@ -35,11 +35,4 @@ RedirectMatch 403 ^/phpunit.xml RedirectMatch 403 ^/webpack.mix.js RedirectMatch 403 ^/yarn.lock -# Redirect Trailing Slashes If Not A Folder... -RewriteCond %{REQUEST_FILENAME} !-d -RewriteRule ^(.*)/$ /$1 [L,R=301] - -# Handle Front Controller... -RewriteCond %{REQUEST_FILENAME} !-d -RewriteCond %{REQUEST_FILENAME} !-f -RewriteRule ^ index.php [L] +RewriteRule ^(.*)$ public/$1 [L] diff --git a/app/Database/seeds/settings.yml b/app/Database/seeds/settings.yml index 0d8c6659..3b30cf8b 100644 --- a/app/Database/seeds/settings.yml +++ b/app/Database/seeds/settings.yml @@ -12,6 +12,13 @@ options: '' type: text description: 'Email where system notices, etc are sent' +- key: general.auto_airport_lookup + name: 'Automatic airport lookup' + group: general + value: true + options: + type: boolean + description: If an airport isn't added, try to look it up when adding schedules - key: general.check_prerelease_version name: 'Pre-release versions in version check' group: general diff --git a/app/Http/Controllers/Admin/FlightController.php b/app/Http/Controllers/Admin/FlightController.php index 3b46ddbd..28a5eb72 100644 --- a/app/Http/Controllers/Admin/FlightController.php +++ b/app/Http/Controllers/Admin/FlightController.php @@ -175,6 +175,7 @@ class FlightController extends Controller return redirect(route('admin.flights.edit', $flight->id)); } catch (\Exception $e) { + Log::error($e); Flash::error($e->getMessage()); return redirect()->back()->withInput($request->all()); } @@ -258,8 +259,8 @@ class FlightController extends Controller return redirect(route('admin.flights.index')); } catch (\Exception $e) { + Log::error($e); Flash::error($e->getMessage()); - return redirect()->back()->withInput($request->all()); } } diff --git a/app/Models/Observers/FlightObserver.php b/app/Models/Observers/FlightObserver.php new file mode 100644 index 00000000..c593f584 --- /dev/null +++ b/app/Models/Observers/FlightObserver.php @@ -0,0 +1,29 @@ +dpt_airport_id = strtoupper(trim($flight->dpt_airport_id)); + $flight->arr_airport_id = strtoupper(trim($flight->arr_airport_id)); + } + + /** + * @param Flight $flight + */ + public function updating(Flight $flight): void + { + $flight->dpt_airport_id = strtoupper(trim($flight->dpt_airport_id)); + $flight->arr_airport_id = strtoupper(trim($flight->arr_airport_id)); + } +} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 7d718b8f..08de6322 100755 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -4,12 +4,14 @@ namespace App\Providers; use App\Models\Aircraft; use App\Models\Airport; +use App\Models\Flight; use App\Models\FlightField; use App\Models\FlightFieldValue; use App\Models\Journal; use App\Models\JournalTransaction; use App\Models\Observers\AircraftObserver; use App\Models\Observers\AirportObserver; +use App\Models\Observers\FlightObserver; use App\Models\Observers\JournalObserver; use App\Models\Observers\JournalTransactionObserver; use App\Models\Observers\SettingObserver; @@ -47,6 +49,7 @@ class AppServiceProvider extends ServiceProvider Journal::observe(JournalObserver::class); JournalTransaction::observe(JournalTransactionObserver::class); + Flight::observe(FlightObserver::class); FlightField::observe(Sluggable::class); FlightFieldValue::observe(Sluggable::class); diff --git a/app/Services/AirportService.php b/app/Services/AirportService.php index 2d644e4c..179fdc0e 100644 --- a/app/Services/AirportService.php +++ b/app/Services/AirportService.php @@ -6,6 +6,7 @@ use App\Contracts\AirportLookup as AirportLookupProvider; use App\Contracts\Metar as MetarProvider; use App\Contracts\Service; use App\Exceptions\AirportNotFound; +use App\Models\Airport; use App\Repositories\AirportRepository; use App\Support\Metar; use App\Support\Units\Distance; @@ -73,6 +74,8 @@ class AirportService extends Service return []; } + $airport = (array) $airport; + Cache::add( $key, $airport, @@ -82,6 +85,47 @@ class AirportService extends Service return $airport; } + /** + * Lookup an airport and save it if it hasn't been found + * + * @param string $icao + * + * @return \Illuminate\Database\Eloquent\Model|null + */ + public function lookupAirportIfNotFound($icao) + { + $icao = strtoupper($icao); + $airport = $this->airportRepo->findWithoutFail($icao); + if ($airport !== null) { + return $airport; + } + + // Don't lookup the airport, so just add in something generic + if (!setting('general.auto_airport_lookup')) { + $airport = new Airport([ + 'id' => $icao, + 'icao' => $icao, + 'name' => $icao, + 'lat' => 0, + 'lon' => 0, + ]); + + $airport->save(); + + return $airport; + } + + $lookup = $this->lookupAirport($icao); + if (empty($lookup)) { + return; + } + + $airport = new Airport($lookup); + $airport->save(); + + return $airport; + } + /** * Calculate the distance from one airport to another * diff --git a/app/Services/FlightService.php b/app/Services/FlightService.php index 5ca53650..d30cbc2f 100644 --- a/app/Services/FlightService.php +++ b/app/Services/FlightService.php @@ -53,11 +53,17 @@ class FlightService extends Service */ public function createFlight($fields) { + $fields['dpt_airport_id'] = strtoupper($fields['dpt_airport_id']); + $fields['arr_airport_id'] = strtoupper($fields['arr_airport_id']); + $flightTmp = new Flight($fields); if ($this->isFlightDuplicate($flightTmp)) { throw new DuplicateFlight($flightTmp); } + $this->airportSvc->lookupAirportIfNotFound($fields['dpt_airport_id']); + $this->airportSvc->lookupAirportIfNotFound($fields['arr_airport_id']); + $fields = $this->transformFlightFields($fields); $flight = $this->flightRepo->create($fields); diff --git a/app/Services/ImportExport/FlightImporter.php b/app/Services/ImportExport/FlightImporter.php index 3b08ecc0..6984101f 100644 --- a/app/Services/ImportExport/FlightImporter.php +++ b/app/Services/ImportExport/FlightImporter.php @@ -9,6 +9,7 @@ use App\Models\Enums\FlightType; use App\Models\Fare; use App\Models\Flight; use App\Models\Subfleet; +use App\Services\AirportService; use App\Services\FareService; use App\Services\FlightService; use Log; @@ -47,8 +48,8 @@ class FlightImporter extends ImportExport 'fields' => 'nullable', ]; + private $airportSvc; private $fareSvc; - private $flightSvc; /** @@ -56,6 +57,7 @@ class FlightImporter extends ImportExport */ public function __construct() { + $this->airportSvc = app(AirportService::class); $this->fareSvc = app(FareService::class); $this->flightSvc = app(FlightService::class); } @@ -94,6 +96,9 @@ class FlightImporter extends ImportExport 'route_leg' => $row['route_leg'], ], $row); + $row['dpt_airport'] = strtoupper($row['dpt_airport']); + $row['arr_airport'] = strtoupper($row['arr_airport']); + // Airport atttributes $flight->setAttribute('days', $this->setDays($row['days'])); $flight->setAttribute('dpt_airport_id', $row['dpt_airport']); @@ -189,9 +194,7 @@ class FlightImporter extends ImportExport */ protected function processAirport($airport) { - return Airport::firstOrCreate([ - 'id' => $airport, - ], ['icao' => $airport, 'name' => $airport]); + return $this->airportSvc->lookupAirportIfNotFound($airport); } /** diff --git a/app/Services/Installer/MigrationService.php b/app/Services/Installer/MigrationService.php index 76ab2859..5a85d9fb 100644 --- a/app/Services/Installer/MigrationService.php +++ b/app/Services/Installer/MigrationService.php @@ -5,7 +5,6 @@ namespace App\Services\Installer; use App\Contracts\Service; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Artisan; -use Illuminate\Support\Facades\Log; use Nwidart\Modules\Facades\Module; class MigrationService extends Service @@ -37,7 +36,7 @@ class MigrationService extends Service } } - Log::info('Update - migration paths', $paths); + // Log::info('Update - migration paths', $paths); return $paths; } @@ -53,7 +52,7 @@ class MigrationService extends Service $files = $migrator->getMigrationFiles(array_values($migration_dirs)); $availMigrations = array_diff(array_keys($files), $migrator->getRepository()->getRan()); - Log::info('Migrations available:', $availMigrations); + // Log::info('Migrations available:', $availMigrations); return $availMigrations; } diff --git a/composer.lock b/composer.lock index dfda1a71..5af78180 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "60c8d4678763253ee0fa8711dce01cfb", + "content-hash": "65a0339b4ba756f5d21382236bdcc42b", "packages": [ { "name": "akaunting/money", diff --git a/index.php b/index.php index 8886dbb6..417ecec5 100755 --- a/index.php +++ b/index.php @@ -10,7 +10,10 @@ require $path_to_phpvms_folder.'/bootstrap/autoload.php'; $app = require_once $path_to_phpvms_folder.'/bootstrap/app.php'; $app->setPublicPath(__DIR__.'/public'); -$app->setPublicUrlPath(env('APP_PUBLIC_URL', '/public')); +$app->setPublicUrlPath(env('APP_PUBLIC_URL', '/')); + +// Uncomment this line if you're having issues with the redirecting not working properly +//$app->setPublicUrlPath(env('APP_PUBLIC_URL', '/public')); $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class); diff --git a/public/.htaccess b/public/.htaccess index 969eb19f..5bd23954 100755 --- a/public/.htaccess +++ b/public/.htaccess @@ -8,8 +8,9 @@ Options -Indexes RewriteEngine On - RewriteCond %{HTTP:Authorization} ^(.*) - RewriteRule .* - [e=HTTP_AUTHORIZATION:%1] + # Handle Authorization Header + RewriteCond %{HTTP:Authorization} + RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] # Redirect Trailing Slashes If Not A Folder... RewriteCond %{REQUEST_FILENAME} !-d diff --git a/resources/views/admin/flights/fields.blade.php b/resources/views/admin/flights/fields.blade.php index f9cfdae9..b6475e3b 100644 --- a/resources/views/admin/flights/fields.blade.php +++ b/resources/views/admin/flights/fields.blade.php @@ -82,7 +82,7 @@ class="required">* {{ Form::select('arr_airport_id', $airports, null , [ 'id' => 'arr_airport_id', - 'class' => 'form-control select2' + 'class' => 'form-control select2 select2' ]) }}

{{ $errors->first('arr_airport_id') }}

diff --git a/resources/views/admin/menu.blade.php b/resources/views/admin/menu.blade.php index 92508324..46725061 100644 --- a/resources/views/admin/menu.blade.php +++ b/resources/views/admin/menu.blade.php @@ -76,7 +76,7 @@ @endability @ability('admin', 'maintenance') -
  • maintenance
  • +
  • maintenance
  • @endability