1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys BlogBundle package.
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Rapsys\BlogBundle\Repository
;
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
;
22 class UserRepository
extends EntityRepository
implements PasswordUpgraderInterface
{
24 * Find user count as int
26 * @return integer The keywords count
28 public function findCountAsInt(): int {
31 SELECT COUNT(u.id) AS count
32 FROM RapsysBlogBundle:User AS u
35 //Get result set mapping instance
36 $req = $this->replace($req);
38 //Get result set mapping instance
39 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
40 $rsm = new ResultSetMapping();
43 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
44 //addScalarResult($sqlColName, $resColName, $type = 'string');
45 $rsm->addScalarResult('count', 'count', 'integer');
49 ->createNativeQuery($req, $rsm)
50 ->getSingleScalarResult();
54 * Find all users as array
56 * @param integer $page The page
57 * @param integer $count The count
58 * @return array The users sorted by id
60 public function findAllAsArray(int $page, int $count): array {
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
92 at.description AS at_description,
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
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)
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)
126 LIMIT :offset, :count
129 //Replace bundle entity name by table name
130 $req = $this->replace($req);
132 //Get result set mapping instance
133 //XXX: DEBUG: see ../blog.orig/src/Rapsys/UserBundle/Repository/ArticleRepository.php
134 $rsm = new ResultSetMapping();
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');
167 ->createNativeQuery($req, $rsm)
168 ->setParameter('offset', $page * $count)
169 ->setParameter('count', $count)
176 foreach($res as $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)]),
191 if (!empty($data['g_ids'])) {
193 $titles = explode("\n", $data['g_titles']);
195 //Iterate on each group
196 foreach(explode("\n", $data['g_ids']) as $k => $id) {
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)])
207 if (!empty($data['a_ids'])) {
209 $descriptions = explode("\n", $data['at_descriptions']);
212 $slugs = explode("\n", $data['at_slugs']);
215 $titles = explode("\n", $data['at_titles']);
219 'ids' => explode("\n", $data['ak_ids']),
220 'slugs' => explode("\n", $data['kt_slugs']),
221 'titles' => explode("\n", $data['kt_titles'])
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])) {
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 !!!
238 //With article keywords
239 if (!empty($keywords['ids'][$k])) {
241 $slugs = explode("\r", $keywords['slugs'][$k]);
244 $titles = explode("\r", $keywords['titles'][$k]);
246 //Iterate on each keyword
247 foreach(explode("\r", $keywords['ids'][$k]) as $k => $kid) {
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]]),
268 public function upgradePassword(PasswordAuthenticatedUserInterface
$user, string $hash): void {
269 //Set new hashed password
270 $user->setPassword($hash);
272 //Flush data to database
273 $this->getEntityManager()->flush();