You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.5 KiB

<?php
declare(strict_types=1);
namespace Silex\Controller;
use Silex\DI\DI;
use Silex\Http\HttpResponse;
use Silex\Util\Pagination;
class UserController
{
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, 'router' => $di->getRouter(), 'nbComments' => $nbComments, 'nbCommentsByUser' => $nbCommentsByUser]);
}
public function viewPost(DI $di, array $params): HttpResponse
{
$newsId = intval($params['id']);
$news = $di->getNewsGateway()->getById($newsId);
return new HttpResponse(200, 'newsView', ['news' => $news]);
}
public function viewPostComments(DI $di, array $params): HttpResponse
{
$newsId = intval($params['id']);
$comments = $di->getCommentGateway()->getByNewsId($newsId);
return new HttpResponse(200, 'comment', ['comments' => $comments]);
}
}