X-Git-Url: https://git.rapsys.eu/.gitweb.cgi/treebundle/blobdiff_plain/c74ab618abe6dad2df2e2e2139cec77188da6208..2cec3e449469a1981d46d7700ed5c9c50c242595:/Repository/AlbumRepository.php diff --git a/Repository/AlbumRepository.php b/Repository/AlbumRepository.php new file mode 100644 index 0000000..c1901d9 --- /dev/null +++ b/Repository/AlbumRepository.php @@ -0,0 +1,170 @@ + + * + * 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 = <<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 = <<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; + } +}