Permet de poster un commentaire sans être connecté

This commit is contained in:
2022-12-11 12:04:47 +01:00
parent 7596d26652
commit c2412de95a
8 changed files with 88 additions and 75 deletions

View File

@@ -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()));
}
}