1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys BlogBundle package.
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Rapsys\BlogBundle\Controller
;
14 use Symfony\Component\HttpFoundation\Request
;
15 use Symfony\Component\HttpFoundation\Response
;
17 use Rapsys\BlogBundle\Entity\Article
;
18 use Rapsys\BlogBundle\Entity\Keyword
;
19 use Rapsys\BlogBundle\Entity\User
;
24 class UserController
extends AbstractController
{
30 * @param Request $request The request instance
31 * @return Response The rendered view
33 public function index(Request
$request): Response
{
34 //With not enough users
35 if (($this->count
= $this->doctrine
->getRepository(User
::class)->findCountAsInt()) < $this->page
* $this->limit
) {
37 throw $this->createNotFoundException($this->translator
->trans('Unable to find users'));
41 if ($this->context
['users'] = $this->doctrine
->getRepository(User
::class)->findAllAsArray($this->page
, $this->limit
)) {
43 $this->modified
= max(array_map(function ($v) { return $v
['modified']; }, $this->context
['users']));
47 $this->modified
= new \
DateTime('-1 year');
51 $response = new Response();
54 if ($this->checker
->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
56 $response->setLastModified(new \
DateTime('-1 year'));
59 $response->setPrivate();
63 //XXX: only for public to force revalidation by last modified
64 $response->setEtag(md5(serialize($this->context
['users'])));
67 $response->setLastModified($this->modified
);
70 $response->setPublic();
72 //Without role and modification
73 if ($response->isNotModified($request)) {
80 $this->context
['head']['keywords'] = implode(
82 //Use closure to extract each unique article keywords sorted
90 if (!empty($u['articles'])) {
92 foreach($u['articles'] as $a) {
94 if (!empty($a['keywords'])) {
96 foreach($a['keywords'] as $k) {
98 $r[$k['title']] = $k['title'];
110 })($this->context
['users'])
114 $this->context
['title'] = $this->translator
->trans('Users list');
117 $this->context
['description'] = $this->translator
->trans('Welcome to raphaël\'s developer diary user listing');
120 return $this->render('@RapsysBlog/user/index.html.twig', $this->context
, $response);
126 * Display user, articles and keywords
128 * @param Request $request The request instance
129 * @param integer $id The user id
130 * @param ?string $slug The user slug
131 * @return Response The rendered view
133 public function view(Request
$request, int $id, ?string $slug): Response
{
135 if (empty($this->context
['user'] = $this->doctrine
->getRepository(User
::class)->findByIdAsArray($id))) {
137 throw $this->createNotFoundException($this->translator
->trans('Unable to find user: %id%', ['%id%' => $id]));
141 if ($slug !== $this->context
['user']['slug']) {
142 //Redirect on correctly spelled user
143 return $this->redirectToRoute('rapsys_blog_user_view', ['id' => $this->context
['user']['id'], 'slug' => $this->context
['user']['slug']], Response
::HTTP_MOVED_PERMANENTLY
);
147 $this->modified
= $this->context
['user']['modified'];
150 $response = new Response();
153 if ($this->checker
->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
155 $response->setLastModified(new \
DateTime('-1 year'));
158 $response->setPrivate();
159 //Without logged user
162 //XXX: only for public to force revalidation by last modified
163 $response->setEtag(md5(serialize($this->context
['user'])));
166 $response->setLastModified($this->modified
);
169 $response->setPublic();
171 //Without role and modification
172 if ($response->isNotModified($request)) {
173 //Return 304 response
179 $this->context
['head']['keywords'] = implode(
181 //Use closure to extract each unique article keywords sorted
187 if (!empty($u['articles'])) {
188 //Iterate on articles
189 foreach($u['articles'] as $a) {
191 if (!empty($a['keywords'])) {
192 //Iterate on keywords
193 foreach($a['keywords'] as $k) {
195 $r[$k['title']] = $k['title'];
206 })($this->context
['user'])
210 $this->context
['title'] = $this->context
['user']['pseudonym'];
213 //TODO: Add user creation ? Add a description field ?
214 #$this->context['description'] = $this->context['user']['description'];
217 return $this->render('@RapsysBlog/user/view.html.twig', $this->context
, $response);