Plantilla PHP
This commit is contained in:
166
Practicas/Practicas_PHP/ejercicios/Plantilla Examen/recibe_formulario_hotel.php
Executable file
166
Practicas/Practicas_PHP/ejercicios/Plantilla Examen/recibe_formulario_hotel.php
Executable file
@@ -0,0 +1,166 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="stylesheet" type="text/css" href="estilos.css" />
|
||||
<title>Reservas Hotel!!!</title>
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
|
||||
<?php
|
||||
// Validar el método de solicitud HTTP
|
||||
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||||
$datosReserva = recopilarDatosReserva($_POST,['entrada', 'salida', 'habitacion', 'regimen', 'estancia', 'spa', 'guia', 'total', 'nombre', 'email', 'telefono', 'dni', 'noches', 'info']);
|
||||
$resultadoProcesamiento = procesarReserva($datosReserva);
|
||||
mostrarResultado($resultadoProcesamiento);
|
||||
} else {
|
||||
echo "<h2>No se han recibido datos del formulario</h2>";
|
||||
}
|
||||
|
||||
|
||||
// Función para recopilar datos del formulario y sanitizarlos
|
||||
function recopilarDatosReserva($formulario, $campos )
|
||||
{
|
||||
$datos = [];
|
||||
foreach ($campos as $campo) {
|
||||
$datos[$campo] = trim($formulario[$campo]) ?? '';
|
||||
}
|
||||
return $datos;
|
||||
}
|
||||
|
||||
|
||||
// Función para procesar la reserva
|
||||
function procesarReserva($datosReserva)
|
||||
{
|
||||
// Variables para el resultado del procesamiento
|
||||
$exito = true;
|
||||
$msgExito = [];
|
||||
$msgError = [];
|
||||
|
||||
// Obtener datos de reserva
|
||||
$entrada = $datosReserva['entrada'];
|
||||
$salida = $datosReserva['salida'];
|
||||
$habitacion = $datosReserva['habitacion'];
|
||||
$regimen = $datosReserva['regimen'];
|
||||
$spa = $datosReserva['spa'];
|
||||
$guia = $datosReserva['guia'];
|
||||
$nombre = $datosReserva['nombre'];
|
||||
$email = $datosReserva['email'];
|
||||
$telefono = $datosReserva['telefono'];
|
||||
$dni = $datosReserva['dni'];
|
||||
$noches = $datosReserva['noches'];
|
||||
$total = $datosReserva['total'];
|
||||
|
||||
// Generar identificador de reserva
|
||||
$idReserva = time() . '_' . mt_rand(1000, 9999);
|
||||
// Fecha de reserva
|
||||
$fechaReserva = date("d-m-Y H:i:s");
|
||||
|
||||
// Estructurar ficha de reserva
|
||||
$fichaReserva = "Id reserva: $idReserva \r\n";
|
||||
$fichaReserva .= "Fecha de reserva: $fechaReserva \r\n";
|
||||
$fichaReserva .= "Fecha de entrada: $entrada \r\n";
|
||||
$fichaReserva .= "Fecha de salida: $salida \r\n";
|
||||
$fichaReserva .= "Tipo de habitación: $habitacion \r\n";
|
||||
$fichaReserva .= "Regimen de alojamiento: $regimen \r\n";
|
||||
$fichaReserva .= "Días Spa: $spa \r\n";
|
||||
$fichaReserva .= "Días Guia: $guia \r\n";
|
||||
$fichaReserva .= "Duración estancia: $noches \r\n";
|
||||
$fichaReserva .= "Coste Total: $total \r\n";
|
||||
$fichaReserva .= "Nombre: $nombre \r\n";
|
||||
$fichaReserva .= "Correo electrónico: $email \r\n";
|
||||
$fichaReserva .= "Teléfono: $telefono \r\n";
|
||||
$fichaReserva .= "DNI: $dni \r\n";
|
||||
|
||||
// Enviar correo electrónico con los detalles de la reserva
|
||||
$destinatario = "appasin04@gmail.com";
|
||||
$asunto = "Informacion de reserva - $idReserva";
|
||||
$headers = 'Reply-To: appasin04@gmail.com' . "\r\n";
|
||||
$headers .= 'Bcc: ' . $email . "\r\n";
|
||||
if (!mail($destinatario, $asunto, $fichaReserva, $headers)) {
|
||||
$exito = false;
|
||||
$msgError[] = "Error al enviar el correo electrónico.";
|
||||
}
|
||||
|
||||
|
||||
// Guardar la ficha de reserva en un archivo
|
||||
$dir_reservas ="reservas/";
|
||||
!is_dir($dir_reservas) ? !mkdir($dir_reservas, 0777, true) : null;
|
||||
$nombre_archivo = $dir_reservas . $idReserva . ".txt";
|
||||
if (file_put_contents($nombre_archivo, $fichaReserva) === false) {
|
||||
$exito = false;
|
||||
$msgError[] = "Error al guardar la ficha de reserva.";
|
||||
} else {
|
||||
$msgExito[] = "<h2> Reserva confirmada ! </h2>" . "<p>". nl2br($fichaReserva)."</p>";
|
||||
}
|
||||
|
||||
// Añadir al listado_reservas.txt una línea de reserva
|
||||
$registroReserva = implode('/_/', [$idReserva, $nombre, $email, $telefono, $entrada, $salida, $fechaReserva, $noches]) . "\r\n";
|
||||
$nombre_archivo = "listado_reservas.txt";
|
||||
if (file_put_contents($nombre_archivo, $registroReserva, FILE_APPEND | LOCK_EX) === false) {
|
||||
$exito = false;
|
||||
$msgError[] = "Error al guardar la lista de reservas.";
|
||||
}
|
||||
|
||||
if (isset($_FILES['dnifile']) && $_FILES['dnifile']['error'] === UPLOAD_ERR_OK){
|
||||
$exitoUpload = true;
|
||||
$dir_uploads= "dni_clientes/";
|
||||
!is_dir($dir_uploads) ? !mkdir($dir_uploads, 0777, true) : null;
|
||||
$nombreArchivo = implode("_", [$dni, $idReserva, $_FILES['dnifile']['name']]);
|
||||
$extension_archivo = strtolower(pathinfo($_FILES["dnifile"]["name"], PATHINFO_EXTENSION));
|
||||
$tamano_maximo = 2 * 1024 * 1024;
|
||||
$extensiones_permitidas = array("jpg", "jpeg", "pdf");
|
||||
|
||||
if(!in_array($extension_archivo, $extensiones_permitidas)){
|
||||
$exitoUpload =false;
|
||||
$exito = false;
|
||||
$msgError[] = "El fichero no tiene una extension valida.";
|
||||
}
|
||||
|
||||
if ($_FILES["dnifile"]["size"] > $tamano_maximo){
|
||||
$exitoUpload =false;
|
||||
$exito = false;
|
||||
$msgError[] = "El fichero excede el tamaño maximo (2Mb).";
|
||||
}
|
||||
|
||||
if ($exitoUpload){
|
||||
if(move_uploaded_file($_FILES["dnifile"]["tmp_name"],$dir_uploads . $nombreArchivo)){
|
||||
$msgExito [] = "<h4>El fichero adjunto se almaceno correctamente</h4>";
|
||||
}else{
|
||||
$exito = false;
|
||||
$msgError = "Ocurrio un error al almacenar el fichero adjunto";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$resultado = [
|
||||
'exito' => $exito,
|
||||
'msgExito'=> $msgExito,
|
||||
'msgError' => $msgError
|
||||
];
|
||||
return $resultado;
|
||||
}
|
||||
|
||||
|
||||
|
||||
function mostrarResultado($resultado)
|
||||
{
|
||||
|
||||
foreach ($resultado['msgExito'] as $key => $value) {
|
||||
echo "<p> $value </p>";
|
||||
}
|
||||
if (!$resultado['exito']) {
|
||||
echo "<h4>Se han encontrado los siguientes problemas al procesar su solicitud:</h4>";
|
||||
echo "<ul>";
|
||||
foreach ($resultado['msgError'] as $key => $value) {
|
||||
echo "<li> $value </li>";
|
||||
}
|
||||
echo "</ul>";
|
||||
}
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user