--- /dev/null
+<?php declare(strict_types=1);
+
+/*
+ * This file is part of the Rapsys TreeBundle package.
+ *
+ * (c) Raphaël Gertz <symfony@rapsys.eu>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Rapsys\TreeBundle;
+
+use Doctrine\ORM\EntityManagerInterface;
+use Doctrine\ORM\EntityRepository;
+use Doctrine\ORM\Mapping\ClassMetadata;
+
+use Psr\Container\ContainerInterface;
+
+use Rapsys\PackBundle\Util\SluggerUtil;
+
+use Symfony\Component\Routing\RouterInterface;
+use Symfony\Contracts\Translation\TranslatorInterface;
+
+/**
+ * Repository
+ *
+ * {@inheritdoc}
+ */
+class Repository extends EntityRepository {
+ /**
+ * The table keys array
+ *
+ * @var array
+ */
+ protected array $keys;
+
+ /**
+ * The table names array
+ *
+ * @var array
+ */
+ protected array $names;
+
+ /**
+ * Initializes a new LocationRepository instance
+ *
+ * @param EntityManagerInterface $manager The EntityManagerInterface instance
+ * @param ClassMetadata $class The ClassMetadata instance
+ * @param ContainerInterface $container The container instance
+ * @param RouterInterface $router The router instance
+ * @param SluggerUtil $slugger The SluggerUtil instance
+ * @param TranslatorInterface $translator The TranslatorInterface instance
+ * @param string $locale The current locale
+ * @param array $languages The languages list
+ */
+ public function __construct(protected EntityManagerInterface $manager, protected ClassMetadata $class, protected ContainerInterface $container, protected RouterInterface $router, protected SluggerUtil $slugger, protected TranslatorInterface $translator, protected string $locale, protected array $languages) {
+ //Call parent constructor
+ parent::__construct($this->manager, $this->class);
+
+ //Get quote strategy
+ $qs = $this->manager->getConfiguration()->getQuoteStrategy();
+ $dp = $this->manager->getConnection()->getDatabasePlatform();
+
+ //Set quoted table names
+ //XXX: this allow to make this code table name independent
+ //XXX: remember to place longer prefix before shorter to avoid strange replacings
+ //XXX: entity short syntax removed in doctrine/persistence 3.x: https://github.com/doctrine/orm/issues/8818
+ $tables = [
+ 'Rapsys\TreeBundle\Entity\UserGroup' => $qs->getJoinTableName($manager->getClassMetadata('Rapsys\TreeBundle\Entity\User')->getAssociationMapping('groups'), $manager->getClassMetadata('Rapsys\TreeBundle\Entity\User'), $dp),
+ 'Rapsys\TreeBundle\Entity\Album' => $qs->getTableName($manager->getClassMetadata('Rapsys\TreeBundle\Entity\Album'), $dp),
+ 'Rapsys\TreeBundle\Entity\Asset' => $qs->getTableName($manager->getClassMetadata('Rapsys\TreeBundle\Entity\Asset'), $dp),
+ 'Rapsys\TreeBundle\Entity\Civility' => $qs->getTableName($manager->getClassMetadata('Rapsys\TreeBundle\Entity\Civility'), $dp),
+ 'Rapsys\TreeBundle\Entity\Group' => $qs->getTableName($manager->getClassMetadata('Rapsys\TreeBundle\Entity\Group'), $dp),
+ 'Rapsys\TreeBundle\Entity\User' => $qs->getTableName($manager->getClassMetadata('Rapsys\TreeBundle\Entity\User'), $dp),
+ //Set locale
+ //XXX: or $manager->getConnection()->quote($this->locale) ???
+ ':locale' => $dp->quoteStringLiteral($this->locale),
+ //Set limit
+ //XXX: Set limit used to workaround mariadb subselect optimization
+ ':limit' => PHP_INT_MAX,
+ //Set cleanup
+ "\t" => '',
+ "\r" => ' ',
+ "\n" => ' '
+ ];
+
+ //Set quoted table keys
+ $this->keys = array_keys($tables);
+
+ //Set quoted table names
+ $this->names = array_values($tables);
+ }
+
+ /**
+ * Get replaced query
+ *
+ * @param string $req The request to replace
+ * @return string The replaced request
+ */
+ protected function replace(string $req): string {
+ //Replace bundle entity name by table name
+ return str_replace($this->keys, $this->names, $req);
+ }
+}
--- /dev/null
+<?php declare(strict_types=1);
+
+/*
+ * This file is part of the Rapsys TreeBundle package.
+ *
+ * (c) Raphaël Gertz <symfony@rapsys.eu>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Rapsys\TreeBundle\Repository;
+
+use Doctrine\ORM\Query\ResultSetMapping;
+
+use Rapsys\TreeBundle\Repository;
+
+/**
+ * Album repository
+ */
+class AlbumRepository extends Repository {
+ /**
+ * Find album count as int
+ *
+ * @param ?integer $id The user id
+ * @return integer The albums count
+ */
+ public function findCountAsInt(?int $id): int {
+ //Set user sql
+ $userSql = <<<SQL
+a.user_id = :id
+SQL;
+
+ //With null id
+ if ($id === null) {
+ //Set user sql
+ $userSql = <<<SQL
+a.user_id IS NULL
+SQL;
+ }
+
+ //Set the request
+ $req = <<<SQL
+SELECT COUNT(DISTINCT a.album_id) AS count
+FROM Rapsys\TreeBundle\Entity\Asset AS a
+WHERE $userSql
+SQL;
+
+ //Get result set mapping instance
+ $req = $this->replace($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('count', 'count', 'integer');
+
+ //Get result
+ return $this->_em
+ ->createNativeQuery($req, $rsm)
+ ->setParameter('id', $id)
+ ->getSingleScalarResult();
+ }
+
+ /**
+ * Find albums as array
+ *
+ * @param ?integer $id The user id
+ * @param integer $page The page
+ * @param integer $count The count
+ * @return array The albums array
+ */
+ public function findAllAsArray(?int $id, int $page, int $count): array {
+ //Set user sql
+ $userSql = <<<SQL
+s.user_id = :id
+SQL;
+
+ //With null id
+ if ($id === null) {
+ //Set user sql
+ $userSql = <<<SQL
+s.user_id IS NULL
+SQL;
+ }
+
+ //Set the request
+ $req = <<<SQL
+SELECT
+ a.id,
+ a.path,
+ GROUP_CONCAT(s.id ORDER BY s.id SEPARATOR "\\n") AS s_ids,
+ GROUP_CONCAT(IFNULL(s.path, '/') ORDER BY s.id SEPARATOR "\\n") AS s_paths,
+ a.slug,
+ GREATEST(a.created, s.created) AS created,
+ GREATEST(a.updated, s.updated) AS updated,
+ GREATEST(a.created, s.created, a.updated, s.updated) AS modified
+FROM Rapsys\TreeBundle\Entity\Album AS a
+JOIN Rapsys\TreeBundle\Entity\Asset AS s ON (s.album_id = a.id AND {$userSql})
+GROUP BY a.id
+ORDER BY updated, created DESC, a.id
+LIMIT :offset, :count
+SQL;
+
+ //Replace bundle entity name by table name
+ $req = $this->replace($req);
+
+ //Get result set mapping instance
+ $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('path', 'path', 'string')
+ ->addScalarResult('slug', 'slug', 'string')
+ ->addScalarResult('s_ids', 's_ids', 'string')
+ ->addScalarResult('s_paths', 's_paths', 'string')
+ ->addScalarResult('created', 'created', 'datetime')
+ ->addScalarResult('updated', 'updated', 'datetime')
+ ->addScalarResult('modified', 'modified', 'datetime')
+ ->addIndexByScalar('id');
+
+ //Get result
+ $result = $this->_em
+ ->createNativeQuery($req, $rsm)
+ ->setParameter('offset', $page * $count)
+ ->setParameter('count', $count)
+ ->setParameter('id', $id)
+ ->getArrayResult();
+
+ //Set return
+ $return = [];
+
+ //Iterate on each city
+ foreach($result as $data) {
+ //Add to return
+ $return[$data['id']] = [
+ 'id' => $id = $data['id'],
+ 'slug' => $slug = $data['slug'],
+ 'path' => $data['path'],
+ 'created' => $data['created'],
+ 'updated' => $data['updated'],
+ 'modified' => $data['modified'],
+ 'link' => $this->router->generate('rapsystree_album', ['id' => $id, 'slug' => $slug, 'path' => '/']),
+ 'assets' => []
+ ];
+
+ //Explode asset ids
+ $data['s_ids'] = explode("\n", $data['s_ids']);
+
+ //Explode asset paths
+ $data['s_paths'] = explode("\n", $data['s_paths']);
+
+ foreach($data['s_ids'] as $s => $id) {
+ $return[$data['id']]['assets'][$id] = [
+ 'id' => $id,
+ 'path' => $path = $data['s_paths'][$s],
+ 'link' => $this->router->generate('rapsystree_asset', ['id' => $id, 'path' => $path]),
+ ];
+ }
+ }
+
+ //Return return
+ return $return;
+ }
+}