2025-08-22 15:57:44 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Services\TenantBootstrap\Steps;
|
|
|
|
|
|
|
|
|
|
use App\Services\TenantBootstrap\Contracts\TenantBootstrapStep;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
|
|
|
|
class MenusStep implements TenantBootstrapStep
|
|
|
|
|
{
|
2025-11-06 17:24:42 +09:00
|
|
|
public function key(): string
|
|
|
|
|
{
|
|
|
|
|
return 'menus_seed';
|
|
|
|
|
}
|
2025-08-22 15:57:44 +09:00
|
|
|
|
|
|
|
|
public function run(int $tenantId): void
|
|
|
|
|
{
|
2025-11-06 17:24:42 +09:00
|
|
|
if (! DB::getSchemaBuilder()->hasTable('menus')) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if tenant already has menus
|
|
|
|
|
$exists = DB::table('menus')->where('tenant_id', $tenantId)->exists();
|
|
|
|
|
if ($exists) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get all global menus ordered by parent_id, sort_order
|
|
|
|
|
// Order by: root menus first (parent_id IS NULL), then by parent_id ASC, then sort_order ASC
|
|
|
|
|
$globalMenus = DB::table('menus')
|
|
|
|
|
->whereNull('tenant_id')
|
|
|
|
|
->orderByRaw('parent_id IS NULL DESC, parent_id ASC, sort_order ASC')
|
|
|
|
|
->get();
|
|
|
|
|
|
|
|
|
|
if ($globalMenus->isEmpty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$parentIdMap = []; // old_id => new_id mapping
|
|
|
|
|
|
|
|
|
|
foreach ($globalMenus as $menu) {
|
|
|
|
|
// Map parent_id: if parent exists in map, use new parent_id, else null
|
|
|
|
|
$newParentId = null;
|
|
|
|
|
if ($menu->parent_id !== null && isset($parentIdMap[$menu->parent_id])) {
|
|
|
|
|
$newParentId = $parentIdMap[$menu->parent_id];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Insert new menu for tenant
|
|
|
|
|
$newId = DB::table('menus')->insertGetId([
|
|
|
|
|
'tenant_id' => $tenantId,
|
|
|
|
|
'parent_id' => $newParentId,
|
|
|
|
|
'name' => $menu->name,
|
|
|
|
|
'icon' => $menu->icon ?? null,
|
|
|
|
|
'url' => $menu->url ?? null,
|
|
|
|
|
'sort_order' => $menu->sort_order ?? 0,
|
|
|
|
|
'is_active' => $menu->is_active ?? 1,
|
2025-11-10 09:35:43 +09:00
|
|
|
'hidden' => $menu->hidden ?? 0,
|
|
|
|
|
'is_external' => $menu->is_external ?? 0,
|
|
|
|
|
'external_url' => $menu->external_url ?? null,
|
2025-11-06 17:24:42 +09:00
|
|
|
'created_at' => now(),
|
|
|
|
|
'updated_at' => now(),
|
2025-08-22 15:57:44 +09:00
|
|
|
]);
|
2025-11-06 17:24:42 +09:00
|
|
|
|
|
|
|
|
// Store mapping for children menus
|
|
|
|
|
$parentIdMap[$menu->id] = $newId;
|
2025-08-22 15:57:44 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|