]> Raphaƫl G. Git Repositories - treebundle/blobdiff - Repository/AlbumRepository.php
Add element repository class with findOneByUidIdPathAsArray method
[treebundle] / Repository / AlbumRepository.php
index e56ae8a1e269683abd69b6ce9a0f67c919e9e226..5d54fe6ee4f382cb1228e8f08ea2841ca104473a 100644 (file)
@@ -22,17 +22,17 @@ class AlbumRepository extends Repository {
        /**
         * Find album count as int
         *
        /**
         * Find album count as int
         *
-        * @param ?integer $id The user id
+        * @param ?integer $uid The user id
         * @return integer The albums count
         */
         * @return integer The albums count
         */
-       public function findCountAsInt(?int $id): int {
+       public function countByUidAsInt(?int $uid): int {
                //Set user sql
                $userSql = <<<SQL
                //Set user sql
                $userSql = <<<SQL
-a.user_id = :id
+a.user_id = :uid
 SQL;
 
 SQL;
 
-               //With null id
-               if ($id === null) {
+               //With null uid
+               if ($uid === null) {
                        //Set user sql
                        $userSql = <<<SQL
 a.user_id IS NULL
                        //Set user sql
                        $userSql = <<<SQL
 a.user_id IS NULL
@@ -61,45 +61,32 @@ SQL;
                //Get result
                return $this->_em
                        ->createNativeQuery($req, $rsm)
                //Get result
                return $this->_em
                        ->createNativeQuery($req, $rsm)
-                       ->setParameter('id', $id)
+                       ->setParameter('uid', $uid)
                        ->getSingleScalarResult();
        }
 
        /**
         * Find albums as array
         *
                        ->getSingleScalarResult();
        }
 
        /**
         * Find albums as array
         *
-        * @param ?integer $id The user id
+        * @param ?integer $uid The user id
         * @param integer $page The page
         * @param integer $count The count
         * @return array The albums array
         */
         * @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
-e.user_id = :id
-SQL;
-
-               //With null id
-               if ($id === null) {
-                       //Set user sql
-                       $userSql = <<<SQL
-e.user_id IS NULL
-SQL;
-               }
-
+       public function findByUidAsArray(?int $uid, int $page, int $count): array {
                //Set the request
                $req = <<<SQL
 SELECT
        a.id,
        a.path,
                //Set the request
                $req = <<<SQL
 SELECT
        a.id,
        a.path,
-       GROUP_CONCAT(e.id ORDER BY e.id SEPARATOR "\\n") AS s_ids,
-       GROUP_CONCAT(IFNULL(e.path, '/') ORDER BY e.id SEPARATOR "\\n") AS s_paths,
        a.slug,
        GREATEST(a.created, e.created) AS created,
        GREATEST(a.updated, e.updated) AS updated,
        a.slug,
        GREATEST(a.created, e.created) AS created,
        GREATEST(a.updated, e.updated) AS updated,
-       GREATEST(a.created, e.created, a.updated, e.updated) AS modified
+       GREATEST(a.created, e.created, a.updated, e.updated) AS modified,
+       GROUP_CONCAT(e.id ORDER BY e.id SEPARATOR "\\n") AS e_ids,
+       GROUP_CONCAT(e.path ORDER BY e.id SEPARATOR "\\n") AS e_paths
 FROM Rapsys\TreeBundle\Entity\Album AS a
 FROM Rapsys\TreeBundle\Entity\Album AS a
-JOIN Rapsys\TreeBundle\Entity\Element AS e ON (e.album_id = a.id AND {$userSql})
+JOIN Rapsys\TreeBundle\Entity\Element AS e ON (e.album_id = a.id AND e.user_id IN (NULL, :uid))
 GROUP BY a.id
 ORDER BY updated, created DESC, a.id
 LIMIT :offset, :count
 GROUP BY a.id
 ORDER BY updated, created DESC, a.id
 LIMIT :offset, :count
@@ -117,11 +104,11 @@ SQL;
                $rsm->addScalarResult('id', 'id', 'integer')
                        ->addScalarResult('path', 'path', 'string')
                        ->addScalarResult('slug', 'slug', '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')
                        ->addScalarResult('created', 'created', 'datetime')
                        ->addScalarResult('updated', 'updated', 'datetime')
                        ->addScalarResult('modified', 'modified', 'datetime')
+                       ->addScalarResult('e_ids', 'e_ids', 'string')
+                       ->addScalarResult('e_paths', 'e_paths', 'string')
                        ->addIndexByScalar('id');
 
                //Get result
                        ->addIndexByScalar('id');
 
                //Get result
@@ -129,15 +116,15 @@ SQL;
                        ->createNativeQuery($req, $rsm)
                        ->setParameter('offset', $page * $count)
                        ->setParameter('count', $count)
                        ->createNativeQuery($req, $rsm)
                        ->setParameter('offset', $page * $count)
                        ->setParameter('count', $count)
-                       ->setParameter('id', $id)
+                       ->setParameter('uid', $uid)
                        ->getArrayResult();
 
                //Set return
                $return = [];
 
                        ->getArrayResult();
 
                //Set return
                $return = [];
 
-               //Iterate on each city
+               //Iterate on each album
                foreach($result as $data) {
                foreach($result as $data) {
-                       //Add to return
+                       //Append album
                        $return[$data['id']] = [
                                'id' => $id = $data['id'],
                                'slug' => $slug = $data['slug'],
                        $return[$data['id']] = [
                                'id' => $id = $data['id'],
                                'slug' => $slug = $data['slug'],
@@ -145,21 +132,22 @@ SQL;
                                'created' => $data['created'],
                                'updated' => $data['updated'],
                                'modified' => $data['modified'],
                                'created' => $data['created'],
                                'updated' => $data['updated'],
                                'modified' => $data['modified'],
-                               'link' => $this->router->generate('rapsystree_album', ['id' => $id, 'slug' => $slug, 'path' => '/']),
+                               'link' => $this->router->generate('rapsystree_album', [ 'id' => $id, 'slug' => $slug ]),
                                'elements' => []
                        ];
 
                        //Explode element ids
                                'elements' => []
                        ];
 
                        //Explode element ids
-                       $data['s_ids'] = explode("\n", $data['s_ids']);
+                       $data['e_ids'] = explode("\n", $data['e_ids']);
 
                        //Explode element paths
 
                        //Explode element paths
-                       $data['s_paths'] = explode("\n", $data['s_paths']);
+                       $data['e_paths'] = explode("\n", $data['e_paths']);
 
 
-                       foreach($data['s_ids'] as $s => $id) {
-                               $return[$data['id']]['elements'][$id] = [
-                                       'id' => $id,
-                                       'path' => $path = $data['s_paths'][$s],
-                                       'link' => $this->router->generate('rapsystree_element', ['id' => $id, 'path' => $path]),
+                       foreach($data['e_ids'] as $e => $eid) {
+                               $return[$data['id']]['elements'][$eid] = [
+                                       'id' => $eid,
+                                       'name' => trim(str_replace('/', ' / ', '/'.($path = $data['e_paths'][$e]))),
+                                       'path' => $path,
+                                       'link' => $this->router->generate('rapsystree_element', [ 'id' => $eid, 'path' => $path ]),
                                ];
                        }
                }
                                ];
                        }
                }
@@ -167,4 +155,193 @@ SQL;
                //Return return
                return $return;
        }
                //Return return
                return $return;
        }
+
+       /**
+        * Find album as array
+        *
+        * @param integer $id The album id
+        * @param string $path The album path
+        * @return ?array The album array
+        */
+       public function findOneByIdPathAsArray(int $id, string $path): ?array {
+               //Set the request
+               $req = <<<SQL
+SELECT
+       a.id,
+       a.path,
+       a.slug,
+       GREATEST(a.created, e.created) AS created,
+       GREATEST(a.updated, e.updated) AS updated,
+       GREATEST(a.created, e.created, a.updated, e.updated) AS modified,
+       GROUP_CONCAT(e.id ORDER BY e.id SEPARATOR "\\n") AS e_ids,
+       GROUP_CONCAT(e.path ORDER BY e.id SEPARATOR "\\n") AS e_paths,
+       GROUP_CONCAT(e.user_id ORDER BY e.id SEPARATOR "\\n") AS e_uids
+FROM Rapsys\TreeBundle\Entity\Album AS a
+JOIN Rapsys\TreeBundle\Entity\Element AS e ON (e.album_id = a.id)
+WHERE a.id = :id
+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('created', 'created', 'datetime')
+                       ->addScalarResult('updated', 'updated', 'datetime')
+                       ->addScalarResult('modified', 'modified', 'datetime')
+                       ->addScalarResult('e_ids', 'e_ids', 'string')
+                       ->addScalarResult('e_paths', 'e_paths', 'string')
+                       ->addScalarResult('e_uids', 'e_uids', 'string')
+                       ->addIndexByScalar('id');
+
+               //Get result
+               $result = $this->_em
+                       ->createNativeQuery($req, $rsm)
+                       ->setParameter('id', $id)
+                       ->getOneOrNullResult();
+
+               //Check result
+               if (
+                       //Without result
+                       $result === null ||
+                       //Without realpath
+                       !($realpath = realpath($result['path'].($path?DIRECTORY_SEPARATOR.$path:''))) ||
+                       //Realpath not matching element path
+                       $result['path'] !== substr($realpath, 0, strlen($result['path']))
+               ) {
+                       //Return null
+                       return null;
+               }
+
+               //Set breadcrumbs
+               $breadcrumbs = [
+                       //Add album
+                       [
+                               'name' => $name = ucfirst($slug = $result['slug']),
+                               'link' => $link = $this->router->generate('rapsystree_album', [ 'id' => $result['id'], 'path' => '', 'slug' => $slug ])
+                       ]
+               ];
+
+               //Set base
+               $base = '';
+
+               //Iterate on intermediate breadcrumbs
+               foreach(array_slice(explode('/', substr($realpath, strlen($result['path']))), 1) as $value) {
+                       //Add breadcrumb
+                       $breadcrumbs[] = [
+                               'name' => '/ '.$value,
+                               'link' => $this->router->generate('rapsystree_album', [ 'id' => $result['id'], 'path' => ($base .= ($base == '' ? '' : '/').$value), 'slug' => $slug ])
+                       ];
+               }
+
+               //Set directories
+               $directories = [];
+
+               //Set files
+               $files = [];
+
+               //Set file
+               $file = [];
+
+               //With directory
+               if (is_dir($realpath)) {
+                       //Iterate on directory
+                       foreach(array_diff(scandir($realpath), ['.', '..']) as $item) {
+                               //TODO: exclude .svn, .git, .passwd, .*.un~, etc... (if not already protected by haproxy/apache)
+
+                               //Check item
+                               if (
+                                       //Without item realpath
+                                       !($itempath = realpath($realpath.DIRECTORY_SEPARATOR.$item)) ||
+                                       //Item realpath not matching album path
+                                       $result['path'] !== substr($itempath, 0, strlen($result['path']))
+                               ) {
+                                       //Skip
+                                       continue;
+                               }
+
+                               //With directory
+                               if (is_dir($itempath)) {
+                                       //Append directory
+                                       $directories[$item.'/'] = $this->router->generate('rapsystree_album', [ 'id' => $result['id'], 'path' => ($path ? $path.'/' : '').$item, 'slug' => $slug ]);
+                               //With file
+                               } elseif (is_file($itempath)) {
+                                       //Get file infos
+                                       $fileinfos = $this->file->infos($itempath);
+
+                                       //Append file
+                                       $files[$fileinfos['name']] = [
+                                               //Set link
+                                               'link' => $this->router->generate('rapsystree_album', [ 'id' => $result['id'], 'path' => ($path ? $path.'/' : '').$item, 'slug' => $slug ])
+                                       ]+$fileinfos;
+                               //With unknown type
+                               } else {
+                                       //Throw 404
+                                       throw new \Exception('Unknown element item type');
+                               }
+                       }
+               //With file
+               } elseif (is_file($realpath)) {
+                       //Get file infos
+                       $fileinfos = $this->file->infos($realpath);
+
+                       //Append file
+                       $file = [
+                               //Set link
+                               'link' => $this->router->generate('rapsystree_album', [ 'id' => $result['id'], 'path' => $path, 'slug' => $slug ])
+                       ]+$fileinfos;
+               //With unknown type
+               } else {
+                       //Throw 404
+                       throw new \Exception('Unknown element type');
+               }
+
+               //Set album
+               $album = [
+                       'id' => $result['id'],
+                       'name' => $name,
+                       'path' => $result['path'],
+                       'slug' => $slug,
+                       'created' => $result['created'],
+                       'updated' => $result['updated'],
+                       'modified' => $result['modified'],
+                       'link' => $link,
+                       'elements' => [],
+                       'breadcrumbs' => $breadcrumbs,
+                       'directories' => $directories,
+                       'files' => $files,
+                       'file' => $file
+               ];
+
+               //Explode element ids
+               $result['e_ids'] = explode("\n", $result['e_ids']);
+
+               //Explode element paths
+               $result['e_paths'] = explode("\n", $result['e_paths']);
+
+               //Explode element uids
+               $result['e_uids'] = explode("\n", $result['e_uids']);
+
+               //Iterate on elements
+               foreach($result['e_ids'] as $e => $eid) {
+                       //Append element
+                       $album['elements'][$eid] = [
+                               'id' => $eid,
+                               'name' => trim(str_replace('/', ' / ', '/'.($epath = $result['e_paths'][$e]))),
+                               'path' => $epath,
+                               'uid' => $result['e_uids'][$e],
+                               'link' => $this->router->generate('rapsystree_album', ['id' => $result['id'], 'path' => $epath, 'slug' => $slug]),
+                       ];
+               }
+
+               //Return album
+               return $album;
+       }
 }
 }