misuzu/public-legacy/members.php

143 lines
3.9 KiB
PHP
Raw Permalink Normal View History

2022-09-13 13:14:49 +00:00
<?php
namespace Misuzu;
use RuntimeException;
2022-09-13 13:14:49 +00:00
use Misuzu\Users\User;
2023-07-27 23:26:05 +00:00
$roles = $msz->getRoles();
$roleId = filter_has_var(INPUT_GET, 'r') ? (string)filter_input(INPUT_GET, 'r') : null;
$orderBy = strtolower((string)filter_input(INPUT_GET, 'ss'));
$orderDir = strtolower((string)filter_input(INPUT_GET, 'sd'));
2022-09-13 13:14:49 +00:00
$orderDirs = [
'asc' => 'Ascending',
'desc' => 'Descending',
];
$defaultOrder = 'last-online';
$orderFields = [
'id' => [
'column' => 'u.`user_id`',
'default-dir' => 'asc',
'title' => 'User ID',
],
'name' => [
'column' => 'u.`username`',
'default-dir' => 'asc',
'title' => 'Username',
],
'country' => [
'column' => 'u.`user_country`',
'default-dir' => 'asc',
'title' => 'Country',
],
'registered' => [
'column' => 'u.`user_created`',
'default-dir' => 'desc',
'title' => 'Registration Date',
],
'last-online' => [
'column' => 'u.`user_active`',
'default-dir' => 'desc',
'title' => 'Last Online',
],
'forum-topics' => [
'column' => '`user_count_topics`',
'default-dir' => 'desc',
'title' => 'Forum Topics',
],
'forum-posts' => [
'column' => '`user_count_posts`',
'default-dir' => 'desc',
'title' => 'Forum Posts',
],
];
if(empty($orderBy)) {
$orderBy = $defaultOrder;
} elseif(!array_key_exists($orderBy, $orderFields)) {
echo render_error(400);
return;
}
if(empty($orderDir)) {
$orderDir = $orderFields[$orderBy]['default-dir'];
} elseif(!array_key_exists($orderDir, $orderDirs)) {
echo render_error(400);
return;
}
$canManageUsers = perms_check_user(MSZ_PERMS_USER, User::hasCurrent() ? User::getCurrent()->getId() : 0, MSZ_PERM_USER_MANAGE_USERS);
2023-07-27 23:26:05 +00:00
if($roleId === null) {
$roleInfo = $roles->getDefaultRole();
} else {
try {
$roleInfo = $roles->getRole($roleId);
} catch(RuntimeException $ex) {
echo render_error(404);
return;
}
2022-09-13 13:14:49 +00:00
}
2023-07-27 23:26:05 +00:00
$pagination = new Pagination($roles->countRoleUsers($roleInfo), 15);
$rolesAll = $roles->getRoles(hidden: false);
2022-09-13 13:14:49 +00:00
$getUsers = DB::prepare(sprintf(
'
SELECT
:current_user_id AS `current_user_id`,
u.`user_id`, u.`username`, u.`user_country`,
u.`user_created`, u.`user_active`, r.`role_id`,
COALESCE(u.`user_title`, r.`role_title`) AS `user_title`,
COALESCE(u.`user_colour`, r.`role_colour`) AS `user_colour`,
(
SELECT COUNT(`topic_id`)
FROM `msz_forum_topics`
WHERE `user_id` = u.`user_id`
AND `topic_deleted` IS NULL
) AS `user_count_topics`,
(
SELECT COUNT(`post_Id`)
FROM `msz_forum_posts`
WHERE `user_id` = u.`user_id`
AND `post_deleted` IS NULL
) AS `user_count_posts`
FROM `msz_users` AS u
LEFT JOIN `msz_roles` AS r
ON r.`role_id` = u.`display_role`
LEFT JOIN `msz_users_roles` AS ur
2022-09-13 13:14:49 +00:00
ON ur.`user_id` = u.`user_id`
WHERE ur.`role_id` = :role_id
%1$s
ORDER BY %2$s %3$s
LIMIT %4$d, %5$d
',
$canManageUsers ? '' : 'AND u.`user_deleted` IS NULL',
$orderFields[$orderBy]['column'],
$orderDir,
$pagination->getOffset(),
$pagination->getRange()
));
$getUsers->bind('role_id', $roleInfo->getId());
$getUsers->bind('current_user_id', User::hasCurrent() ? User::getCurrent()->getId() : 0);
$users = $getUsers->fetchAll();
if(empty($users))
http_response_code(404);
Template::render('user.listing', [
2023-07-27 23:26:05 +00:00
'roles' => $rolesAll,
2022-09-13 13:14:49 +00:00
'role' => $roleInfo,
'users' => $users,
'order_fields' => $orderFields,
'order_directions' => $orderDirs,
'order_field' => $orderBy,
'order_direction' => $orderDir,
'order_default' => $defaultOrder,
'can_manage_users' => $canManageUsers,
'users_pagination' => $pagination,
]);