File "code_009.php"

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

<?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 '</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>

        // Désactive le rechargement avec F5 ou Ctrl+R

        window.addEventListener('keydown', function(e) {

            if (e.key === "F5" || (e.ctrlKey && e.key === "r")) {

                e.preventDefault();  // Empêche le rechargement

                alert("Le rechargement est désactivé !");

            }

        });



        // Désactive également le clic droit pour rafraîchir

        window.addEventListener('contextmenu', function(e) {

            e.preventDefault();  // Empêche le menu contextuel

            alert("Le rechargement est désactivé !");

        });

    </script>



<script>

    document.addEventListener('DOMContentLoaded', () => {

        const tableBody = document.getElementById('dataBody');



        // Gestion du clic sur les cellules

        document.addEventListener('click', (e) => {

            if (e.target.classList.contains('table_date')) {

                e.target.dataset.status = e.target.dataset.status === '0' ? '1' : '0';

                e.target.textContent = e.target.dataset.status;

            }

        });



        // Gestion de l'ajout d'une nouvelle ligne

        document.getElementById('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

                newRow.appendChild(cell);

            });



            // Ajouter la nouvelle ligne au tableau

            tableBody.appendChild(newRow);

        });

    });

</script>

</body>

</html>