Permet de poster un commentaire sans être connecté
This commit is contained in:
@@ -10,14 +10,12 @@ $loader->register();
|
||||
|
||||
$security = new \Silex\Controller\SecurityController();
|
||||
$visitor = new \Silex\Controller\VisitorController();
|
||||
$user = new \Silex\Controller\UserController();
|
||||
$admin = new \Silex\Controller\AdminController();
|
||||
$router = new Router($_SERVER['REQUEST_URI']);
|
||||
$router->setBasePath("~cofrizot/silex/index.php");
|
||||
$router->get('/^$/', [$visitor, 'index']);
|
||||
$router->get('/^recent\/(?<page>\d+)$/', [$visitor, 'index']);
|
||||
$router->get('/^news\/(?<slug>[A-Za-z0-9-]+)-(?<id>\d+)$/', [$visitor, 'viewPost']);
|
||||
$router->post('/^comment\/(?<id>\d+)$/', [$user, 'comment']);
|
||||
$router->post('/^comment\/(?<id>\d+)$/', [$visitor, 'comment']);
|
||||
$router->match('/^login$/', [$security, 'login']);
|
||||
$router->match('/^register$/', [$security, 'register']);
|
||||
$router->match('/^logout$/', [$security, 'logout']);
|
||||
|
@@ -23,13 +23,6 @@ class FrontController
|
||||
&& ($di->getSecurity()->getCurrentUser() === null || !$di->getSecurity()->getCurrentUser()->isAdmin())) {
|
||||
HttpResponse::redirect($di->getRouter()->url('login'));
|
||||
}
|
||||
|
||||
if($this->route->getController() instanceof UserController
|
||||
&& $di->getSecurity()->getCurrentUser() === null)
|
||||
{
|
||||
HttpResponse::redirect($di->getRouter()->url('login'));
|
||||
}
|
||||
|
||||
return $this->route->call($di);
|
||||
}
|
||||
}
|
||||
|
@@ -1,24 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Silex\Controller;
|
||||
|
||||
use DateTime;
|
||||
use Silex\DI\DI;
|
||||
use Silex\Http\HttpResponse;
|
||||
use Silex\Model\Comment;
|
||||
use Silex\Util\Pagination;
|
||||
|
||||
class UserController
|
||||
{
|
||||
private const PER_PAGE = 12;
|
||||
|
||||
public function comment(DI $di, array $params): void
|
||||
{
|
||||
$newsId = intval($params['id']);
|
||||
$news = $di->getNewsGateway()->getById($newsId);
|
||||
$di->getCommentGateway()->insert(new Comment(-1, $newsId, new DateTime(), $_POST['content'], $di->getSecurity()->getCurrentUserId()));
|
||||
HttpResponse::redirect($di->getRouter()->url($news->getSlugRedirect()));
|
||||
}
|
||||
}
|
@@ -4,8 +4,10 @@ declare(strict_types=1);
|
||||
|
||||
namespace Silex\Controller;
|
||||
|
||||
use DateTime;
|
||||
use Silex\DI\DI;
|
||||
use Silex\Http\HttpResponse;
|
||||
use Silex\Model\Comment;
|
||||
use Silex\Util\Pagination;
|
||||
|
||||
class VisitorController {
|
||||
@@ -23,7 +25,7 @@ class VisitorController {
|
||||
$nbPages = Pagination::getNbPages($total, self::PER_PAGE);
|
||||
$news = $gw->getPaginatedRecentNews($page , self::PER_PAGE);
|
||||
$nbComments = $gwc->getCommentNumber();
|
||||
if($user !== null){
|
||||
if ($user !== null) {
|
||||
$nbCommentsByUser = $gwc->getCommentNumberFromUser($user->getId());
|
||||
} else {
|
||||
$nbCommentsByUser = 0;
|
||||
@@ -35,10 +37,25 @@ class VisitorController {
|
||||
{
|
||||
$newsId = intval($params['id']);
|
||||
$news = $di->getNewsGateway()->getById($newsId);
|
||||
if($news->getSlug() !== $params['slug']){
|
||||
if ($news->getSlug() !== $params['slug']) {
|
||||
HttpResponse::redirect($di->getRouter()->url($news->getSlugRedirect()));
|
||||
}
|
||||
$comments = $di->getCommentGateway()->getByNewsId($newsId);
|
||||
return new HttpResponse(200, 'newsView', ['news' => $news, 'comments' => $comments]);
|
||||
}
|
||||
}
|
||||
|
||||
public function comment(DI $di, array $params): void
|
||||
{
|
||||
$newsId = intval($params['id']);
|
||||
$news = $di->getNewsGateway()->getById($newsId);
|
||||
$comment = new Comment(-1, $newsId, new DateTime(), $_POST['content']);
|
||||
$author = $di->getSecurity()->getCurrentUser();
|
||||
if ($author !== null) {
|
||||
$comment->setAuthor($author);
|
||||
} else {
|
||||
$comment->setAuthorName($_POST['name']);
|
||||
}
|
||||
$di->getCommentGateway()->insert($comment);
|
||||
HttpResponse::redirect($di->getRouter()->url($news->getSlugRedirect()));
|
||||
}
|
||||
}
|
||||
|
@@ -19,8 +19,16 @@ class CommentGateway
|
||||
|
||||
public function insert(Comment $comment): bool
|
||||
{
|
||||
$req = $this->pdo->prepare('INSERT INTO comment (news_id, content, author_id) VALUES (:news_id, :content, :author_id);');
|
||||
$req->execute(['news_id' => $comment->getNewsId(), 'content' => $comment->getContent(), 'author_id' => $comment->getAuthorId()]);
|
||||
$req = $this->pdo->prepare('INSERT INTO comment (news_id, content, author_id, author_name) VALUES (:news_id, :content, :author_id, :author_name);');
|
||||
$req->bindValue(':news_id', $comment->getNewsId(), PDO::PARAM_INT);
|
||||
$req->bindValue(':content', $comment->getContent());
|
||||
$req->bindValue(':author_id', $comment->getAuthorId(), $comment->getAuthorId() !== null ? PDO::PARAM_INT : PDO::PARAM_NULL);
|
||||
if ($comment->getAuthorId() === null) {
|
||||
$req->bindValue(':author_name', $comment->getAuthorName());
|
||||
} else {
|
||||
$req->bindValue(':author_name', null, PDO::PARAM_NULL);
|
||||
}
|
||||
$req->execute();
|
||||
$comment->setId(intval($this->pdo->lastInsertId()));
|
||||
return true;
|
||||
}
|
||||
@@ -41,9 +49,9 @@ class CommentGateway
|
||||
/**
|
||||
* @return Comment[]
|
||||
*/
|
||||
public function getByNewsId(int $id): array
|
||||
public function getByNewsId(int $id): array
|
||||
{
|
||||
$req = $this->pdo->prepare('SELECT c.*, u.login author_login FROM comment c INNER JOIN registered_user u ON u.id_user = c.author_id WHERE c.news_id = :id ORDER BY c.publication_date ASC');
|
||||
$req = $this->pdo->prepare('SELECT c.*, COALESCE(c.author_name, u.login) author_name FROM comment c LEFT JOIN registered_user u ON u.id_user = c.author_id WHERE c.news_id = :id ORDER BY c.publication_date ASC');
|
||||
$req->bindValue(':id', $id, PDO::PARAM_INT);
|
||||
if (!$req->execute()) {
|
||||
return [];
|
||||
@@ -60,8 +68,7 @@ class CommentGateway
|
||||
$req = $this->pdo->prepare('SELECT COUNT(*) FROM comment');
|
||||
$req->execute();
|
||||
$data = $req->fetch();
|
||||
$nbComment = intval($data[0]);
|
||||
return $nbComment;
|
||||
return intval($data[0]);
|
||||
}
|
||||
|
||||
public function getCommentNumberFromUser(int $id): int
|
||||
@@ -70,8 +77,7 @@ class CommentGateway
|
||||
$req->bindValue(':id', $id, PDO::PARAM_INT);
|
||||
$req->execute();
|
||||
$data = $req->fetch();
|
||||
$nbComment = intval($data[0]);
|
||||
return $nbComment;
|
||||
return intval($data[0]);
|
||||
}
|
||||
|
||||
private function createComment(array $data): Comment
|
||||
@@ -83,9 +89,9 @@ class CommentGateway
|
||||
$data['content'],
|
||||
intval($data['author_id'])
|
||||
);
|
||||
if (isset($data['author_login'])) {
|
||||
$comment->setAuthorLogin($data['author_login']);
|
||||
if (isset($data['author_name'])) {
|
||||
$comment->setAuthorName($data['author_name']);
|
||||
}
|
||||
return $comment;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -12,16 +12,15 @@ class Comment
|
||||
private int $newsId;
|
||||
private DateTime $publicationDate;
|
||||
private string $content;
|
||||
private int $authorId;
|
||||
private string $authorLogin;
|
||||
private ?int $authorId = null;
|
||||
private string $authorName;
|
||||
|
||||
public function __construct(int $idComment, int $newsId, DateTime $publicationDate, string $content, int $authorId)
|
||||
public function __construct(int $idComment, int $newsId, DateTime $publicationDate, string $content)
|
||||
{
|
||||
$this->idComment = $idComment;
|
||||
$this->newsId = $newsId;
|
||||
$this->publicationDate = $publicationDate;
|
||||
$this->content = $content;
|
||||
$this->authorId = $authorId;
|
||||
}
|
||||
|
||||
public function getId(): int
|
||||
@@ -44,14 +43,14 @@ class Comment
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
public function getAuthorId(): int
|
||||
public function getAuthorId(): ?int
|
||||
{
|
||||
return $this->authorId;
|
||||
}
|
||||
|
||||
public function getAuthorLogin(): string
|
||||
public function getAuthorName(): string
|
||||
{
|
||||
return $this->authorLogin;
|
||||
return $this->authorName;
|
||||
}
|
||||
|
||||
public function setId(int $id): void
|
||||
@@ -59,8 +58,14 @@ class Comment
|
||||
$this->idComment = $id;
|
||||
}
|
||||
|
||||
public function setAuthorLogin(string $authorLogin): void
|
||||
public function setAuthorName(string $authorName): void
|
||||
{
|
||||
$this->authorLogin = $authorLogin;
|
||||
$this->authorName = $authorName;
|
||||
}
|
||||
}
|
||||
|
||||
public function setAuthor(User $author): void
|
||||
{
|
||||
$this->authorId = $author->getId();
|
||||
$this->authorName = $author->getLogin();
|
||||
}
|
||||
}
|
||||
|
@@ -20,7 +20,9 @@ CREATE TABLE comment (
|
||||
news_id INT NOT NULL,
|
||||
publication_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
content TEXT NOT NULL,
|
||||
author_id INT NOT NULL,
|
||||
author_id INT NULL,
|
||||
author_name VARCHAR(32) NULL,
|
||||
CONSTRAINT either_authors CHECK ((author_id IS NULL) != (author_name IS NULL)),
|
||||
FOREIGN KEY (news_id) REFERENCES news(id_news)
|
||||
ON DELETE CASCADE,
|
||||
FOREIGN KEY (author_id) REFERENCES registered_user(id_user)
|
||||
|
@@ -1,43 +1,59 @@
|
||||
<?php $title = $params['news']->getTitle(); ?>
|
||||
<?php
|
||||
/**
|
||||
* @var \Silex\Model\News $news
|
||||
*/
|
||||
$news = $params['news'];
|
||||
/**
|
||||
* @var \Silex\Model\Comment[] $comments
|
||||
*/
|
||||
$comments = $params['comments'];
|
||||
$title = $news->getTitle();
|
||||
?>
|
||||
<h1>News</h1>
|
||||
<div class="card">
|
||||
<header class="card-header">
|
||||
<p class="card-header-title">
|
||||
<?= $params['news']->getTitle() ?>
|
||||
<?= $news->getTitle() ?>
|
||||
</p>
|
||||
</header>
|
||||
<div class="card-content">
|
||||
<div class="content">
|
||||
<?= $params['news']->getContent() ?>
|
||||
<?= $news->getContent() ?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<section class="section comments">
|
||||
<?php foreach ($params['comments'] as $comment) : ?>
|
||||
<?php foreach ($comments as $comment) : ?>
|
||||
<article class="message">
|
||||
<header class="message-header">
|
||||
From <?= $comment->getAuthorLogin() ?> published on <?= $comment->getPublicationDate()->format('Y-m-d H:i:s') ?>
|
||||
From <?= $comment->getAuthorName() ?> published on <?= $comment->getPublicationDate()->format('Y-m-d H:i:s') ?>
|
||||
</header>
|
||||
<div class="message-body">
|
||||
<?= $comment->getContent() ?>
|
||||
</div>
|
||||
</article>
|
||||
<?php endforeach; ?>
|
||||
<?php if ($security->getCurrentUserId() !== null) : ?>
|
||||
<form action="<?= $router->url('comment/' . $params['news']->getId()) ?>" method="post">
|
||||
<form action="<?= $router->url('comment/' . $params['news']->getId()) ?>" method="post">
|
||||
<?php if ($security->getCurrentUserId() === null): ?>
|
||||
<div class="field">
|
||||
<label class="label" for="content">Comment</label>
|
||||
<label class="label" for="name">Name</label>
|
||||
<div class="control">
|
||||
<textarea class="textarea" id="content" name="content"></textarea>
|
||||
<input class="input" id="name" name="name">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<div class="control">
|
||||
<button class="button is-link">Submit</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
<?php endif; ?>
|
||||
<div class="field">
|
||||
<label class="label" for="content">Comment</label>
|
||||
<div class="control">
|
||||
<textarea class="textarea" id="content" name="content"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<div class="control">
|
||||
<button class="button is-link">Submit</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</section>
|
||||
|
Reference in New Issue
Block a user