154 lines
5.2 KiB
PHP
154 lines
5.2 KiB
PHP
<!-- ************************** -->
|
|
<!-- Examen práctico - UF1844 -->
|
|
<!-- ALUMNO: MARCOS LOPEZ GOMEZ -->
|
|
<!-- ************************** -->
|
|
|
|
<!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>Busqueda y eliminacion de registros</title>
|
|
</head>
|
|
|
|
<body>
|
|
<form method="post">
|
|
<p>*Término a buscar: </p>
|
|
<p><input type="text" name="termBusqueda" id="termBusqueda" size="30" placeholder="Introduzca la palabra de Busqueda" required></p>
|
|
<input type="submit" name="buscar" value="Buscar">
|
|
</form>
|
|
<br>
|
|
|
|
|
|
<?php
|
|
include_once('caracteres.php');
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['buscar'])) {
|
|
$termBusqueda = isset($_POST['termBusqueda']) ? $_POST['termBusqueda'] : '';
|
|
$termBusqueda !== "" ? busqueda($termBusqueda) : null;
|
|
unset($_POST);
|
|
}
|
|
|
|
function busqueda($termBusqueda)
|
|
{
|
|
$termBusqueda = eliminar_tildes(strtolower($termBusqueda));
|
|
$nombre_archivo = "listado_reservas.txt";
|
|
$registros = [];
|
|
$archivo = fopen($nombre_archivo, 'r');
|
|
|
|
while (($linea = fgets($archivo)) !== false) {
|
|
if (strpos(eliminar_tildes(strtolower($linea)), $termBusqueda) !== false) {
|
|
$registros[] = $linea;
|
|
}
|
|
}
|
|
fclose($archivo);
|
|
|
|
if (count($registros) > 0) {
|
|
echo "<table border= \"2\">
|
|
<tbody>
|
|
<thead>
|
|
<th>Id Alquiler</th>
|
|
<th>Nombre</th>
|
|
<th>Correo</th>
|
|
<th>Telefono</th>
|
|
<th>Fecha de Inicio</th>
|
|
<th>Fecha de Alquiler</th>
|
|
<th>Duracion</th>
|
|
<th>Tipo Vehiculo</th>
|
|
<th>Acciones</th>
|
|
</thead>";
|
|
foreach ($registros as $registro) {
|
|
$campos = explode("/_/", $registro);
|
|
echo "<tr>";
|
|
foreach ($campos as $campo) {
|
|
echo "<td> $campo </td>";
|
|
}
|
|
echo "<td>" . genButtonDel($campos[0]) . "</td>";
|
|
echo "</tr>";
|
|
}
|
|
echo "</tbody>";
|
|
echo "</table>";
|
|
} else {
|
|
echo "<div><h4> No se han encontrado registros con los terminos indicados </h4></div>";
|
|
}
|
|
}
|
|
|
|
function genButtonDel($idReserva)
|
|
{
|
|
return "<form method=\"POST\" style=\"border: none; padding:0.2rem; min-width: auto; margin:0\">
|
|
<input type=\"hidden\" name=\"idReserva\" value=\"$idReserva\" style=\"border: none; padding:0; min-width: auto; margin:0\">
|
|
<input type=\"submit\" name=\"borrar\" value=\"Eliminar\" style=\"border: none; background-color:#122f50; color:white;cursor: pointer; padding:0.2rem 0.4rem; min-width: auto; margin:0\">
|
|
</form>";
|
|
}
|
|
?>
|
|
|
|
|
|
|
|
|
|
<form method="post">
|
|
<p>*ID de reserva para borrar: </p>
|
|
<input type="text" name="idReserva" value="" id="idReserva" size="15" minlength="15" maxlength="15" placeholder="Id de reserva" />
|
|
<input type="submit" name="borrar" value="Borrar reserva de alquiler">
|
|
</form>
|
|
<br>
|
|
|
|
<?php
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['borrar'])) {
|
|
$idReserva = isset($_POST['idReserva']) ? $_POST['idReserva'] : '';
|
|
if ($idReserva !== "") {
|
|
echo "<div>";
|
|
echo "<h4>Actuaciones sobre el registro: $idReserva</h4> <br>";
|
|
echo eliminaDeLista($idReserva) ?
|
|
"<p>Se ha encontrado el registro en el archivo y se ha eliminado</p>" :
|
|
"<p>No se encontro el registro especificado en el archivo</p>";
|
|
echo eliminaRegistro($idReserva) ?
|
|
"<p>Se ha eliminado correctamente el registro de reserva</p>" :
|
|
"No se ha encontrado el registro de reserva especificado</p>";
|
|
echo eliminaUpload($idReserva) ?
|
|
"<p>Se ha eliminado el documento almacenado con el registro</p>" :
|
|
"<p>No se ha encontrado documentos asociados al registro</p>";
|
|
echo "</div>";
|
|
}
|
|
unset($_POST);
|
|
}
|
|
|
|
function eliminaDeLista($idReserva)
|
|
{
|
|
$nombre_archivo = "listado_reservas.txt";
|
|
$flagEncontrado = false;
|
|
$registros = [];
|
|
$archivo = fopen($nombre_archivo, 'r');
|
|
while (($linea = fgets($archivo)) !== false) {
|
|
if (strpos($linea, $idReserva) === false) {
|
|
$registros[] = $linea;
|
|
} else {
|
|
$flagEncontrado = true;
|
|
}
|
|
}
|
|
fclose($archivo);
|
|
$res=file_put_contents($nombre_archivo, $registros);
|
|
return ($flagEncontrado && ($res!==false));
|
|
}
|
|
|
|
function eliminaRegistro($idReserva)
|
|
{
|
|
$dir_reservas = "alquileres/";
|
|
$nombre_archivo = "alquiler_" . "$idReserva.txt";
|
|
return file_exists($dir_reservas . $nombre_archivo) ?
|
|
unlink($dir_reservas . $nombre_archivo) :
|
|
false;
|
|
}
|
|
|
|
function eliminaUpload($idReserva)
|
|
{
|
|
$dir_uploads = "dni_clientes/";
|
|
$file = glob($dir_uploads . "*" . $idReserva . "*");
|
|
return (count($file) > 0) ? unlink($file[0]) : false;
|
|
}
|
|
|
|
?>
|
|
|
|
</body>
|
|
|
|
</html>
|