From ff83320f9afe24f511d44151c7b7bb5ce7eeeab1 Mon Sep 17 00:00:00 2001 From: "fefe.clochette" Date: Sat, 20 Jun 2026 13:00:53 +0200 Subject: [PATCH] opti save --- .../holidays/includes/api/save_checkpoint.php | 92 ++++++++++++------- 1 file changed, 57 insertions(+), 35 deletions(-) diff --git a/modules/holidays/includes/api/save_checkpoint.php b/modules/holidays/includes/api/save_checkpoint.php index 313e8ec..1ab8209 100644 --- a/modules/holidays/includes/api/save_checkpoint.php +++ b/modules/holidays/includes/api/save_checkpoint.php @@ -4,57 +4,74 @@ require dirname(__DIR__, 4) . '/includes/auth.php'; require dirname(__DIR__, 4) . '/includes/db.php'; require_login(); -// INTERCEPTION AJAX : Sauvegarde du planning (Drag & Drop / Durée) ET Dépense Rapide +// --------------------------------=========================================== +// INTERCEPTIONS AJAX (Execution ultra rapide sans rechargement de page) +// --------------------------------=========================================== if (isset($_POST['action']) && in_array($_POST['action'], ['update_item_datetime', 'update_item_duration', 'add_single_item'])) { + header('Content-Type: application/json'); - // 🔥 NOUVEAU : Ajout sécurisé d'une dépense unique (Essence OSRM) + try { + // 🔥 AJOUT SÉCURISÉ D'UNE DÉPENSE UNIQUE (Essence OSRM) if ($_POST['action'] === 'add_single_item') { $holiday_id = (int)$_POST['holiday_id']; $sort_order = (int)$_POST['sort_order']; - // On récupère les infos de l'étape existante pour ne rien casser (lat, lng, dates...) + // On récupère proprement les métadonnées de l'étape pour lier le frais $stmt = $pdo->prepare("SELECT location_name, lat, lng, step_start_date, step_end_date, step_type FROM pf_holidays_items WHERE holiday_id = ? AND sort_order = ? LIMIT 1"); $stmt->execute([$holiday_id, $sort_order]); $stepInfo = $stmt->fetch(); if ($stepInfo) { - $ins = $pdo->prepare("INSERT INTO pf_holidays_items (holiday_id, category, name, amount, is_paid, location_name, lat, lng, sort_order, step_start_date, step_end_date, step_type, expense_context, duration) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); + $ins = $pdo->prepare("INSERT INTO pf_holidays_items (holiday_id, category, name, amount, is_paid, location_name, lat, lng, sort_order, step_start_date, step_end_date, step_type, expense_context, duration) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1)"); $ins->execute([ $holiday_id, $_POST['category'], $_POST['name'], (float)$_POST['amount'], 0, $stepInfo['location_name'], $stepInfo['lat'], $stepInfo['lng'], $sort_order, $stepInfo['step_start_date'], $stepInfo['step_end_date'], - $stepInfo['step_type'], $_POST['context'], 1 + $stepInfo['step_type'], $_POST['context'] ]); + echo json_encode(['success' => true]); + exit; } - echo json_encode(['success' => true]); + echo json_encode(['success' => false, 'error' => 'Etape introuvable']); exit; } - // --- Ancien code Drag&Drop conservé --- - $itemId = (int)$_POST['item_id']; - if ($_POST['action'] === 'update_item_datetime') { - $itemDate = !empty($_POST['item_date']) ? $_POST['item_date'] : null; - $itemTime = !empty($_POST['item_time']) ? $_POST['item_time'] : null; - $stmt = $pdo->prepare("UPDATE pf_holidays_items SET item_date = ?, item_time = ? WHERE id = ?"); - $stmt->execute([$itemDate, $itemTime, $itemId]); - } else if ($_POST['action'] === 'update_item_duration') { - $duration = (int)$_POST['duration']; - $stmt = $pdo->prepare("UPDATE pf_holidays_items SET duration = ? WHERE id = ?"); - $stmt->execute([$duration, $itemId]); + // Drag & Drop planning : Date et Heure + if ($_POST['action'] === 'update_item_datetime') { + $itemId = (int)$_POST['item_id']; + $itemDate = !empty($_POST['item_date']) ? $_POST['item_date'] : null; + $itemTime = !empty($_POST['item_time']) ? $_POST['item_time'] : null; + $stmt = $pdo->prepare("UPDATE pf_holidays_items SET item_date = ?, item_time = ? WHERE id = ?"); + $stmt->execute([$itemDate, $itemTime, $itemId]); + echo json_encode(['success' => true]); + exit; + } + + // Changement de durée (+/-) + if ($_POST['action'] === 'update_item_duration') { + $itemId = (int)$_POST['item_id']; + $duration = (int)$_POST['duration']; + $stmt = $pdo->prepare("UPDATE pf_holidays_items SET duration = ? WHERE id = ?"); + $stmt->execute([$duration, $itemId]); + echo json_encode(['success' => true]); + exit; + } + } catch (Exception $e) { + echo json_encode(['success' => false, 'error' => $e->getMessage()]); + exit; } - - echo json_encode(['success' => true]); - exit; } +// --------------------------------=========================================== +// FORMULAIRE TRADITIONNEL (Soumission de la modale d'étape) +// --------------------------------=========================================== $holiday_id = (int)$_POST['holiday_id']; -// ... (LE RESTE DE TON FICHIER NE CHANGE PAS) $location_name = trim($_POST['location_name']); $lat = (float)$_POST['lat']; $lng = (float)$_POST['lng']; $old_sort_order = isset($_POST['old_sort_order']) && $_POST['old_sort_order'] !== '' ? (int)$_POST['old_sort_order'] : null; -// 1. SUPPRESSION +// SUPPRESSION DE L'ÉTAPE if (isset($_POST['action_delete']) && $_POST['action_delete'] === '1') { $pdo->prepare("DELETE FROM pf_holidays_items WHERE holiday_id = ? AND sort_order = ?")->execute([$holiday_id, $old_sort_order]); header("Location: /holidays.php?tab=holiday_detail&id=" . $holiday_id); @@ -65,29 +82,24 @@ if ($holiday_id > 0 && !empty($location_name)) { try { $pdo->beginTransaction(); - // 2. DÉTERMINER L'ORDRE (Identifiant de l'étape) if ($old_sort_order !== null) { - // Modification : on supprime l'ancien contenu de cette étape précise $pdo->prepare("DELETE FROM pf_holidays_items WHERE holiday_id = ? AND sort_order = ?")->execute([$holiday_id, $old_sort_order]); $target_order = $old_sort_order; } else { - // Ajout : on place à la fin (Max + 1) $stmtMax = $pdo->prepare("SELECT MAX(sort_order) FROM pf_holidays_items WHERE holiday_id = ?"); $stmtMax->execute([$holiday_id]); $max = $stmtMax->fetchColumn(); $target_order = ($max !== null) ? (int)$max + 1 : 0; } - // Récupération des dates de l'étape globale et du type $step_start = !empty($_POST['step_start_date']) ? $_POST['step_start_date'] : null; $step_end = !empty($_POST['step_end_date']) ? $_POST['step_end_date'] : null; $step_type = $_POST['step_type'] ?? 'stop'; - // Nettoyage des dates selon le type d'étape - if ($step_type === 'origin') $step_end = null; // Un départ n'a pas de date de fin - if ($step_type === 'destination') $step_end = null; // Une arrivée finale n'a pas de date de départ + if ($step_type === 'origin' || $step_type === 'destination') { + $step_end = null; + } - // 3. INSERTION DES LIGNES $stmt = $pdo->prepare("INSERT INTO pf_holidays_items (holiday_id, category, name, amount, is_paid, location_name, lat, lng, sort_order, notes, item_date, item_time, step_start_date, step_end_date, duration, step_type, expense_context) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); $validItemsCount = 0; @@ -106,7 +118,7 @@ if ($holiday_id > 0 && !empty($location_name)) { $dur = !empty($_POST['items']['duration'][$i]) ? (int)$_POST['items']['duration'][$i] : 1; $context = !empty($_POST['items']['context'][$i]) ? $_POST['items']['context'][$i] : 'local'; - $stmt->execute([$holiday_id, $cat, $name ?: tr('hdl_default_exp_name'), $amount, $paid, $location_name, $lat, $lng, $target_order, $note, $date, $time, $step_start, $step_end, $dur, $step_type, $context]); + $stmt->execute([$holiday_id, $cat, $name, $amount, $paid, $location_name, $lat, $lng, $target_order, $note, $date, $time, $step_start, $step_end, $dur, $step_type, $context]); $validItemsCount++; } } @@ -116,16 +128,26 @@ if ($holiday_id > 0 && !empty($location_name)) { $stmt->execute([$holiday_id, 'activity', 'PF_TECHNICAL_POINT', 0, 1, $location_name, $lat, $lng, $target_order, '', null, null, $step_start, $step_end, 1, $step_type, 'local']); } - // 4. GESTION DU RETOUR (Si l'utilisateur définit cette étape comme point de retour) if (isset($_POST['set_as_return']) && $_POST['set_as_return'] == '1') { - // On enregistre l'ID de cette étape technique comme point de retour global du voyage $pdo->prepare("UPDATE pf_holidays SET return_step_id = ? WHERE id = ?")->execute([$target_order, $holiday_id]); } - // 5. GESTION DES FAVORIS ... (Garde ton code existant ici) + if (isset($_POST['save_favorite']) && $_POST['save_favorite'] == '1') { + $stmtFav = $pdo->query("SELECT content FROM pf_notes WHERE note_type = 'holiday_favorites'"); + $favs = json_decode($stmtFav->fetchColumn() ?: '[]', true); + $exists = false; + foreach ($favs as $f) { if ($f['name'] === $location_name) $exists = true; } + if (!$exists) { + $favs[] = ['name' => $location_name, 'lat' => $lat, 'lng' => $lng]; + $pdo->prepare("INSERT INTO pf_notes (note_type, reference_id, content) VALUES ('holiday_favorites', 'GLOBAL', ?) ON DUPLICATE KEY UPDATE content = VALUES(content)")->execute([json_encode($favs)]); + } + } $pdo->commit(); - } catch (Exception $e) { $pdo->rollBack(); die($e->getMessage()); } + } catch (Exception $e) { + $pdo->rollBack(); + die($e->getMessage()); + } } header("Location: /holidays.php?tab=holiday_detail&id=" . $holiday_id);