Files
breadcrumb-the-shire/pages/admin/stats/index().php
2026-02-11 19:28:12 +01:00

68 lines
3.5 KiB
PHP

<?php
use MintyPHP\Buffer;
use MintyPHP\Support\Guard;
use MintyPHP\Service\Access\PermissionService;
use MintyPHP\DB;
Guard::requirePermissionOrForbidden(PermissionService::STATS_VIEW);
$activeUserCount = (int) (DB::selectValue('select count(*) from users where active = 1') ?? 0);
$inactiveUserCount = (int) (DB::selectValue('select count(*) from users where active = 0') ?? 0);
$activeTenantCount = (int) (DB::selectValue("select count(*) from tenants where status = 'active'") ?? 0);
$inactiveTenantCount = (int) (DB::selectValue("select count(*) from tenants where status = 'inactive'") ?? 0);
$activeDepartmentCount = (int) (DB::selectValue('select count(*) from departments where active = 1') ?? 0);
$inactiveDepartmentCount = (int) (DB::selectValue('select count(*) from departments where active = 0') ?? 0);
$activeRoleCount = (int) (DB::selectValue('select count(*) from roles where active = 1') ?? 0);
$inactiveRoleCount = (int) (DB::selectValue('select count(*) from roles where active = 0') ?? 0);
$rolesWithoutPermissionsCount = (int) (DB::selectValue(
'select count(*) from roles r left join role_permissions rp on rp.role_id = r.id where r.active = 1 and rp.role_id is null'
) ?? 0);
$permissionsWithoutRolesCount = (int) (DB::selectValue(
'select count(*) from permissions p left join role_permissions rp on rp.permission_id = p.id where p.active = 1 and rp.permission_id is null'
) ?? 0);
$usersWithoutRolesCount = (int) (DB::selectValue(
'select count(*) from users u left join user_roles ur on ur.user_id = u.id where u.active = 1 and ur.user_id is null'
) ?? 0);
$usersWithoutTenantsCount = (int) (DB::selectValue(
'select count(*) from users u left join user_tenants ut on ut.user_id = u.id where u.active = 1 and ut.user_id is null'
) ?? 0);
$departmentsWithoutTenantsCount = (int) (DB::selectValue(
'select count(*) from departments d left join tenant_departments td on td.department_id = d.id where d.active = 1 and td.department_id is null'
) ?? 0);
$usersUnverifiedCount = (int) (DB::selectValue(
'select count(*) from users where active = 1 and email_verified_at is null'
) ?? 0);
$usersNeverLoggedCount = (int) (DB::selectValue(
'select count(*) from users where active = 1 and last_login_at is null'
) ?? 0);
$recentLogins = DB::select(
'select id, uuid, first_name, last_name, email, last_login_at from users where active = 1 and last_login_at is not null order by last_login_at desc limit 10'
);
$recentLogins = is_array($recentLogins) ? array_map(
static fn ($row) => $row['users'] ?? $row,
$recentLogins
) : [];
$neverLoggedUsers = DB::select(
'select id, uuid, first_name, last_name, email, created from users where active = 1 and last_login_at is null order by created desc limit 10'
);
$neverLoggedUsers = is_array($neverLoggedUsers) ? array_map(
static fn ($row) => $row['users'] ?? $row,
$neverLoggedUsers
) : [];
$mailLogFailedCount = (int) (DB::selectValue("select count(*) from mail_log where status = 'failed'") ?? 0);
$mailLogSentCount = (int) (DB::selectValue("select count(*) from mail_log where status = 'sent'") ?? 0);
$mailLogLastSentAt = (string) (DB::selectValue("select max(sent_at) from mail_log where status = 'sent'") ?? '');
$mailLogRecentFailed = DB::select(
"select id, to_email, subject, error_message, created_at from mail_log where status = 'failed' order by created_at desc limit 5"
);
$mailLogRecentFailed = is_array($mailLogRecentFailed) ? array_map(
static fn ($row) => $row['mail_log'] ?? $row,
$mailLogRecentFailed
) : [];
Buffer::set('title', t('Statistics'));