Move the settings into a yml seed file and sync during install/update

This commit is contained in:
Nabeel Shahzad 2018-09-02 13:18:32 -04:00
parent 1c1c0d55f4
commit f14f7c1a5e
7 changed files with 332 additions and 368 deletions

View File

@ -5,6 +5,13 @@ use Illuminate\Database\Schema\Blueprint;
class CreateSettingsTable extends Migration
{
private $migrationSvc;
public function __construct()
{
$this->migrationSvc = new \Modules\Installer\Services\MigrationService();
}
/**
* Run the migrations.
*
@ -30,260 +37,7 @@ class CreateSettingsTable extends Migration
$table->timestamps();
});
/*
* Initial default settings
*/
$this->addSetting('general.start_date', [
'name' => 'Start Date',
'group' => 'general',
'value' => '',
'type' => 'date',
'description' => 'The date your VA started',
]);
$this->addSetting('general.admin_email', [
'name' => 'Admin Email',
'group' => 'general',
'value' => '',
'type' => 'text',
'description' => 'Email where notices, etc are sent',
]);
/*$this->addSetting('general.currency', [
'name' => 'Currency to Use',
'group' => 'general',
'value' => 'USD',
'type' => 'select',
'options' => 'USD,EUR,GBP,JPY,RUB',
'description' => 'Currency to use. NOTE: If you change this, then current amounts won\'t be converted',
]);*/
$this->addSetting('units.distance', [
'name' => 'Distance Units',
'group' => 'units',
'value' => 'nmi',
'type' => 'select',
'options' => 'km=kilometers,mi=miles,nmi=nautical miles',
'description' => 'The distance unit for display',
]);
$this->addSetting('units.weight', [
'name' => 'Weight Units',
'group' => 'units',
'value' => 'lbs',
'type' => 'select',
'options' => 'lbs,kg',
'description' => 'The weight unit for display',
]);
$this->addSetting('units.speed', [
'name' => 'Speed Units',
'group' => 'units',
'value' => 'knot',
'type' => 'select',
'options' => 'km/h,knot',
'description' => 'The speed unit for display',
]);
$this->addSetting('units.altitude', [
'name' => 'Altitude Units',
'group' => 'units',
'value' => 'ft',
'type' => 'select',
'options' => 'ft=feet,m=meters',
'description' => 'The altitude unit for display',
]);
$this->addSetting('units.fuel', [
'name' => 'Fuel Units',
'group' => 'units',
'value' => 'lbs',
'type' => 'select',
'options' => 'lbs,kg',
'description' => 'The units for fuel for display',
]);
$this->addSetting('units.volume', [
'name' => 'Volume Units',
'group' => 'units',
'value' => 'gallons',
'type' => 'select',
'options' => 'gallons,l=liters',
'description' => 'The units for fuel for display',
]);
$this->addSetting('units.temperature', [
'name' => 'Temperature Units',
'group' => 'units',
'value' => 'F',
'type' => 'select',
'options' => 'F=Fahrenheit,C=Celsius',
'description' => 'The units for temperature',
]);
/*
* ACARS Settings
*/
$this->addSetting('acars.live_time', [
'name' => 'Live Time',
'group' => 'acars',
'value' => 12,
'type' => 'int',
'description' => 'Age of flights to show on the map in hours. '
.'Set to 0 to show only all in-progress flights',
]);
$this->addSetting('acars.center_coords', [
'name' => 'Center Coords',
'group' => 'acars',
'value' => '30.1945,-97.6699',
'type' => 'text',
'description' => 'Where to center the map; enter as LAT,LON',
]);
$this->addSetting('acars.default_zoom', [
'name' => 'Default Zoom',
'group' => 'acars',
'value' => 5,
'type' => 'int',
'description' => 'Initial zoom level on the map',
]);
/*
* BIDS
*/
$this->addSetting('bids.disable_flight_on_bid', [
'name' => 'Disable flight on bid',
'group' => 'bids',
'value' => true,
'type' => 'boolean',
'description' => 'When a flight is bid on, no one else can bid on it',
]);
$this->addSetting('bids.allow_multiple_bids', [
'name' => 'Allow multiple bids',
'group' => 'bids',
'value' => true,
'type' => 'boolean',
'description' => 'Whether or not someone can bid on multiple flights',
]);
$this->addSetting('bids.expire_time', [
'name' => 'Expire Time',
'group' => 'bids',
'value' => 48,
'type' => 'int',
'description' => 'Number of hours to expire bids after',
]);
/*
* PIREPS
*/
$this->addSetting('pireps.duplicate_check_time', [
'name' => 'PIREP duplicate time check',
'group' => 'pireps',
'value' => 10,
'type' => 'int',
'description' => 'The time in minutes to check for a duplicate PIREP',
]);
/*$this->addSetting('pireps.hide_cancelled_pireps', [
'name' => 'Hide Cancelled PIREPs',
'group' => 'pireps',
'value' => true,
'type' => 'boolean',
'description' => 'Hide any cancelled PIREPs in the front-end',
]);*/
$this->addSetting('pireps.restrict_aircraft_to_rank', [
'name' => 'Restrict Aircraft to Ranks',
'group' => 'pireps',
'value' => true,
'type' => 'boolean',
'description' => 'Aircraft that can be flown are restricted to a user\'s rank',
]);
$this->addSetting('pireps.only_aircraft_at_dpt_airport', [
'name' => 'Restrict Aircraft At Departure',
'group' => 'pireps',
'value' => false,
'type' => 'boolean',
'description' => 'Only allow aircraft that are at the departure airport',
]);
$this->addSetting('pireps.remove_bid_on_accept', [
'name' => 'Remove bid on accept',
'group' => 'pireps',
'value' => false,
'type' => 'boolean',
'description' => 'When a PIREP is accepted, remove the bid, if it exists',
]);
/*
* PILOTS
*/
$this->addSetting('pilots.id_length', [
'name' => 'Pilot ID Length',
'group' => 'pilots',
'value' => 4,
'default' => 4,
'type' => 'int',
'description' => 'The length of a pilot\'s ID',
]);
$this->addSetting('pilots.auto_accept', [
'name' => 'Auto Accept New Pilot',
'group' => 'pilots',
'value' => true,
'type' => 'boolean',
'description' => 'Automatically accept a pilot when they register',
]);
$this->addSetting('pilots.home_hubs_only', [
'name' => 'Hubs as home airport',
'group' => 'pilots',
'value' => false,
'type' => 'boolean',
'description' => 'Pilots can only select hubs as their home airport',
]);
$this->addSetting('pilots.only_flights_from_current', [
'name' => 'Flights from Current',
'group' => 'pilots',
'value' => false,
'type' => 'boolean',
'description' => 'Only show/allow flights from their current location',
]);
$this->addSetting('pilots.auto_leave_days', [
'name' => 'Pilot to ON LEAVE days',
'group' => 'pilots',
'value' => 30,
'default' => 30,
'type' => 'int',
'description' => 'Automatically set a pilot to ON LEAVE status after N days of no activity',
]);
$this->addSetting('pilots.hide_inactive', [
'name' => 'Hide Inactive Pilots',
'group' => 'pilots',
'value' => true,
'type' => 'boolean',
'description' => 'Don\'t show inactive pilots in the public view',
]);
$this->addSetting('pilots.restrict_to_company', [
'name' => 'Restrict the flights to company',
'group' => 'pilots',
'value' => false,
'type' => 'boolean',
'description' => 'Restrict flights to the user\'s airline',
]);
$this->migrationSvc->updateAllSettings();
}
/**

View File

@ -1,5 +1,6 @@
<?php
use App\Services\DatabaseService;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
@ -33,7 +34,7 @@ class DatabaseSeeder extends Seeder
$path = database_path('seeds/prod.yml');
}
$svc = app('App\Services\DatabaseService');
$svc = app(DatabaseService::class);
$svc->seed_from_yaml_file($path);
}
}

View File

@ -0,0 +1,182 @@
- key: general.start_date
name: 'Start Date'
group: general
value: ''
options: ''
type: date
description: 'The date your VA started'
- key: general.admin_email
name: 'Admin Email'
group: general
value: ''
options: ''
type: text
description: 'Email where notices, etc are sent'
- key: units.distance
name: 'Distance Units'
group: units
value: nmi
options: 'km=kilometers,mi=miles,nmi=nautical miles'
type: select
description: 'The distance unit for display'
- key: units.weight
name: 'Weight Units'
group: units
value: lbs
options: 'lbs,kg'
type: select
description: 'The weight unit for display'
- key: units.speed
name: 'Speed Units'
group: units
value: knot
options: 'km/h,knot'
type: select
description: 'The speed unit for display'
- key: units.altitude
name: 'Altitude Units'
group: units
value: ft
options: 'ft=feet,m=meters'
type: select
description: 'The altitude unit for display'
- key: units.fuel
name: 'Fuel Units'
group: units
value: lbs
options: 'lbs,kg'
type: select
description: 'The units for fuel for display'
- key: units.volume
name: 'Volume Units'
group: units
value: gallons
options: 'gallons,l=liters'
type: select
description: 'The units for fuel for display'
- key: units.temperature
name: 'Temperature Units'
group: units
value: F
options: 'F=Fahrenheit,C=Celsius'
type: select
description: 'The units for temperature'
- key: acars.live_time
name: 'Live Time'
group: acars
value: 12
options: ''
type: int
description: 'Age of flights to show on the map in hours. Set to 0 to show only all in-progress flights'
- key: acars.center_coords
name: 'Center Coords'
group: acars
value: '30.1945,-97.6699'
options: ''
type: text
description: 'Where to center the map; enter as LAT,LON'
- key: acars.default_zoom
name: 'Default Zoom'
group: acars
value: 5
options: ''
type: int
description: 'Initial zoom level on the map'
- key: bids.disable_flight_on_bid
name: 'Disable flight on bid'
group: bids
value: true
options: ''
type: boolean
description: 'When a flight is bid on, no one else can bid on it'
- key: bids.allow_multiple_bids
name: 'Allow multiple bids'
group: bids
value: true
options: ''
type: boolean
description: 'Whether or not someone can bid on multiple flights'
- key: bids.expire_time
name: 'Expire Time'
group: bids
value: 48
options: ''
type: int
description: 'Number of hours to expire bids after'
- key: pireps.duplicate_check_time
name: 'PIREP duplicate time check'
group: pireps
value: 10
options: ''
type: int
description: 'The time in minutes to check for a duplicate PIREP'
- key: pireps.restrict_aircraft_to_rank
name: 'Restrict Aircraft to Ranks'
group: pireps
value: true
options: ''
type: boolean
description: 'Aircraft that can be flown are restricted to a user''s rank'
- key: pireps.only_aircraft_at_dpt_airport
name: 'Restrict Aircraft At Departure'
group: pireps
value: false
options: ''
type: boolean
description: 'Only allow aircraft that are at the departure airport'
- key: pireps.remove_bid_on_accept
name: 'Remove bid on accept'
group: pireps
value: false
options: ''
type: boolean
description: 'When a PIREP is accepted, remove the bid, if it exists'
- key: pilots.id_length
name: 'Pilot ID Length'
group: pilots
value: 4
options: ''
type: int
description: 'The length of a pilot''s ID'
- key: pilots.auto_accept
name: 'Auto Accept New Pilot'
group: pilots
value: true
options: ''
type: boolean
description: 'Automatically accept a pilot when they register'
- key: pilots.home_hubs_only
name: 'Hubs as home airport'
group: pilots
value: false
options: ''
type: boolean
description: 'Pilots can only select hubs as their home airport'
- key: pilots.only_flights_from_current
name: 'Flights from Current'
group: pilots
value: false
options: ''
type: boolean
description: 'Only show/allow flights from their current location'
- key: pilots.auto_leave_days
name: 'Pilot to ON LEAVE days'
group: pilots
value: 30
options: ''
type: int
description: 'Automatically set a pilot to ON LEAVE status after N days of no activity'
- key: pilots.hide_inactive
name: 'Hide Inactive Pilots'
group: pilots
value: true
options: ''
type: boolean
description: 'Don''t show inactive pilots in the public view'
- key: pilots.restrict_to_company
name: 'Restrict the flights to company'
group: pilots
value: false
options: ''
type: boolean
description: 'Restrict flights to the user''s airline'

View File

@ -2,7 +2,6 @@
namespace App\Interfaces;
use App\Models\Setting;
use DB;
/**
@ -10,9 +9,6 @@ use DB;
*/
abstract class Migration extends \Illuminate\Database\Migrations\Migration
{
protected $counters = [];
protected $offsets = [];
/**
* At a minimum, this function needs to be implemented
*
@ -27,108 +23,6 @@ abstract class Migration extends \Illuminate\Database\Migrations\Migration
{
}
/**
* Dynamically figure out the offset and the start number for a group.
* This way we don't need to mess with how to order things
* When calling getNextOrderNumber(users) 31, will be returned, then 32, and so on
*
* @param $name
* @param null $offset
* @param int $start_offset
*/
protected function addCounterGroup($name, $offset = null, $start_offset = 0)
{
if ($offset === null) {
$group = DB::table('settings')
->where('group', $name)
->first();
if ($group === null) {
$offset = (int) DB::table('settings')->max('offset');
if ($offset === null) {
$offset = 0;
$start_offset = 1;
} else {
$offset += 100;
$start_offset = $offset + 1;
}
} else {
// Now find the number to start from
$start_offset = (int) DB::table('settings')->where('group', $name)->max('order');
if ($start_offset === null) {
$start_offset = $offset + 1;
} else {
$start_offset++;
}
$offset = $group->offset;
}
}
$this->counters[$name] = $start_offset;
$this->offsets[$name] = $offset;
}
/**
* Get the next increment number from a group
*
* @param $group
*
* @return int
*/
public function getNextOrderNumber($group): int
{
if (!\in_array($group, $this->counters, true)) {
$this->addCounterGroup($group);
}
$idx = $this->counters[$group];
$this->counters[$group]++;
return $idx;
}
/**
* @param $key
* @param $attrs
*/
public function addSetting($key, $attrs)
{
$group = $attrs['group'];
$order = $this->getNextOrderNumber($group);
$attrs = array_merge([
'id' => Setting::formatKey($key),
'key' => $key,
'offset' => $this->offsets[$group],
'order' => $order,
'name' => '',
'group' => $group,
'value' => '',
'default' => '',
'options' => '',
'type' => 'hidden',
'description' => '',
], $attrs);
return $this->addData('settings', [$attrs]);
}
/**
* Update a setting
*
* @param $key
* @param $value
* @param array $attrs
*/
public function updateSetting($key, $value, array $attrs = [])
{
$attrs['value'] = $value;
DB::table('settings')
->where('id', Setting::formatKey($key))
->update($attrs);
}
/**
* Add rows to a table
*

View File

@ -41,11 +41,10 @@ class UpdaterController extends Controller
{
$migrations = $this->migrationSvc->migrationsAvailable();
if(\count($migrations) > 0) {
return view('installer::update/steps/step1-update-available');
Log::info('No migrations found');
}
Log::info('No migrations found');
return view('installer::update/steps/step1-no-update');
return view('installer::update/steps/step1-update-available');
}
/**
@ -57,9 +56,12 @@ class UpdaterController extends Controller
{
Log::info('Update: run_migrations', $request->post());
// Resync all of the settings
$this->migrationSvc->updateAllSettings();
$migrations = $this->migrationSvc->migrationsAvailable();
if(\count($migrations) === 0) {
return redirect(route('update.complete'));
return view('installer::update/steps/step3-update-complete');
}
$output = $this->migrationSvc->runAllMigrations();

View File

@ -0,0 +1,13 @@
@extends('installer::app')
@section('title', 'Update Completed')
@section('content')
<h2>phpvms updater</h2>
<p>Update completed!.</p>
{{ Form::open(['route' => 'update.complete', 'method' => 'GET']) }}
<p style="text-align: right">
{{ Form::submit('Finish >>', ['class' => 'btn btn-success']) }}
</p>
{{ Form::close() }}
@endsection

View File

@ -3,8 +3,11 @@
namespace Modules\Installer\Services;
use App\Interfaces\Service;
use App\Models\Setting;
use DB;
use Log;
use Nwidart\Modules\Facades\Module;
use Symfony\Component\Yaml\Yaml;
/**
* Class MigrationsService
@ -12,9 +15,9 @@ use Nwidart\Modules\Facades\Module;
*/
class MigrationService extends Service
{
/**
* @return \Illuminate\Database\Migrations\Migrator
*/
private $counters = [];
private $offsets = [];
protected function getMigrator()
{
$m = app('migrator');
@ -22,6 +25,121 @@ class MigrationService extends Service
return $m;
}
/**
* Update all of the settings and sync them with the settings.yml file
*/
public function updateAllSettings(): void
{
$data = file_get_contents(database_path('/seeds/settings.yml'));
$yml = Yaml::parse($data);
foreach ($yml as $setting) {
if ($setting['key'] === '') {
continue;
}
$this->addSetting($setting['key'], $setting);
}
}
/**
* @param $key
* @param $attrs
*/
public function addSetting($key, $attrs): void
{
$id = Setting::formatKey($key);
$group = $attrs['group'];
$order = $this->getNextOrderNumber($group);
$attrs = array_merge(
[
'id' => $id,
'key' => $key,
'offset' => $this->offsets[$group],
'order' => $order,
'name' => '',
'group' => $group,
'value' => '',
'default' => $attrs['value'],
'options' => '',
'type' => 'hidden',
'description' => '',
],
$attrs
);
$count = DB::table('settings')->where('id', $id)->count('id');
if ($count === 0) {
DB::table('settings')->insert($attrs);
} else {
unset($attrs['value']); // Don't overwrite this
DB::table('settings')
->where('id', $id)
->update($attrs);
}
}
/**
* Dynamically figure out the offset and the start number for a group.
* This way we don't need to mess with how to order things
* When calling getNextOrderNumber(users) 31, will be returned, then 32, and so on
*
* @param $name
* @param null $offset
* @param int $start_offset
*/
private function addCounterGroup($name, $offset = null, $start_offset = 0): void
{
if ($offset === null) {
$group = DB::table('settings')
->where('group', $name)
->first();
if ($group === null) {
$offset = (int)DB::table('settings')->max('offset');
if ($offset === null) {
$offset = 0;
$start_offset = 1;
} else {
$offset += 100;
$start_offset = $offset + 1;
}
} else {
// Now find the number to start from
$start_offset = (int)DB::table('settings')->where('group', $name)->max('order');
if ($start_offset === null) {
$start_offset = $offset + 1;
} else {
$start_offset++;
}
$offset = $group->offset;
}
}
$this->counters[$name] = $start_offset;
$this->offsets[$name] = $offset;
}
/**
* Get the next increment number from a group
*
* @param $group
*
* @return int
*/
private function getNextOrderNumber($group): int
{
if (!\in_array($group, $this->counters, true)) {
$this->addCounterGroup($group);
}
$idx = $this->counters[$group];
$this->counters[$group]++;
return $idx;
}
/**
* Find all of the possible paths that migrations exist.
* Include looking in all of the modules Database/migrations directories