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:
1 parent
7037077b48
commit
df1aad8769
4 files changed
+37
-11
No files matched your search
@@ -38,6 +38,36 @@ function db_get_active_admins(PDO $pdo): array {
|
|||||||
)->fetchAll(PDO::FETCH_ASSOC);
|
)->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.
|
// PT-PT label for the role ladder.
|
||||||
function role_label(string $role): string {
|
function role_label(string $role): string {
|
||||||
return [
|
return [
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ $user = require_admin();
|
|||||||
|
|
||||||
// Counts for the dashboard widgets.
|
// Counts for the dashboard widgets.
|
||||||
$counts = [
|
$counts = [
|
||||||
'clientes' => (int) $pdo->query("SELECT COUNT(*) FROM users WHERE role = 'client'")->fetchColumn(),
|
'clientes' => db_users_count_clients($pdo),
|
||||||
'equipa' => (int) $pdo->query("SELECT COUNT(*) FROM users WHERE role IN ('admin','staff')")->fetchColumn(),
|
'equipa' => db_users_count_team($pdo),
|
||||||
'leads_novos' => db_membership_leads_count_new($pdo),
|
'leads_novos' => db_membership_leads_count_new($pdo),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
@@ -3,10 +3,7 @@ require __DIR__ . '/../../config/db.php';
|
|||||||
require __DIR__ . '/../../config/functions.php';
|
require __DIR__ . '/../../config/functions.php';
|
||||||
$user = require_admin();
|
$user = require_admin();
|
||||||
|
|
||||||
$users = $pdo->query(
|
$users = db_users_list($pdo);
|
||||||
"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);
|
|
||||||
|
|
||||||
include __DIR__ . '/../../includes/header.php';
|
include __DIR__ . '/../../includes/header.php';
|
||||||
?>
|
?>
|
||||||
|
|||||||
@@ -42,11 +42,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
$sms_opt = isset($_POST['sms_opt_out']) ? 1 : 0;
|
$sms_opt = isset($_POST['sms_opt_out']) ? 1 : 0;
|
||||||
$marketing = isset($_POST['marketing_consent']) ? 1 : 0;
|
$marketing = isset($_POST['marketing_consent']) ? 1 : 0;
|
||||||
$before = db_get_user($pdo, (int) $user['id']) ?? [];
|
$before = db_get_user($pdo, (int) $user['id']) ?? [];
|
||||||
$pdo->prepare(
|
db_user_update_profile($pdo, (int) $user['id'], [
|
||||||
"UPDATE users SET phone=?, address=?, city=?, postal_code=?,
|
'phone' => $phone, 'address' => $address, 'city' => $city, 'postal_code' => $postal_code,
|
||||||
email_opt_out=?, sms_opt_out=?, marketing_consent=?
|
'email_opt_out' => $email_opt, 'sms_opt_out' => $sms_opt, 'marketing_consent' => $marketing,
|
||||||
WHERE id=?"
|
]);
|
||||||
)->execute([$phone, $address, $city, $postal_code, $email_opt, $sms_opt, $marketing, $user['id']]);
|
|
||||||
audit('user.profile_update', [
|
audit('user.profile_update', [
|
||||||
'target_type' => 'user', 'target_id' => (int) $user['id'], 'target_label' => (string) $user['name'],
|
'target_type' => 'user', 'target_id' => (int) $user['id'], 'target_label' => (string) $user['name'],
|
||||||
'changes' => platform_audit_diff(
|
'changes' => platform_audit_diff(
|
||||||
|
|||||||
Reference in new issue
Block a user