X-Git-Url: https://git.rapsys.eu/.gitweb.cgi/airbundle/blobdiff_plain/401c43f206e2952e9608ce83c2b8e897b438de6f..932f56b35e3ba155db515d27068344ae73f254fe:/Repository/UserRepository.php?ds=inline diff --git a/Repository/UserRepository.php b/Repository/UserRepository.php index e7ff076..9e869e1 100644 --- a/Repository/UserRepository.php +++ b/Repository/UserRepository.php @@ -1,81 +1,70 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ namespace Rapsys\AirBundle\Repository; -use Symfony\Component\Translation\TranslatorInterface; use Doctrine\ORM\Query\ResultSetMapping; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; /** * UserRepository */ -class UserRepository extends \Doctrine\ORM\EntityRepository { +class UserRepository extends EntityRepository { /** * Find users with translated highest group and civility * - * @param $translator The TranslatorInterface instance + * @return array The user ids keyed by group and pseudonym */ - public function findAllWithTranslatedGroupAndCivility(TranslatorInterface $translator) { - //Get entity manager - $em = $this->getEntityManager(); - - //Get quote strategy - $qs = $em->getConfiguration()->getQuoteStrategy(); - $dp = $em->getConnection()->getDatabasePlatform(); - - //Get quoted table names - //XXX: this allow to make this code table name independent - $tables = [ - 'RapsysAirBundle:UserGroup' => $qs->getJoinTableName($em->getClassMetadata('RapsysAirBundle:User')->getAssociationMapping('groups'), $em->getClassMetadata('RapsysAirBundle:User'), $dp), - 'RapsysAirBundle:Group' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Group'), $dp), - 'RapsysAirBundle:Civility' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Civility'), $dp), - 'RapsysAirBundle:User' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:User'), $dp) - ]; - + public function findIndexByGroupPseudonym(): array { //Set the request - $req = 'SELECT a.id, a.forename, a.surname, a.t_id, a.t_short, a.t_title, a.g_id, a.g_title FROM ( - SELECT u.id, u.forename, u.surname, t.id AS t_id, t.short AS t_short, t.title AS t_title, g.id AS g_id, g.title AS g_title - FROM RapsysAirBundle:User AS u - JOIN RapsysAirBundle:Civility AS t ON (t.id = u.civility_id) - LEFT JOIN RapsysAirBundle:UserGroup AS gu ON (gu.user_id = u.id) - LEFT JOIN RapsysAirBundle:Group AS g ON (g.id = gu.group_id) - ORDER BY g.id DESC, NULL LIMIT '.PHP_INT_MAX.' - ) AS a GROUP BY a.id ORDER BY NULL'; + $req =<<tableKeys, $this->tableValues, $req); //Get result set mapping instance //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php $rsm = new ResultSetMapping(); - /*XXX: we don't want a result set for our request - $rsm->addEntityResult('RapsysAirBundle:User', 'u'); - $rsm->addFieldResult('u', 'id', 'id'); - $rsm->addFieldResult('u', 'forename', 'forename'); - $rsm->addFieldResult('u', 'surname', 'surname'); - $rsm->addFieldResult('u', 't_id', 'title_id'); - $rsm->addJoinedEntityResult('RapsysAirBundle:Title', 't', 'u', 'title'); - $rsm->addFieldResult('t', 't_id', 'id'); - $rsm->addFieldResult('t', 't_title', 'title'); - $rsm->addJoinedEntityResult('RapsysAirBundle:Group', 'g', 'u', 'groups'); - $rsm->addFieldResult('g', 'g_id', 'id'); - $rsm->addFieldResult('g', 'g_title', 'title');*/ - //Declare all fields //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php - //addScalarResult($sqlColName, $resColName, $type = 'string'); + //XXX: we don't use a result set as we want to translate group and civility $rsm->addScalarResult('id', 'id', 'integer') - ->addScalarResult('forename', 'forename', 'string') - ->addScalarResult('surname', 'surname', 'string') - ->addScalarResult('t_id', 't_id', 'integer') - ->addScalarResult('t_short', 't_short', 'string') - ->addScalarResult('t_title', 't_title', 'string') + ->addScalarResult('pseudonym', 'pseudonym', 'string') ->addScalarResult('g_id', 'g_id', 'integer') ->addScalarResult('g_title', 'g_title', 'string') ->addIndexByScalar('id'); //Fetch result - $res = $em + $res = $this->_em ->createNativeQuery($req, $rsm) ->getResult(); @@ -84,17 +73,23 @@ class UserRepository extends \Doctrine\ORM\EntityRepository { //Process result foreach($res as $data) { + //Without group or simple user + if (empty($data['g_title']) || $data['g_title'] == 'User') { + //Skip it + continue; + } + //Get translated group - $group = $translator->trans($data['g_title']?:'User'); - //Get translated title - $title = $translator->trans($data['t_short']); + $group = $this->translator->trans($data['g_title']); + //Init group subarray if (!isset($ret[$group])) { $ret[$group] = []; } + //Set data //XXX: ChoiceType use display string as key - $ret[$group][$title.' '.$data['forename'].' '.$data['surname']] = $data['id']; + $ret[$group][$data['pseudonym']] = $data['id']; } //Send result @@ -102,70 +97,279 @@ class UserRepository extends \Doctrine\ORM\EntityRepository { } /** - * Find all applicant by session + * Find applicant by session id * - * @param $session The Session + * @param int $sessionId The Session id + * @return array The pseudonym array keyed by id */ - public function findAllApplicantBySession($session) { - //Get entity manager - $em = $this->getEntityManager(); - - //Fetch sessions - $ret = $this->getEntityManager() - ->createQuery('SELECT u.id, u.pseudonym FROM RapsysAirBundle:Application a JOIN RapsysAirBundle:User u WITH u.id = a.user WHERE a.session = :session') - ->setParameter('session', $session) - ->getResult(); + public function findBySessionId(int $sessionId): array { + //Set the request + $req =<<tableKeys, $this->tableValues, $req); - //Send result - return $ret; + //Get result set mapping instance + //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php + $rsm = new ResultSetMapping(); + + //Declare all fields + //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php + //XXX: we don't use a result set as we want to translate group and civility + $rsm->addScalarResult('id', 'id', 'integer') + ->addIndexByScalar('pseudonym'); + + //Get result + $result = $this->_em + ->createNativeQuery($req, $rsm) + ->setParameter('id', $sessionId) + ->getArrayResult(); + + //Set return + $return = []; + + //Iterate on each result + foreach($result as $id => $data) { + //Add to return + $return[$id] = $data['id']; + } + + //Return return + return $return; } /** - * Find all users grouped by translated group + * Find user as array by id * - * @param $translator The TranslatorInterface instance - * @return array|null The user array or null + * @param int $id The location id + * @param string $locale The locale + * @return array The location data */ - public function findUserGroupedByTranslatedGroup(TranslatorInterface $translator) { - //Get entity manager - $em = $this->getEntityManager(); - - //Get quote strategy - $qs = $em->getConfiguration()->getQuoteStrategy(); - $dp = $em->getConnection()->getDatabasePlatform(); - - //Get quoted table names - //XXX: this allow to make this code table name independent - $tables = [ - 'RapsysAirBundle:UserGroup' => $qs->getJoinTableName($em->getClassMetadata('RapsysAirBundle:User')->getAssociationMapping('groups'), $em->getClassMetadata('RapsysAirBundle:User'), $dp), - 'RapsysAirBundle:Group' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Group'), $dp), - 'RapsysAirBundle:User' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:User'), $dp), - //XXX: Set limit used to workaround mariadb subselect optimization - ':limit' => PHP_INT_MAX, - "\t" => '', - "\n" => ' ' + public function findOneByIdAsArray(int $id, string $locale): ?array { + //Set the request + //TODO: zipcode/city/country (on pourra matcher les locations avec ça ?) + $req =<<tableKeys, $this->tableValues, $req); + + //Get result set mapping instance + //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php + $rsm = new ResultSetMapping(); + + //Declare all fields + //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php + //addScalarResult($sqlColName, $resColName, $type = 'string'); + $rsm->addScalarResult('id', 'id', 'integer') + ->addScalarResult('city', 'city', 'string') + ->addScalarResult('forename', 'forename', 'string') + ->addScalarResult('mail', 'mail', 'string') + ->addScalarResult('phone', 'phone', 'string') + ->addScalarResult('pseudonym', 'pseudonym', 'string') + ->addScalarResult('surname', 'surname', 'string') + ->addScalarResult('updated', 'updated', 'datetime') + ->addScalarResult('zipcode', 'zipcode', 'string') + ->addScalarResult('c_id', 'c_id', 'integer') + ->addScalarResult('c_title', 'c_title', 'string') + ->addScalarResult('o_id', 'o_id', 'integer') + ->addScalarResult('o_title', 'o_title', 'string') + //XXX: is a string because of \n separator + ->addScalarResult('ids', 'ids', 'string') + //XXX: is a string because of \n separator + ->addScalarResult('titles', 'titles', 'string') + ->addScalarResult('modified', 'modified', 'datetime') + ->addIndexByScalar('id'); + + //Get result + $result = $this->_em + ->createNativeQuery($req, $rsm) + ->setParameter('id', $id) + ->getOneOrNullResult(); + + //Without result + if ($result === null) { + //Return result + return $result; + } + + //Set alternates + $result['alternates'] = []; + + //Set route + $route = 'rapsys_air_user_view'; + + //Set route params + $routeParams = ['id' => $id, 'user' => $this->slugger->slug($result['pseudonym'])]; + + //Milonga Raphaël exception + if ($routeParams['id'] == 1 && $routeParams['user'] == 'milonga-raphael') { + //Set route + $route = 'rapsys_air_user_milongaraphael'; + //Set route params + $routeParams = []; + } + + //Iterate on each languages + foreach($this->languages as $languageId => $language) { + //Without current locale + if ($languageId !== $locale) { + //Set titles + $titles = []; + + //Set route params locale + $routeParams['_locale'] = $languageId; + + //Iterate on each locales + foreach(array_keys($this->languages) as $other) { + //Without other locale + if ($other !== $languageId) { + //Set other locale title + $titles[$other] = $this->translator->trans($language, [], null, $other); + } + } + + //Add alternates locale + $result['alternates'][substr($languageId, 0, 2)] = $result['alternates'][str_replace('_', '-', $languageId)] = [ + 'absolute' => $this->router->generate($route, $routeParams, UrlGeneratorInterface::ABSOLUTE_URL), + 'relative' => $this->router->generate($route, $routeParams), + 'title' => implode('/', $titles), + 'translated' => $this->translator->trans($language, [], null, $languageId) + ]; + } + } + + //Set titles + $titles = explode("\n", $result['titles']); + + //Set groups and roles + $groups = $roles = []; + + //Iterate on each location + foreach(explode("\n", $result['ids']) as $k => $id) { + //Add role + $roles[$role = 'ROLE_'.strtoupper($titles[$k])] = $role; + + //Add group + $groups[$id] = $this->translator->trans($titles[$k]); + } + + //Return result + return [ + 'id' => $result['id'], + 'mail' => $result['mail'], + 'pseudonym' => $result['pseudonym'], + 'forename' => $result['forename'], + 'surname' => $result['surname'], + 'phone' => $result['phone'], + 'zipcode' => $result['zipcode'], + 'city' => $result['city'], + 'civility' => [ + 'id' => $result['c_id'], + 'title' => $this->translator->trans($result['c_title']) + ], + 'country' => [ + 'id' => $result['o_id'], + //XXX: without country, o_title is empty + 'title' => $this->translator->trans($result['o_title']) + ], + 'updated' => $result['updated'], + 'roles' => $roles, + 'groups' => $groups, + 'modified' => $result['modified'], + 'multimap' => $this->translator->trans('%pseudonym% sector map', ['%pseudonym%' => $result['pseudonym']]), + 'slug' => $this->slugger->slug($result['pseudonym']), + 'link' => $this->router->generate($route, ['_locale' => $locale]+$routeParams), + 'alternates' => $result['alternates'] ]; + } + /** + * Find all users grouped by translated group + * + * @return array The user mail and pseudonym keyed by group and id + */ + public function findIndexByGroupId(): array { //Set the request $req = <<tableKeys, $this->tableValues, $req); //Get result set mapping instance //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php @@ -175,13 +379,21 @@ SQL; //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php //addScalarResult($sqlColName, $resColName, $type = 'string'); $rsm->addScalarResult('id', 'id', 'integer') + ->addScalarResult('mail', 'mail', 'string') + ->addScalarResult('forename', 'forename', 'string') + ->addScalarResult('surname', 'surname', 'string') ->addScalarResult('pseudonym', 'pseudonym', 'string') ->addScalarResult('g_id', 'g_id', 'integer') ->addScalarResult('g_title', 'g_title', 'string') - ->addIndexByScalar('id'); + //XXX: is a string because of \n separator + ->addScalarResult('d_ids', 'd_ids', 'string') + //XXX: is a string because of \n separator + ->addScalarResult('d_names', 'd_names', 'string') + //XXX: is a string because of \n separator + ->addScalarResult('d_types', 'd_types', 'string'); //Fetch result - $res = $em + $res = $this->_em ->createNativeQuery($req, $rsm) ->getResult(); @@ -191,15 +403,51 @@ SQL; //Process result foreach($res as $data) { //Get translated group - $group = $translator->trans($data['g_title']?:'User'); + $group = $this->translator->trans($data['g_title']); //Init group subarray if (!isset($ret[$group])) { $ret[$group] = []; } + //Set dances + $dances = []; + //Set data - $ret[$group][$data['id']] = $data['pseudonym']; + $ret[$group][$data['id']] = [ + 'mail' => $data['mail'], + 'forename' => $data['forename'], + 'surname' => $data['surname'], + 'pseudonym' => $data['pseudonym'], + 'dances' => [], + 'slug' => $slug = $this->slugger->slug($data['pseudonym']), + //Milonga Raphaël exception + '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]), + 'edit' => $this->router->generate('rapsys_user_edit', ['mail' => $short = $this->slugger->short($data['mail']), 'hash' => $this->slugger->hash($short)]) + ]; + + //With dances + if (!empty($data['d_ids'])) { + //Set names + $names = explode("\n", $data['d_names']); + + //Set types + $types = explode("\n", $data['d_types']); + + //Iterate on each dance + foreach(explode("\n", $data['d_ids']) as $k => $id) { + //Init dance when missing + if (!isset($ret[$group][$data['id']]['dances'][$name = $this->translator->trans($names[$k])])) { + $ret[$group][$data['id']]['dances'][$name] = [ + 'link' => $this->router->generate('rapsys_air_dance_name', ['name' => $this->slugger->short($names[$k]), 'dance' => $this->slugger->slug($name)]), + 'types' => [] + ]; + } + + //Set type + $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)]); + } + } } //Send result