File "code_007.php"

Full Path: /home/analogde/www/ONSEMI/New folder/code_007.php
File size: 6.33 KB
MIME-type: text/x-php
Charset: utf-8

<?php

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;

}



function isBusinessDay($date, $holidays)

{

    $dayOfWeek = date('N', $date); // 1 (lundi) à 7 (dimanche)

    return $dayOfWeek < 6 && !in_array(date('Y-m-d', $date), $holidays);

}



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">';



    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>';



    $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>';



    $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>';



    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">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 storageKey = 'dynamicTableData';



        function saveDataToStorage() {

            const data = [];

            tableBody.querySelectorAll('tr').forEach(row => {

                const rowData = [];

                row.querySelectorAll('.table_date').forEach(cell => {

                    rowData.push({ status: cell.dataset.status });

                });

                data.push(rowData);

            });

            localStorage.setItem(storageKey, JSON.stringify(data));

        }



        document.getElementById('saveBtn').addEventListener('click', () => {

            const rows = document.querySelectorAll('#dataBody tr');

            const dataToSave = [];



            rows.forEach(row => {

                const rowData = { name: row.cells[0]?.innerText || '' };

                row.querySelectorAll('.table_date').forEach((cell, index) => {

                    rowData[`col${index + 1}`] = cell.dataset.status || '0';

                });

                dataToSave.push(rowData);

            });



            const xhr = new XMLHttpRequest();

            xhr.open('POST', 'sauvegarde.php', true);

            xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');

            xhr.onreadystatechange = function () {

                if (xhr.readyState === 4) {

                    if (xhr.status === 200) {

                        alert('Données sauvegardées avec succès !');

                        console.log(xhr.responseText);

                    } else {

                        console.error('Erreur de sauvegarde :', xhr.status, xhr.statusText);

                    }

                }

            };

            xhr.send(JSON.stringify(dataToSave));

        });

    });

</script>

</body>

</html>