Management controllers: move table queries into models
This commit is contained in:
parent
2d2ef38422
commit
cc0ff71ef7
@ -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);
|
||||
}
|
||||
];
|
||||
|
||||
|
@ -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',
|
||||
];
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
];
|
||||
|
||||
|
@ -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',
|
||||
];
|
||||
|
||||
|
@ -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))
|
||||
|
@ -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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@ -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...
|
||||
|
@ -1,76 +0,0 @@
|
||||
<?php
|
||||
/*****************************************************************************
|
||||
* PhotoAlbum.php
|
||||
* Contains key class PhotoAlbum.
|
||||
*
|
||||
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
|
||||
*****************************************************************************/
|
||||
|
||||
class PhotoAlbum extends Tag
|
||||
{
|
||||
public static function getHierarchy($order, $direction)
|
||||
{
|
||||
$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);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
100
models/Tag.php
100
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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user