Merge branch 'php_add_ajoutersci' into php

pull/7/head
Gwenael PLANCHON 1 year ago
commit db24beba01

@ -11,8 +11,7 @@
"twig/twig": "^3.7", "twig/twig": "^3.7",
"twbs/bootstrap": "^5.3", "twbs/bootstrap": "^5.3",
"ext-iconv": "*", "ext-iconv": "*",
"ext-pdo": "*", "ext-pdo": "*"
"ext-http": "*"
}, },
"scripts": { "scripts": {
"post-update-cmd": [ "post-update-cmd": [

@ -2,6 +2,11 @@
namespace controller; namespace controller;
use Exception; use Exception;
use PDOException; use PDOException;
use model\MdlDifficulte;
use model\MdlScientifique;
use model\MdlSexe;
use model\MdlThematique;
use model\Scientifique;
//gerer la connexion des admins //gerer la connexion des admins
class AdminController { class AdminController {
@ -12,47 +17,69 @@ class AdminController {
//verifier si l'utilisateur est connecté et admin //verifier si l'utilisateur est connecté et admin
if(isset($_SESSION["isAdmin"])){ if(isset($_SESSION["isAdmin"])){
if($_SESSION["isAdmin"]) { if($_SESSION["isAdmin"]==true){
try { //donner la page admin a l'admin
switch ($action) { try {
case '': switch($action) {
echo "accueil admin"; case '':
exit; echo $twig->render('admin/accueil.html');
// echo $twig->render('admin/accueil.html'); break;
case 'stats': case 'stats':
echo "stats admin"; echo $twig->render('admin/stats.html');
exit; break;
// echo $twig->render('admin/stats.html'); case 'ajouterScientifiques':
case 'ajouterScientifiques': $sexe = new MdlSexe();
echo "page ajout scientifiques admin"; $theme = new MdlThematique();
exit; $diff = new MdlDifficulte();
// echo $twig->render('admin/ajouter.html'); if(!empty($_POST)){
//mauvaise action $sci=new MdlScientifique();
default: $sci->addScientifique(new Scientifique(0,
$dVueErreur[] = "Erreur d'appel php"; $_POST["name"],
echo $twig->render('accueil.html', ['dVueErreur' => $dVueErreur]); $_POST["prenom"],
break; $_POST["url"],
} \DateTime::createFromFormat("Y-m-d",$_POST["date"]),
} catch (PDOException $e) { $_POST["description"],
$dVueErreur[] = 'Erreur avec la base de données !'; 0,
echo $twig->render('erreur.html', ['dVueErreur' => $dVueErreur]); $theme->getFromId(intval($_POST["theme"])),
} catch (Exception $e2) { $diff->getFromId(intval($_POST["difficulte"])),
$dVueErreur[] = 'Erreur inattendue !'; $sexe->getFromId(intval($_POST["sexe"]))
echo $twig->render('erreur.html', ['dVueErreur' => $dVueErreur]); ));
} }
} echo $twig->render('admin/ajouterScientifiques.html',['sexe' => $sexe->getAll(), 'themes' => $theme->getAll(), 'difficultes' => $diff->getAll()]);
break;
//mauvaise action
default:
$dVueErreur[] = "Erreur d'appel php";
echo $twig->render('erreur.html', ['dVueErreur' => $dVueErreur]);
break;
}
} catch (\PDOException $e) {
$dVueErreur[] = 'Erreur avec la base de données !';
echo $twig->render('erreur.html', ['dVueErreur' => $dVueErreur]);
} catch (\Exception $e2) {
$dVueErreur[] = 'Erreur inattendue !';
echo $twig->render('erreur.html', ['dVueErreur' => $dVueErreur]);
}
}
} }
//verifier si l'utilisateur est connecté mais pas admin else if(isset($_SESSION["isLogged"])){
if(isset($_SESSION["isLogged"])){ //verifier si l'utilisateur est connecté mais pas admin
if($_SESSION["isLogged"]) { if($_SESSION["isLogged"]==true) {
//dire acces interdit aux non admins //dire acces interdit aux non admins
$dVueErreur[] = "Erreur 403 : Acces interdit"; $dVueErreur[] = 'Erreur 403 : Accès interdit !';
echo $twig->render('erreur.html', ['dVueErreur' => $dVueErreur]); echo $twig->render('erreur.html', ['dVueErreur' => $dVueErreur]);
exit(0); exit(0);
} }
} else {
//renvoyer a la page de connexion pour les non connectés
echo '<meta http-equiv="refresh" content="0; url=login">';
} }
//renvoyer a la page de connexion pour les non connectés
echo $twig->render('login.html');
exit(0); exit(0);
} }
} }
?>

@ -35,6 +35,7 @@ class FrontController
$router->map('GET|POST','/pseudo/[a:action]?','PseudoController'); $router->map('GET|POST','/pseudo/[a:action]?','PseudoController');
$router->map('GET|POST','/admin/[a:action]','AdminController'); $router->map('GET|POST','/admin/[a:action]','AdminController');
$router->map('GET|POST','/[a:action]?','UserController'); $router->map('GET|POST','/[a:action]?','UserController');
$router->map('GET|POST','/login','login');
session_start(); session_start();
@ -56,16 +57,15 @@ class FrontController
case 'AdminController': case 'AdminController':
$action = $match['params']['action']; $action = $match['params']['action'];
if (!MdlAdmin::isAdmin()) { //if (!MdlAdmin::isAdmin()) {
$action = 'login'; // $action = 'login';
} //}
$this->callController('AdminController',$action); new AdminController($action);
break; break;
case 'PseudoController': case 'PseudoController':
$this->callController('PseudoController',$match); $this->callController('PseudoController',$match);
break; break;
//mauvaise action //mauvaise action
default: default:
$dVueErreur[] = "Erreur d'appel php"; $dVueErreur[] = "Erreur d'appel php";

@ -10,6 +10,9 @@ use model\GameGateway;
use model\MdlDifficulte; use model\MdlDifficulte;
use model\MdlJeu; use model\MdlJeu;
use model\ValidationException; use model\ValidationException;
use model\MdlUser;
use model\MdlAdmin;
use model\LoginException;
class UserController { class UserController {
@ -62,6 +65,33 @@ class UserController {
} }
} }
public function login() {
global $twig;
if($_SERVER['REQUEST_METHOD'] === 'POST'){
Validation::valUserLogin($_REQUEST['login'], $dVueErreur);
$ug = new MdlUser();
if($ug->login($_REQUEST['login'], $_REQUEST['password'])) {
$_SESSION['pseudo'] = $_REQUEST['login'];
$_SESSION['isLogged'] = true;
header("Location: .");
} else {
//voir si c'est un admin
$ug = new MdlAdmin();
if($ug->login($_REQUEST['login'], $_REQUEST['password'])) {
$_SESSION['pseudo'] = $_REQUEST['login'];
$_SESSION['isAdmin'] = true;
$_SESSION['isLogged'] = true;
header("Location: .");
} else {
$dVueErreur[] = "Connexion échouée";
throw new LoginException("Connexion err");
}
}
} else {
echo $twig->render('login.html');
}
}
public function createParty(array $params) : void public function createParty(array $params) : void
{ {
global $twig; global $twig;

@ -25,4 +25,20 @@ class ScientifiqueGateway
); );
return $this->con->getOneResult(); return $this->con->getOneResult();
} }
public function addScientifique(Scientifique $sci): bool{
return $this->con->executeQuery(
"INSERT INTO Scientifique(nom, prenom, photo, dateNaissance, descriptif, ratioTrouvee, idThematique, idDifficulte, idSexe) VALUES (:nom, :prenom, :photo, :dateNaissance, :descriptif, :ratioTrouvee, :idThematique, :idDifficulte, :idSexe);"
,[
":nom"=>[$sci->getNom(),$this->con::PARAM_STR],
":prenom"=>[$sci->getPrenom(),$this->con::PARAM_STR],
":photo"=>[$sci->getPhoto(),$this->con::PARAM_STR],
":dateNaissance"=>[date("Y-m-d H:i:s", $sci->getDateNaiss()->getTimestamp()),$this->con::PARAM_STR],
":descriptif"=>[$sci->getDescriptif(),$this->con::PARAM_STR],
":ratioTrouvee"=>[$sci->getRatioTrouvee(),$this->con::PARAM_STR],
":idThematique"=>[$sci->getThematique()->getId(),$this->con::PARAM_STR],
":idDifficulte"=>[$sci->getDifficulte()->getId(),$this->con::PARAM_STR],
":idSexe"=>[$sci->getSexe()->getId(),$this->con::PARAM_STR]
]);
}
} }

