You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
3.8 KiB
137 lines
3.8 KiB
<?php
|
|
namespace controleur;
|
|
|
|
use model\AdminModel;
|
|
use model\ArticleModel;
|
|
use model\Parser;
|
|
|
|
class UserControleur
|
|
{
|
|
public function __construct()
|
|
{
|
|
global $twig; // nécessaire pour utiliser variables globales
|
|
//debut
|
|
|
|
//on initialise un tableau d'erreur
|
|
$dVueEreur = [];
|
|
|
|
try {
|
|
$action = $_REQUEST['action'] ?? null;
|
|
|
|
switch($action) {
|
|
//pas d'action, on réinitialise 1er appel
|
|
case 'listArticle':
|
|
case null:
|
|
$this->listArticle();
|
|
break;
|
|
|
|
case 'connection':
|
|
$this->connection();;
|
|
break;
|
|
|
|
case 'deconnection':
|
|
$this->deconnection();
|
|
break;
|
|
case 'validationFormulaire':
|
|
$this->ValidationFormulaire($dVueEreur);
|
|
break;
|
|
|
|
//mauvaise action
|
|
default:
|
|
$dVueEreur[] = "Erreur d'appel php";
|
|
echo $twig->render('erreur.html', ['dVueErreur'=>$dVueEreur, 'isAdmin' => AdminModel::isAdmin()]);
|
|
break;
|
|
}
|
|
} catch (\PDOException $e) {
|
|
//si erreur BD, pas le cas ici
|
|
$dVueEreur[] = 'Erreur PDO : ' . $e->getMessage();
|
|
echo $twig->render('erreur.html', ['dVueEreur' => $dVueEreur]);
|
|
} catch (\Exception $e2) {
|
|
$dVueEreur[] = 'Erreur : ' . $e2->getMessage();
|
|
echo $twig->render('erreur.html', ['dVueEreur' => $dVueEreur]);
|
|
}
|
|
|
|
//fin
|
|
exit(0);
|
|
}//fin constructeur
|
|
|
|
public function listArticle()
|
|
{
|
|
global $twig;
|
|
$articleModel = new ArticleModel();
|
|
$dVue = [
|
|
'data' => $articleModel->findAllArticleByAllFlux()
|
|
];
|
|
echo $twig->render('listArticle.html', [
|
|
'dVue' => $dVue,
|
|
'isAdmin' => AdminModel::isAdmin()
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @throws \Twig\Error\RuntimeError
|
|
* @throws \Twig\Error\SyntaxError
|
|
* @throws \Twig\Error\LoaderError
|
|
*/
|
|
public function connection(){
|
|
global $twig; // nécessaire pour utiliser variables globales
|
|
if (AdminModel::isAdmin()) {
|
|
$this->listArticle();
|
|
}
|
|
else {
|
|
echo $twig->render('Connection.html');
|
|
if (isset($_POST['username']) && isset($_POST['password'])) {
|
|
$this->login();
|
|
}
|
|
}
|
|
}
|
|
|
|
public function deconnection(){
|
|
AdminModel::deconnection();
|
|
$this->listArticle();
|
|
}
|
|
|
|
/**
|
|
* @throws \Twig\Error\RuntimeError
|
|
* @throws \Twig\Error\SyntaxError
|
|
* @throws \Twig\Error\LoaderError
|
|
* @throws \Exception
|
|
*/
|
|
public function login(){
|
|
$username = $_POST['username'];
|
|
$password = $_POST['password'];
|
|
|
|
$adminModel = new AdminModel();
|
|
$admin = $adminModel->connection($username, $password);
|
|
if ($admin != null) {
|
|
$this->listArticle();
|
|
}
|
|
else{
|
|
$this->connection();
|
|
}
|
|
}
|
|
|
|
public function ValidationFormulaire(array $dVueEreur)
|
|
{
|
|
global $twig; // nécessaire pour utiliser variables globales
|
|
|
|
//si exception, ca remonte !!!
|
|
$nom = $_POST['txtNom']; // txtNom = nom du champ texte dans le formulaire
|
|
$age = $_POST['txtAge'];
|
|
\config\Validation::val_form($nom, $age, $dVueEreur);
|
|
|
|
/*
|
|
$model = new \metier\Simplemodel();
|
|
$data = $model->get_data();
|
|
*/
|
|
|
|
$dVue = [
|
|
'nom' => $nom,
|
|
'age' => $age,
|
|
//'data' => $data,
|
|
];
|
|
|
|
echo $twig->render('Connection.html', ['dVue' => $dVue, 'dVueEreur' => $dVueEreur]);
|
|
}
|
|
}//fin class
|