[label, max length, input type]. Order is the form order. function info_fields(): array { return [ 'name' => ['Nome do ginásio', 200, 'text'], 'adress' => ['Morada', 200, 'text'], 'phone' => ['Telefone principal', 25, 'tel'], 'telephone' => ['Telefone secundário', 25, 'tel'], 'whatsapp' => ['WhatsApp (só dígitos, ex.: 351964201137)', 13, 'text'], 'email' => ['E-mail', 100, 'email'], 'website' => ['Website', 100, 'text'], 'instagram' => ['Instagram (URL)', 200, 'url'], 'facebook' => ['Facebook (URL)', 200, 'url'], 'messenger' => ['Messenger (URL)', 200, 'url'], 'google_maps_link' => ['Google Maps — link', 255, 'url'], 'google_maps' => ['Google Maps — código embed (iframe)', 0, 'textarea'], ]; } function info_from_post(array $post, array &$errors): array { $data = []; foreach (info_fields() as $key => [$label, $max, $type]) { $v = trim((string) ($post[$key] ?? '')); if ($key === 'whatsapp') $v = preg_replace('/\D+/', '', $v); if ($max > 0 && mb_strlen($v) > $max) $errors[] = "«{$label}» tem mais de {$max} caracteres."; if ($v !== '' && $type === 'email' && !filter_var($v, FILTER_VALIDATE_EMAIL)) $errors[] = 'O e-mail não é válido.'; if ($v !== '' && $type === 'url' && !preg_match('~^https?://~i', $v)) $errors[] = "«{$label}» tem de começar por https://"; $data[$key] = $v; } // Only a Google Maps iframe survives; anything else is dropped here rather // than stored and filtered on every page view. $data['google_maps'] = clean_map_embed($data['google_maps']); if ($data['name'] === '') $errors[] = 'O nome é obrigatório.'; if ($data['phone'] === '') $errors[] = 'O telefone principal é obrigatório.'; return $data; } function db_info_save(PDO $pdo, array $data): bool { $cols = array_keys(info_fields()); // One row per gym (id = gyms.id): the gym being edited in the backoffice. $gid = admin_gym_id(); $exists = (bool) $pdo->query("SELECT COUNT(*) FROM fitme3070_info WHERE id = " . $gid)->fetchColumn(); $vals = array_map(fn($c) => $data[$c] ?? '', $cols); if ($exists) { $sets = implode(', ', array_map(fn($c) => "`$c` = ?", $cols)); $stmt = $pdo->prepare("UPDATE fitme3070_info SET $sets, updated_at = NOW() WHERE id = " . $gid); } else { $names = implode(', ', array_map(fn($c) => "`$c`", $cols)); $marks = implode(', ', array_fill(0, count($cols), '?')); $stmt = $pdo->prepare("INSERT INTO fitme3070_info (id, $names, updated_at) VALUES (" . $gid . ", $marks, NOW())"); } $stmt->execute($vals); return $stmt->rowCount() > 0; }