106 lines
3.2 KiB
PHP
106 lines
3.2 KiB
PHP
<?php
|
|
require_once('include/headscript.php');
|
|
|
|
/**
|
|
* Propagazione di una classe sul calendario (serviceschedule).
|
|
* Genera le occorrenze settimanali tra data inizio e data fine,
|
|
* saltando le date già presenti e i giorni di pausa (dayoff).
|
|
*
|
|
* Script di sola logica: nessun output, redirect finale.
|
|
*/
|
|
|
|
$pdo = DBHandlerSelect::getInstance()->getConnection();
|
|
|
|
// --- Input dal form (con default vuoti) ---
|
|
$propagatestartdate = trim($_POST['propagatestartdate'] ?? '');
|
|
$propagateenddate = trim($_POST['propagateenddate'] ?? '');
|
|
$idservice = isset($_POST['idservice']) ? (int) $_POST['idservice'] : 0;
|
|
$dayclass = trim($_POST['dayclass'] ?? '');
|
|
$timeclass = trim($_POST['timeclass'] ?? '');
|
|
$durationtime = isset($_POST['durationtime']) ? (float) $_POST['durationtime'] : 0;
|
|
|
|
// --- Mappa giorni settimana ---
|
|
$daysOfWeek = [
|
|
"Sunday" => 0,
|
|
"Monday" => 1,
|
|
"Tuesday" => 2,
|
|
"Wednesday" => 3,
|
|
"Thursday" => 4,
|
|
"Friday" => 5,
|
|
"Saturday" => 6,
|
|
];
|
|
|
|
// --- Validazione minima: senza questi non si procede ---
|
|
if (
|
|
$propagatestartdate === '' || $propagateenddate === ''
|
|
|| $idservice <= 0 || !isset($daysOfWeek[$dayclass])
|
|
) {
|
|
header('Location: admin-services.php?message=error');
|
|
exit;
|
|
}
|
|
|
|
// --- Calcolo della prima occorrenza del giorno desiderato ---
|
|
$startTs = strtotime($propagatestartdate);
|
|
$endTs = strtotime($propagateenddate);
|
|
|
|
if ($startTs === false || $endTs === false) {
|
|
header('Location: admin-services.php?message=error');
|
|
exit;
|
|
}
|
|
|
|
$dayOfWeekStart = (int) date('w', $startTs);
|
|
$desiredDayValue = $daysOfWeek[$dayclass];
|
|
$daysUntilNext = ($desiredDayValue + 7 - $dayOfWeekStart) % 7;
|
|
|
|
$currentTs = $startTs + $daysUntilNext * 86400;
|
|
$dateEnd = date('Y-m-d', $endTs);
|
|
|
|
// --- Statement preparati una sola volta e riusati nel loop ---
|
|
$checkClass = $pdo->prepare(
|
|
"SELECT idserviceschedule FROM serviceschedule
|
|
WHERE dateschedule = :dateschedule AND idservice = :idservice
|
|
LIMIT 1"
|
|
);
|
|
$checkDayoff = $pdo->prepare(
|
|
"SELECT iddayoff FROM dayoff WHERE dayoffdate = :dayoffdate LIMIT 1"
|
|
);
|
|
$insertSchedule = $pdo->prepare(
|
|
"INSERT INTO serviceschedule (idservice, dateschedule, scheduleday, startingtime, durationtime)
|
|
VALUES (:idservice, :dateschedule, :scheduleday, :startingtime, :durationtime)"
|
|
);
|
|
|
|
$inserted = 0;
|
|
|
|
while (date('Y-m-d', $currentTs) <= $dateEnd) {
|
|
$currentDate = date('Y-m-d', $currentTs);
|
|
$datetimeSchedule = $currentDate . ' ' . $timeclass;
|
|
|
|
// La data è già schedulata per questo servizio?
|
|
$checkClass->execute([
|
|
':dateschedule' => $datetimeSchedule,
|
|
':idservice' => $idservice,
|
|
]);
|
|
$alreadyScheduled = $checkClass->fetchColumn();
|
|
|
|
// È un giorno di pausa?
|
|
$checkDayoff->execute([':dayoffdate' => $currentDate]);
|
|
$isDayoff = $checkDayoff->fetchColumn();
|
|
|
|
if (!$alreadyScheduled && !$isDayoff) {
|
|
$insertSchedule->execute([
|
|
':idservice' => $idservice,
|
|
':dateschedule' => $datetimeSchedule,
|
|
':scheduleday' => $dayclass,
|
|
':startingtime' => $timeclass,
|
|
':durationtime' => $durationtime,
|
|
]);
|
|
$inserted++;
|
|
}
|
|
|
|
// Avanza di 7 giorni
|
|
$currentTs = strtotime('+7 day', $currentTs);
|
|
}
|
|
|
|
header('Location: admin-services.php?message=success');
|
|
exit;
|