Files
comelifacciamo/public/userarea/admin_shop_action.php
T
2026-09-09 08:26:26 +02:00

109 lines
3.4 KiB
PHP

<?php
// admin_shop_action.php — Azioni admin sui saloni
// Gestisce: attiva/disattiva, modifica dati, elimina. Solo ROLE_ADMIN.
ini_set('display_errors', 1);
error_reporting(E_ALL);
if (session_status() === PHP_SESSION_NONE) session_start();
include('include/headscript.php');
$dbHandler = DBHandlerSelect::getInstance();
$pdo = $dbHandler->getConnection();
if (!isset($iduserlogin)) {
header("Location: login.php");
exit;
}
if ((int)$kindofrole !== ROLE_ADMIN) {
header("Location: index.php");
exit;
}
function setFlash(string $type, string $text): void
{
$_SESSION['flash'] = ['type' => $type, 'text' => $text];
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
setFlash('danger', 'Richiesta non valida.');
header("Location: admin_shops.php");
exit;
}
$shop_id = (int)($_POST['shop_id'] ?? 0);
$action = trim($_POST['action'] ?? '');
$ret = trim($_POST['ret'] ?? '');
function back(string $ret): void
{
header("Location: admin_shops.php" . ($ret !== '' ? '?' . $ret : ''));
exit;
}
if ($shop_id <= 0 || $action === '') {
setFlash('danger', 'Dati azione mancanti.');
back($ret);
}
try {
$s = $pdo->prepare("SELECT id FROM shops WHERE id = ?");
$s->execute([$shop_id]);
if (!$s->fetch()) {
setFlash('danger', 'Salone non trovato.');
back($ret);
}
// ===== Attiva / disattiva =====
if ($action === 'set_active') {
$active = (int)($_POST['active'] ?? 0) === 1 ? 1 : 0;
$upd = $pdo->prepare("UPDATE shops SET active = ? WHERE id = ?");
$upd->execute([$active, $shop_id]);
setFlash('success', $active ? 'Salone attivato.' : 'Salone disattivato.');
back($ret);
}
// ===== Modifica dati essenziali =====
if ($action === 'edit') {
$name = trim($_POST['name'] ?? '');
$address = trim($_POST['address'] ?? '');
$city = trim($_POST['city'] ?? '');
$province = trim($_POST['province'] ?? '');
$zip = trim($_POST['zip_code'] ?? '');
$phone = trim($_POST['phone'] ?? '');
$email = trim($_POST['email'] ?? '');
// Campi NOT NULL della tabella: name, address, city, zip_code
if ($name === '' || $address === '' || $city === '' || $zip === '') {
setFlash('danger', 'Nome, indirizzo, città e CAP sono obbligatori.');
back($ret);
}
$upd = $pdo->prepare("
UPDATE shops
SET name = ?, address = ?, city = ?, province = ?, zip_code = ?, phone = ?, email = ?
WHERE id = ?
");
$upd->execute([$name, $address, $city, ($province ?: null), $zip, ($phone ?: null), ($email ?: null), $shop_id]);
setFlash('success', 'Dati salone aggiornati.');
back($ret);
}
// ===== Elimina =====
if ($action === 'delete') {
// shops ha FK con ON DELETE CASCADE su molte tabelle figlie (staff, services,
// appointments...), quindi eliminare un salone rimuove anche i suoi dati collegati.
$del = $pdo->prepare("DELETE FROM shops WHERE id = ?");
$del->execute([$shop_id]);
setFlash('success', 'Salone eliminato (con tutti i dati collegati).');
back($ret);
}
setFlash('danger', 'Azione non riconosciuta.');
back($ret);
} catch (Throwable $e) {
error_log('admin_shop_action: ' . $e->getMessage());
setFlash('danger', 'Operazione non riuscita.');
back($ret);
}