Management controllers: move table queries into models

This commit is contained in:
2025-01-08 17:17:53 +01:00
parent 2d2ef38422
commit cc0ff71ef7
10 changed files with 144 additions and 156 deletions

View File

@@ -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;
}
}