2017-12-10 11:21:49 +08:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Migration base class with some extra functionality
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Models\Migrations;
|
|
|
|
|
|
|
|
use DB;
|
2018-02-10 02:57:00 +08:00
|
|
|
use App\Models\Setting;
|
2017-12-10 11:21:49 +08:00
|
|
|
use Illuminate\Database\Migrations\Migration as MigrationBase;
|
|
|
|
|
|
|
|
class Migration extends MigrationBase
|
|
|
|
{
|
2018-02-10 02:57:00 +08:00
|
|
|
private $counters = [];
|
|
|
|
private $offsets = [];
|
2018-01-03 01:42:26 +08:00
|
|
|
|
|
|
|
/**
|
2018-02-10 02:57:00 +08:00
|
|
|
* 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
|
2018-01-03 01:42:26 +08:00
|
|
|
* When calling getNextOrderNumber(users) 31, will be returned, then 32, and so on
|
2018-02-10 02:57:00 +08:00
|
|
|
* @param $name
|
|
|
|
* @param null $offset
|
|
|
|
* @param int $start_offset
|
2018-01-03 01:42:26 +08:00
|
|
|
*/
|
2018-02-10 02:57:00 +08:00
|
|
|
protected function addCounterGroup($name, $offset=null, $start_offset=0)
|
2018-01-03 01:42:26 +08:00
|
|
|
{
|
2018-02-10 02:57:00 +08:00
|
|
|
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;
|
|
|
|
}
|
2018-01-03 01:42:26 +08:00
|
|
|
}
|
2018-02-10 02:57:00 +08:00
|
|
|
|
|
|
|
$this->counters[$name] = $start_offset;
|
|
|
|
$this->offsets[$name] = $offset;
|
2018-01-03 01:42:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the next increment number from a group
|
|
|
|
* @param $group
|
|
|
|
* @return int
|
|
|
|
*/
|
2018-02-10 02:57:00 +08:00
|
|
|
public function getNextOrderNumber($group): int
|
2018-01-03 01:42:26 +08:00
|
|
|
{
|
2018-02-10 02:57:00 +08:00
|
|
|
if(!\in_array($group, $this->counters, true)) {
|
|
|
|
$this->addCounterGroup($group);
|
2018-01-03 01:42:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
$idx = $this->counters[$group];
|
|
|
|
++$this->counters[$group];
|
|
|
|
|
|
|
|
return $idx;
|
|
|
|
}
|
|
|
|
|
2018-02-10 02:57:00 +08:00
|
|
|
/**
|
|
|
|
* @param $key
|
|
|
|
* @param $attrs
|
|
|
|
*/
|
|
|
|
public function addSetting($key, $attrs)
|
|
|
|
{
|
|
|
|
$group = $attrs['group'];
|
|
|
|
$order = $this->getNextOrderNumber($group);
|
|
|
|
|
2018-02-20 01:19:18 +08:00
|
|
|
$attrs = array_merge([
|
2018-02-10 02:57:00 +08:00
|
|
|
'id' => Setting::formatKey($key),
|
|
|
|
'key' => $key,
|
|
|
|
'offset' => $this->offsets[$group],
|
|
|
|
'order' => $order,
|
2018-02-20 01:19:18 +08:00
|
|
|
'name' => '',
|
2018-02-10 02:57:00 +08:00
|
|
|
'group' => $group,
|
2018-02-20 01:19:18 +08:00
|
|
|
'value' => '',
|
|
|
|
'default' => '',
|
|
|
|
'options' => '',
|
|
|
|
'type' => 'hidden',
|
|
|
|
'description' => '',
|
|
|
|
], $attrs);
|
2018-02-10 02:57:00 +08:00
|
|
|
|
|
|
|
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')
|
2018-02-20 01:19:18 +08:00
|
|
|
->where('id', Setting::formatKey($key))
|
2018-02-10 02:57:00 +08:00
|
|
|
->update($attrs);
|
|
|
|
}
|
|
|
|
|
2017-12-10 11:21:49 +08:00
|
|
|
/**
|
|
|
|
* Add rows to a table
|
|
|
|
* @param $table
|
|
|
|
* @param $rows
|
|
|
|
*/
|
|
|
|
public function addData($table, $rows)
|
|
|
|
{
|
|
|
|
foreach ($rows as $row) {
|
|
|
|
try {
|
|
|
|
DB::table($table)->insert($row);
|
2018-02-10 02:57:00 +08:00
|
|
|
} catch (\Exception $e) {
|
2017-12-10 11:56:26 +08:00
|
|
|
# setting already exists, just ignore it
|
2017-12-10 11:21:49 +08:00
|
|
|
if ($e->getCode() === 23000) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|