Files
fitme3070.pt/site/agendar.php
T
andreandClaude Opus 4.8 e68d357177 Initial import of fitme3070.pt platform
Plain-PHP fleet platform with id.andmore.pt SSO. Wires shared/platform-tools
as a submodule (platform_update, backups, git/migration tools) like the other
platforms.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-12 10:44:58 +01:00

80 lines
3.0 KiB
PHP

<?php
// POST handler for the "aula experimental" (trial class) modal on the homepage.
// Builds a booking-request email and relays it to id.andmore.pt's centralized
// mail service via the enqueue_email()/flush_message() façade, then redirects
// back to #contactos with an ?agendar=ok|erro flag the homepage renders.
//
// Reached via .htaccess: ^home/agendar/?$ → site/agendar.php
require_once __DIR__ . '/../config/db.php';
require_once __DIR__ . '/../config/functions.php';
// Only POST is meaningful here; anything else just goes home.
if (($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'POST') {
header('Location: ' . base_url('home'));
exit;
}
csrf_check(); // 403s and exits on a bad/missing token
function agendar_redirect(string $status): void {
header('Location: ' . base_url('home?agendar=' . $status . '#contactos'));
exit;
}
$nome = trim((string) ($_POST['nome'] ?? ''));
$email = trim((string) ($_POST['email'] ?? ''));
$telefone = trim((string) ($_POST['telefone'] ?? ''));
$data = trim((string) ($_POST['data'] ?? ''));
$periodo = trim((string) ($_POST['periodo'] ?? ''));
$mensagem = trim((string) ($_POST['mensagem'] ?? ''));
if ($nome === '' || $telefone === '' || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
agendar_redirect('erro');
}
// Pretty-print the preferred date if it parses; otherwise leave it blank.
$data_fmt = '';
if ($data !== '') {
$d = DateTime::createFromFormat('Y-m-d', $data);
$data_fmt = ($d && $d->format('Y-m-d') === $data) ? $d->format('d/m/Y') : '';
}
$info = site_info($pdo);
$to = $info['email'] !== '' ? $info['email'] : EMAIL_FROM;
$e = static fn($v) => htmlspecialchars((string) $v, ENT_QUOTES, 'UTF-8');
$rows = [
'Nome' => $nome,
'Telemóvel' => $telefone,
'E-mail' => $email,
'Dia preferido' => $data_fmt,
'Período preferido'=> $periodo,
'Mensagem' => $mensagem,
];
$body = '<h2 style="margin:0 0 16px;">Novo pedido de aula experimental</h2><table cellpadding="6" style="border-collapse:collapse;font-family:Arial,sans-serif;font-size:14px;">';
foreach ($rows as $label => $value) {
if (trim((string) $value) === '') continue;
$body .= '<tr><td style="font-weight:bold;vertical-align:top;">' . $e($label)
. ':</td><td>' . nl2br($e($value)) . '</td></tr>';
}
$body .= '</table><p style="font-family:Arial,sans-serif;font-size:13px;color:#666;margin-top:18px;">'
. 'Pedido enviado através do formulário do site FitMe 3070. Responde diretamente a '
. $e($email) . ' ou liga para ' . $e($telefone) . '.</p>';
$alt = "Novo pedido de aula experimental\n\n";
foreach ($rows as $label => $value) {
if (trim((string) $value) === '') continue;
$alt .= "$label: $value\n";
}
$id = enqueue_email([
'to_address' => $to,
'to_name' => $info['name'] !== '' ? $info['name'] : 'FitMe 3070',
'subject' => 'Pedido de aula experimental — ' . $nome,
'body' => $body,
'alt_body' => $alt,
'context_type' => 'fitme_trial',
]);
agendar_redirect(flush_message($id) ? 'ok' : 'erro');