@ -16,4 +16,10 @@ class SexeGateway
[':id' => [$id, $this->con::PARAM_INT]]); [':id' => [$id, $this->con::PARAM_INT]]);
return $this->con->getOneResult(); return $this->con->getOneResult();
} }
public function getAll(): array
{
$this->con->executeQuery("SELECT id, libelle FROM Sexe;");
return $this->con->getResults();
}
} }

@ -16,4 +16,9 @@ class ThematiqueGateway
[':id' => [$id, $this->con::PARAM_INT]]); [':id' => [$id, $this->con::PARAM_INT]]);
return $this->con->getOneResult(); return $this->con->getOneResult();
} }
public function getAll(): array
{
$this->con->executeQuery("SELECT id, libelle FROM Thematique;");
return $this->con->getResults();
}
} }

@ -41,4 +41,7 @@ class MdlScientifique extends MdlBase{
$difficulte, $difficulte,
$sexe); $sexe);
} }
public function addScientifique(Scientifique $s){
return $this->gw->addScientifique($s);
}
} }

@ -14,4 +14,12 @@ class MdlSexe extends MdlBase{
$row = $this->gw->getFromId($id); $row = $this->gw->getFromId($id);
return new Sexe($row['id'], $row['libelle']); return new Sexe($row['id'], $row['libelle']);
} }
public function getAll(): array {
$ret=array();
$row = $this->gw->getAll();
for($i=0; $i< count($row); $i++){
array_push($ret, new Sexe($row[$i]['id'], $row[$i]['libelle']));
}
return $ret;
}
} }

