Exercice

Indiquer, ligne par ligne, si l'instruction PHP fait partie du modèle, de la vue ou du contrôleur.


<?php
require_once 'connect.php';
error_reporting(E_ALL); // Afficher toutes les erreurs, utilise pour debugger

$isbn = "";
$titre = "";
$type = "";
$annee = "";
$prix = "";

// Test des donnees du formulaires
if (isset($_POST['ISBN'])) {
    $isbn = getPost('ISBN');

    // Suppression
    if (isset($_POST['Supprimer']) && $isbn != "") {
        try {
            // Suppresion de la jointure avec les auteurs
            $query = $db_server->prepare("DELETE FROM AuteurLivre WHERE idLivre=?");
            $query->execute(array($isbn));

            // Suppression du livre
            $query = $db_server->prepare("DELETE FROM Livre WHERE isbn=?");
            $query->execute(array($isbn));

            $isbn = "";
        } catch (PDOException $e) {
            die("Erreur base :" . $e->getMessage());
        }
    } else if (isset($_POST['Editer']) && $isbn != "") {
        list($isbn, $titre, $type, $annee, $prix) = getLivre($db_server, $isbn);
    } else {
        if (
            isset($_POST['Titre']) &&
            isset($_POST['Type']) &&
            isset($_POST['Annee']) &&
            isset($_POST['Prix'])) {

            $titre = getPost('Titre');
            $type = getPost('Type');
            $annee = getPost('Annee');
            $prix = getPost('Prix');


            // Ajout
            if (isset($_POST['Ajouter']) && $isbn != "") {
                $query = $db_server->prepare("INSERT INTO Livre (`ISBN`, `Titre`, `Type`, `Annee`, `Prix`) VALUES (?, ?, ?, ?, ?)");
                try{
                    $query->execute(array($isbn, $titre, $type, $annee, $prix));
                }catch (PDOException $e) {
                    die("Erreur base :" . $e->getMessage());
                }
                $isbn = "";
                $titre = "";
                $type = "";
                $annee = "";
                $prix = "";
            }
            if (isset($_POST['Enregistrer']) && $isbn != "") {
                $query = $db_server->prepare("UPDATE Livre SET `Titre`=?, `Type`=?, `Annee`= ?, `Prix`=? WHERE isbn=?");
                try{
                    $query->execute(array($titre, $type, $annee, $prix, $isbn));
                }catch (PDOException $e) {
                    die("Erreur base :" . $e->getMessage());
                }

                $isbn = "";
                $titre = "";
                $type = "";
                $annee = "";
                $prix = "";
            }

        }
    }
}

// Formulaire
echo <<<_END
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8" />
<title>Exercice</title>
_END;

if (array_key_exists("css", $_GET) && $_GET["css"] == "bootstrap")
    echo '<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous"><link rel="stylesheet" href="./css/bootstrap.css" type="text/css"/><style>label{display: block;width: 75px;float: left;}.card{margin-bottom: 2em;}</style>';
else echo '<link rel="stylesheet" href="../../css/alsa.css" type="text/css"/>';

echo <<<_END
</head>
<body class="container"><header id="logo" class="jumbotron">
<h1>Gestion des livres</h1></header>
<article>
<section class="card">
<header><h2>Saisie</h2></header>
<form action="manageLivre.php" method="post">

_END;
echo '<label for="isbn">ISBN</label> <input type="text" id="isbn" name="ISBN" value="' . $isbn . '"/><br>';
echo '<label for="titre">Titre</label> <input type="text" id="titre" name="Titre" value="' . $titre . '"/><br>';
echo '<label for="type">Type</label> <input type="text" id="type" name="Type" value="' . $type . '"/><br>';
echo '<label for="annee">Annee</label> <input type="text" id="annee" name="Annee" value="' . $annee . '"/><br>';
echo '<label for="prix">Prix</label> <input type="text" id="prix" name="Prix" value="' . $prix . '"/><br>';
if (isset($_POST['Editer']) && $isbn != "") {
    echo '<input type="submit" value="Enregistrer" name="Enregistrer"/>';
} else {
    echo '<input type="submit" value="Ajouter" name = "Ajouter">';
}
echo <<<_END

</form>
</section>
</article>
<article>
<h2>Ouvrages</h2>
_END;


$query = "SELECT * FROM Livre ORDER BY Annee, isbn";
$results = $db_server->query($query);

foreach ($results as $row) {
    list($isbn, $titre, $type, $annee, $prix) = donneesLivre($row);
// Resultats
    echo '<section class="card"><header class="card-header"><h2>' . $titre . '</h2></header><div class="card-body">';
    echo $type . ' de ' . $annee . '<br>';
    echo 'ISBN : ' . $isbn . ' / Prix :' . $prix . ' &euro;<br>';
    echo <<<_END
<form action="manageLivre.php" method="post">
<input type="hidden" name="ISBN" value="$isbn"/>
<input type="submit" name="Supprimer" value="Supprimer"/>
<input type="submit" name="Editer" value="Editer"/>
</form>
</div></section>
_END;
}
if (array_key_exists("css", $_GET) && $_GET["css"] == "bootstrap") {
    echo '<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>';
    echo '<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>';
    echo '<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>';
}
echo "</article></body></html>";

$db_server = NULL;


// Fonction de securite
function getPost($var)
{
    return $_POST[$var];
}

function getLivre($db_server, $isbn)
{
    try {
        /** @var PDOStatement $query */
        $query = $db_server->prepare("select `ISBN`, `Titre`, `Type`, `Annee`, `Prix` from Livre where isbn=?");
        /** @var bool $result */
        $result = $query->execute(array($isbn));

        if($query->rowCount())
            return donneesLivre($query->fetch());
        return null;
    }catch (PDOException $e) {
        die("Erreur base :" . $e->getMessage());
    }
}

function donneesLivre($row)
{
    $isbn = stripslashes($row['ISBN']);
    $titre = stripslashes($row['Titre']);
    $type = stripslashes($row['Type']);
    $annee = stripslashes($row['Annee']);
    $prix = stripslashes($row['Prix']);

    return array($isbn, $titre, $type, $annee, $prix);
}


?>