129 lines
4.0 KiB
PHP
129 lines
4.0 KiB
PHP
<?php
|
|
// admin_plan_action.php — Azioni admin sui piani di abbonamento (crea/modifica/elimina/attiva)
|
|
// 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_plans.php");
|
|
exit;
|
|
}
|
|
|
|
$action = trim($_POST['action'] ?? '');
|
|
$plan_id = (int)($_POST['plan_id'] ?? 0);
|
|
|
|
function back(): void
|
|
{
|
|
header("Location: admin_plans.php");
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
// ===== Crea / modifica =====
|
|
if ($action === 'save') {
|
|
$name = trim($_POST['name'] ?? '');
|
|
$desc = trim($_POST['description'] ?? '');
|
|
$price = (float)str_replace(',', '.', $_POST['price'] ?? '0');
|
|
$period = trim($_POST['billing_period'] ?? 'monthly');
|
|
$active = (int)($_POST['is_active'] ?? 0) === 1 ? 1 : 0;
|
|
|
|
if ($name === '') {
|
|
setFlash('danger', 'Il nome del piano è obbligatorio.');
|
|
back();
|
|
}
|
|
if (!in_array($period, ['monthly', 'yearly'], true)) {
|
|
setFlash('danger', 'Periodo di fatturazione non valido.');
|
|
back();
|
|
}
|
|
if ($price < 0) {
|
|
setFlash('danger', 'Il prezzo non può essere negativo.');
|
|
back();
|
|
}
|
|
|
|
if ($plan_id > 0) {
|
|
// Modifica
|
|
$upd = $pdo->prepare("
|
|
UPDATE subscription_plans
|
|
SET name = ?, description = ?, price = ?, billing_period = ?, is_active = ?
|
|
WHERE id = ?
|
|
");
|
|
$upd->execute([$name, ($desc ?: null), $price, $period, $active, $plan_id]);
|
|
setFlash('success', 'Piano aggiornato.');
|
|
} else {
|
|
// Crea
|
|
$ins = $pdo->prepare("
|
|
INSERT INTO subscription_plans (name, description, price, billing_period, is_active)
|
|
VALUES (?, ?, ?, ?, ?)
|
|
");
|
|
$ins->execute([$name, ($desc ?: null), $price, $period, $active]);
|
|
setFlash('success', 'Piano creato.');
|
|
}
|
|
back();
|
|
}
|
|
|
|
// ===== Attiva / disattiva =====
|
|
if ($action === 'toggle_active') {
|
|
if ($plan_id <= 0) {
|
|
setFlash('danger', 'Piano non valido.');
|
|
back();
|
|
}
|
|
$new = (int)($_POST['is_active'] ?? 0) === 1 ? 1 : 0;
|
|
$upd = $pdo->prepare("UPDATE subscription_plans SET is_active = ? WHERE id = ?");
|
|
$upd->execute([$new, $plan_id]);
|
|
setFlash('success', $new ? 'Piano attivato.' : 'Piano disattivato.');
|
|
back();
|
|
}
|
|
|
|
// ===== Elimina (solo se non in uso) =====
|
|
if ($action === 'delete') {
|
|
if ($plan_id <= 0) {
|
|
setFlash('danger', 'Piano non valido.');
|
|
back();
|
|
}
|
|
|
|
// È usato da qualche abbonamento? Se sì, NON eliminare.
|
|
$chk = $pdo->prepare("SELECT COUNT(*) FROM shop_subscriptions WHERE plan_id = ?");
|
|
$chk->execute([$plan_id]);
|
|
$inUse = (int)$chk->fetchColumn();
|
|
|
|
if ($inUse > 0) {
|
|
setFlash('warning', "Impossibile eliminare: il piano è usato da {$inUse} salone/i. Disattivalo invece, così non sarà più proponibile ma gli abbonamenti esistenti restano validi.");
|
|
back();
|
|
}
|
|
|
|
$del = $pdo->prepare("DELETE FROM subscription_plans WHERE id = ?");
|
|
$del->execute([$plan_id]);
|
|
setFlash('success', 'Piano eliminato.');
|
|
back();
|
|
}
|
|
|
|
setFlash('danger', 'Azione non riconosciuta.');
|
|
back();
|
|
} catch (Throwable $e) {
|
|
error_log('admin_plan_action: ' . $e->getMessage());
|
|
setFlash('danger', 'Operazione non riuscita.');
|
|
back();
|
|
}
|