diff --git a/controllers/ManageAlbums.php b/controllers/ManageAlbums.php index aa48f20..0300409 100644 --- a/controllers/ManageAlbums.php +++ b/controllers/ManageAlbums.php @@ -65,11 +65,10 @@ class ManageAlbums extends HTMLController 'index_class' => 'col-md-6', 'base_url' => BASEURL . '/managealbums/', 'get_data' => function($offset, $limit, $order, $direction) { - assert(in_array($order, ['id_tag', 'tag', 'slug', 'count'])); - return PhotoAlbum::getHierarchy($order, $direction); + return Tag::getOffset($offset, $limit, $order, $direction, true); }, 'get_count' => function() { - return 9999; + return Tag::getCount(false, 'Album', true); } ]; diff --git a/controllers/ManageAssets.php b/controllers/ManageAssets.php index 88d9c46..9054450 100644 --- a/controllers/ManageAssets.php +++ b/controllers/ManageAssets.php @@ -131,23 +131,7 @@ class ManageAssets extends HTMLController 'items_per_page' => 30, 'index_class' => 'col-md-6', 'base_url' => BASEURL . '/manageassets/', - 'get_data' => function($offset, $limit, $order, $direction) { - assert(in_array($order, ['id_asset', 'id_user_uploaded', 'title', 'subdir', 'filename'])); - - return Registry::get('db')->queryAssocs(' - SELECT a.id_asset, a.subdir, a.filename, - a.image_width, a.image_height, a.mimetype, - u.id_user, u.first_name, u.surname - FROM assets AS a - LEFT JOIN users AS u ON a.id_user_uploaded = u.id_user - ORDER BY {raw:order} - LIMIT {int:offset}, {int:limit}', - [ - 'order' => $order . ($direction == 'up' ? ' ASC' : ' DESC'), - 'offset' => $offset, - 'limit' => $limit, - ]); - }, + 'get_data' => 'Asset::getOffset', 'get_count' => 'Asset::getCount', ]; diff --git a/controllers/ManageErrors.php b/controllers/ManageErrors.php index a3c7628..7dcda24 100644 --- a/controllers/ManageErrors.php +++ b/controllers/ManageErrors.php @@ -105,20 +105,7 @@ class ManageErrors extends HTMLController 'index_class' => 'col-md-6', 'base_url' => BASEURL . '/manageerrors/', 'get_count' => 'ErrorLog::getCount', - 'get_data' => function($offset, $limit, $order, $direction) { - assert(in_array($order, ['id_entry', 'file', 'line', 'time', 'ipaddress', 'id_user'])); - - return Registry::get('db')->queryAssocs(' - SELECT * - FROM log_errors - ORDER BY {raw:order} - LIMIT {int:offset}, {int:limit}', - [ - 'order' => $order . ($direction === 'up' ? ' ASC' : ' DESC'), - 'offset' => $offset, - 'limit' => $limit, - ]); - }, + 'get_data' => 'ErrorLog::getOffset', ]; $error_log = new GenericTable($options); diff --git a/controllers/ManageTags.php b/controllers/ManageTags.php index 34431c9..cca4140 100644 --- a/controllers/ManageTags.php +++ b/controllers/ManageTags.php @@ -81,28 +81,10 @@ class ManageTags extends HTMLController 'index_class' => 'col-md-6', 'base_url' => BASEURL . '/managetags/', 'get_data' => function($offset, $limit, $order, $direction) { - assert(in_array($order, ['id_tag', 'tag', 'slug', 'count'])); - - return Registry::get('db')->queryAssocs(' - SELECT t.*, u.id_user, u.first_name, u.surname - FROM tags AS t - LEFT JOIN users AS u ON t.id_user_owner = u.id_user - WHERE kind != {string:album} - ORDER BY {raw:order} - LIMIT {int:offset}, {int:limit}', - [ - 'order' => $order . ($direction == 'up' ? ' ASC' : ' DESC'), - 'offset' => $offset, - 'limit' => $limit, - 'album' => 'Album', - ]); + return Tag::getOffset($offset, $limit, $order, $direction, false); }, 'get_count' => function() { - return Registry::get('db')->queryValue(' - SELECT COUNT(*) - FROM tags - WHERE kind != {string:album}', - ['album' => 'Album']); + return Tag::getCount(false, null, false); } ]; diff --git a/controllers/ManageUsers.php b/controllers/ManageUsers.php index 7f43b88..a3c9a81 100644 --- a/controllers/ManageUsers.php +++ b/controllers/ManageUsers.php @@ -100,20 +100,7 @@ class ManageUsers extends HTMLController 'items_per_page' => 30, 'index_class' => 'col-md-6', 'base_url' => BASEURL . '/manageusers/', - 'get_data' => function($offset, $limit, $order, $direction) { - assert(in_array($order, ['id_user', 'surname', 'first_name', 'slug', 'emailaddress', 'last_action_time', 'ip_address', 'is_admin'])); - - return Registry::get('db')->queryAssocs(' - SELECT * - FROM users - ORDER BY {raw:order} - LIMIT {int:offset}, {int:limit}', - [ - 'order' => $order . ($direction == 'up' ? ' ASC' : ' DESC'), - 'offset' => $offset, - 'limit' => $limit, - ]); - }, + 'get_data' => 'Member::getOffset', 'get_count' => 'Member::getCount', ]; diff --git a/models/Asset.php b/models/Asset.php index edadd5f..b3803bf 100644 --- a/models/Asset.php +++ b/models/Asset.php @@ -680,6 +680,23 @@ class Asset FROM assets'); } + public static function getOffset($offset, $limit, $order, $direction) + { + return Registry::get('db')->queryAssocs(' + SELECT a.id_asset, a.subdir, a.filename, + a.image_width, a.image_height, a.mimetype, + u.id_user, u.first_name, u.surname + FROM assets AS a + LEFT JOIN users AS u ON a.id_user_uploaded = u.id_user + ORDER BY {raw:order} + LIMIT {int:offset}, {int:limit}', + [ + 'order' => $order . ($direction == 'up' ? ' ASC' : ' DESC'), + 'offset' => $offset, + 'limit' => $limit, + ]); + } + public function save() { if (empty($this->id_asset)) diff --git a/models/ErrorLog.php b/models/ErrorLog.php index 979073b..f406ae3 100644 --- a/models/ErrorLog.php +++ b/models/ErrorLog.php @@ -33,4 +33,20 @@ class ErrorLog SELECT COUNT(*) FROM log_errors'); } + + public static function getOffset($offset, $limit, $order, $direction) + { + assert(in_array($order, ['id_entry', 'file', 'line', 'time', 'ipaddress', 'id_user'])); + + return Registry::get('db')->queryAssocs(' + SELECT * + FROM log_errors + ORDER BY {raw:order} + LIMIT {int:offset}, {int:limit}', + [ + 'order' => $order . ($direction === 'up' ? ' ASC' : ' DESC'), + 'offset' => $offset, + 'limit' => $limit, + ]); + } } diff --git a/models/Member.php b/models/Member.php index 84912a4..0bb2ef5 100644 --- a/models/Member.php +++ b/models/Member.php @@ -187,6 +187,22 @@ class Member extends User FROM users'); } + public static function getOffset($offset, $limit, $order, $direction) + { + assert(in_array($order, ['id_user', 'surname', 'first_name', 'slug', 'emailaddress', 'last_action_time', 'ip_address', 'is_admin'])); + + return Registry::get('db')->queryAssocs(' + SELECT * + FROM users + ORDER BY {raw:order} + LIMIT {int:offset}, {int:limit}', + [ + 'order' => $order . ($direction === 'up' ? ' ASC' : ' DESC'), + 'offset' => $offset, + 'limit' => $limit, + ]); + } + public function getProps() { // We should probably phase out the use of this function, or refactor the access levels of member properties... diff --git a/models/PhotoAlbum.php b/models/PhotoAlbum.php deleted file mode 100644 index efdc41f..0000000 --- a/models/PhotoAlbum.php +++ /dev/null @@ -1,76 +0,0 @@ -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; - } -} diff --git a/models/Tag.php b/models/Tag.php index 145db1e..d5f6db7 100644 --- a/models/Tag.php +++ b/models/Tag.php @@ -24,6 +24,11 @@ class Tag $this->$attribute = $value; } + public function __toString() + { + return $this->tag; + } + public static function fromId($id_tag, $return_format = 'object') { $db = Registry::get('db'); @@ -409,27 +414,98 @@ class Tag ['tags' => $tags]); } - public static function getCount($only_active = 1, $kind = '') + public static function getCount($only_used = true, $kind = '', $isAlbum = false) { $where = []; - if ($only_active) + if ($only_used) $where[] = 'count > 0'; - if (!empty($kind)) - $where[] = 'kind = {string:kind}'; + if (empty($kind)) + $kind = 'Album'; - if (!empty($where)) - $where = 'WHERE ' . implode(' AND ', $where); - else - $where = ''; + $where[] = 'kind {raw:operator} {string:kind}'; + $where = implode(' AND ', $where); return Registry::get('db')->queryValue(' SELECT COUNT(*) - FROM tags ' . $where, - ['kind' => $kind]); + FROM tags + WHERE ' . $where, + [ + 'kind' => $kind, + 'operator' => $isAlbum ? '=' : '!=', + ]); } - public function __toString() + public static function getOffset($offset, $limit, $order, $direction, $isAlbum = false) { - return $this->tag; + assert(in_array($order, ['id_tag', 'tag', 'slug', 'count'])); + + $db = Registry::get('db'); + $res = $db->query(' + SELECT t.*, u.id_user, u.first_name, u.surname + FROM tags AS t + LEFT JOIN users AS u ON t.id_user_owner = u.id_user + WHERE kind {raw:operator} {string:album} + ORDER BY id_parent, {raw:order} + LIMIT {int:offset}, {int:limit}', + [ + 'order' => $order . ($direction === 'up' ? ' ASC' : ' DESC'), + 'offset' => $offset, + 'limit' => $limit, + 'album' => 'Album', + 'operator' => $isAlbum ? '=' : '!=', + ]); + + $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) + { + static $headers_to_keep = ['id_tag', 'tag', 'slug', 'count', 'id_user', 'first_name', 'surname']; + $rows[] = array_intersect_key($album, array_flip($headers_to_keep)); + if (!empty($album['children'])) + { + $children = self::flattenChildrenRecursively($album['children']); + foreach ($children as $child) + $rows[] = array_intersect_key($child, array_flip($headers_to_keep)); + } + } + + return $rows; } }