]> Raphaël G. Git Repositories - userbundle/blob - Repository/UserRepository.php
d75e229959d12b4ccacb903f73d8c574839ba40e
[userbundle] / Repository / UserRepository.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys UserBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\UserBundle\Repository;
13
14 use Doctrine\ORM\Query\ResultSetMapping;
15 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
16
17 /**
18 * UserRepository
19 */
20 class UserRepository extends EntityRepository {
21 /**
22 * Find user count as int
23 *
24 * @return integer The keywords count
25 */
26 public function findCountAsInt(): int {
27 //Set the request
28 $req = <<<SQL
29 SELECT COUNT(u.id) AS count
30 FROM RapsysUserBundle:User AS u
31 SQL;
32
33 //Get result set mapping instance
34 $req = $this->replace($req);
35
36 //Get result set mapping instance
37 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
38 $rsm = new ResultSetMapping();
39
40 //Declare all fields
41 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
42 //addScalarResult($sqlColName, $resColName, $type = 'string');
43 $rsm->addScalarResult('count', 'count', 'integer');
44
45 //Get result
46 return $this->_em
47 ->createNativeQuery($req, $rsm)
48 ->getSingleScalarResult();
49 }
50
51 /**
52 * Find all users as array
53 *
54 * @param integer $page The page
55 * @param integer $count The count
56 * @return array The users sorted by id
57 */
58 public function findAllAsArray(int $page, int $count): array {
59 //Set the request
60 $req = <<<SQL
61 SELECT
62 u.id,
63 u.mail,
64 u.forename,
65 u.surname,
66 CONCAT_WS(" ", u.forename, u.surname) AS pseudonym,
67 c.id AS c_id,
68 c.title AS c_title,
69 GROUP_CONCAT(g.id ORDER BY g.id SEPARATOR "\\n") AS g_ids,
70 GROUP_CONCAT(g.title ORDER BY g.id SEPARATOR "\\n") AS g_titles
71 FROM RapsysUserBundle:User AS u
72 JOIN RapsysUserBundle:UserGroup AS gu ON (gu.user_id = u.id)
73 JOIN RapsysUserBundle:Group AS g ON (g.id = gu.group_id)
74 JOIN RapsysUserBundle:Civility AS c ON (c.id = u.civility_id)
75 GROUP BY u.id
76 ORDER BY u.id ASC
77 LIMIT :offset, :count
78 SQL;
79
80 //Replace bundle entity name by table name
81 $req = $this->replace($req);
82
83 //Get result set mapping instance
84 //XXX: DEBUG: see ../blog.orig/src/Rapsys/UserBundle/Repository/ArticleRepository.php
85 $rsm = new ResultSetMapping();
86
87 //Declare all fields
88 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
89 //addScalarResult($sqlColName, $resColName, $type = 'string');
90 $rsm->addScalarResult('id', 'id', 'integer')
91 ->addScalarResult('mail', 'mail', 'string')
92 ->addScalarResult('forename', 'forename', 'string')
93 ->addScalarResult('surname', 'surname', 'string')
94 ->addScalarResult('pseudonym', 'pseudonym', 'string')
95 ->addScalarResult('c_id', 'c_id', 'integer')
96 ->addScalarResult('c_title', 'c_title', 'string')
97 //XXX: is a string because of \n separator
98 ->addScalarResult('g_ids', 'g_ids', 'string')
99 //XXX: is a string because of \n separator
100 ->addScalarResult('g_titles', 'g_titles', 'string');
101
102 //Fetch result
103 $res = $this->_em
104 ->createNativeQuery($req, $rsm)
105 ->setParameter('offset', $page * $count)
106 ->setParameter('count', $count)
107 ->getResult();
108
109 //Init return
110 $ret = [];
111
112 //Process result
113 foreach($res as $data) {
114 //Set data
115 $ret[$data['id']] = [
116 'mail' => $data['mail'],
117 'forename' => $data['forename'],
118 'surname' => $data['surname'],
119 'pseudonym' => $data['pseudonym'],
120 'groups' => [],
121 'slug' => $this->slugger->slug($data['pseudonym']),
122 'link' => $this->router->generate('rapsys_user_edit', ['mail' => $short = $this->slugger->short($data['mail']), 'hash' => $this->slugger->hash($short)])
123 ];
124
125 //With groups
126 if (!empty($data['g_ids'])) {
127 //Set titles
128 $titles = explode("\n", $data['g_titles']);
129
130 //Iterate on each group
131 foreach(explode("\n", $data['g_ids']) as $k => $id) {
132 //Add group
133 $ret[$data['id']]['groups'][$id] = [
134 'title' => $group = $this->translator->trans($titles[$k]),
135 #'slug' => $this->slugger->slug($group)
136 #'link' => $this->router->generate('rapsys_user_group_view', ['id' => $id, 'slug' => $this->slugger->short($group)])
137 ];
138 }
139 }
140 }
141
142 //Send result
143 return $ret;
144 }
145 }