added tags to document

This commit is contained in:
Claudio 2025-06-06 17:14:16 +02:00
parent 3e9262d6bf
commit 799a5506ef
5 changed files with 90 additions and 63 deletions

View File

@ -1,9 +1,5 @@
<?php
// Mostra errori per il debug
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// add-document.php
include('include/headscript.php');
// Connessione al database
@ -13,46 +9,37 @@ if ($conn->connect_error) {
die("Errore di connessione: " . $conn->connect_error);
}
// Verifica se la richiesta è POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Recupera i dati dal form
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$document_name = $conn->real_escape_string($_POST['document_name']);
$page_id = isset($_POST['page_id']) ? (int)$_POST['page_id'] : null;
$idsections = isset($_POST['idsections']) ? (int)$_POST['idsections'] : null;
$max_documents = isset($_POST['max_documents']) ? (int)$_POST['max_documents'] : 0;
$is_required = isset($_POST['is_required']) ? (int)$_POST['is_required'] : 0;
$notes = !empty($_POST['notes']) ? $conn->real_escape_string($_POST['notes']) : null;
$tags = isset($_POST['tags']) ? $_POST['tags'] : []; // Array di tag selezionati
// Prepara la query di inserimento
$query = "
INSERT INTO documents (document_name, page_id, idsections, max_documents, is_required, notes)
VALUES (?, ?, ?, ?, ?, ?)
";
$stmt = $conn->prepare($query);
if ($stmt === false) {
die("Errore nella preparazione della query: " . $conn->error);
}
// Associa i parametri alla query
$stmt->bind_param('siiiss', $document_name, $page_id, $idsections, $max_documents, $is_required, $notes);
// Esegue la query e controlla il risultato
if ($stmt->execute()) {
// Reindirizza con messaggio di successo
header("Location: documents-settings.php?success=1");
exit();
} else {
// Reindirizza con messaggio di errore
header("Location: documents-settings.php?error=1");
exit();
}
// Inserisci il documento
$stmt = $conn->prepare("INSERT INTO documents (document_name, page_id, idsections, max_documents, is_required, notes) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->bind_param("siiiss", $document_name, $page_id, $idsections, $max_documents, $is_required, $notes);
$stmt->execute();
$document_id = $conn->insert_id;
$stmt->close();
// Inserisci i tag nella tabella document_tags
if (!empty($tags)) {
$stmt = $conn->prepare("INSERT INTO document_tags (document_id, tag_id) VALUES (?, ?)");
foreach ($tags as $tag_id) {
$tag_id = (int)$tag_id;
$stmt->bind_param("ii", $document_id, $tag_id);
$stmt->execute();
}
$stmt->close();
}
header("Location: documents-settings.php?success=1");
exit();
} else {
// Metodo non consentito
header("HTTP/1.1 405 Method Not Allowed");
echo "Metodo non consentito.";
header("Location: documents-settings.php?error=1");
exit();
}

View File

