2018-03-20 09:50:40 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Interfaces;
|
|
|
|
|
2018-04-07 06:21:10 +08:00
|
|
|
use App\Models\Setting;
|
|
|
|
use DB;
|
|
|
|
|
2018-03-20 10:01:32 +08:00
|
|
|
/**
|
|
|
|
* Class Migration
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
abstract class Migration extends \Illuminate\Database\Migrations\Migration
|
|
|
|
{
|
2018-04-07 06:21:10 +08:00
|
|
|
protected $counters = [];
|
|
|
|
protected $offsets = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* At a minimum, this function needs to be implemented
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-07 06:21:10 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
abstract public function up();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A method to reverse a migration doesn't need to be made
|
|
|
|
*/
|
|
|
|
public function down()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-07 06:21:10 +08:00
|
|
|
* @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 {
|
2018-08-27 00:40:04 +08:00
|
|
|
// Now find the number to start from
|
2018-04-07 06:21:10 +08:00
|
|
|
$start_offset = (int) DB::table('settings')->where('group', $name)->max('order');
|
|
|
|
if ($start_offset === null) {
|
|
|
|
$start_offset = $offset + 1;
|
|
|
|
} else {
|
2018-08-27 00:40:04 +08:00
|
|
|
$start_offset++;
|
2018-04-07 06:21:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
$offset = $group->offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->counters[$name] = $start_offset;
|
|
|
|
$this->offsets[$name] = $offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the next increment number from a group
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-07 06:21:10 +08:00
|
|
|
* @param $group
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-07 06:21:10 +08:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getNextOrderNumber($group): int
|
|
|
|
{
|
|
|
|
if (!\in_array($group, $this->counters, true)) {
|
|
|
|
$this->addCounterGroup($group);
|
|
|
|
}
|
|
|
|
|
|
|
|
$idx = $this->counters[$group];
|
2018-08-27 00:40:04 +08:00
|
|
|
$this->counters[$group]++;
|
2018-04-07 06:21:10 +08:00
|
|
|
|
|
|
|
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
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-07 06:21:10 +08:00
|
|
|
* @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);
|
|
|
|
}
|
2018-03-20 10:01:32 +08:00
|
|
|
|
2018-04-07 06:21:10 +08:00
|
|
|
/**
|
|
|
|
* Add rows to a table
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-07 06:21:10 +08:00
|
|
|
* @param $table
|
|
|
|
* @param $rows
|
|
|
|
*/
|
|
|
|
public function addData($table, $rows)
|
|
|
|
{
|
|
|
|
foreach ($rows as $row) {
|
|
|
|
try {
|
|
|
|
DB::table($table)->insert($row);
|
|
|
|
} catch (\Exception $e) {
|
2018-08-27 00:40:04 +08:00
|
|
|
// setting already exists, just ignore it
|
2018-04-07 06:21:10 +08:00
|
|
|
if ($e->getCode() === 23000) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-20 09:50:40 +08:00
|
|
|
}
|