oazaSrdceWeb / admin / addseminar.php
addseminar.php
Raw
<?php

/**
 * @file addseminar.php
 * @brief Skript pro přidání nového semináře do databáze.
 *
 * Tento skript zajišťuje zobrazení formuláře a zpracování dat pro přidání nového semináře.
 * Umožňuje zadání názvu, času, kategorie, popisu, lektora, více lektorů a barvy.
 * Přístup je povolen pouze přihlášeným uživatelům.
 */

include '../db_connection.php'; ///< Připojení k databázi.
$conn->set_charset("utf8mb4");
session_start();
/**
 * Kontrola, zda je uživatel přihlášen.
 * Pokud není, přesměruje na přihlašovací stránku.
 */
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
    header('Location: login.php');
    exit();
}

/**
 * Zpracování odeslaného formuláře metodou POST.
 */
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $heading = $_POST['heading'];
    $timefrom = $_POST['timefrom'];
    $timeto = $_POST['timeto'];
    $category = $_POST['category'];
    $description = $_POST['description'];
    $lector_id = $_POST['lector_id'];
    $lectors = $_POST['lectors'];
    $color = $_POST['color'];

    // Pokud je kategorie 1 nebo 2, nastaví se lektor na null.
    if ($category == 1 || $category == 2) {
        $lector_id = null;
    }

    /**
     * Vložení nového semináře do databáze.
     */
    $stmt = $conn->prepare('INSERT INTO events (heading, timefrom, timeto, category, description, lector_id, lectors, color) VALUES (?, ?, ?, ?, ?, ?, ?, ?)');
    $stmt->bind_param('sssssiss', $heading, $timefrom, $timeto, $category, $description, $lector_id, $lectors, $color);

    if ($stmt->execute()) {
        $success = 'Seminář byl přidán';
    } else {
        $error = 'Vyskytla se chyba';
    }
    $stmt->close();
}
?>

<!DOCTYPE html>
<html lang="cs">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Seminář</title>
</head>

<body>
    <!DOCTYPE html>
    <html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Přidat Seminář</title>
        <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet" />
        <link rel="stylesheet" href="../admin.css" />
    </head>

    <body>
        <script>
            /**
             * Funkce pro přepínání viditelnosti částí formuláře na základě vybrané kategorie.
             */
            function toggleFormParts() {
                const category1 = document.getElementById('category1');
                const category2 = document.getElementById('category2');
                const category3 = document.getElementById('category3');
                const heading = document.getElementById('heading');
                const description = document.getElementById('description').parentElement;
                const lector = document.getElementById('lector_id').parentElement;
                const lector_id = document.getElementById('lector_id');
                const lectors = document.getElementById('lectors').parentElement;
                const color = document.querySelector('input[name="color"]').parentElement;

                if (category1.checked) {
                    description.style.display = 'none';
                    lector.style.display = 'none';
                    lectors.style.display = 'none';
                    color.style.display = 'none';
                    heading.value = 'Dětský pobyt';
                } else if (category2.checked) {
                    description.style.display = 'none';
                    lector.style.display = 'none';
                    lectors.style.display = 'none';
                    color.style.display = 'none';
                    heading.value = 'Sportovní soustředění';
                } else {
                    description.style.display = 'block';
                    lector.style.display = 'block';
                    lectors.style.display = 'block';
                    color.style.display = 'block';
                    heading.value = '';
                }
                if (lector_id.value != "4") {
                    lectors.style.display = 'none';
                } else {
                    lectors.style.display = 'block';
                }
            }

            /**
             * Event listener pro změnu kategorie a lektora.
             */
            document.addEventListener('DOMContentLoaded', function() {
                const categories = document.querySelectorAll('input[name="category"]');
                categories.forEach(category => {
                    category.addEventListener('change', function() {
                        toggleFormParts();
                    });
                });
                const lector_id = document.getElementById('lector_id');
                lector_id.addEventListener('change', function() {
                    toggleFormParts();
                });
                toggleFormParts();
            });
        </script>
        <div class="container mt-5">
            <a href="../admin.php" class="backbtn">Zpět na panel</a>
            <h2 class="text-center mb-4 hedingForm">Přidat Seminář</h2>
            <?php if (isset($success)) {
                echo '<div class="alert alert-success text-center">' . $success . '</div>';
            } ?>
            <?php if (isset($error)) {
                echo '<div class="alert alert-danger text-center">' . $error . '</div>';
            } ?>
            <div class="row justify-content-center">
                <div class="col-md-8">
                    <form action="addseminar.php" method="post" id="createseminar">
                        <div class="form-group">
                            <h5>Kategorie</h5>
                            <label for="category1">Dětský pobyt</label>
                            <input type="radio" id="category1" name="category" value="1"> <br>
                            <label for="category2">Sportovní soustředění</label>
                            <input type="radio" id="category2" name="category" value="2"><br>
                            <label for="category3">Ostatní</label>
                            <input type="radio" id="category3" name="category" value="3" checked><br>
                        </div>
                        <div class="form-group">
                            <label for="heading">Nadpis</label>
                            <input type="text" class="form-control" id="heading" name="heading" required>
                        </div>
                        <div class="form-group">
                            <label for="timefrom">Čas Od</label>
                            <input type="date" class="form-control" id="timefrom" name="timefrom" required>
                        </div>
                        <div class="form-group">
                            <label for="timeto">Čas Do</label>
                            <input type="date" class="form-control" id="timeto" name="timeto" required>
                        </div>
                        <div class="form-group">
                            <label for="description">Bližší info:</label>
                            <input type="text" class="form-control" id="description" name="description">
                        </div>
                        <div class="form-group">
                            <label for="lector_id">Lektor</label>
                            <select class="form-control" id="lector_id" name="lector_id">
                                <?php
                                ///Výpis lektorů z databáze pro přiřazení k semináři
                                $result = $conn->query('SELECT id, name FROM lectors');
                                while ($row = $result->fetch_assoc()) {
                                    echo '<option value="' . $row['id'] . '">' . $row['name'] . '</option>';
                                }
                                ?>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="lectors">Bez medailonku / více lektorů</label>
                            <input type="text" id="lectors" name="lectors" class="form-control">
                        </div>
                        <div class="form-group">
                            <label>Barva</label><br>
                            <input type="radio" id="color1" name="color" value="1">
                            <label for="color1">Seminář</label><br>
                            <input type="radio" id="color2" name="color" value="2">
                            <label for="color2">Ozdravné pobyty</label><br>
                            <input type="radio" id="color3" name="color" value="3">
                            <label for="color3">Firemní</label><br>
                            <input type="radio" id="color4" name="color" value="4" checked>
                            <label for="color4">Ostatní</label>
                        </div>
                        <button type="submit" class="btn btn-primary btn-block">Přidat</button>
                    </form>
                </div>
            </div>
        </div>
    </body>

    </html>
</body>

</html>