]> Raphaël G. Git Repositories - blogbundle/blob - Repository/UserRepository.php
Add edit form extension
[blogbundle] / Repository / UserRepository.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys BlogBundle 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\BlogBundle\Repository;
13
14 use Doctrine\ORM\Query\ResultSetMapping;
15 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
16 use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
17 use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
18
19 /**
20 * UserRepository
21 */
22 class UserRepository extends EntityRepository implements PasswordUpgraderInterface {
23 /**
24 * Find user count as int
25 *
26 * @return integer The keywords count
27 */
28 public function findCountAsInt(): int {
29 //Set the request
30 $req = <<<SQL
31 SELECT COUNT(u.id) AS count
32 FROM RapsysBlogBundle:User AS u
33 SQL;
34
35 //Get result set mapping instance
36 $req = $this->replace($req);
37
38 //Get result set mapping instance
39 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
40 $rsm = new ResultSetMapping();
41
42 //Declare all fields
43 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
44 //addScalarResult($sqlColName, $resColName, $type = 'string');
45 $rsm->addScalarResult('count', 'count', 'integer');
46
47 //Get result
48 return $this->_em
49 ->createNativeQuery($req, $rsm)
50 ->getSingleScalarResult();
51 }
52
53 /**
54 * Find all users as array
55 *
56 * @param integer $page The page
57 * @param integer $count The count
58 * @return array The users sorted by id
59 */
60 public function findAllAsArray(int $page, int $count): array {
61 //Set the request
62 $req = <<<SQL
63 SELECT
64 t.id,
65 t.mail,
66 t.forename,
67 t.surname,
68 t.pseudonym,
69 t.slug,
70 t.civility,
71 t.g_ids,
72 t.g_titles,
73 GROUP_CONCAT(t.a_id ORDER BY t.a_id SEPARATOR "\\n") AS a_ids,
74 GROUP_CONCAT(t.at_description ORDER BY t.a_id SEPARATOR "\\n") AS at_descriptions,
75 GROUP_CONCAT(t.at_slug ORDER BY t.a_id SEPARATOR "\\n") AS at_slugs,
76 GROUP_CONCAT(t.at_title ORDER BY t.a_id SEPARATOR "\\n") AS at_titles,
77 GROUP_CONCAT(t.ak_ids ORDER BY t.a_id SEPARATOR "\\n") AS ak_ids,
78 GROUP_CONCAT(t.kt_slugs ORDER BY t.a_id SEPARATOR "\\n") AS kt_slugs,
79 GROUP_CONCAT(t.kt_titles ORDER BY t.a_id SEPARATOR "\\n") AS kt_titles
80 FROM (
81 SELECT
82 c.id,
83 c.mail,
84 c.forename,
85 c.surname,
86 c.pseudonym,
87 c.slug,
88 c.civility,
89 c.g_ids,
90 c.g_titles,
91 a.id AS a_id,
92 at.description AS at_description,
93 at.slug AS at_slug,
94 at.title AS at_title,
95 GROUP_CONCAT(ak.keyword_id ORDER BY ak.keyword_id SEPARATOR "\\r") AS ak_ids,
96 GROUP_CONCAT(kt.slug ORDER BY ak.keyword_id SEPARATOR "\\r") AS kt_slugs,
97 GROUP_CONCAT(kt.title ORDER BY ak.keyword_id SEPARATOR "\\r") AS kt_titles
98 FROM (
99 SELECT
100 u.id,
101 u.mail,
102 u.forename,
103 u.surname,
104 u.pseudonym,
105 u.slug,
106 c.title AS civility,
107 GROUP_CONCAT(g.id ORDER BY g.id SEPARATOR "\\n") AS g_ids,
108 GROUP_CONCAT(g.title ORDER BY g.id SEPARATOR "\\n") AS g_titles
109 FROM RapsysBlogBundle:User AS u
110 JOIN RapsysBlogBundle:UserGroup AS gu ON (gu.user_id = u.id)
111 JOIN RapsysBlogBundle:Group AS g ON (g.id = gu.group_id)
112 JOIN RapsysBlogBundle:Civility AS c ON (c.id = u.civility_id)
113 ORDER BY NULL
114 LIMIT 0, :limit
115 ) AS c
116 LEFT JOIN RapsysBlogBundle:Article AS a ON (a.user_id = c.id)
117 LEFT JOIN RapsysBlogBundle:ArticleTranslation AS at ON (at.article_id = a.id AND at.locale = :locale)
118 LEFT JOIN RapsysBlogBundle:ArticleKeyword AS ak ON (ak.article_id = a.id)
119 LEFT JOIN RapsysBlogBundle:KeywordTranslation AS kt ON (kt.keyword_id = ak.keyword_id AND at.locale = :locale)
120 GROUP BY a.id
121 ORDER BY NULL
122 LIMIT 0, :limit
123 ) AS t
124 GROUP BY t.id
125 ORDER BY t.id ASC
126 LIMIT :offset, :count
127 SQL;
128
129 //Replace bundle entity name by table name
130 $req = $this->replace($req);
131
132 //Get result set mapping instance
133 //XXX: DEBUG: see ../blog.orig/src/Rapsys/UserBundle/Repository/ArticleRepository.php
134 $rsm = new ResultSetMapping();
135
136 //Declare all fields
137 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
138 //addScalarResult($sqlColName, $resColName, $type = 'string');
139 $rsm->addScalarResult('id', 'id', 'integer')
140 ->addScalarResult('mail', 'mail', 'string')
141 ->addScalarResult('forename', 'forename', 'string')
142 ->addScalarResult('surname', 'surname', 'string')
143 ->addScalarResult('pseudonym', 'pseudonym', 'string')
144 ->addScalarResult('slug', 'slug', 'string')
145 ->addScalarResult('civility', 'civility', 'string')
146 //XXX: is a string because of \n separator
147 ->addScalarResult('g_ids', 'g_ids', 'string')
148 //XXX: is a string because of \n separator
149 ->addScalarResult('g_titles', 'g_titles', 'string')
150 //XXX: is a string because of \n separator
151 ->addScalarResult('a_ids', 'a_ids', 'string')
152 //XXX: is a string because of \n separator
153 ->addScalarResult('at_descriptions', 'at_descriptions', 'string')
154 //XXX: is a string because of \n separator
155 ->addScalarResult('at_slugs', 'at_slugs', 'string')
156 //XXX: is a string because of \n separator
157 ->addScalarResult('at_titles', 'at_titles', 'string')
158 //XXX: is a string because of \n separator
159 ->addScalarResult('ak_ids', 'ak_ids', 'string')
160 //XXX: is a string because of \n separator
161 ->addScalarResult('kt_slugs', 'kt_slugs', 'string')
162 //XXX: is a string because of \n separator
163 ->addScalarResult('kt_titles', 'kt_titles', 'string');
164
165 //Fetch result
166 $res = $this->_em
167 ->createNativeQuery($req, $rsm)
168 ->setParameter('offset', $page * $count)
169 ->setParameter('count', $count)
170 ->getResult();
171
172 //Init return
173 $ret = [];
174
175 //Process result
176 foreach($res as $data) {
177 //Set data
178 $ret[$data['id']] = [
179 'mail' => $data['mail'],
180 'forename' => $data['forename'],
181 'surname' => $data['surname'],
182 'pseudonym' => $data['pseudonym'],
183 #'slug' => $data['slug'],
184 'link' => $this->router->generate('rapsys_blog_user_view', ['id' => $data['id'], 'slug' => $data['slug']]),
185 'edit' => $this->router->generate('rapsys_user_edit', ['mail' => $short = $this->slugger->short($data['mail']), 'hash' => $this->slugger->hash($short)]),
186 'articles' => [],
187 'groups' => []
188 ];
189
190 //With groups
191 if (!empty($data['g_ids'])) {
192 //Set titles
193 $titles = explode("\n", $data['g_titles']);
194
195 //Iterate on each group
196 foreach(explode("\n", $data['g_ids']) as $k => $id) {
197 //Add group
198 $ret[$data['id']]['groups'][$id] = [
199 'title' => /*$group = */$this->translator->trans($titles[$k]),
200 #'slug' => $this->slugger->slug($group)
201 #'link' => $this->router->generate('rapsys_user_group_view', ['id' => $id, 'slug' => $this->slugger->short($group)])
202 ];
203 }
204 }
205
206 //With articles
207 if (!empty($data['a_ids'])) {
208 //Set descriptions
209 $descriptions = explode("\n", $data['at_descriptions']);
210
211 //Set slugs
212 $slugs = explode("\n", $data['at_slugs']);
213
214 //Set titles
215 $titles = explode("\n", $data['at_titles']);
216
217 //Set keyword ids
218 $keywords = [
219 'ids' => explode("\n", $data['ak_ids']),
220 'slugs' => explode("\n", $data['kt_slugs']),
221 'titles' => explode("\n", $data['kt_titles'])
222 ];
223
224 //Iterate on each dance
225 foreach(explode("\n", $data['a_ids']) as $k => $id) {
226 //Init article when missing
227 if (!isset($ret[$data['id']]['articles'][$id])) {
228 //Add article
229 $ret[$data['id']]['articles'][$id] = [
230 'description' => $descriptions[$k],
231 #'slug' => $slugs[$k],
232 'title' => $titles[$k],
233 'link' => $this->router->generate('rapsys_blog_article_view', ['id' => $id, 'slug' => $slugs[$k]]),
234 //TODO: replace with keywords !!!
235 'keywords' => []
236 ];
237
238 //With article keywords
239 if (!empty($keywords['ids'][$k])) {
240 //Set slugs
241 $slugs = explode("\r", $keywords['slugs'][$k]);
242
243 //Set titles
244 $titles = explode("\r", $keywords['titles'][$k]);
245
246 //Iterate on each keyword
247 foreach(explode("\r", $keywords['ids'][$k]) as $k => $kid) {
248 //Add keyword
249 $ret[$data['id']]['articles'][$id]['keywords'][$kid] = [
250 #'slug' => $slugs[$k],
251 'title' => $titles[$k],
252 'link' => $this->router->generate('rapsys_blog_keyword_view', ['id' => $kid, 'slug' => $slugs[$k]]),
253 ];
254 }
255 }
256 }
257 }
258 }
259 }
260
261 //Send result
262 return $ret;
263 }
264
265 /**
266 * {@inheritdoc}
267 */
268 public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $hash): void {
269 //Set new hashed password
270 $user->setPassword($hash);
271
272 //Flush data to database
273 $this->getEntityManager()->flush();
274 }
275 }