calendar
This commit is contained in:
@@ -47,9 +47,12 @@ try {
|
||||
if ($action === 'bulk_delete_day_types') {
|
||||
$dates = $input['dates'] ?? [];
|
||||
$types = $input['types'] ?? [];
|
||||
$dates = array_filter($dates, function($d) {
|
||||
return preg_match('/^\d{4}-\d{2}-\d{2}$/', $d);
|
||||
});
|
||||
|
||||
if (empty($dates) || empty($types)) {
|
||||
echo json_encode(['status' => 'success']); // Rien à faire
|
||||
echo json_encode(['status' => 'success']);
|
||||
exit;
|
||||
}
|
||||
|
||||
@@ -70,7 +73,11 @@ try {
|
||||
// --- SUPPRESSION TOTALE SUR DES DATES ---
|
||||
if ($action === 'bulk_delete_all') {
|
||||
$dates = $input['dates'] ?? [];
|
||||
if (empty($dates)) {
|
||||
$dates = array_filter($dates, function($d) {
|
||||
return preg_match('/^\d{4}-\d{2}-\d{2}$/', $d);
|
||||
});
|
||||
|
||||
if (empty($dates) || empty($types)) {
|
||||
echo json_encode(['status' => 'success']);
|
||||
exit;
|
||||
}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
<?php
|
||||
// includes/api/save-events.php
|
||||
header('Content-Type: application/json');
|
||||
require __DIR__ . '/../../../../includes/db.php';
|
||||
|
||||
$eventsToSave = json_decode(file_get_contents('php://input'), true);
|
||||
$rawInput = file_get_contents('php://input');
|
||||
$eventsToSave = json_decode($rawInput, true);
|
||||
|
||||
// Optionnel : tu peux commenter/supprimer ces logs en production pour économiser du disque
|
||||
file_put_contents(
|
||||
__DIR__ . '/events-debug.log',
|
||||
"[" . date('c') . "] RAW INPUT: " . $rawInput . PHP_EOL,
|
||||
FILE_APPEND
|
||||
);
|
||||
|
||||
$eventsToSave = json_decode($rawInput, true);
|
||||
|
||||
|
||||
if (empty($eventsToSave) || !is_array($eventsToSave)) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['status' => 'error', 'message' => 'Aucune donnée d\'événement reçue.']);
|
||||
@@ -22,6 +22,14 @@ if (empty($eventsToSave) || !is_array($eventsToSave)) {
|
||||
$inserted = [];
|
||||
|
||||
try {
|
||||
// 1. OPTIMISATION : On récupère toutes les personnes d'un coup (Mapping)
|
||||
$stmtPeople = $pdo->query("SELECT id, name FROM pf_people");
|
||||
$peopleMap = [];
|
||||
while ($row = $stmtPeople->fetch(PDO::FETCH_ASSOC)) {
|
||||
// On crée un tableau associatif : ['Carole' => 1, 'Alex' => 2, etc.]
|
||||
$peopleMap[$row['name']] = $row['id'];
|
||||
}
|
||||
|
||||
$pdo->beginTransaction();
|
||||
|
||||
$sql = "INSERT INTO pf_events (event_date, event_type, person_id, duration)
|
||||
@@ -31,14 +39,9 @@ try {
|
||||
foreach ($eventsToSave as $event) {
|
||||
$person_id = null;
|
||||
|
||||
// Si un nom de personne est fourni
|
||||
if (!empty($event['person'])) {
|
||||
$personStmt = $pdo->prepare("SELECT id FROM pf_people WHERE name = ?");
|
||||
$personStmt->execute([$event['person']]);
|
||||
$personRow = $personStmt->fetch();
|
||||
if ($personRow) {
|
||||
$person_id = $personRow['id'];
|
||||
}
|
||||
// 2. On vérifie simplement dans notre tableau (plus de requête SQL ici !)
|
||||
if (!empty($event['person']) && isset($peopleMap[$event['person']])) {
|
||||
$person_id = $peopleMap[$event['person']];
|
||||
}
|
||||
|
||||
$stmt->execute([
|
||||
@@ -68,4 +71,4 @@ try {
|
||||
$pdo->rollBack();
|
||||
http_response_code(500);
|
||||
echo json_encode(['status' => 'error', 'message' => 'Erreur lors de la sauvegarde : ' . $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
header('Content-Type: application/json');
|
||||
require __DIR__ . '/../../../../includes/db.php';
|
||||
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
if (!$input || empty($input['person_id']) || empty($input['leave_type']) || empty($input['snapshot_date']) || !isset($input['remaining_balance'])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['status' => 'error', 'message' => 'Données manquantes.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
// Étape 1 : Par sécurité, on supprime un éventuel snapshot existant exactement à la même date pour cette personne et ce type
|
||||
$stmtDel = $pdo->prepare("DELETE FROM pf_leave_snapshots WHERE person_id = ? AND leave_type = ? AND snapshot_date = ?");
|
||||
$stmtDel->execute([$input['person_id'], $input['leave_type'], $input['snapshot_date']]);
|
||||
|
||||
// Étape 2 : On insère le nouveau solde
|
||||
$stmt = $pdo->prepare("INSERT INTO pf_leave_snapshots (person_id, leave_type, snapshot_date, remaining_balance) VALUES (?, ?, ?, ?)");
|
||||
$stmt->execute([
|
||||
$input['person_id'],
|
||||
$input['leave_type'],
|
||||
$input['snapshot_date'],
|
||||
$input['remaining_balance']
|
||||
]);
|
||||
|
||||
echo json_encode(['status' => 'success']);
|
||||
} catch (PDOException $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
|
||||
}
|
||||
Reference in New Issue
Block a user