refactor: move raw SQL out of views into config/functions_db.php

Auditoria 6.3 apontava dashboard.php (2×), user/index.php e
user/profile.php como as únicas vistas com SQL direto, quebrando a
convenção da frota (config/functions_*.php como camada de models). As
quatro passam a chamar db_users_count_clients(), db_users_count_team(),
db_users_list() e db_user_update_profile() — mesmo SQL, mesmos
parâmetros, só movido para onde o resto do código já vai buscar dados.

Nota: views/admin/gyms/{create,edit}.php e views/admin/reviews/index.php
também têm SQL direto, mas não estavam no âmbito dos "3 vistas" da
auditoria (entraram depois, no trabalho de multi-ginásio) — por tratar
numa próxima passagem.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
andreandClaude Sonnet 5 committed 2026-09-25 17:06:00 +01:00
1 parent 7037077b48
commit df1aad8769
4 files changed
+37 -11

No files matched your search

+30
View File
@@ -38,6 +38,36 @@ function db_get_active_admins(PDO $pdo): array {
)->fetchAll(PDO::FETCH_ASSOC);
}
// Every user, in role order — the listing at views/user/index.php.
function db_users_list(PDO $pdo): array {
return $pdo->query(
"SELECT id, name, email, phone, role, is_active, password_hash, last_login_at, last_login_ua
FROM users ORDER BY FIELD(role,'sysadmin','admin','staff','client'), name"
)->fetchAll(PDO::FETCH_ASSOC);
}
// Dashboard widget counts (views/admin/dashboard.php).
function db_users_count_clients(PDO $pdo): int {
return (int) $pdo->query("SELECT COUNT(*) FROM users WHERE role = 'client'")->fetchColumn();
}
function db_users_count_team(PDO $pdo): int {
return (int) $pdo->query("SELECT COUNT(*) FROM users WHERE role IN ('admin','staff')")->fetchColumn();
}
// Profile self-edit (views/user/profile.php) — the contact-details half of
// the form; the password half already goes through auth_set_password().
function db_user_update_profile(PDO $pdo, int $userId, array $data): void {
$pdo->prepare(
"UPDATE users SET phone=?, address=?, city=?, postal_code=?,
email_opt_out=?, sms_opt_out=?, marketing_consent=?
WHERE id=?"
)->execute([
$data['phone'], $data['address'], $data['city'], $data['postal_code'],
$data['email_opt_out'], $data['sms_opt_out'], $data['marketing_consent'], $userId,
]);
}
// PT-PT label for the role ladder.
function role_label(string $role): string {
return [