]> Raphaƫl G. Git Repositories - airbundle/blob - Repository/UserRepository.php
Add weather and slot glyphs
[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 title
14 *
15 * @param $translator The TranslatorInterface instance
16 */
17 public function findAllWithTranslatedGroupAndTitle(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:GroupUser' => $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:Title' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Title'), $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:Title AS t ON (t.id = u.title_id)
39 LEFT JOIN RapsysAirBundle:GroupUser 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 user's pseudonym from session applications
106 *
107 * @param $session The Session instance
108 */
109 public function findAllPseudonymBySession($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 INNER JOIN RapsysAirBundle:User u WHERE u.id = a.user AND a.session = :session')
116 ->setParameter('session', $session)
117 ->getResult();
118
119 //Process result
120 $ret = array_column($ret, 'pseudonym', 'id');
121
122 //Send result
123 return $ret;
124 }
125 }