From 4684482d6719ba0b2b2c147597ff606daf705f52 Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Sat, 11 Mar 2023 17:28:21 +0100 Subject: [PATCH] ManageAlbums: move hierarchy logic to PhotoAlbum model --- controllers/ManageAlbums.php | 61 +---------------------------- models/PhotoAlbum.php | 76 ++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 60 deletions(-) create mode 100644 models/PhotoAlbum.php diff --git a/controllers/ManageAlbums.php b/controllers/ManageAlbums.php index 6f578b1..662714b 100644 --- a/controllers/ManageAlbums.php +++ b/controllers/ManageAlbums.php @@ -68,28 +68,7 @@ class ManageAlbums extends HTMLController if (!in_array($direction, ['up', 'down'])) $direction = 'up'; - $db = Registry::get('db'); - $res = $db->query(' - SELECT * - FROM tags - WHERE kind = {string:album} - ORDER BY id_parent, {raw:order}', - [ - 'order' => $order . ($direction == 'up' ? ' ASC' : ' DESC'), - 'album' => 'Album', - ]); - - $albums_by_parent = []; - while ($row = $db->fetch_assoc($res)) - { - if (!isset($albums_by_parent[$row['id_parent']])) - $albums_by_parent[$row['id_parent']] = []; - - $albums_by_parent[$row['id_parent']][] = $row + ['children' => []]; - } - - $albums = self::getChildrenRecursively(0, 0, $albums_by_parent); - $rows = self::flattenChildrenRecursively($albums); + $rows = PhotoAlbum::getHierarchy($order, $direction); return [ 'rows' => $rows, @@ -106,42 +85,4 @@ class ManageAlbums extends HTMLController parent::__construct('Album management - Page ' . $table->getCurrentPage() .' - ' . SITE_TITLE); $this->page->adopt(new TabularData($table)); } - - private static function getChildrenRecursively($id_parent, $level, &$albums_by_parent) - { - $children = []; - if (!isset($albums_by_parent[$id_parent])) - return $children; - - foreach ($albums_by_parent[$id_parent] as $child) - { - if (isset($albums_by_parent[$child['id_tag']])) - $child['children'] = self::getChildrenRecursively($child['id_tag'], $level + 1, $albums_by_parent); - - $child['tag'] = ($level ? str_repeat('—', $level * 2) . ' ' : '') . $child['tag']; - $children[] = $child; - } - - return $children; - } - - private static function flattenChildrenRecursively($albums) - { - if (empty($albums)) - return []; - - $rows = []; - foreach ($albums as $album) - { - $rows[] = array_intersect_key($album, array_flip(['id_tag', 'tag', 'slug', 'count'])); - if (!empty($album['children'])) - { - $children = self::flattenChildrenRecursively($album['children']); - foreach ($children as $child) - $rows[] = array_intersect_key($child, array_flip(['id_tag', 'tag', 'slug', 'count'])); - } - } - - return $rows; - } } diff --git a/models/PhotoAlbum.php b/models/PhotoAlbum.php new file mode 100644 index 0000000..efdc41f --- /dev/null +++ b/models/PhotoAlbum.php @@ -0,0 +1,76 @@ +query(' + SELECT * + FROM tags + WHERE kind = {string:album} + ORDER BY id_parent, {raw:order}', + [ + 'order' => $order . ($direction == 'up' ? ' ASC' : ' DESC'), + 'album' => 'Album', + ]); + + $albums_by_parent = []; + while ($row = $db->fetch_assoc($res)) + { + if (!isset($albums_by_parent[$row['id_parent']])) + $albums_by_parent[$row['id_parent']] = []; + + $albums_by_parent[$row['id_parent']][] = $row + ['children' => []]; + } + + $albums = self::getChildrenRecursively(0, 0, $albums_by_parent); + $rows = self::flattenChildrenRecursively($albums); + + return $rows; + } + + private static function getChildrenRecursively($id_parent, $level, &$albums_by_parent) + { + $children = []; + if (!isset($albums_by_parent[$id_parent])) + return $children; + + foreach ($albums_by_parent[$id_parent] as $child) + { + if (isset($albums_by_parent[$child['id_tag']])) + $child['children'] = self::getChildrenRecursively($child['id_tag'], $level + 1, $albums_by_parent); + + $child['tag'] = ($level ? str_repeat('—', $level * 2) . ' ' : '') . $child['tag']; + $children[] = $child; + } + + return $children; + } + + private static function flattenChildrenRecursively($albums) + { + if (empty($albums)) + return []; + + $rows = []; + foreach ($albums as $album) + { + $rows[] = array_intersect_key($album, array_flip(['id_tag', 'tag', 'slug', 'count'])); + if (!empty($album['children'])) + { + $children = self::flattenChildrenRecursively($album['children']); + foreach ($children as $child) + $rows[] = array_intersect_key($child, array_flip(['id_tag', 'tag', 'slug', 'count'])); + } + } + + return $rows; + } +}