Lis le nombre de commentaires écrits depuis un cookie
This commit is contained in:
@@ -8,6 +8,7 @@ use DateTime;
|
||||
use Silex\DI\DI;
|
||||
use Silex\Http\HttpResponse;
|
||||
use Silex\Model\Comment;
|
||||
use Silex\Util\CommentCounter;
|
||||
use Silex\Util\Pagination;
|
||||
use Silex\Validation\CommentValidation;
|
||||
use Silex\Validation\NewsValidation;
|
||||
@@ -37,7 +38,7 @@ class VisitorController {
|
||||
if ($user !== null) {
|
||||
$nbCommentsByUser = $gwc->getCommentNumberFromUser($user->getId());
|
||||
} else {
|
||||
$nbCommentsByUser = 0;
|
||||
$nbCommentsByUser = $_COOKIE['comments'] ?? '0';
|
||||
}
|
||||
return new HttpResponse(200, 'home', ['news' => $news, 'page' => $page, 'nbPages' => $nbPages, 'nbComments' => $nbComments, 'nbCommentsByUser' => $nbCommentsByUser, 'errors' => $errors]);
|
||||
}
|
||||
@@ -76,6 +77,7 @@ class VisitorController {
|
||||
$comment->setAuthorName($_POST['name']);
|
||||
}
|
||||
$di->getCommentGateway()->insert($comment);
|
||||
CommentCounter::incrementCommentCounter();
|
||||
HttpResponse::redirect($di->getRouter()->url($news->getSlugRedirect()));
|
||||
exit();
|
||||
}
|
||||
|
17
src/Silex/Util/CommentCounter.php
Normal file
17
src/Silex/Util/CommentCounter.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Silex\Util;
|
||||
|
||||
const COMMENTS = 'comments';
|
||||
|
||||
final class CommentCounter
|
||||
{
|
||||
public static function incrementCommentCounter()
|
||||
{
|
||||
$c = $_COOKIE[COMMENTS] ?? '0';
|
||||
if (!is_numeric($c)) {
|
||||
$c = '0';
|
||||
}
|
||||
setcookie(COMMENTS, intval($c) + 1, time() + 60*60*24*30, '/');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user