59 lines
1.8 KiB
PHP
59 lines
1.8 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/../includes/taxilanz.php';
|
|
app_boot();
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
redirect_to('/');
|
|
}
|
|
|
|
$pickup = trim((string) ($_POST['pickup_label'] ?? ''));
|
|
$destination = trim((string) ($_POST['destination_label'] ?? ''));
|
|
$scheduledFor = trim((string) ($_POST['scheduled_for'] ?? ''));
|
|
$errors = [];
|
|
|
|
if ($pickup === '') {
|
|
$errors[] = 'Introduce un origen.';
|
|
}
|
|
if ($destination === '') {
|
|
$errors[] = 'Introduce un destino.';
|
|
}
|
|
if ($pickup !== '' && $destination !== '' && strtolower($pickup) === strtolower($destination)) {
|
|
$errors[] = 'Origen y destino deben ser distintos.';
|
|
}
|
|
if ($scheduledFor !== '') {
|
|
$timestamp = strtotime($scheduledFor);
|
|
if ($timestamp === false || $timestamp < time() - 300) {
|
|
$errors[] = 'La hora indicada debe ser actual o futura.';
|
|
}
|
|
}
|
|
|
|
if ($errors) {
|
|
remember_form('ride_request', [
|
|
'pickup_label' => $pickup,
|
|
'destination_label' => $destination,
|
|
'scheduled_for' => $scheduledFor,
|
|
]);
|
|
set_flash('danger', 'Solicitud incompleta', implode(' ', $errors));
|
|
redirect_to('/');
|
|
}
|
|
|
|
try {
|
|
$ride = create_ride([
|
|
'pickup_label' => $pickup,
|
|
'destination_label' => $destination,
|
|
'scheduled_for' => $scheduledFor,
|
|
]);
|
|
set_flash('success', 'Taxi confirmado', 'Tu trayecto ya tiene ETA y recomendaciones activadas para esta espera.');
|
|
redirect_to('/rides/confirmed.php?ride=' . urlencode($ride['uuid']));
|
|
} catch (Throwable $e) {
|
|
remember_form('ride_request', [
|
|
'pickup_label' => $pickup,
|
|
'destination_label' => $destination,
|
|
'scheduled_for' => $scheduledFor,
|
|
]);
|
|
set_flash('danger', 'No pudimos confirmar el taxi', 'Vuelve a intentarlo. El formulario sigue listo para una nueva solicitud.');
|
|
redirect_to('/');
|
|
}
|