2018-05-27 23:24:16 +00:00
|
|
|
<?php
|
|
|
|
use Misuzu\Database;
|
|
|
|
|
|
|
|
require_once __DIR__ . '/../misuzu.php';
|
|
|
|
|
|
|
|
$usersOffset = max((int)($_GET['o'] ?? 0), 0);
|
|
|
|
$usersTake = max(min((int)($_GET['t'] ?? 15), 30), 6);
|
|
|
|
|
|
|
|
$roleId = (int)($_GET['r'] ?? MSZ_ROLE_MAIN);
|
|
|
|
$orderBy = strtolower($_GET['ss'] ?? '');
|
2018-07-11 20:03:43 +00:00
|
|
|
$orderDir = strtolower($_GET['sd'] ?? '');
|
2018-05-27 23:24:16 +00:00
|
|
|
|
|
|
|
$orderDirs = [
|
2018-07-11 20:03:43 +00:00
|
|
|
'asc' => 'Ascending',
|
|
|
|
'desc' => 'Descending',
|
2018-05-27 23:24:16 +00:00
|
|
|
];
|
|
|
|
|
2018-07-11 20:03:43 +00:00
|
|
|
$defaultOrder = 'last-online';
|
2018-05-27 23:24:16 +00:00
|
|
|
$orderFields = [
|
|
|
|
'id' => [
|
|
|
|
'column' => 'user_id',
|
2018-07-11 20:03:43 +00:00
|
|
|
'default-dir' => 'asc',
|
2018-05-27 23:24:16 +00:00
|
|
|
'title' => 'User ID',
|
|
|
|
],
|
|
|
|
'name' => [
|
|
|
|
'column' => 'username',
|
2018-07-11 20:03:43 +00:00
|
|
|
'default-dir' => 'asc',
|
2018-05-27 23:24:16 +00:00
|
|
|
'title' => 'Username',
|
|
|
|
],
|
|
|
|
'country' => [
|
|
|
|
'column' => 'user_country',
|
2018-07-11 20:03:43 +00:00
|
|
|
'default-dir' => 'asc',
|
2018-05-27 23:24:16 +00:00
|
|
|
'title' => 'Country',
|
|
|
|
],
|
|
|
|
'registered' => [
|
|
|
|
'column' => 'created_at',
|
2018-07-11 20:03:43 +00:00
|
|
|
'default-dir' => 'desc',
|
2018-05-27 23:24:16 +00:00
|
|
|
'title' => 'Registration Date',
|
|
|
|
],
|
|
|
|
'last-online' => [
|
|
|
|
'column' => 'last_seen',
|
2018-07-11 20:03:43 +00:00
|
|
|
'default-dir' => 'desc',
|
2018-05-27 23:24:16 +00:00
|
|
|
'title' => 'Last Online',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
if (empty($orderBy)) {
|
2018-07-11 20:03:43 +00:00
|
|
|
$orderBy = $defaultOrder;
|
2018-05-27 23:24:16 +00:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$tpl = $app->getTemplating();
|
|
|
|
|
2018-07-15 22:59:14 +00:00
|
|
|
$getRole = Database::prepare('
|
2018-05-27 23:24:16 +00:00
|
|
|
SELECT
|
|
|
|
`role_id`, `role_name`, `role_colour`, `role_description`, `created_at`,
|
|
|
|
(
|
|
|
|
SELECT COUNT(`user_id`)
|
|
|
|
FROM `msz_user_roles`
|
|
|
|
WHERE `role_id` = r.`role_id`
|
|
|
|
) as `role_user_count`
|
|
|
|
FROM `msz_roles` as r
|
|
|
|
WHERE `role_id` = :role_id
|
|
|
|
');
|
|
|
|
$getRole->bindValue('role_id', $roleId);
|
2018-07-15 22:59:14 +00:00
|
|
|
$role = $getRole->execute() ? $getRole->fetch(PDO::FETCH_ASSOC) : [];
|
2018-05-27 23:24:16 +00:00
|
|
|
|
|
|
|
if (!$role) {
|
|
|
|
echo render_error(404);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-15 22:59:14 +00:00
|
|
|
$roles = Database::query('
|
2018-05-27 23:24:16 +00:00
|
|
|
SELECT `role_id`, `role_name`, `role_colour`
|
|
|
|
FROM `msz_roles`
|
|
|
|
WHERE `role_secret` = 0
|
|
|
|
ORDER BY `role_id`
|
2018-07-15 22:59:14 +00:00
|
|
|
')->fetchAll(PDO::FETCH_ASSOC);
|
2018-05-27 23:24:16 +00:00
|
|
|
|
2018-07-15 22:59:14 +00:00
|
|
|
$getUsers = Database::prepare("
|
2018-05-27 23:24:16 +00:00
|
|
|
SELECT
|
|
|
|
u.`user_id`, u.`username`, u.`user_country`,
|
|
|
|
u.`created_at` as `user_joined`, u.`last_seen` as `user_last_seen`,
|
|
|
|
COALESCE(u.`user_title`, r.`role_title`) as `user_title`,
|
2018-07-18 16:01:17 +00:00
|
|
|
COALESCE(u.`user_colour`, r.`role_colour`) as `user_colour`,
|
2018-05-27 23:24:16 +00:00
|
|
|
(
|
|
|
|
SELECT COUNT(`topic_id`)
|
|
|
|
FROM `msz_forum_topics`
|
|
|
|
WHERE `user_id` = u.`user_id`
|
|
|
|
AND `topic_deleted` IS NULL
|
|
|
|
) as `user_topic_count`,
|
|
|
|
(
|
|
|
|
SELECT COUNT(`post_id`)
|
|
|
|
FROM `msz_forum_posts`
|
|
|
|
WHERE `user_id` = u.`user_id`
|
|
|
|
AND `post_deleted` IS NULL
|
|
|
|
) as `user_post_count`
|
|
|
|
FROM `msz_users` as u
|
|
|
|
LEFT JOIN `msz_roles` as r
|
|
|
|
ON r.`role_id` = u.`display_role`
|
|
|
|
LEFT JOIN `msz_user_roles` as ur
|
|
|
|
ON ur.`user_id` = u.`user_id`
|
|
|
|
WHERE ur.`role_id` = :role_id
|
|
|
|
ORDER BY u.`{$orderFields[$orderBy]['column']}` {$orderDir}
|
|
|
|
LIMIT :offset, :take
|
|
|
|
");
|
|
|
|
$getUsers->bindValue('role_id', $role['role_id']);
|
|
|
|
$getUsers->bindValue('offset', $usersOffset);
|
|
|
|
$getUsers->bindValue('take', $usersTake);
|
2018-07-15 22:59:14 +00:00
|
|
|
$users = $getUsers->execute() ? $getUsers->fetchAll(PDO::FETCH_ASSOC) : [];
|
2018-05-27 23:24:16 +00:00
|
|
|
|
|
|
|
echo $tpl->render('user.listing', [
|
|
|
|
'roles' => $roles,
|
|
|
|
'role' => $role,
|
|
|
|
'users' => $users,
|
|
|
|
'order_fields' => $orderFields,
|
|
|
|
'order_directions' => $orderDirs,
|
|
|
|
'order_field' => $orderBy,
|
|
|
|
'order_direction' => $orderDir,
|
2018-07-11 20:03:43 +00:00
|
|
|
'order_default' => $defaultOrder,
|
2018-05-27 23:24:16 +00:00
|
|
|
'users_offset' => $usersOffset,
|
|
|
|
'users_take' => $usersTake,
|
|
|
|
]);
|