X-Git-Url: https://git.rapsys.eu/airbundle/blobdiff_plain/0f0d50eb8832ffe2e1c3d8f40f64d384e6b94b5d..refs/heads/master:/Repository/DanceRepository.php diff --git a/Repository/DanceRepository.php b/Repository/DanceRepository.php index e7dd475..f7986d1 100644 --- a/Repository/DanceRepository.php +++ b/Repository/DanceRepository.php @@ -14,6 +14,10 @@ namespace Rapsys\AirBundle\Repository; use Doctrine\ORM\AbstractQuery; use Doctrine\ORM\Query\ResultSetMapping; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; + +use Rapsys\AirBundle\Repository; + /** * DanceRepository */ @@ -30,7 +34,7 @@ SELECT d.id, d.name, d.type -FROM RapsysAirBundle:Dance AS d +FROM Rapsys\AirBundle\Entity\Dance AS d SQL; //Replace bundle entity name by table name @@ -43,7 +47,7 @@ SQL; //Declare all fields //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php //addScalarResult($sqlColName, $resColName, $type = 'string'); - $rsm->addEntityResult('RapsysAirBundle:Dance', 'd') + $rsm->addEntityResult('Rapsys\AirBundle\Entity\Dance', 'd') ->addFieldResult('d', 'id', 'id') ->addFieldResult('d', 'name', 'name') ->addFieldResult('d', 'type', 'type') @@ -67,7 +71,7 @@ SELECT d.name, GROUP_CONCAT(d.id ORDER BY d.id SEPARATOR "\\n") AS ids, GROUP_CONCAT(d.type ORDER BY d.id SEPARATOR "\\n") AS types -FROM RapsysAirBundle:Dance AS d +FROM Rapsys\AirBundle\Entity\Dance AS d GROUP BY d.name ORDER BY d.name SQL; @@ -131,7 +135,7 @@ SQL; $req = <<getResult(AbstractQuery::HYDRATE_SCALAR_COLUMN); } + /** + * Find dance as array by id + * + * @param int $id The dance id + * @return array The dance data + */ + public function findOneByIdAsArray(int $id): ?array { + //Set the request + $req = <<tableKeys, $this->tableValues, $req); + + //Get result set mapping instance + //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php + $rsm = new ResultSetMapping(); + + //Declare all fields + //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php + //addScalarResult($sqlColName, $resColName, $type = 'string'); + $rsm->addScalarResult('id', 'id', 'integer') + ->addScalarResult('name', 'name', 'string') + ->addScalarResult('type', 'type', 'string') + ->addScalarResult('modified', 'modified', 'datetime') + ->addIndexByScalar('id'); + + //Get result + $result = $this->_em + ->createNativeQuery($req, $rsm) + ->setParameter('id', $id) + ->getOneOrNullResult(); + + //Without result + if ($result === null) { + //Return result + return $result; + } + + //Set alternates + $result['alternates'] = []; + + //Set route + $route = 'rapsysair_dance_view'; + + //Set route params + $routeParams = ['id' => $id]; + + //Iterate on each languages + foreach($this->languages as $languageId => $language) { + //Without current locale + if ($languageId !== $this->locale) { + //Set titles + $titles = []; + + //Set route params locale + $routeParams['_locale'] = $languageId; + + //Set route params name + $routeParams['name'] = $this->slugger->slug($this->translator->trans($result['name'], [], null, $languageId)); + + //Set route params type + $routeParams['type'] = $this->slugger->slug($this->translator->trans($result['type'], [], null, $languageId)); + + //Iterate on each locales + foreach(array_keys($this->languages) as $other) { + //Without other locale + if ($other !== $languageId) { + //Set other locale title + $titles[$other] = $this->translator->trans($language, [], null, $other); + } + } + + //Add alternates locale + $result['alternates'][substr($languageId, 0, 2)] = $result['alternates'][str_replace('_', '-', $languageId)] = [ + 'absolute' => $this->router->generate($route, $routeParams, UrlGeneratorInterface::ABSOLUTE_URL), + 'relative' => $this->router->generate($route, $routeParams), + 'title' => implode('/', $titles), + 'translated' => $this->translator->trans($language, [], null, $languageId) + ]; + } + } + + //Return result + return [ + 'id' => $result['id'], + 'name' => $name = $this->translator->trans($result['name']), + 'type' => $type = $this->translator->trans($result['type']), + 'slug' => [ + 'name' => $sname = $this->slugger->slug($name), + 'type' => $stype = $this->slugger->slug($type) + ], + 'modified' => $result['modified'], + //XXX: Useless ??? + 'link' => $this->router->generate($route, ['_locale' => $this->locale, 'name' => $sname, 'type' => $stype]+$routeParams), + 'alternates' => $result['alternates'] + ]; + } + /** * Find dance names as array * @@ -169,7 +279,7 @@ SELECT GROUP_CONCAT(d.id ORDER BY d.id SEPARATOR "\\n") AS ids, GROUP_CONCAT(d.type ORDER BY d.id SEPARATOR "\\n") AS types, MAX(d.updated) AS modified -FROM RapsysAirBundle:Dance AS d +FROM Rapsys\AirBundle\Entity\Dance AS d GROUP BY d.name ORDER BY d.name SQL; @@ -219,7 +329,7 @@ SQL; 'id' => $id, 'type' => $type = $this->translator->trans($name['types'][$k]), 'slug' => $stype = $this->slugger->slug($type), - 'link' => $this->router->generate('rapsys_air_dance_view', ['id' => $id, 'name' => $slug, 'type' => $stype]) + 'link' => $this->router->generate('rapsysair_dance_view', ['id' => $id, 'name' => $slug, 'type' => $stype]) ]; } @@ -227,7 +337,7 @@ SQL; $return[$sname = $this->slugger->short($name['name'])] = [ 'name' => $tname, 'slug' => $slug, - 'link' => $this->router->generate('rapsys_air_dance_name', ['name' => $sname, 'dance' => $slug]), + 'link' => $this->router->generate('rapsysair_dance_name', ['name' => $sname, 'dance' => $slug]), 'types' => $types, 'modified' => $name['modified'] ]; @@ -246,8 +356,8 @@ SQL; public function findByUserId($userId): array { //Set the request $req = 'SELECT d.id, d.name, d.type -FROM RapsysAirBundle:UserDance AS ud -JOIN RapsysAirBundle:Dance AS d ON (d.id = ud.dance_id) +FROM Rapsys\AirBundle\Entity\UserDance AS ud +JOIN Rapsys\AirBundle\Entity\Dance AS d ON (d.id = ud.dance_id) WHERE ud.user_id = :uid'; //Replace bundle entity name by table name @@ -258,7 +368,7 @@ WHERE ud.user_id = :uid'; $rsm = new ResultSetMapping(); //Declare result set for our request - $rsm->addEntityResult('RapsysAirBundle:Dance', 'd'); + $rsm->addEntityResult('Rapsys\AirBundle\Entity\Dance', 'd'); $rsm->addFieldResult('d', 'id', 'id'); $rsm->addFieldResult('d', 'name', 'name'); $rsm->addFieldResult('d', 'type', 'type');