L3 Info : PHP et Applications Web
 
◃  Ch. 7 CRUD  ▹
 

Appli CRUD

  • index.php
    <?php
    
    include 'util.php';
    include 'vardsn.php';
    include 'MyPDO.php';
    include 'AbstractEntite.php';
    include 'EntiteLivre.php';
    include 'InterfaceVue.php';
    include 'VueLivre.php';
    
    use Crud2023\MyPDO;
    use Crud2023\EntiteLivre;
    use Crud2023\EntiteAuteur;
    use Crud2023\VueLivre;
    use Crud2023\VueAuteur;
    
    function getListeTables(): string
    {
        $tn_array = ['livre'];
        $ch = "<form action='action.php' method='GET'>\n";
        $ch .= "<select name='table_name'>\n";
        foreach ($tn_array as $tn) {
            $ch .= "\t<option value='$tn'>$tn</option>\n";
        }
        $ch .= "</select>\n";
        $ch .= "<input type='submit' name='action' value='selectionnerTable'/>\n";
        return $ch . "</form>\n";
    }
    
    // initialisation de la session
    session_start();
    
    // initialisation des variables $contenu et $message pour alimenter <body>
    $contenu = '';
    $message = '';
    // initialisation du connecteur myPDO pour la connexion (sans nom de Table à renseigner selon le contexte)
    $myPDO = new MyPDO('mysql', $_ENV['host'], $_ENV['db'], $_ENV['user'], $_ENV['password']);
    
    if (!isset($_SESSION['etat']))
        $_SESSION['etat'] = 'initialisation';
    
    switch ($_SESSION['etat']) {
        case 'initialisation':
            $contenu = getListeTables();
            break;
        case 'affichageTable':
            $myPDO->setNomTable($_SESSION['table_name']);
            $lesEntites = $myPDO->getAll();
            $contenu = VueLivre::getHTML4Entities($lesEntites);
            break;
        case 'affichageEntite':
            $myPDO->setNomTable($_SESSION['table_name']);
            $entite = $myPDO->get($_SESSION['pk_name'], intval($_SESSION['pk_value']));
            $contenu = VueLivre::getHTML4Entity($entite);
            break;
        case 'modificationEntite':
            $myPDO->setNomTable($_SESSION['table_name']);
            $entite = $myPDO->get($_SESSION['pk_name'], intval($_SESSION['pk_value']));
            $contenu = VueLivre::getForm4Entity($entite);
            break;
        case 'saisieEntite':
            $myPDO->setNomTable($_SESSION['table_name']);
            $contenu = VueLivre::getForm4Entity();
            break;
        default:
            exit('etat non valide : ' . $_SESSION['etat']);
    }
    
    $contenu .= "<form action='action.php'><button name='action' value='initialiser'>Accueil</button></form>\n";
    
    // Production de la page HTML
    echo getDebutHTML();
    echo $message;
    echo $contenu;
    echo getFinHTML();
    
    
    
  • action.php
    <?php
    include "vardsn.php";
    include "MyPDO.php";
    include "AbstractEntite.php";
    include "EntiteLivre.php";
    include "InterfaceVue.php";
    include "VueLivre.php";
    
    use Crud2023\MyPDO;
    use Crud2023\EntiteLivre;
    use Crud2023\EntiteAuteur;
    use Crud2023\VueLivre;
    use Crud2023\VueAuteur;
    
    session_start();
    
    // initialisation du connecteur myPDO pour la connexion (sans nom de Table à renseigner selon le contexte)
    $myPDO = new MyPDO('mysql', $_ENV['host'], $_ENV['db'], $_ENV['user'], $_ENV['password']);
    
    if (!isset($_REQUEST['action']))
        exit ('pas d\'action précisée');
    
    switch ($_REQUEST['action']) {
        case 'selectionnerTable':
            $_SESSION['table_name'] = $_REQUEST['table_name'];
            $_SESSION['etat'] = 'affichageTable';
            header('HTTP/1.1 303 See Other');
            header('Location: index.php');
            break;
        case 'afficherEntite':
            $_SESSION['pk_name'] = array_keys($_REQUEST)[0];
            $_SESSION['pk_value'] = array_values($_REQUEST)[0];
            $_SESSION['etat'] = 'affichageEntite';
            header('HTTP/1.1 303 See Other');
            header('Location: index.php');
            break;
        case 'modifierEntite':
            $_SESSION['pk_name'] = array_keys($_REQUEST)[1];
            $_SESSION['pk_value'] = array_values($_REQUEST)[1];
            $_SESSION['etat'] = 'modificationEntite';
            header('HTTP/1.1 303 See Other');
            header('Location: index.php');
            break;
        case 'supprimerEntite':
            $myPDO->setNomTable($_SESSION['table_name']);
            $record = array_diff_key($_REQUEST, ['action' => '']);
            $myPDO->delete($record);
            $_SESSION['etat'] = 'affichageTable';
            header('HTTP/1.1 303 See Other');
            header('Location: index.php');
            break;
        case 'creerEntite':
            $_SESSION['etat'] = 'saisieEntite';
            header('HTTP/1.1 303 See Other');
            header('Location: index.php');
            break;
        case 'sauverEntite':
            $myPDO->setNomTable($_SESSION['table_name']);
            $record = array_diff_key($_REQUEST, ['action' => '']);
            if ($_SESSION['etat'] == 'saisieEntite')
                $myPDO->insert($record);
            else
                $myPDO->update($record);
            $_SESSION['etat'] = 'affichageTable';
            header('HTTP/1.1 303 See Other');
            header('Location: index.php');
            break;
        case 'initialiser':
            session_unset();
            header('HTTP/1.1 303 See Other');
            header('Location: index.php');
            break;
        default:
            exit ('action non valide :' . $_REQUEST['action']);
    }

Résultat :
screenshot