diff --git a/admin.php b/admin.php
index 9fd9e23..51485d0 100644
--- a/admin.php
+++ b/admin.php
@@ -98,6 +98,46 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$message = 'Тематика создана';
}
+ if ($action === 'update_topic') {
+ $topicId = (int)($_POST['topic_id'] ?? 0);
+ $name = trim((string)($_POST['name'] ?? ''));
+ $sort = (int)($_POST['sort_order'] ?? 1000);
+ $parentId = (int)($_POST['parent_id'] ?? 0);
+
+ if ($topicId < 1) throw new RuntimeException('Некорректная тематика');
+ if ($name === '') throw new RuntimeException('Название тематики пустое');
+
+ $topic = topicById($topicId);
+ if (!$topic) throw new RuntimeException('Тематика не найдена');
+
+ if ($parentId === $topicId) {
+ throw new RuntimeException('Тематика не может быть родителем самой себя');
+ }
+
+ if ($parentId > 0) {
+ $parent = topicById($parentId);
+ if (!$parent) throw new RuntimeException('Родительская тематика не найдена');
+ if (!empty($parent['parent_id'])) {
+ throw new RuntimeException('Разрешено только 2 уровня вложенности тематик');
+ }
+ if (topicChildrenCount($topicId) > 0) {
+ throw new RuntimeException('Тематика с дочерними элементами должна оставаться в верхнем уровне');
+ }
+ }
+
+ topicUpdate($topicId, $name, $parentId > 0 ? $parentId : null, $sort);
+ $message = 'Тематика обновлена';
+ }
+
+ if ($action === 'delete_topic') {
+ $topicId = (int)($_POST['topic_id'] ?? 0);
+ if ($topicId < 1) throw new RuntimeException('Некорректная тематика');
+ if (!topicById($topicId)) throw new RuntimeException('Тематика не найдена');
+
+ topicDelete($topicId);
+ $message = 'Тематика удалена';
+ }
+
if ($action === 'delete_section') {
$sectionId = (int)($_POST['section_id'] ?? 0);
if ($sectionId < 1) throw new RuntimeException('Некорректный раздел');
@@ -819,11 +859,33 @@ function nextUniqueCodeName(string $base): string
Тематик пока нет.
- | Тематика | Уровень |
+ | Тематика | Уровень | Действия |
- | = h((string)$topic['full_name']) ?> |
+
+
+ |
= (int)$topic['level'] === 0 ? '1' : '2' ?> |
+
+
+ |
@@ -924,12 +986,11 @@ function nextUniqueCodeName(string $base): string
-
@@ -1380,9 +1441,7 @@ function nextUniqueCodeName(string $base): string
throw new Error('Некорректные параметры');
}
- const addBtn = editor.querySelector('.js-topic-add');
const select = editor.querySelector('.js-topic-select');
- if (addBtn) addBtn.disabled = true;
if (select) select.disabled = true;
const fd = new FormData();
@@ -1419,28 +1478,28 @@ function nextUniqueCodeName(string $base): string
select.value = '';
}
} finally {
- if (addBtn) addBtn.disabled = false;
if (select) select.disabled = false;
}
};
- document.addEventListener('click', (e) => {
- const addBtn = e.target.closest('.js-topic-add');
- if (addBtn) {
- const editor = addBtn.closest('.js-topic-editor');
- if (!editor) return;
- const select = editor.querySelector('.js-topic-select');
- const topicId = Number(select?.value || 0);
+ document.querySelectorAll('.js-topic-editor').forEach((editor) => {
+ const select = editor.querySelector('.js-topic-select');
+ if (!select) {
+ return;
+ }
+
+ select.addEventListener('change', () => {
+ const topicId = Number(select.value || 0);
if (topicId < 1) {
- setTopicStatus(editor, 'Сначала выбери тематику', true);
return;
}
postTopicAction(editor, 'attach_photo_topic', topicId)
.catch((err) => setTopicStatus(editor, err?.message || 'Ошибка добавления', true));
- return;
- }
+ });
+ });
+ document.addEventListener('click', (e) => {
const removeBtn = e.target.closest('.js-topic-remove');
if (removeBtn) {
const editor = removeBtn.closest('.js-topic-editor');
diff --git a/lib/db_gallery.php b/lib/db_gallery.php
index 3d84416..e45bf0e 100644
--- a/lib/db_gallery.php
+++ b/lib/db_gallery.php
@@ -109,6 +109,23 @@ function topicCreate(string $name, ?int $parentId, int $sortOrder = 1000): int
return (int)db()->lastInsertId();
}
+function topicUpdate(int $topicId, string $name, ?int $parentId, int $sortOrder = 1000): void
+{
+ $st = db()->prepare('UPDATE topics SET parent_id=:pid, name=:name, sort_order=:sort WHERE id=:id');
+ $st->bindValue('id', $topicId, PDO::PARAM_INT);
+ $st->bindValue('pid', $parentId, $parentId === null ? PDO::PARAM_NULL : PDO::PARAM_INT);
+ $st->bindValue('name', $name, PDO::PARAM_STR);
+ $st->bindValue('sort', $sortOrder, PDO::PARAM_INT);
+ $st->execute();
+}
+
+function topicChildrenCount(int $topicId): int
+{
+ $st = db()->prepare('SELECT COUNT(*) FROM topics WHERE parent_id=:id');
+ $st->execute(['id' => $topicId]);
+ return (int)$st->fetchColumn();
+}
+
function topicDelete(int $topicId): void
{
$st = db()->prepare('DELETE FROM topics WHERE id=:id');