1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys AirBundle 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\AirBundle\Repository
;
14 use Doctrine\ORM\AbstractQuery
;
15 use Doctrine\ORM\Query\ResultSetMapping
;
17 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
;
19 use Rapsys\AirBundle\Repository
;
24 class DanceRepository
extends Repository
{
26 * Find dances indexed by id
28 * @return array The dances
30 public function findAllIndexed(): array {
37 FROM Rapsys\AirBundle\Entity\Dance AS d
40 //Replace bundle entity name by table name
41 $req = str_replace($this->tableKeys
, $this->tableValues
, $req);
43 //Get result set mapping instance
44 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
45 $rsm = new ResultSetMapping();
48 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
49 //addScalarResult($sqlColName, $resColName, $type = 'string');
50 $rsm->addEntityResult('Rapsys\AirBundle\Entity\Dance', 'd')
51 ->addFieldResult('d', 'id', 'id')
52 ->addFieldResult('d', 'name', 'name')
53 ->addFieldResult('d', 'type', 'type')
54 ->addIndexByColumn('d', 'id');
58 ->createNativeQuery($req, $rsm)
63 * Find dance choices as array
65 * @return array The dance choices
67 public function findChoicesAsArray(): array {
72 GROUP_CONCAT(d.id ORDER BY d.id SEPARATOR "\\n") AS ids,
73 GROUP_CONCAT(d.type ORDER BY d.id SEPARATOR "\\n") AS types
74 FROM Rapsys\AirBundle\Entity\Dance AS d
79 //Replace bundle entity name by table name
80 $req = str_replace($this->tableKeys
, $this->tableValues
, $req);
82 //Get result set mapping instance
83 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
84 $rsm = new ResultSetMapping();
87 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
88 //addScalarResult($sqlColName, $resColName, $type = 'string');
89 $rsm->addScalarResult('name', 'name', 'string')
90 ->addScalarResult('ids', 'ids', 'string')
91 ->addScalarResult('types', 'types', 'string')
92 ->addIndexByScalar('name');
96 ->createNativeQuery($req, $rsm)
102 //Iterate on each name
103 foreach($result as $name) {
108 $name['ids'] = explode("\n", $name['ids']);
111 $name['types'] = explode("\n", $name['types']);
113 //Iterate on each type
114 foreach($name['ids'] as $k => $id) {
116 $types[$this->translator
->trans($name['types'][$k]).' ('.$id.')'] = intval($id);
120 $return[$this->translator
->trans($name['name'])] = $types;
128 * Find dances ids by nametype
130 * @param array $nametype The nametype filter
131 * @return array The dance ids
133 public function findIdByNameTypeAsArray(array $nametype): array {
138 FROM Rapsys\AirBundle\Entity\Dance AS d
139 WHERE CONCAT_WS(' ', d.name, d.type) IN (:nametype)
140 ORDER BY d.name, d.type
143 //Replace bundle entity name by table name
144 $req = str_replace($this->tableKeys
, $this->tableValues
, $req);
146 //Get result set mapping instance
147 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
148 $rsm = new ResultSetMapping();
151 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
152 //XXX: we don't use a result set as we want to translate group and civility
153 $rsm->addScalarResult('id', 'id', 'integer');
157 ->createNativeQuery($req, $rsm)
158 ->setParameter('nametype', $nametype)
159 //XXX: instead of array_column on the result
160 ->getResult(AbstractQuery
::HYDRATE_SCALAR_COLUMN
);
164 * Find dance as array by id
166 * @param int $id The dance id
167 * @return array The dance data
169 public function findOneByIdAsArray(int $id): ?array {
176 GREATEST(d.created, d.updated) AS modified
177 FROM Rapsys\AirBundle\Entity\Dance AS d
181 //Replace bundle entity name by table name
182 $req = str_replace($this->tableKeys
, $this->tableValues
, $req);
184 //Get result set mapping instance
185 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
186 $rsm = new ResultSetMapping();
189 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
190 //addScalarResult($sqlColName, $resColName, $type = 'string');
191 $rsm->addScalarResult('id', 'id', 'integer')
192 ->addScalarResult('name', 'name', 'string')
193 ->addScalarResult('type', 'type', 'string')
194 ->addScalarResult('modified', 'modified', 'datetime')
195 ->addIndexByScalar('id');
199 ->createNativeQuery($req, $rsm)
200 ->setParameter('id', $id)
201 ->getOneOrNullResult();
204 if ($result === null) {
210 $result['alternates'] = [];
213 $route = 'rapsysair_dance_view';
216 $routeParams = ['id' => $id];
218 //Iterate on each languages
219 foreach($this->languages
as $languageId => $language) {
220 //Without current locale
221 if ($languageId !== $this->locale
) {
225 //Set route params locale
226 $routeParams['_locale'] = $languageId;
228 //Set route params name
229 $routeParams['name'] = $this->slugger
->slug($this->translator
->trans($result['name'], [], null, $languageId));
231 //Set route params type
232 $routeParams['type'] = $this->slugger
->slug($this->translator
->trans($result['type'], [], null, $languageId));
234 //Iterate on each locales
235 foreach(array_keys($this->languages
) as $other) {
236 //Without other locale
237 if ($other !== $languageId) {
238 //Set other locale title
239 $titles[$other] = $this->translator
->trans($language, [], null, $other);
243 //Add alternates locale
244 $result['alternates'][substr($languageId, 0, 2)] = $result['alternates'][str_replace('_', '-', $languageId)] = [
245 'absolute' => $this->router
->generate($route, $routeParams, UrlGeneratorInterface
::ABSOLUTE_URL
),
246 'relative' => $this->router
->generate($route, $routeParams),
247 'title' => implode('/', $titles),
248 'translated' => $this->translator
->trans($language, [], null, $languageId)
255 'id' => $result['id'],
256 'name' => $name = $this->translator
->trans($result['name']),
257 'type' => $type = $this->translator
->trans($result['type']),
259 'name' => $sname = $this->slugger
->slug($name),
260 'type' => $stype = $this->slugger
->slug($type)
262 'modified' => $result['modified'],
264 'link' => $this->router
->generate($route, ['_locale' => $this->locale
, 'name' => $sname, 'type' => $stype]+
$routeParams),
265 'alternates' => $result['alternates']
270 * Find dance names as array
272 * @return array The dance names
274 public function findNamesAsArray(): array {
279 GROUP_CONCAT(d.id ORDER BY d.id SEPARATOR "\\n") AS ids,
280 GROUP_CONCAT(d.type ORDER BY d.id SEPARATOR "\\n") AS types,
281 MAX(d.updated) AS modified
282 FROM Rapsys\AirBundle\Entity\Dance AS d
287 //Replace bundle entity name by table name
288 $req = str_replace($this->tableKeys
, $this->tableValues
, $req);
290 //Get result set mapping instance
291 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
292 $rsm = new ResultSetMapping();
295 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
296 //addScalarResult($sqlColName, $resColName, $type = 'string');
297 $rsm->addScalarResult('name', 'name', 'string')
298 ->addScalarResult('ids', 'ids', 'string')
299 ->addScalarResult('types', 'types', 'string')
300 ->addScalarResult('modified', 'modified', 'datetime')
301 ->addIndexByScalar('name');
305 ->createNativeQuery($req, $rsm)
311 //Iterate on each name
312 foreach($result as $name) {
314 $slug = $this->slugger
->slug($tname = $this->translator
->trans($name['name']));
320 $name['ids'] = explode("\n", $name['ids']);
323 $name['types'] = explode("\n", $name['types']);
325 //Iterate on each type
326 foreach($name['ids'] as $k => $id) {
328 $types[$this->slugger
->short($name['types'][$k])] = [
330 'type' => $type = $this->translator
->trans($name['types'][$k]),
331 'slug' => $stype = $this->slugger
->slug($type),
332 'link' => $this->router
->generate('rapsysair_dance_view', ['id' => $id, 'name' => $slug, 'type' => $stype])
337 $return[$sname = $this->slugger
->short($name['name'])] = [
340 'link' => $this->router
->generate('rapsysair_dance_name', ['name' => $sname, 'dance' => $slug]),
342 'modified' => $name['modified']
351 * Find dances by user id
353 * @param $id The user id
354 * @return array The user dances
356 public function findByUserId($userId): array {
358 $req = 'SELECT d.id, d.name, d.type
359 FROM Rapsys\AirBundle\Entity\UserDance AS ud
360 JOIN Rapsys\AirBundle\Entity\Dance AS d ON (d.id = ud.dance_id)
361 WHERE ud.user_id = :uid';
363 //Replace bundle entity name by table name
364 $req = str_replace($this->tableKeys
, $this->tableValues
, $req);
366 //Get result set mapping instance
367 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
368 $rsm = new ResultSetMapping();
370 //Declare result set for our request
371 $rsm->addEntityResult('Rapsys\AirBundle\Entity\Dance', 'd');
372 $rsm->addFieldResult('d', 'id', 'id');
373 $rsm->addFieldResult('d', 'name', 'name');
374 $rsm->addFieldResult('d', 'type', 'type');
378 ->createNativeQuery($req, $rsm)
379 ->setParameter('uid', $userId)