parent
b188dc8cd0
commit
7596d26652
@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Silex\Controller;
|
||||||
|
|
||||||
|
use Silex\DI\DI;
|
||||||
|
use Silex\Http\HttpResponse;
|
||||||
|
use Silex\Util\Pagination;
|
||||||
|
|
||||||
|
class VisitorController {
|
||||||
|
|
||||||
|
private const PER_PAGE = 12;
|
||||||
|
|
||||||
|
public function index(DI $di, array $params): HttpResponse
|
||||||
|
{
|
||||||
|
$gw = $di->getNewsGateway();
|
||||||
|
$gwc = $di->getCommentGateway();
|
||||||
|
$user = $di->getSecurity()->getCurrentUser();
|
||||||
|
|
||||||
|
$page = intval($params['page'] ?? 1);
|
||||||
|
$total = $gw->getCount();
|
||||||
|
$nbPages = Pagination::getNbPages($total, self::PER_PAGE);
|
||||||
|
$news = $gw->getPaginatedRecentNews($page , self::PER_PAGE);
|
||||||
|
$nbComments = $gwc->getCommentNumber();
|
||||||
|
if($user !== null){
|
||||||
|
$nbCommentsByUser = $gwc->getCommentNumberFromUser($user->getId());
|
||||||
|
} else {
|
||||||
|
$nbCommentsByUser = 0;
|
||||||
|
}
|
||||||
|
return new HttpResponse(200, 'home', ['news' => $news, 'page' => $page, 'nbPages' => $nbPages, 'nbComments' => $nbComments, 'nbCommentsByUser' => $nbCommentsByUser]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function viewPost(DI $di, array $params): HttpResponse
|
||||||
|
{
|
||||||
|
$newsId = intval($params['id']);
|
||||||
|
$news = $di->getNewsGateway()->getById($newsId);
|
||||||
|
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]);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue