Files
web_work/catalog_admin_action.php
2025-12-16 01:28:06 +03:00

111 lines
4.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// catalog_admin_action.php
session_start();
require_once 'config/database.php';
if (!isset($_SESSION['is_admin']) || $_SESSION['is_admin'] !== true) {
header('Location: вход.php');
exit();
}
$db = Database::getInstance()->getConnection();
$action = $_POST['action'] ?? '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
try {
switch ($action) {
case 'add_category':
$name = $_POST['name'] ?? '';
$parent_id = $_POST['parent_id'] ?: null;
$description = $_POST['description'] ?? null;
$sort_order = $_POST['sort_order'] ?? 0;
$is_active = isset($_POST['is_active']) ? 1 : 0;
// Создаем slug из названия
$slug = strtolower(preg_replace('/[^a-z0-9]+/i', '-', $name));
$stmt = $db->prepare("
INSERT INTO categories (name, slug, parent_id, description, sort_order, is_active)
VALUES (?, ?, ?, ?, ?, ?)
");
$stmt->execute([$name, $slug, $parent_id, $description, $sort_order, $is_active]);
header('Location: catalog_admin.php?action=categories&message=Категория успешно добавлена');
exit();
case 'edit_category':
$category_id = $_POST['category_id'] ?? 0;
$name = $_POST['name'] ?? '';
$parent_id = $_POST['parent_id'] ?: null;
$description = $_POST['description'] ?? null;
$sort_order = $_POST['sort_order'] ?? 0;
$is_active = isset($_POST['is_active']) ? 1 : 0;
// Создаем slug из названия
$slug = strtolower(preg_replace('/[^a-z0-9]+/i', '-', $name));
$stmt = $db->prepare("
UPDATE categories SET
name = ?,
slug = ?,
parent_id = ?,
description = ?,
sort_order = ?,
is_active = ?
WHERE category_id = ?
");
$stmt->execute([$name, $slug, $parent_id, $description, $sort_order, $is_active, $category_id]);
header('Location: catalog_admin.php?action=categories&message=Категория успешно обновлена');
exit();
case 'delete_category':
$category_id = $_POST['category_id'] ?? 0;
// Проверяем, есть ли активные товары в этой категории
$checkStmt = $db->prepare("
SELECT COUNT(*)
FROM products
WHERE category_id = ? AND is_available = TRUE
");
$checkStmt->execute([$category_id]);
$active_products = $checkStmt->fetchColumn();
if ($active_products > 0) {
header('Location: catalog_admin.php?action=categories&error=Невозможно удалить категорию с активными товарами');
exit();
}
// Проверяем дочерние категории
$checkChildStmt = $db->prepare("
SELECT COUNT(*)
FROM categories
WHERE parent_id = ? AND is_active = TRUE
");
$checkChildStmt->execute([$category_id]);
$active_children = $checkChildStmt->fetchColumn();
if ($active_children > 0) {
header('Location: catalog_admin.php?action=categories&error=Невозможно удалить категорию с активными дочерними категориями');
exit();
}
// Удаляем категорию
$stmt = $db->prepare("DELETE FROM categories WHERE category_id = ?");
$stmt->execute([$category_id]);
header('Location: catalog_admin.php?action=categories&message=Категория успешно удалена');
exit();
}
} catch (PDOException $e) {
header('Location: catalog_admin.php?action=categories&error=' . urlencode('Ошибка базы данных: ' . $e->getMessage()));
exit();
}
}
header('Location: catalog_admin.php');
exit();
?>