305 lines
9.9 KiB
PHP
305 lines
9.9 KiB
PHP
<?php
|
|
/**
|
|
* Copyright seit 2024 Webshop System
|
|
*
|
|
* Admin-Produkt-Controller für das Webshop-System
|
|
*
|
|
* @author Webshop System
|
|
* @license GPL v3
|
|
*/
|
|
|
|
namespace App\Admin\Controllers;
|
|
|
|
use Doctrine\DBAL\DriverManager;
|
|
use Doctrine\DBAL\Exception;
|
|
|
|
class AdminProductController
|
|
{
|
|
public function index()
|
|
{
|
|
// Session prüfen
|
|
session_start();
|
|
if (!isset($_SESSION['admin_user_id'])) {
|
|
header('Location: /admin/login');
|
|
exit;
|
|
}
|
|
|
|
// DB-Verbindung herstellen
|
|
$connectionParams = [
|
|
'dbname' => getenv('DB_DATABASE') ?: 'freeshop',
|
|
'user' => getenv('DB_USERNAME') ?: 'freeshop_user',
|
|
'password' => getenv('DB_PASSWORD') ?: 'freeshop_password',
|
|
'host' => getenv('DB_HOST') ?: 'db',
|
|
'driver' => 'pdo_mysql',
|
|
'port' => getenv('DB_PORT') ?: 3306,
|
|
'charset' => 'utf8mb4',
|
|
];
|
|
|
|
try {
|
|
$conn = DriverManager::getConnection($connectionParams);
|
|
|
|
// Produkte laden
|
|
$stmt = $conn->prepare('
|
|
SELECT p.*, c.name as category_name
|
|
FROM ws_product p
|
|
LEFT JOIN ws_category c ON p.category_id = c.id
|
|
ORDER BY p.created_at DESC
|
|
');
|
|
$stmt->execute();
|
|
|
|
$products = [];
|
|
while ($row = $stmt->fetchAssociative()) {
|
|
$products[] = $row;
|
|
}
|
|
|
|
$this->render('admin/products/index.html.twig', [
|
|
'title' => 'Webshop Admin - Produkte',
|
|
'user_name' => $_SESSION['admin_user_name'],
|
|
'products' => $products
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
$this->render('admin/products/index.html.twig', [
|
|
'title' => 'Webshop Admin - Produkte',
|
|
'user_name' => $_SESSION['admin_user_name'],
|
|
'products' => [],
|
|
'error' => 'Datenbankfehler: ' . $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
// Session prüfen
|
|
session_start();
|
|
if (!isset($_SESSION['admin_user_id'])) {
|
|
header('Location: /admin/login');
|
|
exit;
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$this->store();
|
|
return;
|
|
}
|
|
|
|
// Kategorien für Dropdown laden
|
|
$connectionParams = [
|
|
'dbname' => getenv('DB_DATABASE') ?: 'freeshop',
|
|
'user' => getenv('DB_USERNAME') ?: 'freeshop_user',
|
|
'password' => getenv('DB_PASSWORD') ?: 'freeshop_password',
|
|
'host' => getenv('DB_HOST') ?: 'db',
|
|
'driver' => 'pdo_mysql',
|
|
'port' => getenv('DB_PORT') ?: 3306,
|
|
'charset' => 'utf8mb4',
|
|
];
|
|
|
|
try {
|
|
$conn = DriverManager::getConnection($connectionParams);
|
|
|
|
$stmt = $conn->prepare('SELECT id, name FROM ws_category ORDER BY name');
|
|
$stmt->execute();
|
|
|
|
$categories = [];
|
|
while ($row = $stmt->fetchAssociative()) {
|
|
$categories[] = $row;
|
|
}
|
|
|
|
$this->render('admin/products/create.html.twig', [
|
|
'title' => 'Webshop Admin - Neues Produkt',
|
|
'user_name' => $_SESSION['admin_user_name'],
|
|
'categories' => $categories
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
$this->render('admin/products/create.html.twig', [
|
|
'title' => 'Webshop Admin - Neues Produkt',
|
|
'user_name' => $_SESSION['admin_user_name'],
|
|
'categories' => [],
|
|
'error' => 'Datenbankfehler: ' . $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
private function store()
|
|
{
|
|
$name = $_POST['name'] ?? '';
|
|
$description = $_POST['description'] ?? '';
|
|
$price = $_POST['price'] ?? 0;
|
|
$stock = $_POST['stock'] ?? 0;
|
|
$category_id = $_POST['category_id'] ?? null;
|
|
|
|
if (empty($name) || empty($price)) {
|
|
header('Location: /admin/products/create?error=Fehlende Pflichtfelder');
|
|
exit;
|
|
}
|
|
|
|
$connectionParams = [
|
|
'dbname' => getenv('DB_DATABASE') ?: 'freeshop',
|
|
'user' => getenv('DB_USERNAME') ?: 'freeshop_user',
|
|
'password' => getenv('DB_PASSWORD') ?: 'freeshop_password',
|
|
'host' => getenv('DB_HOST') ?: 'db',
|
|
'driver' => 'pdo_mysql',
|
|
'port' => getenv('DB_PORT') ?: 3306,
|
|
'charset' => 'utf8mb4',
|
|
];
|
|
|
|
try {
|
|
$conn = DriverManager::getConnection($connectionParams);
|
|
|
|
$stmt = $conn->prepare('
|
|
INSERT INTO ws_product (name, description, price, stock, category_id)
|
|
VALUES (?, ?, ?, ?, ?)
|
|
');
|
|
$stmt->execute([$name, $description, $price, $stock, $category_id]);
|
|
|
|
header('Location: /admin/products?success=Produkt erfolgreich erstellt');
|
|
exit;
|
|
|
|
} catch (Exception $e) {
|
|
header('Location: /admin/products/create?error=Datenbankfehler: ' . $e->getMessage());
|
|
exit;
|
|
}
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
// Session prüfen
|
|
session_start();
|
|
if (!isset($_SESSION['admin_user_id'])) {
|
|
header('Location: /admin/login');
|
|
exit;
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$this->update($id);
|
|
return;
|
|
}
|
|
|
|
$connectionParams = [
|
|
'dbname' => getenv('DB_DATABASE') ?: 'freeshop',
|
|
'user' => getenv('DB_USERNAME') ?: 'freeshop_user',
|
|
'password' => getenv('DB_PASSWORD') ?: 'freeshop_password',
|
|
'host' => getenv('DB_HOST') ?: 'db',
|
|
'driver' => 'pdo_mysql',
|
|
'port' => getenv('DB_PORT') ?: 3306,
|
|
'charset' => 'utf8mb4',
|
|
];
|
|
|
|
try {
|
|
$conn = DriverManager::getConnection($connectionParams);
|
|
|
|
// Produkt laden
|
|
$stmt = $conn->prepare('SELECT * FROM ws_product WHERE id = ?');
|
|
$stmt->execute([$id]);
|
|
$product = $stmt->fetchAssociative();
|
|
|
|
if (!$product) {
|
|
header('Location: /admin/products?error=Produkt nicht gefunden');
|
|
exit;
|
|
}
|
|
|
|
// Kategorien laden
|
|
$stmt = $conn->prepare('SELECT id, name FROM ws_category ORDER BY name');
|
|
$stmt->execute();
|
|
|
|
$categories = [];
|
|
while ($row = $stmt->fetchAssociative()) {
|
|
$categories[] = $row;
|
|
}
|
|
|
|
$this->render('admin/products/edit.html.twig', [
|
|
'title' => 'Webshop Admin - Produkt bearbeiten',
|
|
'user_name' => $_SESSION['admin_user_name'],
|
|
'product' => $product,
|
|
'categories' => $categories
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
header('Location: /admin/products?error=Datenbankfehler: ' . $e->getMessage());
|
|
exit;
|
|
}
|
|
}
|
|
|
|
private function update($id)
|
|
{
|
|
$name = $_POST['name'] ?? '';
|
|
$description = $_POST['description'] ?? '';
|
|
$price = $_POST['price'] ?? 0;
|
|
$stock = $_POST['stock'] ?? 0;
|
|
$category_id = $_POST['category_id'] ?? null;
|
|
|
|
if (empty($name) || empty($price)) {
|
|
header('Location: /admin/products/edit/' . $id . '?error=Fehlende Pflichtfelder');
|
|
exit;
|
|
}
|
|
|
|
$connectionParams = [
|
|
'dbname' => getenv('DB_DATABASE') ?: 'freeshop',
|
|
'user' => getenv('DB_USERNAME') ?: 'freeshop_user',
|
|
'password' => getenv('DB_PASSWORD') ?: 'freeshop_password',
|
|
'host' => getenv('DB_HOST') ?: 'db',
|
|
'driver' => 'pdo_mysql',
|
|
'port' => getenv('DB_PORT') ?: 3306,
|
|
'charset' => 'utf8mb4',
|
|
];
|
|
|
|
try {
|
|
$conn = DriverManager::getConnection($connectionParams);
|
|
|
|
$stmt = $conn->prepare('
|
|
UPDATE ws_product
|
|
SET name = ?, description = ?, price = ?, stock = ?, category_id = ?
|
|
WHERE id = ?
|
|
');
|
|
$stmt->execute([$name, $description, $price, $stock, $category_id, $id]);
|
|
|
|
header('Location: /admin/products?success=Produkt erfolgreich aktualisiert');
|
|
exit;
|
|
|
|
} catch (Exception $e) {
|
|
header('Location: /admin/products/edit/' . $id . '?error=Datenbankfehler: ' . $e->getMessage());
|
|
exit;
|
|
}
|
|
}
|
|
|
|
public function delete($id)
|
|
{
|
|
// Session prüfen
|
|
session_start();
|
|
if (!isset($_SESSION['admin_user_id'])) {
|
|
header('Location: /admin/login');
|
|
exit;
|
|
}
|
|
|
|
$connectionParams = [
|
|
'dbname' => getenv('DB_DATABASE') ?: 'freeshop',
|
|
'user' => getenv('DB_USERNAME') ?: 'freeshop_user',
|
|
'password' => getenv('DB_PASSWORD') ?: 'freeshop_password',
|
|
'host' => getenv('DB_HOST') ?: 'db',
|
|
'driver' => 'pdo_mysql',
|
|
'port' => getenv('DB_PORT') ?: 3306,
|
|
'charset' => 'utf8mb4',
|
|
];
|
|
|
|
try {
|
|
$conn = DriverManager::getConnection($connectionParams);
|
|
|
|
$stmt = $conn->prepare('DELETE FROM ws_product WHERE id = ?');
|
|
$stmt->execute([$id]);
|
|
|
|
header('Location: /admin/products?success=Produkt erfolgreich gelöscht');
|
|
exit;
|
|
|
|
} catch (Exception $e) {
|
|
header('Location: /admin/products?error=Datenbankfehler: ' . $e->getMessage());
|
|
exit;
|
|
}
|
|
}
|
|
|
|
protected function render($template, $data = [])
|
|
{
|
|
// Einfache Template-Engine (später durch Twig ersetzen)
|
|
extract($data);
|
|
include __DIR__ . '/../../templates/' . $template;
|
|
}
|
|
}
|