From b241c9b9b3c205516edfbe865ba3c57641af6783 Mon Sep 17 00:00:00 2001 From: Alexander Andreev Date: Fri, 20 Feb 2026 18:20:03 +0300 Subject: [PATCH] Admin: add section rename and delete actions Let admins manage section names and ordering from the media sidebar without manual DB edits. Add safe section deletion in admin flow and remove the section storage folder after DB cascade cleanup. --- admin.php | 76 ++++++++++++++++++++++++++++++++++++++++++++++ lib/db_gallery.php | 12 ++++++++ 2 files changed, 88 insertions(+) diff --git a/admin.php b/admin.php index 3dcd909..37c93c1 100644 --- a/admin.php +++ b/admin.php @@ -36,6 +36,27 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $message = 'Раздел создан'; } + if ($action === 'update_section') { + $sectionId = (int)($_POST['section_id'] ?? 0); + $name = trim((string)($_POST['name'] ?? '')); + $sort = (int)($_POST['sort_order'] ?? 1000); + if ($sectionId < 1) throw new RuntimeException('Некорректный раздел'); + if ($name === '') throw new RuntimeException('Название раздела пустое'); + if (!sectionById($sectionId)) throw new RuntimeException('Раздел не найден'); + sectionUpdate($sectionId, $name, $sort); + $message = 'Раздел обновлён'; + } + + if ($action === 'delete_section') { + $sectionId = (int)($_POST['section_id'] ?? 0); + if ($sectionId < 1) throw new RuntimeException('Некорректный раздел'); + if (!sectionById($sectionId)) throw new RuntimeException('Раздел не найден'); + + sectionDelete($sectionId); + deleteSectionStorage($sectionId); + $message = 'Раздел удалён'; + } + if ($action === 'update_welcome') { $text = trim((string)($_POST['welcome_text'] ?? '')); settingSet('welcome_text', $text); @@ -143,6 +164,11 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $sections = sectionsAll(); $activeSectionId = (int)($_GET['section_id'] ?? ($_POST['section_id'] ?? ($sections[0]['id'] ?? 0))); +$activeSection = $activeSectionId > 0 ? sectionById($activeSectionId) : null; +if (!$activeSection && $sections !== []) { + $activeSectionId = (int)$sections[0]['id']; + $activeSection = sectionById($activeSectionId); +} $photos = $activeSectionId > 0 ? photosBySection($activeSectionId) : []; $commenters = commentersAll(); $latestComments = commentsLatest(80); @@ -240,6 +266,40 @@ function uniqueName(string $dir, string $base, string $ext): string return $name; } +function deleteSectionStorage(int $sectionId): void +{ + $dir = __DIR__ . '/photos/section_' . $sectionId; + if (!is_dir($dir)) { + return; + } + + deleteDirRecursive($dir); +} + +function deleteDirRecursive(string $dir): void +{ + $items = scandir($dir); + if (!is_array($items)) { + return; + } + + foreach ($items as $item) { + if ($item === '.' || $item === '..') { + continue; + } + + $path = $dir . '/' . $item; + if (is_dir($path)) { + deleteDirRecursive($path); + continue; + } + + @unlink($path); + } + + @rmdir($dir); +} + function nextSortOrderForSection(int $sectionId): int { $st = db()->prepare('SELECT COALESCE(MAX(sort_order),0)+10 FROM photos WHERE section_id=:sid'); @@ -295,6 +355,22 @@ function nextUniqueCodeName(string $base): string () + + +
+

Редактировать выбранный

+
+ +

+

+ +
+
+ + +
+ +
diff --git a/lib/db_gallery.php b/lib/db_gallery.php index 4ea7601..51a9984 100644 --- a/lib/db_gallery.php +++ b/lib/db_gallery.php @@ -25,6 +25,18 @@ function sectionCreate(string $name, int $sort): void $st->execute(['name' => $name, 'sort' => $sort]); } +function sectionUpdate(int $id, string $name, int $sort): void +{ + $st = db()->prepare('UPDATE sections SET name=:name, sort_order=:sort WHERE id=:id'); + $st->execute(['id' => $id, 'name' => $name, 'sort' => $sort]); +} + +function sectionDelete(int $id): void +{ + $st = db()->prepare('DELETE FROM sections WHERE id=:id'); + $st->execute(['id' => $id]); +} + function photosBySection(int $sectionId): array { $sql = 'SELECT p.*,