@ -14,4 +14,13 @@ class MdlThematique extends MdlBase{
$row = $this->gw->getFromId($id); $row = $this->gw->getFromId($id);
return new Thematique($row['id'], $row['libelle']); return new Thematique($row['id'], $row['libelle']);
} }
public function getAll(): array {
$ret=array();
$row = $this->gw->getAll();
for($i=0; $i< count($row); $i++){
array_push($ret, new Thematique($row[$i]['id'], $row[$i]['libelle']));
}
return $ret;
}
} }

@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="css/bootstrap.min.css">
<style>
body {
margin: 10px;
}
</style>
<title>Accueil</title>
</head>
<body>
<h1>Bienvenue sur Mini-Console Admin</h1>
<p align="right"><a href="logout">{{dVue.pseudo}}</a></p>
<br><br><br><br>
<center>
<a href="ajouterScientifiques">Ajouter Scientifiques</a>
</center>
<script src="js/bootstrap.min.js"></script>
</body>
</html>

@ -0,0 +1,83 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Créer une partie</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<style>
p,
label {
font:
1rem 'Fira Sans',
sans-serif;
}
input {
margin: 0.4rem;
}
</style>
</head>
<body>
<h1>ajouterScientifiques</h1>
<br><br><br>
<center>
<form action="" method="post">
<div>
<label for="name">Nom :</label>
<input type="text" id="name" name="name" required minlength="1" maxlength="128"/>
</div>
<div>
<label for="name">Prénom :</label>
<input type="text" id="prenom" name="prenom" required minlength="1" maxlength="128"/>
</div>
<div>
<label for="name">URL de la photo :</label>
<input type="text" id="url" name="url" required minlength="1" maxlength="512"/>
</div>
<div>
<label for="name">Date de naissance :</label>
<input type="date" id="date" name="date" required/>
</div>
<div>
<label for="name">Description (histoire, accomplissements...) :</label>
<textarea name="description" cols="40" rows="5"></textarea>
</div>
<fieldset>
<legend>Sexe :</legend>
{% for se in sexe %}
<div>
<input type="radio" id="sexe{{se.id}}" name="sexe" value="{{se.id}}" />
<label for="{{se.id}}">{{se.libelle}}</label>
</div>
{% endfor %}
</fieldset>
<fieldset>
<legend>Thematique :</legend>
{% for se in themes %}
<div>
<input type="radio" id="theme{{se.id}}" name="theme" value="{{se.id}}" />
<label for="{{se.id}}">{{se.libelle}}</label>
</div>
{% endfor %}
</fieldset>
<fieldset>
<legend>Difficulté :</legend>
{% for se in difficultes %}
<div>
<input type="radio" id="diff{{se.id}}" name="difficulte" value="{{se.id}}" />
<label for="{{se.id}}">{{se.libelle}}</label>
</div>
{% endfor %}
</fieldset>
<input type="submit" value="Envoyer" />
</form>
</center>
<script src="js/bootstrap.min.js"></script>
</body>
</html>

@ -19,7 +19,7 @@
</label> </label>
<br><br> <br><br>
<input type="submit" value="Valider"> <input type="submit" value="Valider">
{ % for error in dErreur % } {% for error in dErreur %}
<br> <br>
<p style="color: red"> <p style="color: red">
{{error}} {{error}}

Loading…
Cancel
Save