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 b873c3e6..83059725 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 @@ -1,6 +1,6 @@ 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 + * @param $table + * @param $rows + */ + public function addData($table, $rows) + { + foreach ($rows as $row) { + try { + DB::table($table)->insert($row); + } catch (\Exception $e) { + # setting already exists, just ignore it + if ($e->getCode() === 23000) { + continue; + } + } + } + } } diff --git a/app/Models/Migrations/Migration.php b/app/Models/Migrations/Migration.php deleted file mode 100644 index 108a1dac..00000000 --- a/app/Models/Migrations/Migration.php +++ /dev/null @@ -1,137 +0,0 @@ -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 - * @param $table - * @param $rows - */ - public function addData($table, $rows) - { - foreach ($rows as $row) { - try { - DB::table($table)->insert($row); - } catch (\Exception $e) { - # setting already exists, just ignore it - if ($e->getCode() === 23000) { - continue; - } - } - } - } -}