@ -1,30 +1,38 @@
<?php
// delete-document.php
header('Content-Type: application/json');
include('include/headscript.php');
$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) {
die(json_encode(["success" => false, "message" => "Errore di connessione al database."]));
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$documentId = isset($_POST['document_id']) ? intval($_POST['document_id']) : 0;
$fileName = isset($_POST['file_name']) ? $_POST['file_name'] : '';
if ($documentId > 0 && !empty($fileName)) {
// Elimina il file dal server
$filePath = "persondocuments/" . $fileName;
if (file_exists($filePath)) {
unlink($filePath);
}
if ($documentId > 0) {
// Elimina i tag associati
$stmt = $conn->prepare("DELETE FROM document_tags WHERE document_id = ?");
$stmt->bind_param("i", $documentId);
$stmt->execute();
$stmt->close();
// Elimina dal database
$query = $conn->prepare("DELETE FROM doc_storage WHERE id = ?");
$query->bind_param("i", $documentId);
if ($query->execute()) {
// Elimina il documento
$stmt = $conn->prepare("DELETE FROM documents WHERE document_id = ?");
$stmt->bind_param("i", $documentId);
if ($stmt->execute()) {
echo json_encode(["success" => true]);
} else {
echo json_encode(["success" => false, "message" => "Errore durante l'eliminazione dal database."]);
}
$stmt->close();
} else {
echo json_encode(["success" => false, "message" => "Parametri non validi."]);
}
} else {
echo json_encode(["success" => false, "message" => "Metodo non consentito."]);
}
$conn->close();

View File

@ -106,7 +106,6 @@ while ($row = $queryTags->fetch_assoc()) {
</thead>
<tbody>
<?php foreach ($documents as $document) {
// Prepara la query per recuperare i tag associati
$tagStmt = $conn->prepare("SELECT t.tag_id, t.tag_name FROM tags t
JOIN document_tags dt ON t.tag_id = dt.tag_id
WHERE dt.document_id = ?");
@ -114,10 +113,18 @@ while ($row = $queryTags->fetch_assoc()) {
$tagStmt->execute();
$tagResult = $tagStmt->get_result();
$documentTags = [];
while ($tagRow = $tagResult->fetch_assoc()) {
$documentTags[] = $tagRow['tag_name'];
$documentTagIds = [];
if ($tagResult) {
while ($tagRow = $tagResult->fetch_assoc()) {
$documentTags[] = $tagRow['tag_name'];
$documentTagIds[] = (int)$tagRow['tag_id'];
}
}
$tagStmt->close();
// Debug: Stampa il valore di data-tags
$tagsJson = json_encode($documentTagIds);
echo "<!-- data-tags value for document_id {$document['document_id']}: $tagsJson -->\n";
?>
<tr>
<td><?php echo $document['document_id']; ?></td>
@ -139,7 +146,7 @@ while ($row = $queryTags->fetch_assoc()) {
data-max-documents="<?php echo htmlspecialchars($document['max_documents']); ?>"
data-is-required="<?php echo $document['is_required']; ?>"
data-notes="<?php echo htmlspecialchars($document['notes']); ?>"
data-tags="<?php echo htmlspecialchars(json_encode(array_column($documentTags, 'tag_id'))); ?>">
data-tags="<?php echo json_encode($documentTagIds); ?>">
<i class="mdi mdi-pencil"></i>
</button>
@ -340,7 +347,7 @@ while ($row = $queryTags->fetch_assoc()) {
var maxDocuments = button.data('max-documents');
var isRequired = button.data('is-required');
var notes = button.data('notes');
var tags = button.data('tags') ? JSON.parse(button.data('tags')) : [];
var tags = button.data('tags') || [];
console.log("Dati ricevuti dal pulsante:", {
documentId,
@ -380,17 +387,24 @@ while ($row = $queryTags->fetch_assoc()) {
$.ajax({
url: 'delete-document.php',
type: 'POST',
data: {
document_id: documentId
},
data: { document_id: documentId },
dataType: 'json',
success: function(response) {
Swal.fire(
'Eliminato!',
'Il documento è stato eliminato con successo.',
'success'
).then(() => {
location.reload();
});
if (response.success) {
Swal.fire(
'Eliminato!',
'Il documento è stato eliminato con successo.',
'success'
).then(() => {
location.reload();
});
} else {
Swal.fire(
'Errore!',
response.message || 'Si è verificato un errore durante l\'eliminazione.',
'error'
);
}
},
error: function() {
Swal.fire(

View File

@ -1,5 +1,5 @@
<?php
// Mostra errori per il debug
// edit-document.php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
@ -23,6 +23,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$max_documents = isset($_POST['max_documents']) ? (int)$_POST['max_documents'] : 0;
$is_required = isset($_POST['is_required']) ? (int)$_POST['is_required'] : 0;
$notes = !empty($_POST['notes']) ? $conn->real_escape_string($_POST['notes']) : null;
$tags = isset($_POST['tags']) ? $_POST['tags'] : []; // Array di tag selezionati
// Prepara la query di aggiornamento
$query = "
@ -41,6 +42,23 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Esegue la query e controlla il risultato
if ($stmt->execute()) {
// Elimina i tag esistenti
$stmt = $conn->prepare("DELETE FROM document_tags WHERE document_id = ?");
$stmt->bind_param("i", $document_id);
$stmt->execute();
$stmt->close();
// Inserisci i nuovi tag
if (!empty($tags)) {
$stmt = $conn->prepare("INSERT INTO document_tags (document_id, tag_id) VALUES (?, ?)");
foreach ($tags as $tag_id) {
$tag_id = (int)$tag_id;
$stmt->bind_param("ii", $document_id, $tag_id);
$stmt->execute();
}
$stmt->close();
}
// Reindirizza con messaggio di successo
header("Location: documents-settings.php?success=1");
exit();

View File