added tags to document
This commit is contained in:
parent
3e9262d6bf
commit
799a5506ef
@ -1,9 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
// Mostra errori per il debug
|
// add-document.php
|
||||||
ini_set('display_errors', 1);
|
|
||||||
ini_set('display_startup_errors', 1);
|
|
||||||
error_reporting(E_ALL);
|
|
||||||
|
|
||||||
include('include/headscript.php');
|
include('include/headscript.php');
|
||||||
|
|
||||||
// Connessione al database
|
// Connessione al database
|
||||||
@ -13,46 +9,37 @@ if ($conn->connect_error) {
|
|||||||
die("Errore di connessione: " . $conn->connect_error);
|
die("Errore di connessione: " . $conn->connect_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verifica se la richiesta è POST
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
||||||
// Recupera i dati dal form
|
|
||||||
$document_name = $conn->real_escape_string($_POST['document_name']);
|
$document_name = $conn->real_escape_string($_POST['document_name']);
|
||||||
$page_id = isset($_POST['page_id']) ? (int)$_POST['page_id'] : null;
|
$page_id = isset($_POST['page_id']) ? (int)$_POST['page_id'] : null;
|
||||||
$idsections = isset($_POST['idsections']) ? (int)$_POST['idsections'] : null;
|
$idsections = isset($_POST['idsections']) ? (int)$_POST['idsections'] : null;
|
||||||
$max_documents = isset($_POST['max_documents']) ? (int)$_POST['max_documents'] : 0;
|
$max_documents = isset($_POST['max_documents']) ? (int)$_POST['max_documents'] : 0;
|
||||||
$is_required = isset($_POST['is_required']) ? (int)$_POST['is_required'] : 0;
|
$is_required = isset($_POST['is_required']) ? (int)$_POST['is_required'] : 0;
|
||||||
$notes = !empty($_POST['notes']) ? $conn->real_escape_string($_POST['notes']) : null;
|
$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
|
// Inserisci il documento
|
||||||
$query = "
|
$stmt = $conn->prepare("INSERT INTO documents (document_name, page_id, idsections, max_documents, is_required, notes) VALUES (?, ?, ?, ?, ?, ?)");
|
||||||
INSERT INTO documents (document_name, page_id, idsections, max_documents, is_required, notes)
|
$stmt->bind_param("siiiss", $document_name, $page_id, $idsections, $max_documents, $is_required, $notes);
|
||||||
VALUES (?, ?, ?, ?, ?, ?)
|
$stmt->execute();
|
||||||
";
|
$document_id = $conn->insert_id;
|
||||||
|
|
||||||
$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();
|
|
||||||
}
|
|
||||||
|
|
||||||
$stmt->close();
|
$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 {
|
} else {
|
||||||
// Metodo non consentito
|
header("Location: documents-settings.php?error=1");
|
||||||
header("HTTP/1.1 405 Method Not Allowed");
|
|
||||||
echo "Metodo non consentito.";
|
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,30 +1,38 @@
|
|||||||
<?php
|
<?php
|
||||||
|
// delete-document.php
|
||||||
header('Content-Type: application/json');
|
header('Content-Type: application/json');
|
||||||
|
|
||||||
include('include/headscript.php');
|
include('include/headscript.php');
|
||||||
$conn = new mysqli($servername, $username, $password, $database);
|
$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') {
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||||
$documentId = isset($_POST['document_id']) ? intval($_POST['document_id']) : 0;
|
$documentId = isset($_POST['document_id']) ? intval($_POST['document_id']) : 0;
|
||||||
$fileName = isset($_POST['file_name']) ? $_POST['file_name'] : '';
|
|
||||||
|
|
||||||
if ($documentId > 0 && !empty($fileName)) {
|
if ($documentId > 0) {
|
||||||
// Elimina il file dal server
|
// Elimina i tag associati
|
||||||
$filePath = "persondocuments/" . $fileName;
|
$stmt = $conn->prepare("DELETE FROM document_tags WHERE document_id = ?");
|
||||||
if (file_exists($filePath)) {
|
$stmt->bind_param("i", $documentId);
|
||||||
unlink($filePath);
|
$stmt->execute();
|
||||||
}
|
$stmt->close();
|
||||||
|
|
||||||
// Elimina dal database
|
// Elimina il documento
|
||||||
$query = $conn->prepare("DELETE FROM doc_storage WHERE id = ?");
|
$stmt = $conn->prepare("DELETE FROM documents WHERE document_id = ?");
|
||||||
$query->bind_param("i", $documentId);
|
$stmt->bind_param("i", $documentId);
|
||||||
if ($query->execute()) {
|
if ($stmt->execute()) {
|
||||||
echo json_encode(["success" => true]);
|
echo json_encode(["success" => true]);
|
||||||
} else {
|
} else {
|
||||||
echo json_encode(["success" => false, "message" => "Errore durante l'eliminazione dal database."]);
|
echo json_encode(["success" => false, "message" => "Errore durante l'eliminazione dal database."]);
|
||||||
}
|
}
|
||||||
|
$stmt->close();
|
||||||
} else {
|
} else {
|
||||||
echo json_encode(["success" => false, "message" => "Parametri non validi."]);
|
echo json_encode(["success" => false, "message" => "Parametri non validi."]);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
echo json_encode(["success" => false, "message" => "Metodo non consentito."]);
|
echo json_encode(["success" => false, "message" => "Metodo non consentito."]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$conn->close();
|
||||||
|
|||||||
@ -106,7 +106,6 @@ while ($row = $queryTags->fetch_assoc()) {
|
|||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<?php foreach ($documents as $document) {
|
<?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
|
$tagStmt = $conn->prepare("SELECT t.tag_id, t.tag_name FROM tags t
|
||||||
JOIN document_tags dt ON t.tag_id = dt.tag_id
|
JOIN document_tags dt ON t.tag_id = dt.tag_id
|
||||||
WHERE dt.document_id = ?");
|
WHERE dt.document_id = ?");
|
||||||
@ -114,10 +113,18 @@ while ($row = $queryTags->fetch_assoc()) {
|
|||||||
$tagStmt->execute();
|
$tagStmt->execute();
|
||||||
$tagResult = $tagStmt->get_result();
|
$tagResult = $tagStmt->get_result();
|
||||||
$documentTags = [];
|
$documentTags = [];
|
||||||
while ($tagRow = $tagResult->fetch_assoc()) {
|
$documentTagIds = [];
|
||||||
$documentTags[] = $tagRow['tag_name'];
|
if ($tagResult) {
|
||||||
|
while ($tagRow = $tagResult->fetch_assoc()) {
|
||||||
|
$documentTags[] = $tagRow['tag_name'];
|
||||||
|
$documentTagIds[] = (int)$tagRow['tag_id'];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$tagStmt->close();
|
$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>
|
<tr>
|
||||||
<td><?php echo $document['document_id']; ?></td>
|
<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-max-documents="<?php echo htmlspecialchars($document['max_documents']); ?>"
|
||||||
data-is-required="<?php echo $document['is_required']; ?>"
|
data-is-required="<?php echo $document['is_required']; ?>"
|
||||||
data-notes="<?php echo htmlspecialchars($document['notes']); ?>"
|
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>
|
<i class="mdi mdi-pencil"></i>
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
@ -340,7 +347,7 @@ while ($row = $queryTags->fetch_assoc()) {
|
|||||||
var maxDocuments = button.data('max-documents');
|
var maxDocuments = button.data('max-documents');
|
||||||
var isRequired = button.data('is-required');
|
var isRequired = button.data('is-required');
|
||||||
var notes = button.data('notes');
|
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:", {
|
console.log("Dati ricevuti dal pulsante:", {
|
||||||
documentId,
|
documentId,
|
||||||
@ -380,17 +387,24 @@ while ($row = $queryTags->fetch_assoc()) {
|
|||||||
$.ajax({
|
$.ajax({
|
||||||
url: 'delete-document.php',
|
url: 'delete-document.php',
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
data: {
|
data: { document_id: documentId },
|
||||||
document_id: documentId
|
dataType: 'json',
|
||||||
},
|
|
||||||
success: function(response) {
|
success: function(response) {
|
||||||
Swal.fire(
|
if (response.success) {
|
||||||
'Eliminato!',
|
Swal.fire(
|
||||||
'Il documento è stato eliminato con successo.',
|
'Eliminato!',
|
||||||
'success'
|
'Il documento è stato eliminato con successo.',
|
||||||
).then(() => {
|
'success'
|
||||||
location.reload();
|
).then(() => {
|
||||||
});
|
location.reload();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
Swal.fire(
|
||||||
|
'Errore!',
|
||||||
|
response.message || 'Si è verificato un errore durante l\'eliminazione.',
|
||||||
|
'error'
|
||||||
|
);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
error: function() {
|
error: function() {
|
||||||
Swal.fire(
|
Swal.fire(
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
// Mostra errori per il debug
|
// edit-document.php
|
||||||
ini_set('display_errors', 1);
|
ini_set('display_errors', 1);
|
||||||
ini_set('display_startup_errors', 1);
|
ini_set('display_startup_errors', 1);
|
||||||
error_reporting(E_ALL);
|
error_reporting(E_ALL);
|
||||||
@ -23,6 +23,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
$max_documents = isset($_POST['max_documents']) ? (int)$_POST['max_documents'] : 0;
|
$max_documents = isset($_POST['max_documents']) ? (int)$_POST['max_documents'] : 0;
|
||||||
$is_required = isset($_POST['is_required']) ? (int)$_POST['is_required'] : 0;
|
$is_required = isset($_POST['is_required']) ? (int)$_POST['is_required'] : 0;
|
||||||
$notes = !empty($_POST['notes']) ? $conn->real_escape_string($_POST['notes']) : null;
|
$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
|
// Prepara la query di aggiornamento
|
||||||
$query = "
|
$query = "
|
||||||
@ -41,6 +42,23 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
|
|
||||||
// Esegue la query e controlla il risultato
|
// Esegue la query e controlla il risultato
|
||||||
if ($stmt->execute()) {
|
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
|
// Reindirizza con messaggio di successo
|
||||||
header("Location: documents-settings.php?success=1");
|
header("Location: documents-settings.php?success=1");
|
||||||
exit();
|
exit();
|
||||||
|
|||||||
0
public/userportal/get_document_tags.php
Normal file
0
public/userportal/get_document_tags.php
Normal file
Loading…
x
Reference in New Issue
Block a user