86 lines
2.8 KiB
PHP
86 lines
2.8 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
|
|
ini_set('display_errors', 0);
|
|
error_reporting(0);
|
|
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
|
|
include('include/headscript.php');
|
|
|
|
$dbHandler = DBHandlerSelect::getInstance();
|
|
$pdo = $dbHandler->getConnection();
|
|
|
|
// Verifica utente loggato
|
|
if (!isset($iduserlogin)) {
|
|
echo json_encode(['type' => 'danger', 'message' => 'Sessione scaduta']);
|
|
exit;
|
|
}
|
|
|
|
// Prendi shop_id
|
|
$stmt = $pdo->prepare("SELECT id FROM shops WHERE owner_id = ? ORDER BY created_at ASC LIMIT 1");
|
|
$stmt->execute([$iduserlogin]);
|
|
$shop = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$shop) {
|
|
echo json_encode(['type' => 'danger', 'message' => 'Salone non trovato']);
|
|
exit;
|
|
}
|
|
|
|
$shop_id = (int)$shop['id'];
|
|
|
|
try {
|
|
$days = ['0', '1', '2', '3', '4', '5', '6'];
|
|
|
|
$pdo->beginTransaction();
|
|
|
|
// Cancella vecchi orari
|
|
$pdo->prepare("DELETE FROM shop_hours WHERE shop_id = ?")->execute([$shop_id]);
|
|
|
|
foreach ($days as $day) {
|
|
$is_open = isset($_POST["is_open_$day"]) ? 1 : 0;
|
|
$open_time = !empty($_POST["open_time_$day"]) ? $_POST["open_time_$day"] : null;
|
|
$close_time = !empty($_POST["close_time_$day"]) ? $_POST["close_time_$day"] : null;
|
|
$open_time_2 = !empty($_POST["open_time_2_$day"]) ? $_POST["open_time_2_$day"] : null;
|
|
$close_time_2 = !empty($_POST["close_time_2_$day"]) ? $_POST["close_time_2_$day"] : null;
|
|
$notes = trim($_POST["notes_$day"] ?? '');
|
|
|
|
// Validazione
|
|
if ($is_open) {
|
|
if (!$open_time || !$close_time) {
|
|
throw new Exception("Per i giorni aperti è obbligatorio impostare orario apertura e chiusura.");
|
|
}
|
|
if (strtotime($close_time) <= strtotime($open_time)) {
|
|
throw new Exception("L'orario di chiusura deve essere dopo l'apertura.");
|
|
}
|
|
if ($open_time_2 && $close_time_2 && strtotime($close_time_2) <= strtotime($open_time_2)) {
|
|
throw new Exception("Il secondo orario di chiusura deve essere dopo l'apertura.");
|
|
}
|
|
}
|
|
|
|
$stmt = $pdo->prepare("
|
|
INSERT INTO shop_hours
|
|
(shop_id, day_of_week, is_open, open_time, close_time, open_time_2, close_time_2, notes)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
");
|
|
$stmt->execute([
|
|
$shop_id,
|
|
$day,
|
|
$is_open,
|
|
$open_time,
|
|
$close_time,
|
|
$open_time_2,
|
|
$close_time_2,
|
|
$notes ?: null
|
|
]);
|
|
}
|
|
|
|
$pdo->commit();
|
|
echo json_encode(['type' => 'success', 'message' => 'Orari di apertura salvati con successo!']);
|
|
} catch (Throwable $e) {
|
|
$pdo->rollBack();
|
|
echo json_encode(['type' => 'danger', 'message' => 'Errore: ' . $e->getMessage()]);
|
|
}
|