mirror of
https://codeberg.org/ThisIsMiseryy/techstore
synced 2026-05-14 14:52:04 +00:00
Merge implementazione pagina admin
This commit is contained in:
+1
-1
@@ -81,7 +81,7 @@ if ($result_total_products) {
|
|||||||
<li><a href="/admin/manageOrders.php">Gestione ordini</a></li>
|
<li><a href="/admin/manageOrders.php">Gestione ordini</a></li>
|
||||||
<li><a href="/admin/manageProducts.php">Gestione prodotti</a></li>
|
<li><a href="/admin/manageProducts.php">Gestione prodotti</a></li>
|
||||||
<li><a href="/admin/manageUsers.php">Gestione utenti</a></li>
|
<li><a href="/admin/manageUsers.php">Gestione utenti</a></li>
|
||||||
<hr>
|
<hr class="admin-divider">
|
||||||
<li><a href="../account.php">Ritorna al tuo account</a></li>
|
<li><a href="../account.php">Ritorna al tuo account</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ if ($result === false) {
|
|||||||
<li><a href="/admin/manageOrders.php">Gestione ordini</a></li>
|
<li><a href="/admin/manageOrders.php">Gestione ordini</a></li>
|
||||||
<li><a href="/admin/manageProducts.php">Gestione prodotti</a></li>
|
<li><a href="/admin/manageProducts.php">Gestione prodotti</a></li>
|
||||||
<li><a href="/admin/manageUsers.php">Gestione utenti</a></li>
|
<li><a href="/admin/manageUsers.php">Gestione utenti</a></li>
|
||||||
<hr>
|
<hr class="admin-divider">
|
||||||
<li><a href="../account.php">Ritorna al tuo account</a></li>
|
<li><a href="../account.php">Ritorna al tuo account</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -22,6 +22,95 @@ if ($role !== 'admin') {
|
|||||||
http_response_code(403);
|
http_response_code(403);
|
||||||
die("Accesso negato. Solo gli amministratori possono accedere a questa sezione.");
|
die("Accesso negato. Solo gli amministratori possono accedere a questa sezione.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Gestione aggiornamenti
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['save'])) {
|
||||||
|
$product_id = $_POST['product_id'];
|
||||||
|
$name = $_POST['name'];
|
||||||
|
$description = $_POST['description'];
|
||||||
|
$category_id = $_POST['category_id'];
|
||||||
|
$price = $_POST['price'];
|
||||||
|
$stock = $_POST['stock'];
|
||||||
|
|
||||||
|
// Gestione immagine
|
||||||
|
$image_path = basename($_POST['current_image']);
|
||||||
|
if (isset($_FILES['image']) && $_FILES['image']['error'] == 0) {
|
||||||
|
$target_dir = "../img/";
|
||||||
|
$image_name = basename($_FILES["image"]["name"]);
|
||||||
|
$target_file = $target_dir . $image_name;
|
||||||
|
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
|
||||||
|
// Controlli base
|
||||||
|
if (in_array($imageFileType, ['jpg', 'png', 'jpeg', 'gif'])) {
|
||||||
|
if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
|
||||||
|
$image_path = $image_name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update DB
|
||||||
|
$sql_update = "UPDATE Products SET ProductName = ?, Description = ?, CategoryID = ?, Price = ?, StockQuantity = ?, ImagePath = ? WHERE ProductID = ?";
|
||||||
|
$stmt_update = $conn->prepare($sql_update);
|
||||||
|
$stmt_update->bind_param("ssidisi", $name, $description, $category_id, $price, $stock, $image_path, $product_id);
|
||||||
|
$stmt_update->execute();
|
||||||
|
$stmt_update->close();
|
||||||
|
|
||||||
|
// Redirect
|
||||||
|
header("Location: manageProducts.php");
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gestione aggiunta nuovo prodotto
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['add'])) {
|
||||||
|
$name = $_POST['name'];
|
||||||
|
$description = $_POST['description'];
|
||||||
|
$category_id = $_POST['category_id'];
|
||||||
|
$price = $_POST['price'];
|
||||||
|
$stock = $_POST['stock'];
|
||||||
|
$image_path = '';
|
||||||
|
|
||||||
|
if (isset($_FILES['image']) && $_FILES['image']['error'] == 0) {
|
||||||
|
$target_dir = "../img/";
|
||||||
|
$image_name = basename($_FILES["image"]["name"]);
|
||||||
|
$target_file = $target_dir . $image_name;
|
||||||
|
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
|
||||||
|
if (in_array($imageFileType, ['jpg', 'png', 'jpeg', 'gif'])) {
|
||||||
|
if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
|
||||||
|
$image_path = $image_name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$sql_insert = "INSERT INTO Products (ProductName, Description, CategoryID, Price, StockQuantity, ImagePath) VALUES (?, ?, ?, ?, ?, ?)";
|
||||||
|
$stmt_insert = $conn->prepare($sql_insert);
|
||||||
|
$stmt_insert->bind_param("ssidis", $name, $description, $category_id, $price, $stock, $image_path);
|
||||||
|
$stmt_insert->execute();
|
||||||
|
$stmt_insert->close();
|
||||||
|
|
||||||
|
header("Location: manageProducts.php");
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ottieni categorie
|
||||||
|
$categories = [];
|
||||||
|
$sql_cat = "SELECT CategoryID, Name FROM Categories";
|
||||||
|
$stmt_cat = $conn->prepare($sql_cat);
|
||||||
|
$stmt_cat->execute();
|
||||||
|
$result_cat = $stmt_cat->get_result();
|
||||||
|
while ($row = $result_cat->fetch_assoc()) {
|
||||||
|
$categories[] = $row;
|
||||||
|
}
|
||||||
|
$stmt_cat->close();
|
||||||
|
|
||||||
|
// Ottieni prodotti
|
||||||
|
$products = [];
|
||||||
|
$sql_prod = "SELECT ProductID, ProductName, Description, Price, StockQuantity, ImagePath, CategoryID FROM Products";
|
||||||
|
$stmt_prod = $conn->prepare($sql_prod);
|
||||||
|
$stmt_prod->execute();
|
||||||
|
$result_prod = $stmt_prod->get_result();
|
||||||
|
while ($row = $result_prod->fetch_assoc()) {
|
||||||
|
$products[] = $row;
|
||||||
|
}
|
||||||
|
$stmt_prod->close();
|
||||||
?>
|
?>
|
||||||
|
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
@@ -32,6 +121,22 @@ if ($role !== 'admin') {
|
|||||||
<title>Gestione Prodotti - TechStore</title>
|
<title>Gestione Prodotti - TechStore</title>
|
||||||
<link rel="stylesheet" href="/assets/style.css">
|
<link rel="stylesheet" href="/assets/style.css">
|
||||||
<script src="/assets/js/cart.js"></script>
|
<script src="/assets/js/cart.js"></script>
|
||||||
|
<script>
|
||||||
|
|
||||||
|
function adjustStock(input, delta) {
|
||||||
|
var value = parseInt(input.value) + delta;
|
||||||
|
if (value < 0) value = 0;
|
||||||
|
input.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
function toggleAddProductPanel() {
|
||||||
|
var panel = document.getElementById('add-product-panel');
|
||||||
|
var button = document.getElementById('toggle-add-product');
|
||||||
|
if (!panel || !button) return;
|
||||||
|
var collapsed = panel.classList.toggle('collapsed');
|
||||||
|
button.textContent = collapsed ? 'Nuovo prodotto' : 'Chiudi aggiunta prodotto';
|
||||||
|
}
|
||||||
|
</script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<header style="display: flex; justify-content: space-between; align-items: center; padding: 10px; background-color: #f2f2f2; border-bottom: 1px solid #ddd;">
|
<header style="display: flex; justify-content: space-between; align-items: center; padding: 10px; background-color: #f2f2f2; border-bottom: 1px solid #ddd;">
|
||||||
@@ -55,13 +160,96 @@ if ($role !== 'admin') {
|
|||||||
<li><a href="/admin/manageOrders.php">Gestione ordini</a></li>
|
<li><a href="/admin/manageOrders.php">Gestione ordini</a></li>
|
||||||
<li><a href="/admin/manageProducts.php">Gestione prodotti</a></li>
|
<li><a href="/admin/manageProducts.php">Gestione prodotti</a></li>
|
||||||
<li><a href="/admin/manageUsers.php">Gestione utenti</a></li>
|
<li><a href="/admin/manageUsers.php">Gestione utenti</a></li>
|
||||||
<hr>
|
<hr class="admin-divider">
|
||||||
<li><a href="../account.php">Ritorna al tuo account</a></li>
|
<li><a href="../account.php">Ritorna al tuo account</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
<div class="my-container">
|
<div class="my-container">
|
||||||
<h1>Gestione prodotti</h1>
|
<h1>Gestione Prodotti</h1>
|
||||||
<p>WIP</p>
|
<button id="toggle-add-product" class="toggle-button" type="button" onclick="toggleAddProductPanel()">Nuovo prodotto</button>
|
||||||
|
<section id="add-product-panel" class="add-product-panel collapsed">
|
||||||
|
<h2>Aggiungi nuovo prodotto</h2>
|
||||||
|
<form class="add-product-form" action="" method="post" enctype="multipart/form-data">
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="new-name">Nome</label>
|
||||||
|
<input id="new-name" class="product-name-input" type="text" name="name" required>
|
||||||
|
</div>
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="new-description">Descrizione</label>
|
||||||
|
<textarea id="new-description" class="product-description-textarea" name="description" required></textarea>
|
||||||
|
</div>
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="new-category">Categoria</label>
|
||||||
|
<select id="new-category" class="product-category-select" name="category_id" required>
|
||||||
|
<?php foreach ($categories as $cat): ?>
|
||||||
|
<option value="<?php echo $cat['CategoryID']; ?>"><?php echo htmlspecialchars($cat['Name']); ?></option>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="new-price">Prezzo</label>
|
||||||
|
<input id="new-price" class="product-price-input" type="number" name="price" step="0.01" required>
|
||||||
|
</div>
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="new-stock">Stock</label>
|
||||||
|
<input id="new-stock" class="stock-input" type="number" name="stock" min="0" value="0" required>
|
||||||
|
</div>
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="new-image">Immagine</label>
|
||||||
|
<input id="new-image" class="product-image-input" type="file" name="image" accept="image/*">
|
||||||
|
</div>
|
||||||
|
<button class="save-button" type="submit" name="add">Aggiungi prodotto</button>
|
||||||
|
</form>
|
||||||
|
</section>
|
||||||
|
<table class="product-table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>ID</th>
|
||||||
|
<th>Nome</th>
|
||||||
|
<th>Descrizione</th>
|
||||||
|
<th>Categoria</th>
|
||||||
|
<th>Prezzo</th>
|
||||||
|
<th>Stock</th>
|
||||||
|
<th>Immagine</th>
|
||||||
|
<th>Azioni</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<?php foreach ($products as $product): ?>
|
||||||
|
<tr class="product-row">
|
||||||
|
<form class="product-form" action="" method="post" enctype="multipart/form-data">
|
||||||
|
<td class="product-id-cell"><?php echo $product['ProductID']; ?><input type="hidden" name="product_id" value="<?php echo $product['ProductID']; ?>"></td>
|
||||||
|
<td class="product-name-cell"><input class="product-name-input" type="text" name="name" value="<?php echo htmlspecialchars($product['ProductName']); ?>" required></td>
|
||||||
|
<td class="product-description-cell"><textarea class="product-description-textarea" name="description" required><?php echo htmlspecialchars($product['Description']); ?></textarea></td>
|
||||||
|
<td class="product-category-cell">
|
||||||
|
<select class="product-category-select" name="category_id" required>
|
||||||
|
<?php foreach ($categories as $cat): ?>
|
||||||
|
<option value="<?php echo $cat['CategoryID']; ?>" <?php if ($cat['CategoryID'] == $product['CategoryID']) echo 'selected'; ?>><?php echo htmlspecialchars($cat['Name']); ?></option>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</select>
|
||||||
|
</td>
|
||||||
|
<td class="product-price-cell"><input class="product-price-input" type="number" name="price" step="0.01" value="<?php echo $product['Price']; ?>" required></td>
|
||||||
|
<td class="product-stock-cell">
|
||||||
|
<div class="stock-control">
|
||||||
|
<button type="button" class="stock-button" onclick="adjustStock(this.nextElementSibling, -1)">-</button>
|
||||||
|
<input class="stock-input" type="number" name="stock" min="0" value="<?php echo $product['StockQuantity']; ?>" required>
|
||||||
|
<button type="button" class="stock-button" onclick="adjustStock(this.previousElementSibling, 1)">+</button>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
<td class="product-image-cell">
|
||||||
|
<?php $image_display = '/img/' . ltrim($product['ImagePath'], '/'); ?>
|
||||||
|
<img class="product-image-preview" src="<?php echo htmlspecialchars($image_display); ?>" alt="Immagine"><br>
|
||||||
|
<div class="image-upload">
|
||||||
|
<input class="product-image-input" type="file" name="image" accept="image/*">
|
||||||
|
<input type="hidden" name="current_image" value="<?php echo htmlspecialchars(basename($product['ImagePath'])); ?>">
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
<td class="product-action-cell"><button class="save-button" type="submit" name="save">Salva</button></td>
|
||||||
|
</form>
|
||||||
|
</tr>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<?php else: ?>
|
<?php else: ?>
|
||||||
|
|||||||
@@ -106,7 +106,7 @@ if ($result === false) {
|
|||||||
<li><a href="/admin/manageOrders.php">Gestione ordini</a></li>
|
<li><a href="/admin/manageOrders.php">Gestione ordini</a></li>
|
||||||
<li><a href="/admin/manageProducts.php">Gestione prodotti</a></li>
|
<li><a href="/admin/manageProducts.php">Gestione prodotti</a></li>
|
||||||
<li><a href="/admin/manageUsers.php">Gestione utenti</a></li>
|
<li><a href="/admin/manageUsers.php">Gestione utenti</a></li>
|
||||||
<hr>
|
<hr class="admin-divider">
|
||||||
<li><a href="../account.php">Ritorna al tuo account</a></li>
|
<li><a href="../account.php">Ritorna al tuo account</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -256,6 +256,149 @@ form button:hover {
|
|||||||
padding: 20px;
|
padding: 20px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.product-table {
|
||||||
|
width: 100%;
|
||||||
|
border-collapse: collapse;
|
||||||
|
margin-top: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.product-table th,
|
||||||
|
.product-table td {
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
padding: 12px;
|
||||||
|
vertical-align: top;
|
||||||
|
}
|
||||||
|
|
||||||
|
.product-table th {
|
||||||
|
background-color: #f8f9fa;
|
||||||
|
font-weight: 700;
|
||||||
|
text-align: left;
|
||||||
|
}
|
||||||
|
|
||||||
|
.product-row:hover {
|
||||||
|
background-color: #f7f9fb;
|
||||||
|
}
|
||||||
|
|
||||||
|
.add-product-panel {
|
||||||
|
margin-bottom: 24px;
|
||||||
|
padding: 18px;
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
border-radius: 8px;
|
||||||
|
background-color: #fdfdfd;
|
||||||
|
}
|
||||||
|
|
||||||
|
.add-product-panel h2 {
|
||||||
|
margin-top: 0;
|
||||||
|
margin-bottom: 16px;
|
||||||
|
color: #333;
|
||||||
|
font-size: 1.25rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.add-product-form {
|
||||||
|
display: grid;
|
||||||
|
gap: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.add-product-form .form-row {
|
||||||
|
display: grid;
|
||||||
|
gap: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.add-product-form label {
|
||||||
|
font-weight: 600;
|
||||||
|
color: #444;
|
||||||
|
}
|
||||||
|
|
||||||
|
.toggle-button {
|
||||||
|
padding: 10px 16px;
|
||||||
|
background-color: #17a2b8;
|
||||||
|
color: #fff;
|
||||||
|
border: none;
|
||||||
|
border-radius: 4px;
|
||||||
|
cursor: pointer;
|
||||||
|
margin-bottom: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.toggle-button:hover {
|
||||||
|
background-color: #117a8b;
|
||||||
|
}
|
||||||
|
|
||||||
|
.add-product-panel.collapsed {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.product-name-input,
|
||||||
|
.product-description-textarea,
|
||||||
|
.product-category-select,
|
||||||
|
.product-price-input,
|
||||||
|
.stock-input,
|
||||||
|
.product-image-input {
|
||||||
|
width: 100%;
|
||||||
|
padding: 10px;
|
||||||
|
border: 1px solid #ccc;
|
||||||
|
border-radius: 4px;
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
|
||||||
|
.product-description-textarea {
|
||||||
|
min-height: 80px;
|
||||||
|
resize: vertical;
|
||||||
|
}
|
||||||
|
|
||||||
|
.stock-control {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.stock-button {
|
||||||
|
width: 32px;
|
||||||
|
height: 32px;
|
||||||
|
border: none;
|
||||||
|
border-radius: 4px;
|
||||||
|
background-color: #007bff;
|
||||||
|
color: #fff;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.stock-button:hover {
|
||||||
|
background-color: #0056b3;
|
||||||
|
}
|
||||||
|
|
||||||
|
.product-image-preview {
|
||||||
|
max-width: 100px;
|
||||||
|
display: block;
|
||||||
|
margin-bottom: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.image-upload {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.save-button {
|
||||||
|
padding: 10px 16px;
|
||||||
|
background-color: #28a745;
|
||||||
|
color: #fff;
|
||||||
|
border: none;
|
||||||
|
border-radius: 4px;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.save-button:hover {
|
||||||
|
background-color: #218838;
|
||||||
|
}
|
||||||
|
|
||||||
|
.admin-divider{
|
||||||
|
display: block;
|
||||||
|
height: 1px;
|
||||||
|
border: 0;
|
||||||
|
border-top: 1px solid #ddd;
|
||||||
|
margin: 1em 0;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
|
||||||
.admin-stats-grid {
|
.admin-stats-grid {
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
|
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
|
||||||
|
|||||||
Reference in New Issue
Block a user