exec("CREATE TABLE IF NOT EXISTS documents ( id INT AUTO_INCREMENT PRIMARY KEY, id_conductor INT, tipo_documento VARCHAR(255), title VARCHAR(255) NOT NULL, description TEXT, file_name VARCHAR(255) NOT NULL, file_path VARCHAR(255) NOT NULL, uploaded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (id_conductor) REFERENCES taxis(id) ON DELETE SET NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"); // Add columns if they don't exist (for existing tables) $check_columns = $pdo->query("SHOW COLUMNS FROM documents LIKE 'id_conductor'"); if ($check_columns->rowCount() == 0) { $pdo->exec("ALTER TABLE documents ADD COLUMN id_conductor INT, ADD FOREIGN KEY (id_conductor) REFERENCES taxis(id) ON DELETE SET NULL;"); } $check_columns = $pdo->query("SHOW COLUMNS FROM documents LIKE 'tipo_documento'"); if ($check_columns->rowCount() == 0) { $pdo->exec("ALTER TABLE documents ADD COLUMN tipo_documento VARCHAR(255);"); } } catch (PDOException $e) { die("DB ERROR: " . $e->getMessage()); } // --- File Upload & Update Logic --- $upload_dir = 'uploads/'; $message = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $pdo = db(); // Handle Update if (isset($_POST['update_document'])) { $id = $_POST['document_id']; $title = $_POST['title']; $description = $_POST['description']; $id_conductor = $_POST['id_conductor']; $tipo_documento = $_POST['tipo_documento']; try { $stmt = $pdo->prepare("UPDATE documents SET title = ?, description = ?, id_conductor = ?, tipo_documento = ? WHERE id = ?"); $stmt->execute([$title, $description, $id_conductor, $tipo_documento, $id]); $message = '
Documento actualizado con éxito.
'; } catch (PDOException $e) { $message = '
Error al actualizar: ' . $e->getMessage() . '
'; } } // Handle Upload elseif (isset($_FILES['document'])) { if (!is_dir($upload_dir)) { mkdir($upload_dir, 0755, true); } $title = $_POST['title'] ?? 'Sin Título'; $description = $_POST['description'] ?? ''; $id_conductor = $_POST['id_conductor'] ?? null; $tipo_documento = $_POST['tipo_documento'] ?? ''; $file_name = basename($_FILES['document']['name']); $file_tmp = $_FILES['document']['tmp_name']; $file_error = $_FILES['document']['error']; if ($file_error === UPLOAD_ERR_OK) { $safe_file_name = uniqid() . '-' . preg_replace("/[^a-zA-Z0-9._-]/", "", $file_name); $file_path = $upload_dir . $safe_file_name; if (move_uploaded_file($file_tmp, $file_path)) { try { $stmt = $pdo->prepare("INSERT INTO documents (title, description, id_conductor, tipo_documento, file_name, file_path) VALUES (?, ?, ?, ?, ?, ?)"); $stmt->execute([$title, $description, $id_conductor, $tipo_documento, $file_name, $file_path]); $message = '
Documento subido con éxito.
'; } catch (PDOException $e) { $message = '
Error al guardar en la base de datos: ' . $e->getMessage() . '
'; } } else { $message = '
Error al mover el fichero subido.
'; } } else { $message = '
Error en la subida del fichero: ' . $file_error . '
'; } } } // --- Fetch Data --- $documents = []; $conductores = []; try { $pdo = db(); // Fetch drivers (taxis) $conductores = $pdo->query("SELECT id, matricula, modelo FROM taxis ORDER BY matricula")->fetchAll(PDO::FETCH_ASSOC); // Fetch documents with driver info $sort_column = $_GET['sort'] ?? 'uploaded_at'; $sort_order = $_GET['order'] ?? 'DESC'; $valid_columns = ['title', 'tipo_documento', 'conductor', 'uploaded_at']; if (!in_array($sort_column, $valid_columns)) { $sort_column = 'uploaded_at'; } $sql = "SELECT d.id, d.title, d.description, d.file_name, d.file_path, d.uploaded_at, d.tipo_documento, t.matricula as conductor, d.id_conductor FROM documents d LEFT JOIN taxis t ON d.id_conductor = t.id ORDER BY $sort_column $sort_order"; $stmt = $pdo->query($sql); $documents = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { $message .= '
Error al obtener los datos: ' . $e->getMessage() . '
'; } function get_sort_link($column, $display) { $sort_column = $_GET['sort'] ?? 'uploaded_at'; $sort_order = $_GET['order'] ?? 'DESC'; $order = ($sort_column == $column && $sort_order == 'ASC') ? 'DESC' : 'ASC'; $icon = $sort_column == $column ? ($sort_order == 'ASC' ? 'bi-sort-up' : 'bi-sort-down') : 'bi-arrow-down-up-square'; return '' . $display . ' '; } include 'header.php'; ?>

Gestor de Documentos

Descripción Fichero Acciones
No hay documentos todavía.