]> Raphaël G. Git Repositories - blogbundle/blob - Repository/UserRepository.php
Replace EntityRepository by Repository class
[blogbundle] / Repository / UserRepository.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\Repository;
13
14 use Doctrine\ORM\Query\ResultSetMapping;
15
16 use Rapsys\BlogBundle\Repository;
17
18 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
19 use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
20 use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
21
22 /**
23 * UserRepository
24 */
25 class UserRepository extends Repository implements PasswordUpgraderInterface {
26 /**
27 * Find user count as int
28 *
29 * @return integer The keywords count
30 */
31 public function findCountAsInt(): int {
32 //Set the request
33 $req = <<<SQL
34 SELECT COUNT(u.id) AS count
35 FROM RapsysBlogBundle:User AS u
36 SQL;
37
38 //Get result set mapping instance
39 $req = $this->replace($req);
40
41 //Get result set mapping instance
42 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
43 $rsm = new ResultSetMapping();
44
45 //Declare all fields
46 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
47 //addScalarResult($sqlColName, $resColName, $type = 'string');
48 $rsm->addScalarResult('count', 'count', 'integer');
49
50 //Get result
51 return $this->manager
52 ->createNativeQuery($req, $rsm)
53 ->getSingleScalarResult();
54 }
55
56 /**
57 * Find all users as array
58 *
59 * @param integer $page The page
60 * @param integer $count The count
61 * @return array The users sorted by id
62 */
63 public function findAllAsArray(int $page, int $count): array {
64 //Set the request
65 $req = <<<SQL
66 SELECT
67 t.id,
68 t.mail,
69 t.forename,
70 t.surname,
71 t.pseudonym,
72 t.slug,
73 t.created,
74 t.updated,
75 GREATEST(t.created, t.updated) AS modified,
76 t.civility,
77 t.g_ids,
78 t.g_titles,
79 GROUP_CONCAT(t.a_id ORDER BY t.a_id SEPARATOR "\\n") AS a_ids,
80 GROUP_CONCAT(t.at_description ORDER BY t.a_id SEPARATOR "\\n") AS at_descriptions,
81 GROUP_CONCAT(t.at_slug ORDER BY t.a_id SEPARATOR "\\n") AS at_slugs,
82 GROUP_CONCAT(t.at_title ORDER BY t.a_id SEPARATOR "\\n") AS at_titles,
83 GROUP_CONCAT(t.ak_ids ORDER BY t.a_id SEPARATOR "\\n") AS ak_ids,
84 GROUP_CONCAT(t.kt_slugs ORDER BY t.a_id SEPARATOR "\\n") AS kt_slugs,
85 GROUP_CONCAT(t.kt_titles ORDER BY t.a_id SEPARATOR "\\n") AS kt_titles
86 FROM (
87 SELECT
88 c.id,
89 c.mail,
90 c.forename,
91 c.surname,
92 c.pseudonym,
93 c.slug,
94 GREATEST(c.created, COALESCE(a.created, 0), COALESCE(at.created, 0), COALESCE(k.created, 0), COALESCE(kt.created, 0)) AS created,
95 GREATEST(c.updated, COALESCE(a.updated, 0), COALESCE(at.updated, 0), COALESCE(k.updated, 0), COALESCE(kt.updated, 0)) AS updated,
96 c.civility,
97 c.g_ids,
98 c.g_titles,
99 a.id AS a_id,
100 at.description AS at_description,
101 at.slug AS at_slug,
102 at.title AS at_title,
103 GROUP_CONCAT(ak.keyword_id ORDER BY ak.keyword_id SEPARATOR "\\r") AS ak_ids,
104 GROUP_CONCAT(kt.slug ORDER BY ak.keyword_id SEPARATOR "\\r") AS kt_slugs,
105 GROUP_CONCAT(kt.title ORDER BY ak.keyword_id SEPARATOR "\\r") AS kt_titles
106 FROM (
107 SELECT
108 u.id,
109 u.mail,
110 u.forename,
111 u.surname,
112 u.pseudonym,
113 u.slug,
114 GREATEST(u.created, c.created, g.created) AS created,
115 GREATEST(u.updated, c.updated, g.updated) AS updated,
116 c.title AS civility,
117 GROUP_CONCAT(g.id ORDER BY g.id SEPARATOR "\\n") AS g_ids,
118 GROUP_CONCAT(g.title ORDER BY g.id SEPARATOR "\\n") AS g_titles
119 FROM RapsysBlogBundle:User AS u
120 JOIN RapsysBlogBundle:UserGroup AS gu ON (gu.user_id = u.id)
121 JOIN RapsysBlogBundle:Group AS g ON (g.id = gu.group_id)
122 JOIN RapsysBlogBundle:Civility AS c ON (c.id = u.civility_id)
123 GROUP BY u.id
124 ORDER BY NULL
125 LIMIT 0, :limit
126 ) AS c
127 LEFT JOIN RapsysBlogBundle:Article AS a ON (a.user_id = c.id)
128 LEFT JOIN RapsysBlogBundle:ArticleTranslation AS at ON (at.article_id = a.id AND at.locale = :locale)
129 LEFT JOIN RapsysBlogBundle:ArticleKeyword AS ak ON (ak.article_id = a.id)
130 LEFT JOIN RapsysBlogBundle:Keyword AS k ON (k.id = ak.keyword_id)
131 LEFT JOIN RapsysBlogBundle:KeywordTranslation AS kt ON (kt.keyword_id = ak.keyword_id AND at.locale = :locale)
132 GROUP BY a.id
133 ORDER BY NULL
134 LIMIT 0, :limit
135 ) AS t
136 GROUP BY t.id
137 ORDER BY t.id ASC
138 LIMIT :offset, :count
139 SQL;
140
141 //Replace bundle entity name by table name
142 $req = $this->replace($req);
143
144 //Get result set mapping instance
145 //XXX: DEBUG: see ../blog.orig/src/Rapsys/UserBundle/Repository/ArticleRepository.php
146 $rsm = new ResultSetMapping();
147
148 //Declare all fields
149 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
150 //addScalarResult($sqlColName, $resColName, $type = 'string');
151 $rsm->addScalarResult('id', 'id', 'integer')
152 ->addScalarResult('mail', 'mail', 'string')
153 ->addScalarResult('forename', 'forename', 'string')
154 ->addScalarResult('surname', 'surname', 'string')
155 ->addScalarResult('pseudonym', 'pseudonym', 'string')
156 ->addScalarResult('slug', 'slug', 'string')
157 ->addScalarResult('created', 'created', 'datetime_immutable')
158 ->addScalarResult('updated', 'updated', 'datetime_immutable')
159 ->addScalarResult('modified', 'modified', 'datetime_immutable')
160 ->addScalarResult('civility', 'civility', 'string')
161 //XXX: is a string because of \n separator
162 ->addScalarResult('g_ids', 'g_ids', 'string')
163 //XXX: is a string because of \n separator
164 ->addScalarResult('g_titles', 'g_titles', 'string')
165 //XXX: is a string because of \n separator
166 ->addScalarResult('a_ids', 'a_ids', 'string')
167 //XXX: is a string because of \n separator
168 ->addScalarResult('at_descriptions', 'at_descriptions', 'string')
169 //XXX: is a string because of \n separator
170 ->addScalarResult('at_slugs', 'at_slugs', 'string')
171 //XXX: is a string because of \n separator
172 ->addScalarResult('at_titles', 'at_titles', 'string')
173 //XXX: is a string because of \n separator
174 ->addScalarResult('ak_ids', 'ak_ids', 'string')
175 //XXX: is a string because of \n separator
176 ->addScalarResult('kt_slugs', 'kt_slugs', 'string')
177 //XXX: is a string because of \n separator
178 ->addScalarResult('kt_titles', 'kt_titles', 'string');
179
180 //Fetch result
181 $res = $this->manager
182 ->createNativeQuery($req, $rsm)
183 ->setParameter('offset', $page * $count)
184 ->setParameter('count', $count)
185 ->getResult();
186
187 //Init return
188 $ret = [];
189
190 //Process result
191 foreach($res as $data) {
192 //Set data
193 $ret[$data['id']] = [
194 'mail' => $data['mail'],
195 'forename' => $data['forename'],
196 'surname' => $data['surname'],
197 'pseudonym' => $data['pseudonym'],
198 'created' => $data['created'],
199 'updated' => $data['updated'],
200 'modified' => $data['modified'],
201 'civility' => $data['civility'],
202 #'slug' => $data['slug'],
203 'link' => $this->router->generate('rapsysblog_user_view', ['id' => $data['id'], 'slug' => $data['slug']]),
204 'edit' => $this->router->generate('rapsysuser_edit', ['mail' => $short = $this->slugger->short($data['mail']), 'hash' => $this->slugger->hash($short)]),
205 'articles' => [],
206 'groups' => []
207 ];
208
209 //With groups
210 if (!empty($data['g_ids'])) {
211 //Set titles
212 $titles = explode("\n", $data['g_titles']);
213
214 //Iterate on each group
215 foreach(explode("\n", $data['g_ids']) as $k => $id) {
216 //Add group
217 $ret[$data['id']]['groups'][$id] = [
218 'title' => /*$group = */$this->translator->trans($titles[$k]),
219 #'slug' => $this->slugger->slug($group)
220 #'link' => $this->router->generate('rapsysuser_group_view', ['id' => $id, 'slug' => $this->slugger->short($group)])
221 ];
222 }
223 }
224
225 //With articles
226 if (!empty($data['a_ids'])) {
227 //Set descriptions
228 $descriptions = explode("\n", $data['at_descriptions']);
229
230 //Set slugs
231 $slugs = explode("\n", $data['at_slugs']);
232
233 //Set titles
234 $titles = explode("\n", $data['at_titles']);
235
236 //Set keyword ids
237 $keywords = [
238 'ids' => explode("\n", $data['ak_ids']),
239 'slugs' => explode("\n", $data['kt_slugs']),
240 'titles' => explode("\n", $data['kt_titles'])
241 ];
242
243 //Iterate on each dance
244 foreach(explode("\n", $data['a_ids']) as $k => $id) {
245 //Init article when missing
246 if (!isset($ret[$data['id']]['articles'][$id])) {
247 //Add article
248 $ret[$data['id']]['articles'][$id] = [
249 'description' => $descriptions[$k],
250 #'slug' => $slugs[$k],
251 'title' => $titles[$k],
252 'link' => $this->router->generate('rapsysblog_article_view', ['id' => $id, 'slug' => $slugs[$k]]),
253 //TODO: replace with keywords !!!
254 'keywords' => []
255 ];
256
257 //With article keywords
258 if (!empty($keywords['ids'][$k])) {
259 //Set slugs
260 $slugs = explode("\r", $keywords['slugs'][$k]);
261
262 //Set titles
263 $titles = explode("\r", $keywords['titles'][$k]);
264
265 //Iterate on each keyword
266 foreach(explode("\r", $keywords['ids'][$k]) as $k => $kid) {
267 //Add keyword
268 $ret[$data['id']]['articles'][$id]['keywords'][$kid] = [
269 #'slug' => $slugs[$k],
270 'title' => $titles[$k],
271 'link' => $this->router->generate('rapsysblog_keyword_view', ['id' => $kid, 'slug' => $slugs[$k]]),
272 ];
273 }
274 }
275 }
276 }
277 }
278 }
279
280 //Send result
281 return $ret;
282 }
283
284 /**
285 * Find user by id as array
286 *
287 * @param integer $id The user id
288 * @return ?array The article array
289 */
290 public function findByIdAsArray(int $id): ?array {
291 //Set the request
292 $req = <<<SQL
293 SELECT
294 t.id,
295 t.mail,
296 t.forename,
297 t.surname,
298 t.pseudonym,
299 t.slug,
300 t.created,
301 t.updated,
302 GREATEST(t.created, t.updated) AS modified,
303 t.civility,
304 t.g_ids,
305 t.g_titles,
306 GROUP_CONCAT(t.a_id ORDER BY t.a_id SEPARATOR "\\n") AS a_ids,
307 GROUP_CONCAT(t.at_description ORDER BY t.a_id SEPARATOR "\\n") AS at_descriptions,
308 GROUP_CONCAT(t.at_slug ORDER BY t.a_id SEPARATOR "\\n") AS at_slugs,
309 GROUP_CONCAT(t.at_title ORDER BY t.a_id SEPARATOR "\\n") AS at_titles,
310 GROUP_CONCAT(t.ak_ids ORDER BY t.a_id SEPARATOR "\\n") AS ak_ids,
311 GROUP_CONCAT(t.kt_slugs ORDER BY t.a_id SEPARATOR "\\n") AS kt_slugs,
312 GROUP_CONCAT(t.kt_titles ORDER BY t.a_id SEPARATOR "\\n") AS kt_titles
313 FROM (
314 SELECT
315 c.id,
316 c.mail,
317 c.forename,
318 c.surname,
319 c.pseudonym,
320 c.slug,
321 GREATEST(c.created, COALESCE(a.created, 0), COALESCE(at.created, 0), COALESCE(k.created, 0), COALESCE(kt.created, 0)) AS created,
322 GREATEST(c.updated, COALESCE(a.updated, 0), COALESCE(at.updated, 0), COALESCE(k.updated, 0), COALESCE(kt.updated, 0)) AS updated,
323 c.civility,
324 c.g_ids,
325 c.g_titles,
326 a.id AS a_id,
327 at.description AS at_description,
328 at.slug AS at_slug,
329 at.title AS at_title,
330 GROUP_CONCAT(ak.keyword_id ORDER BY ak.keyword_id SEPARATOR "\\r") AS ak_ids,
331 GROUP_CONCAT(kt.slug ORDER BY ak.keyword_id SEPARATOR "\\r") AS kt_slugs,
332 GROUP_CONCAT(kt.title ORDER BY ak.keyword_id SEPARATOR "\\r") AS kt_titles
333 FROM (
334 SELECT
335 u.id,
336 u.mail,
337 u.forename,
338 u.surname,
339 u.pseudonym,
340 u.slug,
341 GREATEST(u.created, c.created, g.created) AS created,
342 GREATEST(u.updated, c.updated, g.updated) AS updated,
343 c.title AS civility,
344 GROUP_CONCAT(g.id ORDER BY g.id SEPARATOR "\\n") AS g_ids,
345 GROUP_CONCAT(g.title ORDER BY g.id SEPARATOR "\\n") AS g_titles
346 FROM RapsysBlogBundle:User AS u
347 JOIN RapsysBlogBundle:UserGroup AS gu ON (gu.user_id = u.id)
348 JOIN RapsysBlogBundle:Group AS g ON (g.id = gu.group_id)
349 JOIN RapsysBlogBundle:Civility AS c ON (c.id = u.civility_id)
350 WHERE u.id = :id
351 GROUP BY u.id
352 ORDER BY NULL
353 LIMIT 0, :limit
354 ) AS c
355 LEFT JOIN RapsysBlogBundle:Article AS a ON (a.user_id = c.id)
356 LEFT JOIN RapsysBlogBundle:ArticleTranslation AS at ON (at.article_id = a.id AND at.locale = :locale)
357 LEFT JOIN RapsysBlogBundle:ArticleKeyword AS ak ON (ak.article_id = a.id)
358 LEFT JOIN RapsysBlogBundle:Keyword AS k ON (k.id = ak.keyword_id)
359 LEFT JOIN RapsysBlogBundle:KeywordTranslation AS kt ON (kt.keyword_id = ak.keyword_id AND at.locale = :locale)
360 GROUP BY a.id
361 ORDER BY NULL
362 LIMIT 0, :limit
363 ) AS t
364 GROUP BY t.id
365 SQL;
366
367 //Replace bundle entity name by table name
368 $req = $this->replace($req);
369
370 //Get result set mapping instance
371 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
372 $rsm = new ResultSetMapping();
373
374 //Declare all fields
375 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
376 //addScalarResult($sqlColName, $resColName, $type = 'string');
377 $rsm->addScalarResult('id', 'id', 'integer')
378 ->addScalarResult('mail', 'mail', 'string')
379 ->addScalarResult('forename', 'forename', 'string')
380 ->addScalarResult('surname', 'surname', 'string')
381 ->addScalarResult('pseudonym', 'pseudonym', 'string')
382 ->addScalarResult('slug', 'slug', 'string')
383 ->addScalarResult('created', 'created', 'datetime_immutable')
384 ->addScalarResult('updated', 'updated', 'datetime_immutable')
385 ->addScalarResult('modified', 'modified', 'datetime_immutable')
386 ->addScalarResult('civility', 'civility', 'string')
387 //XXX: is a string because of \n separator
388 ->addScalarResult('g_ids', 'g_ids', 'string')
389 //XXX: is a string because of \n separator
390 ->addScalarResult('g_titles', 'g_titles', 'string')
391 //XXX: is a string because of \n separator
392 ->addScalarResult('a_ids', 'a_ids', 'string')
393 //XXX: is a string because of \n separator
394 ->addScalarResult('at_descriptions', 'at_descriptions', 'string')
395 //XXX: is a string because of \n separator
396 ->addScalarResult('at_slugs', 'at_slugs', 'string')
397 //XXX: is a string because of \n separator
398 ->addScalarResult('at_titles', 'at_titles', 'string')
399 //XXX: is a string because of \n separator
400 ->addScalarResult('ak_ids', 'ak_ids', 'string')
401 //XXX: is a string because of \n separator
402 ->addScalarResult('kt_slugs', 'kt_slugs', 'string')
403 //XXX: is a string because of \n separator
404 ->addScalarResult('kt_titles', 'kt_titles', 'string');
405
406 //Get data
407 if (($data = $this->manager
408 ->createNativeQuery($req, $rsm)
409 ->setParameter('id', $id)
410 ->getOneOrNullResult()) === null) {
411 //Return null
412 return $data;
413 }
414
415 //Set return
416 $return = [
417 'id' => $data['id'],
418 'mail' => $data['mail'],
419 'forename' => $data['forename'],
420 'surname' => $data['surname'],
421 'pseudonym' => $data['pseudonym'],
422 'created' => $data['created'],
423 'updated' => $data['updated'],
424 'modified' => $data['modified'],
425 'civility' => $data['civility'],
426 'slug' => $data['slug'],
427 'link' => $this->router->generate('rapsysblog_user_view', ['id' => $data['id'], 'slug' => $data['slug']]),
428 'edit' => $this->router->generate('rapsysuser_edit', ['mail' => $short = $this->slugger->short($data['mail']), 'hash' => $this->slugger->hash($short)]),
429 'articles' => [],
430 'groups' => []
431 ];
432
433 //With groups
434 if (!empty($data['g_ids'])) {
435 //Set titles
436 $titles = explode("\n", $data['g_titles']);
437
438 //Iterate on each group
439 foreach(explode("\n", $data['g_ids']) as $k => $id) {
440 //Add group
441 $return['groups'][$id] = [
442 'title' => /*$group = */$this->translator->trans($titles[$k]),
443 #'slug' => $this->slugger->slug($group)
444 #'link' => $this->router->generate('rapsysuser_group_view', ['id' => $id, 'slug' => $this->slugger->short($group)])
445 ];
446 }
447 }
448
449 //With articles
450 if (!empty($data['a_ids'])) {
451 //Set descriptions
452 $descriptions = explode("\n", $data['at_descriptions']);
453
454 //Set slugs
455 $slugs = explode("\n", $data['at_slugs']);
456
457 //Set titles
458 $titles = explode("\n", $data['at_titles']);
459
460 //Set keyword ids
461 $keywords = [
462 'ids' => explode("\n", $data['ak_ids']),
463 'slugs' => explode("\n", $data['kt_slugs']),
464 'titles' => explode("\n", $data['kt_titles'])
465 ];
466
467 //Iterate on each dance
468 foreach(explode("\n", $data['a_ids']) as $k => $id) {
469 //Init article when missing
470 if (!isset($ret[$data['id']]['articles'][$id])) {
471 //Add article
472 $return['articles'][$id] = [
473 'description' => $descriptions[$k],
474 #'slug' => $slugs[$k],
475 'title' => $titles[$k],
476 'link' => $this->router->generate('rapsysblog_article_view', ['id' => $id, 'slug' => $slugs[$k]]),
477 //TODO: replace with keywords !!!
478 'keywords' => []
479 ];
480
481 //With article keywords
482 if (!empty($keywords['ids'][$k])) {
483 //Set slugs
484 $slugs = explode("\r", $keywords['slugs'][$k]);
485
486 //Set titles
487 $titles = explode("\r", $keywords['titles'][$k]);
488
489 //Iterate on each keyword
490 foreach(explode("\r", $keywords['ids'][$k]) as $k => $kid) {
491 //Add keyword
492 $return['articles'][$id]['keywords'][$kid] = [
493 #'slug' => $slugs[$k],
494 'title' => $titles[$k],
495 'link' => $this->router->generate('rapsysblog_keyword_view', ['id' => $kid, 'slug' => $slugs[$k]]),
496 ];
497 }
498 }
499 }
500 }
501 }
502
503 //Return return
504 return $return;
505 }
506
507 /**
508 * {@inheritdoc}
509 */
510 public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $hash): void {
511 //Set new hashed password
512 $user->setPassword($hash);
513
514 //Flush data to database
515 $this->getEntityManager()->flush();
516 }
517 }