]> Raphaƫl G. Git Repositories - airbundle/blobdiff - Repository/UserRepository.php
Add hat infos
[airbundle] / Repository / UserRepository.php
index 8991bf4a29cea5b629d6907f62433b19a20d06c0..2d88e3d54bb50af04fb0b673cf5acb45a287ef26 100644 (file)
@@ -10,11 +10,11 @@ use Doctrine\ORM\Query\ResultSetMapping;
  */
 class UserRepository extends \Doctrine\ORM\EntityRepository {
        /**
-        * Find users with translated highest group and title
+        * Find users with translated highest group and civility
         *
         * @param $translator The TranslatorInterface instance
         */
-       public function findAllWithTranslatedGroupAndTitle(TranslatorInterface $translator) {
+       public function findAllWithTranslatedGroupAndCivility(TranslatorInterface $translator) {
                //Get entity manager
                $em = $this->getEntityManager();
 
@@ -25,18 +25,16 @@ class UserRepository extends \Doctrine\ORM\EntityRepository {
                //Get quoted table names
                //XXX: this allow to make this code table name independent
                $tables = [
-                       'RapsysAirBundle:GroupUser' => $qs->getJoinTableName($em->getClassMetadata('RapsysAirBundle:User')->getAssociationMapping('groups'), $em->getClassMetadata('RapsysAirBundle:User'), $dp),
+                       '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:Title' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Title'), $dp),
                        'RapsysAirBundle:User' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:User'), $dp)
                ];
 
                //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
+               $req = 'SELECT a.id, a.pseudonym, a.g_id, a.g_title FROM (
+                       SELECT u.id, u.pseudonym, g.id AS g_id, g.title AS g_title
                        FROM RapsysAirBundle:User AS u
-                       JOIN RapsysAirBundle:Title AS t ON (t.id = u.title_id)
-                       LEFT JOIN RapsysAirBundle:GroupUser AS gu ON (gu.user_id = u.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';
@@ -48,28 +46,11 @@ class UserRepository extends \Doctrine\ORM\EntityRepository {
                //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');
@@ -84,17 +65,20 @@ 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 = $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
@@ -124,12 +108,12 @@ class UserRepository extends \Doctrine\ORM\EntityRepository {
        }
 
        /**
-        * Find all organizer grouped by translated group
+        * Find all users grouped by translated group
         *
         * @param $translator The TranslatorInterface instance
-        * @return array|null The organizer array or null
+        * @return array|null The user array or null
         */
-       public function findOrganizerGroupedByGroup(TranslatorInterface $translator) {
+       public function findUserGroupedByTranslatedGroup(TranslatorInterface $translator) {
                //Get entity manager
                $em = $this->getEntityManager();
 
@@ -140,7 +124,7 @@ class UserRepository extends \Doctrine\ORM\EntityRepository {
                //Get quoted table names
                //XXX: this allow to make this code table name independent
                $tables = [
-                       'RapsysAirBundle:GroupUser' => $qs->getJoinTableName($em->getClassMetadata('RapsysAirBundle:User')->getAssociationMapping('groups'), $em->getClassMetadata('RapsysAirBundle:User'), $dp),
+                       '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
@@ -151,17 +135,11 @@ class UserRepository extends \Doctrine\ORM\EntityRepository {
 
                //Set the request
                $req = <<<SQL
-SELECT a.id, a.pseudonym, a.g_id, a.g_title
-FROM (
-       SELECT u.id, u.pseudonym, g.id AS g_id, g.title AS g_title
-       FROM RapsysAirBundle:User AS u
-       JOIN RapsysAirBundle:GroupUser AS gu ON (gu.user_id = u.id)
-       JOIN RapsysAirBundle:Group AS g ON (g.id = gu.group_id)
-       ORDER BY g.id DESC
-       LIMIT 0, :limit
-) AS a
-GROUP BY a.id
-ORDER BY a.id ASC
+SELECT u.id, u.mail, u.pseudonym, g.id AS g_id, g.title AS g_title
+FROM RapsysAirBundle:User AS u
+JOIN RapsysAirBundle:UserGroup AS gu ON (gu.user_id = u.id)
+JOIN RapsysAirBundle:Group AS g ON (g.id = gu.group_id)
+ORDER BY g.id DESC, u.id ASC
 SQL;
 
                //Replace bundle entity name by table name
@@ -175,10 +153,10 @@ 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('pseudonym', 'pseudonym', 'string')
                        ->addScalarResult('g_id', 'g_id', 'integer')
-                       ->addScalarResult('g_title', 'g_title', 'string')
-                       ->addIndexByScalar('id');
+                       ->addScalarResult('g_title', 'g_title', 'string');
 
                //Fetch result
                $res = $em
@@ -191,7 +169,7 @@ SQL;
                //Process result
                foreach($res as $data) {
                        //Get translated group
-                       $group = $translator->trans($data['g_title']?:'User');
+                       $group = $translator->trans($data['g_title']);
 
                        //Init group subarray
                        if (!isset($ret[$group])) {
@@ -199,32 +177,13 @@ SQL;
                        }
 
                        //Set data
-                       $ret[$group][$data['id']] = $data['pseudonym'];
+                       $ret[$group][$data['id']] = [
+                               'mail' => $data['mail'],
+                               'pseudonym' => $data['pseudonym']
+                       ];
                }
 
                //Send result
                return $ret;
-
-               header('Content-Type: text/plain');
-               var_dump($ret);
-               exit;
-
-               //Get entity manager
-               $em = $this->getEntityManager();
-
-               //Fetch sessions
-               $ret = $this->getEntityManager()
-                       ->createQuery('SELECT u.id, u.pseudonym, g.title FROM RapsysAirBundle:User u JOIN RapsysAirBundle:GroupUser gu WITH gu.user = u.id JOIN RapsysAirBundle:Group g WITH g.id = gu.group GROUP BY u.id ORDER BY g.id DESC')
-                       ->getResult();
-
-               header('Content-Type: text/plain');
-               var_dump($ret);
-               exit;
-
-               //Process result
-               $ret = array_column($ret, 'id', 'pseudonym');
-
-               //Send result
-               return $ret;
        }
 }