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\Query\ResultSetMapping
; 
  15 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
; 
  20 class UserRepository 
extends EntityRepository 
{ 
  22          * Find users with translated highest group and civility 
  24          * @return array The user ids keyed by group and pseudonym 
  26         public function findIndexByGroupPseudonym(): array { 
  40         FROM RapsysAirBundle:User AS u 
  41         LEFT JOIN RapsysAirBundle:UserGroup AS gu ON (gu.user_id = u.id) 
  42         LEFT JOIN RapsysAirBundle:Group AS g ON (g.id = gu.group_id) 
  50                 //Replace bundle entity name by table name 
  51                 $req = str_replace($this->tableKeys
, $this->tableValues
, $req); 
  53                 //Get result set mapping instance 
  54                 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php 
  55                 $rsm = new ResultSetMapping(); 
  58                 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php 
  59                 //XXX: we don't use a result set as we want to translate group and civility 
  60                 $rsm->addScalarResult('id', 'id', 'integer') 
  61                         ->addScalarResult('pseudonym', 'pseudonym', 'string') 
  62                         ->addScalarResult('g_id', 'g_id', 'integer') 
  63                         ->addScalarResult('g_title', 'g_title', 'string') 
  64                         ->addIndexByScalar('id'); 
  68                         ->createNativeQuery($req, $rsm) 
  75                 foreach($res as $data) { 
  76                         //Without group or simple user 
  77                         if (empty($data['g_title']) || $data['g_title'] == 'User') { 
  82                         //Get translated group 
  83                         $group = $this->translator
->trans($data['g_title']); 
  86                         if (!isset($ret[$group])) { 
  91                         //XXX: ChoiceType use display string as key 
  92                         $ret[$group][$data['pseudonym']] = $data['id']; 
 100          * Find applicant by session id 
 102          * @param int $sessionId The Session id 
 103          * @return array The pseudonym array keyed by id 
 105         public function findBySessionId(int $sessionId): array { 
 108 SELECT u.id, u.pseudonym 
 109 FROM RapsysAirBundle:Application AS a 
 110 JOIN RapsysAirBundle:User AS u ON (u.id = a.user_id) 
 111 WHERE a.session_id = :id 
 114                 //Replace bundle entity name by table name 
 115                 $req = str_replace($this->tableKeys
, $this->tableValues
, $req); 
 117                 //Get result set mapping instance 
 118                 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php 
 119                 $rsm = new ResultSetMapping(); 
 122                 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php 
 123                 //XXX: we don't use a result set as we want to translate group and civility 
 124                 $rsm->addScalarResult('id', 'id', 'integer') 
 125                         ->addIndexByScalar('pseudonym'); 
 129                         ->createNativeQuery($req, $rsm) 
 130                         ->setParameter('id', $sessionId) 
 136                 //Iterate on each result 
 137                 foreach($result as $id => $data) { 
 139                         $return[$id] = $data['id']; 
 147          * Find user as array by id 
 149          * @param int $id The location id 
 150          * @param string $locale The locale 
 151          * @return array The location data 
 153         public function findOneByIdAsArray(int $id, string $locale): ?array { 
 155                 //TODO: zipcode/city/country (on pourra matcher les locations avec ça ?) 
 167         u.civility_id AS c_id, 
 169         u.country_id AS o_id, 
 171         GROUP_CONCAT(g.id ORDER BY g.id SEPARATOR "\\n") AS ids, 
 172         GROUP_CONCAT(g.title ORDER BY g.id SEPARATOR "\\n") AS titles, 
 173         GREATEST(COALESCE(u.updated, 0), COALESCE(c.updated, 0), COALESCE(o.updated, 0)) AS modified 
 174 FROM RapsysAirBundle:User AS u 
 175 LEFT JOIN RapsysAirBundle:Civility AS c ON (c.id = u.civility_id) 
 176 LEFT JOIN RapsysAirBundle:Country AS o ON (o.id = u.country_id) 
 177 LEFT JOIN RapsysAirBundle:UserGroup AS gu ON (gu.user_id = u.id) 
 178 LEFT JOIN RapsysAirBundle:Group AS g ON (g.id = gu.group_id) 
 182                 //Replace bundle entity name by table name 
 183                 $req = str_replace($this->tableKeys
, $this->tableValues
, $req); 
 185                 //Get result set mapping instance 
 186                 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php 
 187                 $rsm = new ResultSetMapping(); 
 190                 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php 
 191                 //addScalarResult($sqlColName, $resColName, $type = 'string'); 
 192                 $rsm->addScalarResult('id', 'id', 'integer') 
 193                         ->addScalarResult('city', 'city', 'string') 
 194                         ->addScalarResult('forename', 'forename', 'string') 
 195                         ->addScalarResult('mail', 'mail', 'string') 
 196                         ->addScalarResult('phone', 'phone', 'string') 
 197                         ->addScalarResult('pseudonym', 'pseudonym', 'string') 
 198                         ->addScalarResult('surname', 'surname', 'string') 
 199                         ->addScalarResult('updated', 'updated', 'datetime') 
 200                         ->addScalarResult('zipcode', 'zipcode', 'string') 
 201                         ->addScalarResult('c_id', 'c_id', 'integer') 
 202                         ->addScalarResult('c_title', 'c_title', 'string') 
 203                         ->addScalarResult('o_id', 'o_id', 'integer') 
 204                         ->addScalarResult('o_title', 'o_title', 'string') 
 205                         //XXX: is a string because of \n separator 
 206                         ->addScalarResult('ids', 'ids', 'string') 
 207                         //XXX: is a string because of \n separator 
 208                         ->addScalarResult('titles', 'titles', 'string') 
 209                         ->addScalarResult('modified', 'modified', 'datetime') 
 210                         ->addIndexByScalar('id'); 
 214                         ->createNativeQuery($req, $rsm) 
 215                         ->setParameter('id', $id) 
 216                         ->getOneOrNullResult(); 
 219                 if ($result === null) { 
 225                 $result['alternates'] = []; 
 228                 $route = 'rapsys_air_user_view'; 
 231                 $routeParams = ['id' => $id, 'user' => $this->slugger
->slug($result['pseudonym'])]; 
 233                 //Milonga Raphaël exception 
 234                 if ($routeParams['id'] == 1 && $routeParams['user'] == 'milonga-raphael') { 
 236                         $route = 'rapsys_air_user_milongaraphael'; 
 241                 //Iterate on each languages 
 242                 foreach($this->languages 
as $languageId => $language) { 
 243                         //Without current locale 
 244                         if ($languageId !== $locale) { 
 248                                 //Set route params locale 
 249                                 $routeParams['_locale'] = $languageId; 
 251                                 //Iterate on each locales 
 252                                 foreach(array_keys($this->languages
) as $other) { 
 253                                         //Without other locale 
 254                                         if ($other !== $languageId) { 
 255                                                 //Set other locale title 
 256                                                 $titles[$other] = $this->translator
->trans($language, [], null, $other); 
 260                                 //Add alternates locale 
 261                                 $result['alternates'][substr($languageId, 0, 2)] = $result['alternates'][str_replace('_', '-', $languageId)] = [ 
 262                                         'absolute' => $this->router
->generate($route, $routeParams, UrlGeneratorInterface
::ABSOLUTE_URL
), 
 263                                         'relative' => $this->router
->generate($route, $routeParams), 
 264                                         'title' => implode('/', $titles), 
 265                                         'translated' => $this->translator
->trans($language, [], null, $languageId) 
 271                 $titles = explode("\n", $result['titles']); 
 273                 //Set groups and roles 
 274                 $groups = $roles = []; 
 276                 //Iterate on each location 
 277                 foreach(explode("\n", $result['ids']) as $k => $id) { 
 279                         $roles[$role = 'ROLE_'.strtoupper($titles[$k])] = $role; 
 282                         $groups[$id] = $this->translator
->trans($titles[$k]); 
 287                         'id' => $result['id'], 
 288                         'mail' => $result['mail'], 
 289                         'pseudonym' => $result['pseudonym'], 
 290                         'forename' => $result['forename'], 
 291                         'surname' => $result['surname'], 
 292                         'phone' => $result['phone'], 
 293                         'zipcode' => $result['zipcode'], 
 294                         'city' => $result['city'], 
 296                                 'id' => $result['c_id'], 
 297                                 'title' => $this->translator
->trans($result['c_title']) 
 300                                 'id' => $result['o_id'], 
 301                                 //XXX: without country, o_title is empty 
 302                                 'title' => $this->translator
->trans($result['o_title']) 
 304                         'updated' => $result['updated'], 
 307                         'modified' => $result['modified'], 
 308                         'multimap' => $this->translator
->trans('%pseudonym% sector map', ['%pseudonym%' => $result['pseudonym']]), 
 309                         'slug' => $this->slugger
->slug($result['pseudonym']), 
 310                         'link' => $this->router
->generate($route, ['_locale' => $locale]+
$routeParams), 
 311                         'alternates' => $result['alternates'] 
 316          * Find all users grouped by translated group 
 318          * @return array The user mail and pseudonym keyed by group and id 
 320         public function findIndexByGroupId(): array { 
 331         GROUP_CONCAT(t.d_id ORDER BY t.d_id SEPARATOR "\\n") AS d_ids, 
 332         GROUP_CONCAT(t.d_name ORDER BY t.d_id SEPARATOR "\\n") AS d_names, 
 333         GROUP_CONCAT(t.d_type ORDER BY t.d_id SEPARATOR "\\n") AS d_types 
 355                 FROM RapsysAirBundle:User AS u 
 356                 JOIN RapsysAirBundle:UserGroup AS gu ON (gu.user_id = u.id) 
 357                 JOIN RapsysAirBundle:Group AS g ON (g.id = gu.group_id) 
 361         LEFT JOIN RapsysAirBundle:Application AS a ON (a.user_id = c.id) 
 362         LEFT JOIN RapsysAirBundle:Dance AS d ON (d.id = a.dance_id) 
 367 GROUP BY t.g_id, t.id 
 368 ORDER BY t.g_id DESC, t.id ASC 
 371                 //Replace bundle entity name by table name 
 372                 $req = str_replace($this->tableKeys
, $this->tableValues
, $req); 
 374                 //Get result set mapping instance 
 375                 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php 
 376                 $rsm = new ResultSetMapping(); 
 379                 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php 
 380                 //addScalarResult($sqlColName, $resColName, $type = 'string'); 
 381                 $rsm->addScalarResult('id', 'id', 'integer') 
 382                         ->addScalarResult('mail', 'mail', 'string') 
 383                         ->addScalarResult('forename', 'forename', 'string') 
 384                         ->addScalarResult('surname', 'surname', 'string') 
 385                         ->addScalarResult('pseudonym', 'pseudonym', 'string') 
 386                         ->addScalarResult('g_id', 'g_id', 'integer') 
 387                         ->addScalarResult('g_title', 'g_title', 'string') 
 388                         //XXX: is a string because of \n separator 
 389                         ->addScalarResult('d_ids', 'd_ids', 'string') 
 390                         //XXX: is a string because of \n separator 
 391                         ->addScalarResult('d_names', 'd_names', 'string') 
 392                         //XXX: is a string because of \n separator 
 393                         ->addScalarResult('d_types', 'd_types', 'string'); 
 397                         ->createNativeQuery($req, $rsm) 
 404                 foreach($res as $data) { 
 405                         //Get translated group 
 406                         $group = $this->translator
->trans($data['g_title']); 
 408                         //Init group subarray 
 409                         if (!isset($ret[$group])) { 
 417                         $ret[$group][$data['id']] = [ 
 418                                 'mail' => $data['mail'], 
 419                                 'forename' => $data['forename'], 
 420                                 'surname' => $data['surname'], 
 421                                 'pseudonym' => $data['pseudonym'], 
 423                                 'slug' => $slug = $this->slugger
->slug($data['pseudonym']), 
 424                                 //Milonga Raphaël exception 
 425                                 '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]), 
 426                                 'edit' => $this->router
->generate('rapsys_user_edit', ['mail' => $short = $this->slugger
->short($data['mail']), 'hash' => $this->slugger
->hash($short)]) 
 430                         if (!empty($data['d_ids'])) { 
 432                                 $names = explode("\n", $data['d_names']); 
 435                                 $types = explode("\n", $data['d_types']); 
 437                                 //Iterate on each dance 
 438                                 foreach(explode("\n", $data['d_ids']) as $k => $id) { 
 439                                         //Init dance when missing 
 440                                         if (!isset($ret[$group][$data['id']]['dances'][$name = $this->translator
->trans($names[$k])])) { 
 441                                                 $ret[$group][$data['id']]['dances'][$name] = [ 
 442                                                         'link' => $this->router
->generate('rapsys_air_dance_name', ['name' => $this->slugger
->short($names[$k]), 'dance' => $this->slugger
->slug($name)]), 
 448                                         $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)]);