diff --git a/fluxRSS/src/DAL/ArticleGateway.php b/fluxRSS/src/DAL/ArticleGateway.php index d56f000..a6adc1a 100755 --- a/fluxRSS/src/DAL/ArticleGateway.php +++ b/fluxRSS/src/DAL/ArticleGateway.php @@ -5,7 +5,6 @@ use Exception; use metier\Article; use metier\Flux; use PDO; -use DateTime; class ArticleGateway { private $con; @@ -23,48 +22,40 @@ class ArticleGateway */ public function getAllArticles():array { - try { - $query = 'SELECT * FROM Article;'; - $this->con->executeQuery($query, array()); - return $this->con->getResults(); - }catch (\PDOException $e){ - throw new Exception("PDO error"); - } + $query = 'SELECT * FROM Article;'; + $this->con->executeQuery($query, array()); + return $this->con->getResults(); } - /** * @throws Exception */ public function addArticle(Article $article){ - try { - $query = 'INSERT INTO Article VALUES (:id,:title,:datePub,:description,:guid,:link,:mediaContent,:provenance);'; - - $this->con->executeQuery($query, array(':id' => array($article->getId(), PDO::PARAM_INT), - ':title' => array($article->getTitle(), PDO::PARAM_STR), - ':datePub' => array($article->getDate(), PDO::PARAM_STR), - ':description' => array($article->getDescription(), PDO::PARAM_STR), - ':guid' => array($article->getGuid(), PDO::PARAM_STR), - ':link' => array($article->getLink(), PDO::PARAM_STR), - ':mediaContent' => array($article->getMediaContent(), PDO::PARAM_STR), - ':provenance' => array($article->getProvenance(), PDO::PARAM_INT))); - }catch (\PDOException $e){ - throw new Exception("PDO error"); - } + $query = 'INSERT INTO Article VALUES (:id,:title,to_date(:date,"Dy, DD Mon YYYY"),:description,:guid,:link,:mediaContent,:provenance);'; + $this->con->executeQuery($query, array(':id' => array($article->getId(), PDO::PARAM_STR), + ':title' => array($article->getTitle(), PDO::PARAM_STR), + ':datePub' => array($article->getDate(), PDO::PARAM_STR), + ':description' => array($article->getDescription(), PDO::PARAM_STR), + ':guid' => array($article->getGuid(), PDO::PARAM_STR), + ':link' => array($article->getLink(), PDO::PARAM_STR), + ':mediaContent' => array($article->getMediaContent(), PDO::PARAM_STR), + ':provenance' => array($article->getProvenance(), PDO::PARAM_INT))); } - - public function removeAllArticleForParser(){ - try{ $query = 'DELETE FROM Article;'; $this->con->executeQuery($query); + } + + public function removeAllArticleFromFlux(int $idFlux){ + try { + $query = 'DELETE FROM Article WHERE Provenance = :idFlux;'; + $this->con->executeQuery($query, array(':idFlux' => array($idFlux, PDO::PARAM_INT))); }catch(\PDOException $p){ - throw new Exception("Data is not delete."); + throw new Exception("Data of flux is not delete."); } } - public function findArticleByFlux(int $flux){ $query = 'SELECT * FROM Article WHERE provenance = :flux;'; $this->con->executeQuery($query, array(':flux' => array($flux, PDO::PARAM_INT))); diff --git a/fluxRSS/src/DAL/FluxGateway.php b/fluxRSS/src/DAL/FluxGateway.php index 1538bfc..f0469b8 100755 --- a/fluxRSS/src/DAL/FluxGateway.php +++ b/fluxRSS/src/DAL/FluxGateway.php @@ -15,23 +15,30 @@ class FluxGateway public function addFlux(Flux $flux) { - $query = 'INSERT INTO Flux VALUES (:flux);'; - $this->con->executeQuery($query, array(':flux' => array($flux->getFlux(), PDO::PARAM_STR))); + $this->addFluxBySrc($flux->getFlux()); } - public function getId(): int + public function addFluxBySrc(string $flux) { - - return $this->id; + $query = 'INSERT INTO Flux VALUES (null,:flux);'; + $this->con->executeQuery($query, array(':flux' => array($flux, PDO::PARAM_STR))); } public function removeFlux(Flux $flux){ + $this->removeFluxBySrc($flux->getFlux()); + } + + public function removeFluxBySrc(string $flux){ $query = 'DELETE FROM Flux WHERE flux = :flux;'; - $this->con->executeQuery($query, array(':flux' => array($flux->getFlux(), PDO::PARAM_STR))); + $this->con->executeQuery($query, array(':flux' => array($flux, PDO::PARAM_STR))); } - public function findAllFlux(){ + public function removeFluxById(int $id){ + $query = 'DELETE FROM Flux WHERE id = :id;'; + $this->con->executeQuery($query, array(':id' => array($id, PDO::PARAM_INT))); + } + public function findAllFlux(){ $query = 'SELECT * FROM Flux;'; $this->con->executeQuery($query); return $this->con->getResults(); diff --git a/fluxRSS/src/controleur/AdminControleur.php b/fluxRSS/src/controleur/AdminControleur.php index ce8d5ed..2a522c3 100755 --- a/fluxRSS/src/controleur/AdminControleur.php +++ b/fluxRSS/src/controleur/AdminControleur.php @@ -2,13 +2,24 @@ namespace controleur; +use DAL\ArticleGateway; +use DAL\Connection; +use DAL\FluxGateway; +use http\Exception; +use metier\Flux; use model\AdminModel; use model\ArticleModel; use model\FluxModel; +use model\Parser; class AdminControleur { - public function __construct(){ + /*public function __construct() + { + $this->init(); + }*/ + + public function init(){ global $twig; // nécessaire pour utiliser variables globales //debut @@ -37,6 +48,18 @@ class AdminControleur $this->ValidationFormulaire($dVueEreur); break; + case 'listFlux': + $this->listFlux(); + break; + + case 'ajoutFlux': + $this->ajoutFlux(); + break; + + case 'changeNbArticle': + $this->changeNbArticle(); + break; + //mauvaise action default: $dVueEreur[] = "Erreur d'appel php"; @@ -60,10 +83,12 @@ class AdminControleur { global $twig; $articleModel = new ArticleModel(); + $nbArticle = isset($_SESSION['nbArticle']) ? intval($_SESSION['nbArticle']) : 5; + $allArticles = $articleModel->getArticles(); + $articles = array_slice($allArticles, 0, $nbArticle); if (AdminModel::isAdmin()) { - $data = $articleModel->getArticles(); $dVue = [ - 'data' => $data + 'data' => $articles ]; echo $twig->render('listArticleAdmin.html', [ 'dVue' => $dVue, @@ -75,11 +100,29 @@ class AdminControleur } } + public function listFlux(){ + global $twig; + $fluxModel = new FluxModel(); + if (AdminModel::isAdmin()) { + $dVue = [ + 'data' => $fluxModel->findAllFlux() + ]; + echo $twig->render('listFlux.html', [ + 'dVue' => $dVue, + 'isAdmin' => AdminModel::isAdmin() + ]); + } + else { + $this->connection(); + } + } + public function connection(){ global $twig; // nécessaire pour utiliser variables globales $renderTemplate = true; if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['username'])){ + $_REQUEST['action'] = null; $this->login(); $renderTemplate = false; } @@ -90,9 +133,35 @@ class AdminControleur public function deleteFlux(){ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['flux'])){ + $articleModel = new ArticleModel(); + $fluxModel = new FluxModel(); + $articleModel->removeArticleIdFlux(intval($_POST['flux'])); + $fluxModel->removeFluxById(intval($_POST['flux'])); + $_REQUEST['action'] = 'listFlux'; + $this->init(); + } + else{ + $_REQUEST['action'] = 'listFlux'; + $this->init(); + } + } + + public function ajoutFlux(){ + if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['fluxAdd'])){ + $con = new Connection('mysql:host=londres.uca.local;dbname=dbrorossetto','rorossetto','tpphp'); + $gwArticle = new ArticleGateway($con); + $gwFlux = new FluxGateway($con); + $parser = new Parser($gwFlux,$gwArticle); $fluxModel = new FluxModel(); - $fluxModel->removeFlux($_POST['flux']); - $this->listArticle(); + $fluxModel->addFluxBySrc($_POST['fluxAdd']); + $parser->addAllArticles(); + $_REQUEST['action'] = 'listFlux'; + unset($_POST['fluxAdd']); + $this->init(); + } + else{ + $_REQUEST['action'] = 'listFlux'; + $this->init(); } } @@ -103,7 +172,7 @@ class AdminControleur $adminModel = new AdminModel(); $admin = $adminModel->connection($username, $password); if($admin != null) { - $this->listArticle(); + $this->init(); } else{ unset($_POST['username']); @@ -111,4 +180,13 @@ class AdminControleur $this->connection(); } } + + public function changeNbArticle() + { + if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['nbArticle'])) { + $_SESSION['nbArticle'] = $_POST['nbArticle']; + unset($_POST['action']); + } + $this->init(); + } } \ No newline at end of file diff --git a/fluxRSS/src/controleur/FrontControleur.php b/fluxRSS/src/controleur/FrontControleur.php index 66e646d..55152d8 100755 --- a/fluxRSS/src/controleur/FrontControleur.php +++ b/fluxRSS/src/controleur/FrontControleur.php @@ -30,7 +30,7 @@ class FrontControleur else { session_start(); $controller=$match['target'] ?? null; - $action=$match['params']['action'] ?? null; + $action=$match['params']['action'] ?? "init"; try { if($controller == "AdminControleur"){ if (!AdminModel::isAdmin()){ diff --git a/fluxRSS/src/controleur/UserControleur.php b/fluxRSS/src/controleur/UserControleur.php index 8bec648..3246bc7 100755 --- a/fluxRSS/src/controleur/UserControleur.php +++ b/fluxRSS/src/controleur/UserControleur.php @@ -3,11 +3,10 @@ namespace controleur; use model\AdminModel; use model\ArticleModel; -use model\Parser; class UserControleur { - public function __construct() + public function init() { global $twig; // nécessaire pour utiliser variables globales //debut @@ -59,8 +58,11 @@ class UserControleur { global $twig; $articleModel = new ArticleModel(); + $nbArticle = isset($_SESSION['nbArticle']) ? intval($_SESSION['nbArticle']) : 5; + $allArticles = $articleModel->getArticles(); + $articles = array_slice($allArticles, 0, $nbArticle); $dVue = [ - 'data' => $articleModel->findAllArticleByAllFlux() + 'data' => $articles ]; echo $twig->render('listArticle.html', [ 'dVue' => $dVue, diff --git a/fluxRSS/src/metier/Article.php b/fluxRSS/src/metier/Article.php index 6a2c8b2..e3c8cc7 100755 --- a/fluxRSS/src/metier/Article.php +++ b/fluxRSS/src/metier/Article.php @@ -2,6 +2,8 @@ namespace metier; +use DateTime; + class Article { private int $id; @@ -128,4 +130,8 @@ class Article return $this->title . $this->date . $this->description; } + public function dateStr(): string{ + $dateTime = new DateTime($this->date); + return $dateTime->format('d/m/y à H:i') . ''; + } } \ No newline at end of file diff --git a/fluxRSS/src/metier/Flux.php b/fluxRSS/src/metier/Flux.php index 18b9f38..a09a9ef 100755 --- a/fluxRSS/src/metier/Flux.php +++ b/fluxRSS/src/metier/Flux.php @@ -4,20 +4,13 @@ namespace metier; class Flux { - private int $id; + private ?int $id; private string $flux; - /** - * @param int $id - * @param string $flux - */ - public function __construct(int $id, string $flux) - { - $this->id = $id; + public function __construct(string $flux, int $id=null){ + $this->id =$id; $this->flux = $flux; } - - /** * @return string */ diff --git a/fluxRSS/src/model/ArticleModel.php b/fluxRSS/src/model/ArticleModel.php index e29faae..aecdbe7 100755 --- a/fluxRSS/src/model/ArticleModel.php +++ b/fluxRSS/src/model/ArticleModel.php @@ -36,7 +36,7 @@ class ArticleModel foreach ($res as $row){ $tabArticle[] = new Article($row['id'], $row['title'],$row['datePub'],$row['description'],$row['guid'],$row['link'],$row['mediaContent'],$row['provenance'] ); } - $dicoFluxArticle = [$flux,$tabArticle]; + $dicoFluxArticle[] = [$flux,$tabArticle]; return $dicoFluxArticle; } @@ -47,8 +47,8 @@ class ArticleModel $res = $gwFlux->findAllFlux(); foreach ($res as $row) { - $flux = new Flux((int)($row['id']), $row['flux']); - $tabFluxArticle[] = $this->findArticleByFlux($flux); + $flux = new Flux((int)($row['id']),$row['flux']); + $tabFluxArticle[] = $this->findArticleByFluxAsStr($flux); } return $tabFluxArticle; } @@ -66,4 +66,10 @@ class ArticleModel $dicoFluxArticle = [$flux,$tabArticle]; return $dicoFluxArticle; } + + public static function removeArticleIdFlux(int $idFlux) + { + $gwArticle = new ArticleGateway(new Connection('mysql:host=londres.uca.local;dbname=dbrorossetto','rorossetto','tpphp')); + $gwArticle->removeAllArticleFromFlux($idFlux); + } } \ No newline at end of file diff --git a/fluxRSS/src/model/FluxModel.php b/fluxRSS/src/model/FluxModel.php index fb7461b..5a329c3 100755 --- a/fluxRSS/src/model/FluxModel.php +++ b/fluxRSS/src/model/FluxModel.php @@ -8,13 +8,13 @@ use metier\Flux; class FluxModel { - public function FindAllFlux(){ + public function findAllFlux(){ $gateway = new FluxGateway(new Connection('mysql:host=londres.uca.local;dbname=dbrorossetto','rorossetto','tpphp')); $data = array(); $result = $gateway->findAllFlux(); foreach ($result as $row){ - $data[] = new Flux((int)$row['id'],$row['$flux']); + $data[] = new Flux((int)$row['id'],$row['flux']); } return $data; } @@ -29,7 +29,7 @@ class FluxModel public function addFluxBySrc(string $flux): Flux { $gateway = new FluxGateway(new Connection('mysql:host=londres.uca.local;dbname=dbrorossetto','rorossetto','tpphp')); - $newFlux = new Flux($flux); + $newFlux = new Flux(null,$flux); $gateway->addFlux($newFlux); return $newFlux; } @@ -39,9 +39,14 @@ class FluxModel $gateway->removeFlux($flux); } + public function removeFluxById(int $id){ + $gateway = new FluxGateway(new Connection('mysql:host=londres.uca.local;dbname=dbrorossetto','rorossetto','tpphp')); + $gateway->removeFluxById($id); + } + public function removeFluxBySrc(string $flux) { $gateway = new FluxGateway(new Connection('mysql:host=londres.uca.local;dbname=dbrorossetto','rorossetto','tpphp')); - $gateway->removeFlux($flux); + $gateway->removeFluxBySrc($flux); } public function findFlux(Flux $flux){ @@ -50,7 +55,7 @@ class FluxModel $result = $gateway->findFlux($flux); foreach ($result as $row){ - $data[] = new Flux($row['$flux'],(int)$row['id']); + $data[] = new Flux((int)$row['id'],$row['$flux']); } return $data; } @@ -61,7 +66,7 @@ class FluxModel $result = $gateway->findFluxBySrc($flux); foreach ($result as $row){ - $data[] = new Flux($row['$flux'],(int)$row['id']); + $data[] = new Flux((int)$row['id'],$row['$flux']); } return $data; } diff --git a/fluxRSS/src/test/test.php b/fluxRSS/src/test/test.php index 2c77ddb..ff5c34a 100755 --- a/fluxRSS/src/test/test.php +++ b/fluxRSS/src/test/test.php @@ -10,5 +10,4 @@ require '../../vendor/autoload.php'; $gwArt = new ArticleGateway(new Connection('mysql:host=londres.uca.local;dbname=dbrorossetto', 'rorossetto', 'tpphp')); $gwFl = new FluxGateway(new Connection('mysql:host=londres.uca.local;dbname=dbrorossetto', 'rorossetto', 'tpphp')); $pars = new Parser( $gwFl,$gwArt); -var_dump($pars); -$pars->addAllArticles(); \ No newline at end of file + diff --git a/fluxRSS/templates/listArticle.html b/fluxRSS/templates/listArticle.html index 7cb0003..4decf81 100755 --- a/fluxRSS/templates/listArticle.html +++ b/fluxRSS/templates/listArticle.html @@ -5,22 +5,16 @@
- {{ value.0.getFlux() }} - {% for article in value.1 %} -
- {{article}} -
- {% endfor %} - - - +{% for article in dVue.data %} ++ {{article.dateStr()}} + {{ article.getTitle() }} + {{ article.getDescription() }} +
{% endfor %} {% if not isAdmin %} Connect {% else %} -Vue admin Déconnection {% endif %} diff --git a/fluxRSS/templates/listArticleAdmin.html b/fluxRSS/templates/listArticleAdmin.html index 18b3500..a417b9f 100755 --- a/fluxRSS/templates/listArticleAdmin.html +++ b/fluxRSS/templates/listArticleAdmin.html @@ -5,25 +5,16 @@- {{value}} -
- + +{% for article in dVue.data %} ++ {{article.dateStr()}} + {{ article.getTitle() }} + {{ article.getDescription() }} +
{% endfor %} Vue user Déconnection diff --git a/fluxRSS/templates/listFlux.html b/fluxRSS/templates/listFlux.html new file mode 100755 index 0000000..b503f89 --- /dev/null +++ b/fluxRSS/templates/listFlux.html @@ -0,0 +1,23 @@ + + + + +