]> Raphaël G. Git Repositories - blogbundle/blob - Controller/UserController.php
Add pseudonym and slug translations
[blogbundle] / Controller / UserController.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys BlogBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\BlogBundle\Controller;
13
14 use Symfony\Component\HttpFoundation\Request;
15 use Symfony\Component\HttpFoundation\Response;
16
17 use Rapsys\BlogBundle\Entity\Article;
18 use Rapsys\BlogBundle\Entity\Keyword;
19 use Rapsys\BlogBundle\Entity\User;
20
21 /**
22 * {@inheritdoc}
23 */
24 class UserController extends AbstractController {
25 /**
26 * The user index
27 *
28 * Display users
29 *
30 * @param Request $request The request instance
31 * @return Response The rendered view
32 */
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) {
36 //Throw 404
37 throw $this->createNotFoundException($this->translator->trans('Unable to find users'));
38 }
39
40 //Get users
41 if ($this->context['users'] = $this->doctrine->getRepository(User::class)->findAllAsArray($this->page, $this->limit)) {
42 //Set modified
43 $this->modified = max(array_map(function ($v) { return $v['modified']; }, $this->context['users']));
44 //Without keywords
45 } else {
46 //Set empty modified
47 $this->modified = new \DateTime('-1 year');
48 }
49
50 //Create response
51 $response = new Response();
52
53 //With logged user
54 if ($this->checker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
55 //Set last modified
56 $response->setLastModified(new \DateTime('-1 year'));
57
58 //Set as private
59 $response->setPrivate();
60 //Without logged user
61 } else {
62 //Set etag
63 //XXX: only for public to force revalidation by last modified
64 $response->setEtag(md5(serialize($this->context['users'])));
65
66 //Set last modified
67 $response->setLastModified($this->modified);
68
69 //Set as public
70 $response->setPublic();
71
72 //Without role and modification
73 if ($response->isNotModified($request)) {
74 //Return 304 response
75 return $response;
76 }
77 }
78
79 //Set keywords
80 $this->context['head']['keywords'] = implode(
81 ', ',
82 //Use closure to extract each unique article keywords sorted
83 (function ($t) {
84 //Return array
85 $r = [];
86
87 //Iterate on users
88 foreach($t as $u) {
89 //Non empty articles
90 if (!empty($u['articles'])) {
91 //Iterate on articles
92 foreach($u['articles'] as $a) {
93 //Non empty keywords
94 if (!empty($a['keywords'])) {
95 //Iterate on keywords
96 foreach($a['keywords'] as $k) {
97 //Set keyword
98 $r[$k['title']] = $k['title'];
99 }
100 }
101 }
102 }
103 }
104
105 //Sort array
106 sort($r);
107
108 //Return array
109 return $r;
110 })($this->context['users'])
111 );
112
113 //Set title
114 $this->context['title'] = $this->translator->trans('Users list');
115
116 //Set description
117 $this->context['description'] = $this->translator->trans('Welcome to raphaël\'s developer diary user listing');
118
119 //Render the view
120 return $this->render('@RapsysBlog/user/index.html.twig', $this->context, $response);
121 }
122
123 /**
124 * The user view
125 *
126 * Display user, articles and keywords
127 *
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
132 */
133 public function view(Request $request, int $id, ?string $slug): Response {
134 //Without user
135 if (empty($this->context['user'] = $this->doctrine->getRepository(User::class)->findByIdAsArray($id))) {
136 //Throw 404
137 throw $this->createNotFoundException($this->translator->trans('Unable to find user: %id%', ['%id%' => $id]));
138 }
139
140 //With invalid slug
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);
144 }
145
146 //Set modified
147 $this->modified = $this->context['user']['modified'];
148
149 //Create response
150 $response = new Response();
151
152 //With logged user
153 if ($this->checker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
154 //Set last modified
155 $response->setLastModified(new \DateTime('-1 year'));
156
157 //Set as private
158 $response->setPrivate();
159 //Without logged user
160 } else {
161 //Set etag
162 //XXX: only for public to force revalidation by last modified
163 $response->setEtag(md5(serialize($this->context['user'])));
164
165 //Set last modified
166 $response->setLastModified($this->modified);
167
168 //Set as public
169 $response->setPublic();
170
171 //Without role and modification
172 if ($response->isNotModified($request)) {
173 //Return 304 response
174 return $response;
175 }
176 }
177
178 //Set keywords
179 $this->context['head']['keywords'] = implode(
180 ', ',
181 //Use closure to extract each unique article keywords sorted
182 (function ($u) {
183 //Return array
184 $r = [];
185
186 //Non empty articles
187 if (!empty($u['articles'])) {
188 //Iterate on articles
189 foreach($u['articles'] as $a) {
190 //Non empty keywords
191 if (!empty($a['keywords'])) {
192 //Iterate on keywords
193 foreach($a['keywords'] as $k) {
194 //Set keyword
195 $r[$k['title']] = $k['title'];
196 }
197 }
198 }
199 }
200
201 //Sort array
202 sort($r);
203
204 //Return array
205 return $r;
206 })($this->context['user'])
207 );
208
209 //Set title
210 $this->context['title'] = $this->context['user']['pseudonym'];
211
212 //Set description
213 //TODO: Add user creation ? Add a description field ?
214 #$this->context['description'] = $this->context['user']['description'];
215
216 //Render the view
217 return $this->render('@RapsysBlog/user/view.html.twig', $this->context, $response);
218 }
219 }