diff --git a/app/Controllers/Admin/Menu.php b/app/Controllers/Admin/Menu.php index 01d7886..0e3ca36 100644 --- a/app/Controllers/Admin/Menu.php +++ b/app/Controllers/Admin/Menu.php @@ -30,12 +30,8 @@ class Menu extends BaseController ->with('error', '메뉴를 관리하려면 먼저 지자체를 선택하세요.'); } $types = $this->typeModel->orderBy('mt_sort', 'ASC')->findAll(); - $mtIdx = (int) ($this->request->getGet('mt_idx') ?? 0); - if ($mtIdx <= 0 && ! empty($types)) { - // 기본 선택: 사이트 메뉴(mt_code=site), 없으면 첫 번째 타입 - $siteType = $this->typeModel->where('mt_code', 'site')->first(); - $mtIdx = $siteType ? (int) $siteType->mt_idx : (int) $types[0]->mt_idx; - } + $requestedMtIdx = (int) ($this->request->getGet('mt_idx') ?? 0); + $mtIdx = $this->resolveMtIdx($requestedMtIdx, $types); $list = $mtIdx > 0 ? $this->menuModel->getAllByType($mtIdx, $lgIdx) : []; // 현재 지자체에 메뉴가 없으면, mt_idx별로 기본 지자체(lg_idx=1)의 메뉴를 한 번 복사한다. @@ -73,7 +69,9 @@ class Menu extends BaseController if ($lgIdx === null) { return $this->response->setJSON(['status' => 0, 'msg' => '지자체를 선택하세요.']); } - $mtIdx = (int) $this->request->getGet('mt_idx'); + $types = $this->typeModel->orderBy('mt_sort', 'ASC')->findAll(); + $requestedMtIdx = (int) $this->request->getGet('mt_idx'); + $mtIdx = $this->resolveMtIdx($requestedMtIdx, $types); if ($mtIdx <= 0) { return $this->response->setJSON(['status' => 0, 'msg' => 'mt_idx required']); } @@ -210,4 +208,34 @@ class Menu extends BaseController return implode(',', array_values($levels)); } + + /** + * 요청된 mt_idx를 현재 DB 상태에 맞게 보정. + * - 유효한 mt_idx면 그대로 사용 + * - 레거시 site 값(2) 요청 시 site 타입의 실제 mt_idx로 치환 + * - 그 외 미지정/잘못된 값은 site 우선, 없으면 첫 타입으로 보정 + * + * @param array $types + */ + private function resolveMtIdx(int $requestedMtIdx, array $types): int + { + if (empty($types)) { + return 0; + } + + $validTypeIds = array_map(static fn ($t): int => (int) ($t->mt_idx ?? 0), $types); + if ($requestedMtIdx > 0 && in_array($requestedMtIdx, $validTypeIds, true)) { + return $requestedMtIdx; + } + + $siteType = $this->typeModel->where('mt_code', 'site')->first(); + if ($siteType !== null) { + // 과거 링크(/admin/menus?mt_idx=2) 호환 + if ($requestedMtIdx === 2 || $requestedMtIdx <= 0 || ! in_array($requestedMtIdx, $validTypeIds, true)) { + return (int) $siteType->mt_idx; + } + } + + return (int) $types[0]->mt_idx; + } }