<?php // Fonction pour récupérer les jours fériés function getHolidays($year) { $holidays = [ "$year-01-01", // Jour de l'An "$year-05-01", // Fête du Travail "$year-05-08", // Victoire 1945 "$year-07-14", // Fête Nationale "$year-08-15", // Assomption "$year-11-01", // Toussaint "$year-11-11", // Armistice "$year-12-25", // Noël ]; $easterDate = easter_date($year); $ascension = date('Y-m-d', strtotime('+39 days', $easterDate)); $pentecost = date('Y-m-d', strtotime('+49 days', $easterDate)); $holidays[] = $ascension; $holidays[] = $pentecost; return $holidays; } // Vérifier si c'est un jour ouvré function isBusinessDay($date, $holidays) { $dayOfWeek = date('N', $date); // 1 (lundi) à 7 (dimanche) return $dayOfWeek < 6 && !in_array(date('Y-m-d', $date), $holidays); } // Fonction pour générer le tableau dynamique function generateTable($startDate, $endDate) { setlocale(LC_TIME, 'fr_FR.UTF-8'); $currentDate = strtotime($startDate); $endDate = strtotime($endDate); $holidays = getHolidays(date('Y', $currentDate)); $monthColors = [ "#FFCCCC", "#FFE5CC", "#FFFFCC", "#E5FFCC", "#CCFFCC", "#CCFFE5", "#CCFFFF", "#CCE5FF", "#CCCCFF", "#E5CCFF", "#FFCCFF", "#FFCCE5", ]; $weekOddColor = "#D6EAF8"; $weekEvenColor = "#AED6F1"; echo '<div class="table-responsive">'; echo '<table class="table table-bordered text-center" id="dynamicTable">'; // Première ligne : en-têtes echo '<thead><tr><th>Date</th>'; while ($currentDate <= $endDate) { if (isBusinessDay($currentDate, $holidays)) { $formattedDate = date('d/m/Y', $currentDate); echo '<th style="background-color: ' . $monthColors[date('n', $currentDate) - 1] . ';">' . $formattedDate . '</th>'; } $currentDate = strtotime('+1 day', $currentDate); } echo '</tr>'; // Réinitialiser la date pour la deuxième ligne (numéros de semaine) $currentDate = strtotime($startDate); echo '<tr><td>Semaine</td>'; $lastWeekNumber = null; $weekCellCount = 0; while ($currentDate <= $endDate) { if (isBusinessDay($currentDate, $holidays)) { $weekNumber = date('W', $currentDate); if ($weekNumber === $lastWeekNumber) { $weekCellCount++; } else { if ($lastWeekNumber !== null) { $color = ($lastWeekNumber % 2 === 0) ? $weekEvenColor : $weekOddColor; echo '<td colspan="' . $weekCellCount . '" style="background-color: ' . $color . ';">' . $lastWeekNumber . '</td>'; } $lastWeekNumber = $weekNumber; $weekCellCount = 1; } } $currentDate = strtotime('+1 day', $currentDate); } if ($lastWeekNumber !== null) { $color = ($lastWeekNumber % 2 === 0) ? $weekEvenColor : $weekOddColor; echo '<td colspan="' . $weekCellCount . '" style="background-color: ' . $color . ';">' . $lastWeekNumber . '</td>'; } echo '</tr>'; // Réinitialiser la date pour la troisième ligne (jours de la semaine) $currentDate = strtotime($startDate); echo '<tr><td>Jours</td>'; while ($currentDate <= $endDate) { if (isBusinessDay($currentDate, $holidays)) { $dayName = mb_substr(ucfirst(strftime('%A', $currentDate)), 0, 3) . '.'; echo '<td>' . $dayName . '</td>'; } $currentDate = strtotime('+1 day', $currentDate); } echo '</tr></thead>'; // Ajout d'une ligne vide au corps du tableau echo '<tbody id="dataBody"></tbody>'; echo '</table>'; echo '<button id="addRowBtn" class="btn btn-primary mt-3">Ajouter</button>'; echo '<button id="saveBtn" class="btn btn-success mt-3" style="display: none;">Sauver</button>'; echo '</div>'; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Tableau Dynamique</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> <style> .table_date[data-status="1"] { background-color: #007BFF; color: #fff; } .titre_nom[contenteditable="true"] { border: 1px dashed #000; padding: 5px; } </style> </head> <body> <div class="container my-5"> <h1 class="text-center mb-4">Tableau Dynamique</h1> <?php generateTable('2025-01-01', '2025-12-31'); ?> </div> <script> document.addEventListener('DOMContentLoaded', () => { const tableBody = document.getElementById('dataBody'); const addRowBtn = document.getElementById('addRowBtn'); const saveButton = document.getElementById('saveBtn'); // Ajout de ligne addRowBtn.addEventListener('click', () => { const newRow = document.createElement('tr'); const firstCell = document.createElement('td'); firstCell.classList.add('titre_nom'); firstCell.setAttribute('contenteditable', 'true'); firstCell.textContent = 'Not define'; newRow.appendChild(firstCell); // Ajouter des cellules vides avec un contenu initial de 0 document.querySelectorAll('th').forEach(() => { const cell = document.createElement('td'); cell.classList.add('table_date'); cell.textContent = '0'; // Valeur initiale de la cellule cell.dataset.status = '0'; // Statut initial cell.setAttribute('contenteditable', 'true'); // Permet la modification de la cellule newRow.appendChild(cell); }); // Ajouter la nouvelle ligne au tableau tableBody.appendChild(newRow); // Montrer le bouton Sauver saveButton.style.display = 'inline-block'; }); // Sauvegarde des données saveButton.addEventListener('click', () => { const rows = tableBody.getElementsByTagName('tr'); const dataToSave = []; Array.from(rows).forEach(row => { const rowData = []; const cells = row.getElementsByTagName('td'); // Ajouter la première cellule (nom de la ligne) à l'array rowData const firstCell = cells[0].textContent.trim(); // La première cellule contient le nom rowData.push(firstCell); // Ajouter les cellules de la ligne qui contiennent 0 ou 1 Array.from(cells).forEach((cell, index) => { if (cell.classList.contains('table_date') && (cell.dataset.status === '0' || cell.dataset.status === '1')) { rowData.push(cell.dataset.status); } }); if (rowData.length > 1) { // Si la ligne contient des données (la première cellule + valeurs 0/1) dataToSave.push(rowData); } }); // Envoi des données au script PHP fetch('save_data.php', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(dataToSave) }) .then(response => response.json()) .then(data => { alert("Données sauvegardées !"); }) .catch(error => { console.error("Erreur de sauvegarde:", error); }); }); }); </script> </body> </html>