I'm doing an extension for the Full Calendar application, a function that moves the events that are after the new one.
The function is made to move the events between Monday and Thursday, for example, If you add a new event in Monday, it will move all the events in the same day and forward days while checking if is Monday to move it to Thrusday and the same for Thursday to move it to Monday.
The trouble is that if you add a new event on Friday, Saturday, Wednesday, etc., the function will recognize it wrong, and it'll says that need to move the forward days, as is it programmed, from Monday to Thursday and Thursday to Monday.
This is the code of the function in PhP (I can already add, move and delete the events in FullCalendar, so this is just the problem).
<?php
// Connect to db
require_once('newConnection.php');
function moveEvents($myStart,$myEnd){
// Start connection
$bdd = connect();
// Get date values of the new event
$currentStart = $myStart;
$currentEnd = $myEnd;
// Establish Day classes
$dayMonday = "Monday";
$dayThursday = "Thursday";
// Call function to get ID of the new event
$finalId = getIdEvent($bdd);
// Query SQL
$sql = "SELECT * FROM events";
$sth = $bdd->query($sql);
// Move between the events
while($row = mysqli_fetch_array($sth, MYSQLI_BOTH)) {
// Get values of the actual event
$start = $row["start"];
$end = $row["end"];
$id = $row["id"];
// Check if the date of the current event is after of the new one
if(($start > $currentStart) == true){
// Create aux date objects
$updatedStart = date_create($start);
$updatedEnd = date_create($end);
// Get the day of the current event to know if it is Monday or Thrusday
$day = date_format($updatedStart, 'l');
// If is Monday
if(strcmp($day, $dayMonday) === 0) {
// Modify date
date_modify($updatedStart, "+3 days");
date_modify($updatedEnd, "+3 days");
// Save updates
$start_db = date_format($updatedStart, 'Y-m-d H:i:s');
$sql = "UPDATE events SET start = '$start_db' WHERE id = $id ";
saveMoves($bdd,$sql);
// Save updates
$end_db = date_format($updatedEnd, 'Y-m-d H:i:s');
$sql = "UPDATE events SET end = '$end_db' WHERE id = $id ";
saveMoves($bdd,$sql);
}
// If is Thursday
else if(strcmp($day, $dayThursday) === 0) {
// Modify date
date_modify($updatedStart, "+4 days");
date_modify($updatedEnd, "+4 days");
// Save Updates
$start_db = date_format($updatedStart, 'Y-m-d H:i:s');
$sql = "UPDATE events SET start = '$start_db' WHERE id = $id ";
saveMoves($bdd,$sql);
// Save Updates
$end_db = date_format($updatedEnd, 'Y-m-d H:i:s');
$sql = "UPDATE events SET end = '$end_db' WHERE id = $id ";
// Guardar fecha fin updateada
saveMoves($bdd,$sql);
}
}
// If the date of the current event is the same of the new one and the ID of the event is not the same
else if((($start == $currentStart) == true) && $id != $finalId){
// Create date aux objects
$updatedStart = date_create($start);
$updatedEnd = date_create($end);
// Get the day of the current event to know if it is Monday or Thrusday
$day = date_format($updatedStart, 'l');
// If is Monday
if(strcmp($day, $dayMonday) === 0){
// Modify date
date_modify($updatedStart, "+3 days");
date_modify($updatedEnd, "+3 days");
// Save updates
$start_db = date_format($updatedStart, 'Y-m-d H:i:s');
$sql = "UPDATE events SET start = '$start_db' WHERE id = $id ";
saveMoves($bdd,$sql);
// Save updates
$end_db = date_format($updatedEnd, 'Y-m-d H:i:s');
$sql = "UPDATE events SET end = '$end_db' WHERE id = $id ";
saveMoves($bdd,$sql);
}
// If is Thursday
else if(strcmp($day, $dayThursday) === 0) {
// Modify date
date_modify($updatedStart, "+4 days");
date_modify($updatedEnd, "+4 days");
// Save updates
$start_db = date_format($updatedStart, 'Y-m-d H:i:s');
$sql = "UPDATE events SET start = '$start_db' WHERE id = $id ";
saveMoves($bdd,$sql);
// Save updates
$end_db = date_format($updatedEnd, 'Y-m-d H:i:s');
$sql = "UPDATE events SET end = '$end_db' WHERE id = $id ";
saveMoves($bdd,$sql);
}
}
}
mysqli_free_result($sth);
// Close connection to db
disconnect($bdd);
}
// Save updates Function
function saveMoves($bdd,$sql){
$query = $bdd->prepare( $sql );
if ($query == false){
print_r($bdd->errorInfo());
die ('Error al preparar');
}
$sth = $query->execute();
if ($sth == false) {
print_r($query->errorInfo());
die ('Error al ejecutar');
}
}
// Get ID Function
function getIdEvent($db){
// Especificación del SQL query
$query_2='SELECT * FROM events ORDER BY id DESC LIMIT 1';
// Ejecución del query, regresa el identificador del grupo
$result = $db->query($query_2);
$row = mysqli_fetch_array($result, MYSQLI_BOTH);
return $row["id"];
}
?>
Aucun commentaire:
Enregistrer un commentaire