From 041cef91de2083435324a5e537844fd4a6ced98e Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Fri, 9 Feb 2018 12:57:00 -0600 Subject: [PATCH] Refactor how settings are added to generate the offsets and order --- ...017_06_07_014930_create_settings_table.php | 357 ++++++++---------- app/Models/Migrations/Migration.php | 102 +++-- 2 files changed, 240 insertions(+), 219 deletions(-) diff --git a/app/Database/migrations/2017_06_07_014930_create_settings_table.php b/app/Database/migrations/2017_06_07_014930_create_settings_table.php index e7103b9f..4f088c7a 100644 --- a/app/Database/migrations/2017_06_07_014930_create_settings_table.php +++ b/app/Database/migrations/2017_06_07_014930_create_settings_table.php @@ -14,7 +14,9 @@ class CreateSettingsTable extends Migration { Schema::create('settings', function (Blueprint $table) { $table->string('id'); + $table->unsignedInteger('offset')->default(0); $table->unsignedInteger('order')->default(99); + $table->string('key'); $table->string('name'); $table->string('value'); $table->string('default')->nullable(); @@ -24,219 +26,180 @@ class CreateSettingsTable extends Migration $table->string('description')->nullable(); $table->primary('id'); + $table->index('key'); $table->timestamps(); }); - $this->addCounterGroups([ - 'general' => 1, - 'flights' => 20, - 'finances' => 40, - 'bids' => 60, - 'pireps' => 80, - 'pilots' => 100, - ]); - /** * Initial default settings */ - $settings = [ - [ - 'id' => $this->formatSettingId('general.start_date'), - 'order' => $this->getNextOrderNumber('general'), - 'name' => 'Start Date', - 'group' => 'general', - 'value' => '', - 'type' => 'date', - 'description' => 'The date your VA started', - ], - [ - 'id' => $this->formatSettingId('general.admin_email'), - 'order' => $this->getNextOrderNumber('general'), - 'name' => 'Admin Email', - 'group' => 'general', - 'value' => '', - 'type' => 'text', - 'description' => 'Email where notices, etc are sent', - ], - [ - 'id' => $this->formatSettingId('general.currency'), - 'order' => $this->getNextOrderNumber('general'), - 'name' => 'Currency to Use', - 'group' => 'general', - 'value' => 'dollar', - 'type' => 'select', - 'options' => 'dollar,euro,gbp,yen,jpy,rupee,ruble', - 'description' => 'Currency to show in the interface', - ], - [ - 'id' => $this->formatSettingId('general.distance_unit'), - 'order' => $this->getNextOrderNumber('general'), - 'name' => 'Distance Units', - 'group' => 'general', - 'value' => 'nm', - 'type' => 'select', - 'options' => 'km,mi,nm', - 'description' => 'The distance unit to show', - ], - [ - 'id' => $this->formatSettingId('general.weight_unit'), - 'order' => $this->getNextOrderNumber('general'), - 'name' => 'Weight Units', - 'group' => 'general', - 'value' => 'kg', - 'type' => 'select', - 'options' => 'lbs, kg', - 'description' => 'The weight unit', - ], - [ - 'id' => $this->formatSettingId('general.speed_unit'), - 'order' => $this->getNextOrderNumber('general'), - 'name' => 'Speed Units', - 'group' => 'general', - 'value' => 'Km/H', - 'type' => 'select', - 'options' => 'Km/H,kts', - 'description' => 'The speed unit', - ], - [ - 'id' => $this->formatSettingId('general.altitude_unit'), - 'order' => $this->getNextOrderNumber('general'), - 'name' => 'Altitude Units', - 'group' => 'general', - 'value' => 'ft', - 'type' => 'select', - 'options' => 'ft,m', - 'description' => 'The altitude units', - ], - [ - 'id' => $this->formatSettingId('general.liquid_unit'), - 'order' => $this->getNextOrderNumber('general'), - 'name' => 'Liquid Units', - 'group' => 'general', - 'value' => 'lbs', - 'type' => 'select', - 'options' => 'liters,gal,kg,lbs', - 'description' => 'The liquid units', - ], - /** - * BIDS - */ + $this->addSetting('general.start_date', [ + 'name' => 'Start Date', + 'group' => 'general', + 'value' => '', + 'type' => 'date', + 'description' => 'The date your VA started', + ]); - [ - 'id' => $this->formatSettingId('bids.disable_flight_on_bid'), - 'order' => $this->getNextOrderNumber('bids'), - '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', - ], - [ - 'id' => $this->formatSettingId('bids.allow_multiple_bids'), - 'order' => $this->getNextOrderNumber('bids'), - 'name' => 'Allow multiple bids', - 'group' => 'bids', - 'value' => true, - 'type' => 'boolean', - 'description' => 'Whether or not someone can bid on multiple flights', - ], + $this->addSetting('general.admin_email', [ + 'name' => 'Admin Email', + 'group' => 'general', + 'value' => '', + 'type' => 'text', + 'description' => 'Email where notices, etc are sent', + ]); - /** - * FINANCES - */ + $this->addSetting('general.currency', [ + 'name' => 'Currency to Use', + 'group' => 'general', + 'value' => 'dollar', + 'type' => 'select', + 'options' => 'dollar,euro,gbp,yen,jpy,rupee,ruble', + 'description' => 'Currency to show in the interface', + ]); - /** - * PIREPS - */ + $this->addSetting('general.distance_unit', [ + 'name' => 'Distance Units', + 'group' => 'general', + 'value' => 'nm', + 'type' => 'select', + 'options' => 'km,mi,nm', + 'description' => 'The distance unit to show', + ]); - [ - 'id' => $this->formatSettingId('pireps.duplicate_check_time'), - 'order' => $this->getNextOrderNumber('pireps'), - 'name' => 'PIREP duplicate time check', - 'group' => 'pireps', - 'value' => 10, - 'default' => 10, - 'type' => 'int', - 'description' => 'The time in minutes to check for a duplicate PIREP', - ], - [ - 'id' => $this->formatSettingId('pireps.hide_cancelled_pireps'), - 'order' => $this->getNextOrderNumber('pireps'), - 'name' => 'Hide Cancelled PIREPs', - 'group' => 'pireps', - 'value' => true, - 'default' => true, - 'type' => 'boolean', - 'description' => 'Hide any cancelled PIREPs in the front-end', - ], + $this->addSetting('general.weight_unit', [ + 'name' => 'Weight Units', + 'group' => 'general', + 'value' => 'kg', + 'type' => 'select', + 'options' => 'lbs, kg', + 'description' => 'The weight unit', + ]); - [ - 'id' => $this->formatSettingId('pireps.restrict_aircraft_to_rank'), - 'order' => $this->getNextOrderNumber('pireps'), - 'name' => 'Restrict Aircraft to Ranks', - 'group' => 'pireps', - 'value' => true, - 'default' => true, - 'type' => 'boolean', - 'description' => 'Aircraft that can be flown are restricted to a user\'s rank', - ], + $this->addSetting('general.speed_unit', [ + 'name' => 'Speed Units', + 'group' => 'general', + 'value' => 'Km/H', + 'type' => 'select', + 'options' => 'Km/H,kts', + 'description' => 'The speed unit', + ]); + $this->addSetting('general.altitude_unit', [ + 'name' => 'Altitude Units', + 'group' => 'general', + 'value' => 'ft', + 'type' => 'select', + 'options' => 'ft,m', + 'description' => 'The altitude units', + ]); - /** - * PILOTS - */ + $this->addSetting('general.liquid_unit', [ + 'name' => 'Liquid Units', + 'group' => 'general', + 'value' => 'lbs', + 'type' => 'select', + 'options' => 'liters,gal,kg,lbs', + 'description' => 'The liquid units', + ]); - [ - 'id' => $this->formatSettingId('pilots.id_length'), - 'order' => $this->getNextOrderNumber('pilots'), - 'name' => 'Pilot ID Length', - 'group' => 'pilots', - 'value' => 4, - 'default' => 4, - 'type' => 'int', - 'description' => 'The length of a pilot\'s ID', - ], - [ - 'id' => $this->formatSettingId('pilot.auto_accept'), - 'order' => $this->getNextOrderNumber('pilots'), - 'name' => 'Auto Accept New Pilot', - 'group' => 'pilots', - 'value' => true, - 'type' => 'boolean', - 'description' => 'Automatically accept a pilot when they register', - ], - [ - 'id' => $this->formatSettingId('pilots.only_flights_from_current'), - 'order' => $this->getNextOrderNumber('pilots'), - 'name' => 'Flights from Current', - 'group' => 'pilots', - 'value' => false, - 'type' => 'boolean', - 'description' => 'Only show/allow flights from their current location', - ], - [ - 'id' => $this->formatSettingId('pilot.auto_leave_days'), - 'order' => $this->getNextOrderNumber('pilots'), - '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', - ], - [ - 'id' => $this->formatSettingId('pilots.hide_inactive'), - 'order' => $this->getNextOrderNumber('pilots'), - 'name' => 'Hide Inactive Pilots', - 'group' => 'pilots', - 'value' => true, - 'type' => 'boolean', - 'description' => 'Don\'t show inactive pilots in the public view', - ], - ]; + /** + * BIDS + */ - $this->addData('settings', $settings); + $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', + ]); + + /** + * PIREPS + */ + + $this->addSetting('pireps.duplicate_check_time', [ + 'name' => 'PIREP duplicate time check', + 'group' => 'pireps', + 'value' => 10, + 'default' => 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, + 'default' => 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, + 'default' => true, + 'type' => 'boolean', + 'description' => 'Aircraft that can be flown are restricted to a user\'s rank', + ]); + + /** + * 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('pilot.auto_accept', [ + 'name' => 'Auto Accept New Pilot', + 'group' => 'pilots', + 'value' => true, + 'type' => 'boolean', + 'description' => 'Automatically accept a pilot when they register', + ]); + + $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('pilot.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', + ]); } /** diff --git a/app/Models/Migrations/Migration.php b/app/Models/Migrations/Migration.php index 02bc433a..2615457d 100644 --- a/app/Models/Migrations/Migration.php +++ b/app/Models/Migrations/Migration.php @@ -6,34 +6,54 @@ namespace App\Models\Migrations; use DB; +use App\Models\Setting; use Illuminate\Database\Migrations\Migration as MigrationBase; class Migration extends MigrationBase { - protected $counters; - + private $counters = []; + private $offsets = []; /** - * Just make sure the dotted format converts to all underscores - */ - public function formatSettingId($id) - { - return str_replace('.', '_', $id); - } - - /** - * Create a counter for groups with the start index. E.g: - * pireps: 10 - * users: 30 - * + * 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 array $groups + * @param $name + * @param null $offset + * @param int $start_offset */ - public function addCounterGroups(array $groups) + protected function addCounterGroup($name, $offset=null, $start_offset=0) { - foreach($groups as $group => $start) { - $this->counters[$group] = $start; + 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; } /** @@ -41,10 +61,10 @@ class Migration extends MigrationBase * @param $group * @return int */ - public function getNextOrderNumber($group) + public function getNextOrderNumber($group): int { - if(!isset($this->counters[$group])) { - return 0; + if(!\in_array($group, $this->counters, true)) { + $this->addCounterGroup($group); } $idx = $this->counters[$group]; @@ -53,6 +73,44 @@ class Migration extends MigrationBase return $idx; } + /** + * @param $key + * @param $attrs + */ + public function addSetting($key, $attrs) + { + $group = $attrs['group']; + $order = $this->getNextOrderNumber($group); + + $attrs = [ + 'id' => Setting::formatKey($key), + 'key' => $key, + 'offset' => $this->offsets[$group], + 'order' => $order, + 'name' => $attrs['name'], + 'group' => $group, + 'value' => $attrs['value'], + 'type' => $attrs['type'], + 'description' => $attrs['description'], + ]; + + 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', $this->formatSettingId($key)) + ->update($attrs); + } + /** * Add rows to a table * @param $table @@ -63,7 +121,7 @@ class Migration extends MigrationBase foreach ($rows as $row) { try { DB::table($table)->insert($row); - } catch (Exception $e) { + } catch (\Exception $e) { # setting already exists, just ignore it if ($e->getCode() === 23000) { continue;