push notification setting and update pages with PDO
This commit is contained in:
+92
-82
@@ -1,95 +1,105 @@
|
||||
<?php require_once('Connections/bkngstm.php'); ?>
|
||||
<?php require_once('webassist/mysqli/rsobj.php'); ?>
|
||||
<?php require_once('webassist/mysqli/queryobj.php'); ?>
|
||||
<?php if (isset($_POST['propagatestartdate'])) { $propagatestartdate=$_POST['propagatestartdate']; } ?>
|
||||
<?php if (isset($_POST['propagateenddate'])) { $propagateenddate=$_POST['propagateenddate']; } ?>
|
||||
<?php if (isset($_POST['idservice'])) { $idservice=$_POST['idservice']; } ?>
|
||||
<?php if (isset($_POST['dayclass'])) { $dayclass=$_POST['dayclass']; } ?>
|
||||
<?php if (isset($_POST['timeclass'])) { $timeclass=$_POST['timeclass']; } ?>
|
||||
<?php if (isset($_POST['durationtime'])) { $durationtime=$_POST['durationtime']; }
|
||||
|
||||
?>
|
||||
<?php //propagate classes
|
||||
|
||||
// Create connection
|
||||
$conn = new mysqli($servername, $username, $password, $dbname);
|
||||
?>
|
||||
<?php //fake variable
|
||||
//$propagateenddate="2024-12-01";
|
||||
//$dayclass="Tuesday";
|
||||
//$idservice="1";
|
||||
//$timeclass="18:30";
|
||||
//$durationtime="1";
|
||||
?>
|
||||
<?php
|
||||
//define next day
|
||||
require_once('include/headscript.php');
|
||||
|
||||
$nextClassDay = strtotime($propagatestartdate); // Utilizza la data di propagazione come data di inizio
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
$dayOfWeek = date('w', $nextClassDay); // Ottieni il giorno della settimana della data di partenza
|
||||
$pdo = DBHandlerSelect::getInstance()->getConnection();
|
||||
|
||||
// Mappa dei nomi dei giorni in inglese ai valori numerici dei giorni della settimana
|
||||
$daysOfWeek = array(
|
||||
"Sunday" => 0,
|
||||
"Monday" => 1,
|
||||
"Tuesday" => 2,
|
||||
"Wednesday" => 3,
|
||||
"Thursday" => 4,
|
||||
"Friday" => 5,
|
||||
"Saturday" => 6
|
||||
// --- 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)"
|
||||
);
|
||||
|
||||
// Ottieni il valore numerico del giorno della settimana desiderato
|
||||
$desiredDayValue = $daysOfWeek[$dayclass];
|
||||
$inserted = 0;
|
||||
|
||||
// Calcola quanti giorni mancano fino al prossimo o stesso giorno della settimana desiderato
|
||||
$daysUntilNextDayClass = ($desiredDayValue + 7 - $dayOfWeek) % 7;
|
||||
while (date('Y-m-d', $currentTs) <= $dateEnd) {
|
||||
$currentDate = date('Y-m-d', $currentTs);
|
||||
$datetimeSchedule = $currentDate . ' ' . $timeclass;
|
||||
|
||||
// Aggiungi il numero di giorni al timestamp della data di partenza per ottenere il primo giorno della settimana desiderato
|
||||
$firstDayClassTimestamp = $nextClassDay + $daysUntilNextDayClass * 24 * 60 * 60;
|
||||
// La data è già schedulata per questo servizio?
|
||||
$checkClass->execute([
|
||||
':dateschedule' => $datetimeSchedule,
|
||||
':idservice' => $idservice,
|
||||
]);
|
||||
$alreadyScheduled = $checkClass->fetchColumn();
|
||||
|
||||
$firstDayClassDate = date('Y-m-d', $firstDayClassTimestamp);
|
||||
// È 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++;
|
||||
}
|
||||
|
||||
$datenext = $firstDayClassDate;
|
||||
$datenextTimestamp=strtotime($datenext);
|
||||
|
||||
$endClassDay = strtotime($propagateenddate); // Utilizza la data di propagazione come data di inizio
|
||||
|
||||
$dateend = date('Y-m-d', $endClassDay); // stop date
|
||||
|
||||
|
||||
|
||||
while ($datenext <= $dateend) {
|
||||
// merge time with date
|
||||
$datenextschedule=$datenext.' '.$timeclass;
|
||||
|
||||
//query to check if present
|
||||
$checkdateclass = new WA_MySQLi_RS("checkdateclass",$bkngstm,0);
|
||||
$checkdateclass->setQuery("SELECT * FROM serviceschedule WHERE serviceschedule.dateschedule='$datenextschedule' AND serviceschedule.idservice='$idservice'");
|
||||
$checkdateclass->execute();
|
||||
|
||||
// Query per verificare se il giorno è un giorno di pausa
|
||||
$checkdayoff = new WA_MySQLi_RS("checkdayoff", $bkngstm, 0);
|
||||
$checkdayoff->setQuery("SELECT * FROM dayoff WHERE dayoffdate = '$datenext'");
|
||||
$checkdayoff->execute();
|
||||
|
||||
|
||||
|
||||
if (empty($checkdateclass->getColumnVal("idserviceschedule")) && empty($checkdayoff->getColumnVal("iddayoff"))) {
|
||||
$sql = "INSERT INTO serviceschedule (idservice, dateschedule, scheduleday, startingtime, durationtime)
|
||||
VALUES ($idservice, '$datenextschedule', '$dayclass', '$timeclass', $durationtime)";
|
||||
if ($conn->query($sql) === TRUE) {
|
||||
echo "New record created successfully";
|
||||
} else {
|
||||
echo "Error: " . $sql . "<br>" . $conn->error;
|
||||
// Avanza di 7 giorni
|
||||
$currentTs = strtotime('+7 day', $currentTs);
|
||||
}
|
||||
|
||||
}
|
||||
$datenextTimestamp =strtotime("+7 day", $datenextTimestamp);
|
||||
$datenext=date('Y-m-d', $datenextTimestamp);
|
||||
echo $nextClassDay;
|
||||
echo $datenext;
|
||||
}
|
||||
|
||||
header("Location: admin-services.php?message=success"); ?>
|
||||
header('Location: admin-services.php?message=success');
|
||||
exit;
|
||||
|
||||
Reference in New Issue
Block a user