]> Raphaƫl G. Git Repositories - airbundle/blob - Repository/UserRepository.php
Update user inder template to diplay organizer list for non admin and
[airbundle] / Repository / UserRepository.php
1 <?php
2
3 namespace Rapsys\AirBundle\Repository;
4
5 use Symfony\Component\Translation\TranslatorInterface;
6 use Doctrine\ORM\Query\ResultSetMapping;
7
8 /**
9 * UserRepository
10 */
11 class UserRepository extends \Doctrine\ORM\EntityRepository {
12 /**
13 * Find users with translated highest group and civility
14 *
15 * @param $translator The TranslatorInterface instance
16 */
17 public function findAllWithTranslatedGroupAndCivility(TranslatorInterface $translator) {
18 //Get entity manager
19 $em = $this->getEntityManager();
20
21 //Get quote strategy
22 $qs = $em->getConfiguration()->getQuoteStrategy();
23 $dp = $em->getConnection()->getDatabasePlatform();
24
25 //Get quoted table names
26 //XXX: this allow to make this code table name independent
27 $tables = [
28 'RapsysAirBundle:UserGroup' => $qs->getJoinTableName($em->getClassMetadata('RapsysAirBundle:User')->getAssociationMapping('groups'), $em->getClassMetadata('RapsysAirBundle:User'), $dp),
29 'RapsysAirBundle:Group' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Group'), $dp),
30 'RapsysAirBundle:Civility' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Civility'), $dp),
31 'RapsysAirBundle:User' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:User'), $dp)
32 ];
33
34 //Set the request
35 $req = 'SELECT a.id, a.forename, a.surname, a.t_id, a.t_short, a.t_title, a.g_id, a.g_title FROM (
36 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
37 FROM RapsysAirBundle:User AS u
38 JOIN RapsysAirBundle:Civility AS t ON (t.id = u.civility_id)
39 LEFT JOIN RapsysAirBundle:UserGroup AS gu ON (gu.user_id = u.id)
40 LEFT JOIN RapsysAirBundle:Group AS g ON (g.id = gu.group_id)
41 ORDER BY g.id DESC, NULL LIMIT '.PHP_INT_MAX.'
42 ) AS a GROUP BY a.id ORDER BY NULL';
43
44 //Replace bundle entity name by table name
45 $req = str_replace(array_keys($tables), array_values($tables), $req);
46
47 //Get result set mapping instance
48 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
49 $rsm = new ResultSetMapping();
50
51 /*XXX: we don't want a result set for our request
52 $rsm->addEntityResult('RapsysAirBundle:User', 'u');
53 $rsm->addFieldResult('u', 'id', 'id');
54 $rsm->addFieldResult('u', 'forename', 'forename');
55 $rsm->addFieldResult('u', 'surname', 'surname');
56 $rsm->addFieldResult('u', 't_id', 'title_id');
57 $rsm->addJoinedEntityResult('RapsysAirBundle:Title', 't', 'u', 'title');
58 $rsm->addFieldResult('t', 't_id', 'id');
59 $rsm->addFieldResult('t', 't_title', 'title');
60 $rsm->addJoinedEntityResult('RapsysAirBundle:Group', 'g', 'u', 'groups');
61 $rsm->addFieldResult('g', 'g_id', 'id');
62 $rsm->addFieldResult('g', 'g_title', 'title');*/
63
64 //Declare all fields
65 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
66 //addScalarResult($sqlColName, $resColName, $type = 'string');
67 $rsm->addScalarResult('id', 'id', 'integer')
68 ->addScalarResult('forename', 'forename', 'string')
69 ->addScalarResult('surname', 'surname', 'string')
70 ->addScalarResult('t_id', 't_id', 'integer')
71 ->addScalarResult('t_short', 't_short', 'string')
72 ->addScalarResult('t_title', 't_title', 'string')
73 ->addScalarResult('g_id', 'g_id', 'integer')
74 ->addScalarResult('g_title', 'g_title', 'string')
75 ->addIndexByScalar('id');
76
77 //Fetch result
78 $res = $em
79 ->createNativeQuery($req, $rsm)
80 ->getResult();
81
82 //Init return
83 $ret = [];
84
85 //Process result
86 foreach($res as $data) {
87 //Get translated group
88 $group = $translator->trans($data['g_title']?:'User');
89 //Get translated title
90 $title = $translator->trans($data['t_short']);
91 //Init group subarray
92 if (!isset($ret[$group])) {
93 $ret[$group] = [];
94 }
95 //Set data
96 //XXX: ChoiceType use display string as key
97 $ret[$group][$title.' '.$data['forename'].' '.$data['surname']] = $data['id'];
98 }
99
100 //Send result
101 return $ret;
102 }
103
104 /**
105 * Find all applicant by session
106 *
107 * @param $session The Session
108 */
109 public function findAllApplicantBySession($session) {
110 //Get entity manager
111 $em = $this->getEntityManager();
112
113 //Fetch sessions
114 $ret = $this->getEntityManager()
115 ->createQuery('SELECT u.id, u.pseudonym FROM RapsysAirBundle:Application a JOIN RapsysAirBundle:User u WITH u.id = a.user WHERE a.session = :session')
116 ->setParameter('session', $session)
117 ->getResult();
118
119 //Process result
120 $ret = array_column($ret, 'id', 'pseudonym');
121
122 //Send result
123 return $ret;
124 }
125
126 /**
127 * Find all users grouped by translated group
128 *
129 * @param $translator The TranslatorInterface instance
130 * @return array|null The user array or null
131 */
132 public function findUserGroupedByTranslatedGroup(TranslatorInterface $translator) {
133 //Get entity manager
134 $em = $this->getEntityManager();
135
136 //Get quote strategy
137 $qs = $em->getConfiguration()->getQuoteStrategy();
138 $dp = $em->getConnection()->getDatabasePlatform();
139
140 //Get quoted table names
141 //XXX: this allow to make this code table name independent
142 $tables = [
143 'RapsysAirBundle:UserGroup' => $qs->getJoinTableName($em->getClassMetadata('RapsysAirBundle:User')->getAssociationMapping('groups'), $em->getClassMetadata('RapsysAirBundle:User'), $dp),
144 'RapsysAirBundle:Group' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Group'), $dp),
145 'RapsysAirBundle:User' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:User'), $dp),
146 //XXX: Set limit used to workaround mariadb subselect optimization
147 ':limit' => PHP_INT_MAX,
148 "\t" => '',
149 "\n" => ' '
150 ];
151
152 //Set the request
153 $req = <<<SQL
154 SELECT u.id, u.mail, u.pseudonym, g.id AS g_id, g.title AS g_title
155 FROM RapsysAirBundle:User AS u
156 JOIN RapsysAirBundle:UserGroup AS gu ON (gu.user_id = u.id)
157 JOIN RapsysAirBundle:Group AS g ON (g.id = gu.group_id)
158 ORDER BY g.id DESC, u.id ASC
159 SQL;
160
161 //Replace bundle entity name by table name
162 $req = str_replace(array_keys($tables), array_values($tables), $req);
163
164 //Get result set mapping instance
165 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
166 $rsm = new ResultSetMapping();
167
168 //Declare all fields
169 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
170 //addScalarResult($sqlColName, $resColName, $type = 'string');
171 $rsm->addScalarResult('id', 'id', 'integer')
172 ->addScalarResult('mail', 'mail', 'string')
173 ->addScalarResult('pseudonym', 'pseudonym', 'string')
174 ->addScalarResult('g_id', 'g_id', 'integer')
175 ->addScalarResult('g_title', 'g_title', 'string');
176
177 //Fetch result
178 $res = $em
179 ->createNativeQuery($req, $rsm)
180 ->getResult();
181
182 //Init return
183 $ret = [];
184
185 //Process result
186 foreach($res as $data) {
187 //Get translated group
188 $group = $translator->trans($data['g_title']);
189
190 //Init group subarray
191 if (!isset($ret[$group])) {
192 $ret[$group] = [];
193 }
194
195 //Set data
196 $ret[$group][$data['id']] = [
197 'mail' => $data['mail'],
198 'pseudonym' => $data['pseudonym']
199 ];
200 }
201
202 //Send result
203 return $ret;
204 }
205 }