1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys AirBundle 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\AirBundle\Repository
;
14 use Doctrine\ORM\AbstractQuery
;
15 use Doctrine\ORM\Query\ResultSetMapping
;
17 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
;
19 use Rapsys\AirBundle\Repository
;
24 class UserRepository
extends Repository
{
26 * Find users with translated highest group and civility
28 * @return array The user ids keyed by group and pseudonym
30 public function findChoicesAsArray(): array {
44 FROM RapsysAirBundle:User AS u
45 JOIN RapsysAirBundle:UserGroup AS gu ON (gu.user_id = u.id)
46 JOIN RapsysAirBundle:Group AS g ON (g.id = gu.group_id)
47 WHERE g.title <> 'User'
48 ORDER BY g.id DESC, u.pseudonym ASC
55 //Replace bundle entity name by table name
56 $req = str_replace($this->tableKeys
, $this->tableValues
, $req);
58 //Get result set mapping instance
59 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
60 $rsm = new ResultSetMapping();
63 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
64 //XXX: we don't use a result set as we want to translate group and civility
65 $rsm->addScalarResult('id', 'id', 'integer')
66 ->addScalarResult('pseudonym', 'pseudonym', 'string')
67 ->addScalarResult('g_id', 'g_id', 'integer')
68 ->addScalarResult('g_title', 'g_title', 'string')
69 ->addIndexByScalar('id');
73 ->createNativeQuery($req, $rsm)
80 foreach($res as $data) {
81 //Without group or simple user
82 #XXX: moved in sql by removing LEFT JOIN and excluding user group
83 #if (empty($data['g_title']) || $data['g_title'] == 'User') {
88 //Get translated group
89 $group = $this->translator
->trans($data['g_title']);
92 if (!isset($ret[$group])) {
97 //XXX: ChoiceType use display string as key
98 $ret[$group][trim($data['pseudonym'].' ('.$data['id'].')')] = intval($data['id']);
106 * Find user ids by pseudonym
108 * @param array $pseudonym The pseudonym filter
109 * @return array The user ids
111 public function findIdByPseudonymAsArray(array $pseudonym): array {
119 FROM RapsysAirBundle:User AS u
120 LEFT JOIN RapsysAirBundle:UserGroup AS gu ON (gu.user_id = u.id)
121 WHERE u.pseudonym IN (:pseudonym)
122 ORDER BY gu.group_id DESC, u.pseudonym ASC
129 //Replace bundle entity name by table name
130 $req = str_replace($this->tableKeys
, $this->tableValues
, $req);
132 //Get result set mapping instance
133 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
134 $rsm = new ResultSetMapping();
137 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
138 //XXX: we don't use a result set as we want to translate group and civility
139 $rsm->addScalarResult('id', 'id', 'integer');
143 ->createNativeQuery($req, $rsm)
144 ->setParameter('pseudonym', $pseudonym)
145 //XXX: instead of array_column on the result
146 ->getResult(AbstractQuery
::HYDRATE_SCALAR_COLUMN
);
150 * Find applicant by session id
152 * @param int $sessionId The Session id
153 * @return array The pseudonym array keyed by id
155 public function findBySessionId(int $sessionId): array {
158 SELECT u.id, u.pseudonym
159 FROM RapsysAirBundle:Application AS a
160 JOIN RapsysAirBundle:User AS u ON (u.id = a.user_id)
161 WHERE a.session_id = :id
164 //Replace bundle entity name by table name
165 $req = str_replace($this->tableKeys
, $this->tableValues
, $req);
167 //Get result set mapping instance
168 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
169 $rsm = new ResultSetMapping();
172 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
173 //XXX: we don't use a result set as we want to translate group and civility
174 $rsm->addScalarResult('id', 'id', 'integer')
175 ->addIndexByScalar('pseudonym');
179 ->createNativeQuery($req, $rsm)
180 ->setParameter('id', $sessionId)
186 //Iterate on each result
187 foreach($result as $id => $data) {
189 $return[$id] = $data['id'];
197 * Find user as array by id
199 * @param int $id The location id
200 * @param string $locale The locale
201 * @return array The location data
203 public function findOneByIdAsArray(int $id, string $locale): ?array {
205 //TODO: zipcode/city/country (on pourra matcher les locations avec ça ?)
217 u.civility_id AS c_id,
219 u.country_id AS o_id,
221 GROUP_CONCAT(g.id ORDER BY g.id SEPARATOR "\\n") AS ids,
222 GROUP_CONCAT(g.title ORDER BY g.id SEPARATOR "\\n") AS titles,
223 GREATEST(COALESCE(u.updated, 0), COALESCE(c.updated, 0), COALESCE(o.updated, 0)) AS modified
224 FROM RapsysAirBundle:User AS u
225 LEFT JOIN RapsysAirBundle:Civility AS c ON (c.id = u.civility_id)
226 LEFT JOIN RapsysAirBundle:Country AS o ON (o.id = u.country_id)
227 LEFT JOIN RapsysAirBundle:UserGroup AS gu ON (gu.user_id = u.id)
228 LEFT JOIN RapsysAirBundle:Group AS g ON (g.id = gu.group_id)
232 //Replace bundle entity name by table name
233 $req = str_replace($this->tableKeys
, $this->tableValues
, $req);
235 //Get result set mapping instance
236 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
237 $rsm = new ResultSetMapping();
240 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
241 //addScalarResult($sqlColName, $resColName, $type = 'string');
242 $rsm->addScalarResult('id', 'id', 'integer')
243 ->addScalarResult('city', 'city', 'string')
244 ->addScalarResult('forename', 'forename', 'string')
245 ->addScalarResult('mail', 'mail', 'string')
246 ->addScalarResult('phone', 'phone', 'string')
247 ->addScalarResult('pseudonym', 'pseudonym', 'string')
248 ->addScalarResult('surname', 'surname', 'string')
249 ->addScalarResult('updated', 'updated', 'datetime')
250 ->addScalarResult('zipcode', 'zipcode', 'string')
251 ->addScalarResult('c_id', 'c_id', 'integer')
252 ->addScalarResult('c_title', 'c_title', 'string')
253 ->addScalarResult('o_id', 'o_id', 'integer')
254 ->addScalarResult('o_title', 'o_title', 'string')
255 //XXX: is a string because of \n separator
256 ->addScalarResult('ids', 'ids', 'string')
257 //XXX: is a string because of \n separator
258 ->addScalarResult('titles', 'titles', 'string')
259 ->addScalarResult('modified', 'modified', 'datetime')
260 ->addIndexByScalar('id');
264 ->createNativeQuery($req, $rsm)
265 ->setParameter('id', $id)
266 ->getOneOrNullResult();
269 if ($result === null) {
275 $result['alternates'] = [];
278 $route = 'rapsys_air_user_view';
281 $routeParams = ['id' => $id, 'user' => $this->slugger
->slug($result['pseudonym'])];
283 //Milonga Raphaël exception
284 if ($routeParams['id'] == 1 && $routeParams['user'] == 'milonga-raphael') {
286 $route = 'rapsys_air_user_milongaraphael';
291 //Iterate on each languages
292 foreach($this->languages
as $languageId => $language) {
293 //Without current locale
294 if ($languageId !== $locale) {
298 //Set route params locale
299 $routeParams['_locale'] = $languageId;
301 //Iterate on each locales
302 foreach(array_keys($this->languages
) as $other) {
303 //Without other locale
304 if ($other !== $languageId) {
305 //Set other locale title
306 $titles[$other] = $this->translator
->trans($language, [], null, $other);
310 //Add alternates locale
311 $result['alternates'][substr($languageId, 0, 2)] = $result['alternates'][str_replace('_', '-', $languageId)] = [
312 'absolute' => $this->router
->generate($route, $routeParams, UrlGeneratorInterface
::ABSOLUTE_URL
),
313 'relative' => $this->router
->generate($route, $routeParams),
314 'title' => implode('/', $titles),
315 'translated' => $this->translator
->trans($language, [], null, $languageId)
321 $titles = explode("\n", $result['titles']);
323 //Set groups and roles
324 $groups = $roles = [];
326 //Iterate on each location
327 foreach(explode("\n", $result['ids']) as $k => $id) {
329 $roles[$role = 'ROLE_'.strtoupper($titles[$k])] = $role;
332 $groups[$id] = $this->translator
->trans($titles[$k]);
337 'id' => $result['id'],
338 'mail' => $result['mail'],
339 'pseudonym' => $result['pseudonym'],
340 'forename' => $result['forename'],
341 'surname' => $result['surname'],
342 'phone' => $result['phone'],
343 'zipcode' => $result['zipcode'],
344 'city' => $result['city'],
346 'id' => $result['c_id'],
347 'title' => $this->translator
->trans($result['c_title'])
350 'id' => $result['o_id'],
351 //XXX: without country, o_title is empty
352 'title' => $this->translator
->trans($result['o_title'])
354 'updated' => $result['updated'],
357 'modified' => $result['modified'],
358 'multimap' => $this->translator
->trans('%pseudonym% sector map', ['%pseudonym%' => $result['pseudonym']]),
359 'slug' => $this->slugger
->slug($result['pseudonym']),
360 'link' => $this->router
->generate($route, ['_locale' => $locale]+
$routeParams),
361 'alternates' => $result['alternates']
366 * Find all users grouped by translated group
368 * @return array The user mail and pseudonym keyed by group and id
370 public function findIndexByGroupId(): array {
381 GROUP_CONCAT(t.d_id ORDER BY t.d_id SEPARATOR "\\n") AS d_ids,
382 GROUP_CONCAT(t.d_name ORDER BY t.d_id SEPARATOR "\\n") AS d_names,
383 GROUP_CONCAT(t.d_type ORDER BY t.d_id SEPARATOR "\\n") AS d_types
405 FROM RapsysAirBundle:User AS u
406 JOIN RapsysAirBundle:UserGroup AS gu ON (gu.user_id = u.id)
407 JOIN RapsysAirBundle:Group AS g ON (g.id = gu.group_id)
411 LEFT JOIN RapsysAirBundle:Application AS a ON (a.user_id = c.id)
412 LEFT JOIN RapsysAirBundle:Dance AS d ON (d.id = a.dance_id)
417 GROUP BY t.g_id, t.id
418 ORDER BY t.g_id DESC, t.id ASC
421 //Replace bundle entity name by table name
422 $req = str_replace($this->tableKeys
, $this->tableValues
, $req);
424 //Get result set mapping instance
425 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
426 $rsm = new ResultSetMapping();
429 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
430 //addScalarResult($sqlColName, $resColName, $type = 'string');
431 $rsm->addScalarResult('id', 'id', 'integer')
432 ->addScalarResult('mail', 'mail', 'string')
433 ->addScalarResult('forename', 'forename', 'string')
434 ->addScalarResult('surname', 'surname', 'string')
435 ->addScalarResult('pseudonym', 'pseudonym', 'string')
436 ->addScalarResult('g_id', 'g_id', 'integer')
437 ->addScalarResult('g_title', 'g_title', 'string')
438 //XXX: is a string because of \n separator
439 ->addScalarResult('d_ids', 'd_ids', 'string')
440 //XXX: is a string because of \n separator
441 ->addScalarResult('d_names', 'd_names', 'string')
442 //XXX: is a string because of \n separator
443 ->addScalarResult('d_types', 'd_types', 'string');
447 ->createNativeQuery($req, $rsm)
454 foreach($res as $data) {
455 //Get translated group
456 $group = $this->translator
->trans($data['g_title']);
458 //Init group subarray
459 if (!isset($ret[$group])) {
467 $ret[$group][$data['id']] = [
468 'mail' => $data['mail'],
469 'forename' => $data['forename'],
470 'surname' => $data['surname'],
471 'pseudonym' => $data['pseudonym'],
473 'slug' => $slug = $this->slugger
->slug($data['pseudonym']),
474 //Milonga Raphaël exception
475 'link' => $data['id'] == 1 && $slug == 'milonga-raphael' ? $this->router
->generate('rapsys_air_user_milongaraphael', []) : $this->router
->generate('rapsys_air_user_view', ['id' => $data['id'], 'user' => $slug]),
476 'edit' => $this->router
->generate('rapsys_user_edit', ['mail' => $short = $this->slugger
->short($data['mail']), 'hash' => $this->slugger
->hash($short)])
480 if (!empty($data['d_ids'])) {
482 $names = explode("\n", $data['d_names']);
485 $types = explode("\n", $data['d_types']);
487 //Iterate on each dance
488 foreach(explode("\n", $data['d_ids']) as $k => $id) {
489 //Init dance when missing
490 if (!isset($ret[$group][$data['id']]['dances'][$name = $this->translator
->trans($names[$k])])) {
491 $ret[$group][$data['id']]['dances'][$name] = [
492 'link' => $this->router
->generate('rapsys_air_dance_name', ['name' => $this->slugger
->short($names[$k]), 'dance' => $this->slugger
->slug($name)]),
498 $ret[$group][$data['id']]['dances'][$name]['types'][$type = $this->translator
->trans($types[$k])] = $this->router
->generate('rapsys_air_dance_view', ['id' => $id, 'name' => $this->slugger
->slug($name), 'type' => $this->slugger
->slug($type)]);